Skip to content

Commit 2af6d9c

Browse files
zcbenznornagon
authored andcommitted
fix: use context counter as contextId (backport 2-0-x)
For sandboxed renderer it may not have a node::Environment in the context, using a increasing counter as contextId works for all cases.
1 parent 664c184 commit 2af6d9c

File tree

9 files changed

+65
-23
lines changed

9 files changed

+65
-23
lines changed

atom/common/api/atom_api_v8_util.cc

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,10 @@
1212
#include "atom/common/native_mate_converters/gurl_converter.h"
1313
#include "atom/common/node_includes.h"
1414
#include "base/hash.h"
15-
#include "base/process/process_handle.h"
16-
#include "base/strings/stringprintf.h"
1715
#include "native_mate/dictionary.h"
1816
#include "url/origin.h"
1917
#include "v8/include/v8-profiler.h"
2018

21-
// This is defined in later versions of Chromium, remove this if you see
22-
// compiler complaining duplicate defines.
23-
#if defined(OS_WIN) || defined(OS_FUCHSIA)
24-
#define CrPRIdPid "ld"
25-
#else
26-
#define CrPRIdPid "d"
27-
#endif
28-
2919
namespace std {
3020

3121
// The hash function used by DoubleIDWeakMap.
@@ -100,16 +90,6 @@ int32_t GetObjectHash(v8::Local<v8::Object> object) {
10090
return object->GetIdentityHash();
10191
}
10292

103-
std::string GetContextID(v8::Isolate* isolate) {
104-
// When a page is reloaded, V8 and blink may have optimizations that do not
105-
// free blink::WebLocalFrame and v8::Context and reuse them for the new page,
106-
// while we always recreate node::Environment when a page is loaded.
107-
// So the only reliable way to return an identity for a page, is to return the
108-
// address of the node::Environment instance.
109-
node::Environment* env = node::Environment::GetCurrent(isolate);
110-
return base::StringPrintf("%" CrPRIdPid "-%p", base::GetCurrentProcId(), env);
111-
}
112-
11393
void TakeHeapSnapshot(v8::Isolate* isolate) {
11494
isolate->GetHeapProfiler()->TakeHeapSnapshot();
11595
}
@@ -130,7 +110,6 @@ void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
130110
dict.SetMethod("setHiddenValue", &SetHiddenValue);
131111
dict.SetMethod("deleteHiddenValue", &DeleteHiddenValue);
132112
dict.SetMethod("getObjectHash", &GetObjectHash);
133-
dict.SetMethod("getContextId", &GetContextID);
134113
dict.SetMethod("takeHeapSnapshot", &TakeHeapSnapshot);
135114
dict.SetMethod("setRemoteCallbackFreer", &atom::RemoteCallbackFreer::BindTo);
136115
dict.SetMethod("setRemoteObjectFreer", &atom::RemoteObjectFreer::BindTo);

atom/common/context_counter.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) 2018 GitHub, Inc.
2+
// Use of this source code is governed by the MIT license that can be
3+
// found in the LICENSE file.
4+
5+
#include "atom/common/context_counter.h"
6+
7+
namespace atom {
8+
9+
namespace {
10+
11+
int g_context_id = 0;
12+
13+
} // namespace
14+
15+
int GetNextContextId() {
16+
return ++g_context_id;
17+
}
18+
19+
} // namespace atom

atom/common/context_counter.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) 2018 GitHub, Inc.
2+
// Use of this source code is governed by the MIT license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef ATOM_COMMON_CONTEXT_COUNTER_H_
6+
#define ATOM_COMMON_CONTEXT_COUNTER_H_
7+
8+
namespace atom {
9+
10+
// Increase the context counter, and return current count.
11+
int GetNextContextId();
12+
13+
} // namespace atom
14+
15+
#endif // ATOM_COMMON_CONTEXT_COUNTER_H_

atom/renderer/atom_renderer_client.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ void AtomRendererClient::RunScriptsAtDocumentEnd(
8282

8383
void AtomRendererClient::DidCreateScriptContext(
8484
v8::Handle<v8::Context> context, content::RenderFrame* render_frame) {
85+
RendererClientBase::DidCreateScriptContext(context, render_frame);
86+
8587
// Only allow node integration for the main frame, unless it is a devtools
8688
// extension page.
8789
if (!render_frame->IsMainFrame() && !IsDevToolsExtension(render_frame))

atom/renderer/atom_sandboxed_renderer_client.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ void AtomSandboxedRendererClient::RenderViewCreated(
153153

154154
void AtomSandboxedRendererClient::DidCreateScriptContext(
155155
v8::Handle<v8::Context> context, content::RenderFrame* render_frame) {
156+
RendererClientBase::DidCreateScriptContext(context, render_frame);
156157

157158
// Only allow preload for the main frame or
158159
// For devtools we still want to run the preload_bundle script

atom/renderer/renderer_client_base.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "atom/common/atom_constants.h"
1111
#include "atom/common/color_util.h"
12+
#include "atom/common/context_counter.h"
1213
#include "atom/common/native_mate_converters/value_converter.h"
1314
#include "atom/common/options_switches.h"
1415
#include "atom/renderer/atom_autofill_agent.h"
@@ -19,7 +20,9 @@
1920
#include "atom/renderer/preferences_manager.h"
2021
#include "base/command_line.h"
2122
#include "base/memory/ptr_util.h"
23+
#include "base/process/process_handle.h"
2224
#include "base/strings/string_split.h"
25+
#include "base/strings/stringprintf.h"
2326
#include "chrome/renderer/media/chrome_key_systems.h"
2427
#include "chrome/renderer/pepper/pepper_helper.h"
2528
#include "chrome/renderer/printing/print_web_view_helper.h"
@@ -44,6 +47,14 @@
4447
#include <shlobj.h>
4548
#endif
4649

50+
// This is defined in later versions of Chromium, remove this if you see
51+
// compiler complaining duplicate defines.
52+
#if defined(OS_WIN) || defined(OS_FUCHSIA)
53+
#define CrPRIdPid "ld"
54+
#else
55+
#define CrPRIdPid "d"
56+
#endif
57+
4758
namespace atom {
4859

4960
namespace {
@@ -78,6 +89,19 @@ RendererClientBase::RendererClientBase() {
7889
RendererClientBase::~RendererClientBase() {
7990
}
8091

92+
void RendererClientBase::DidCreateScriptContext(
93+
v8::Handle<v8::Context> context,
94+
content::RenderFrame* render_frame) {
95+
// global.setHidden("contextId", `${processId}-${GetNextContextId()}`)
96+
std::string context_id = base::StringPrintf(
97+
"%" CrPRIdPid "-%d", base::GetCurrentProcId(), GetNextContextId());
98+
v8::Isolate* isolate = context->GetIsolate();
99+
v8::Local<v8::String> key = mate::StringToSymbol(isolate, "contextId");
100+
v8::Local<v8::Private> private_key = v8::Private::ForApi(isolate, key);
101+
v8::Local<v8::Value> value = mate::ConvertToV8(isolate, context_id);
102+
context->Global()->SetPrivate(context, private_key, value);
103+
}
104+
81105
void RendererClientBase::AddRenderBindings(
82106
v8::Isolate* isolate,
83107
v8::Local<v8::Object> binding_object) {

atom/renderer/renderer_client_base.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class RendererClientBase : public content::ContentRendererClient {
2121
virtual ~RendererClientBase();
2222

2323
virtual void DidCreateScriptContext(
24-
v8::Handle<v8::Context> context, content::RenderFrame* render_frame) = 0;
24+
v8::Handle<v8::Context> context, content::RenderFrame* render_frame);
2525
virtual void WillReleaseScriptContext(
2626
v8::Handle<v8::Context> context, content::RenderFrame* render_frame) = 0;
2727
virtual void DidClearWindowObject(content::RenderFrame* render_frame);

filenames.gypi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,8 @@
437437
'atom/common/color_util.h',
438438
'atom/common/common_message_generator.cc',
439439
'atom/common/common_message_generator.h',
440+
'atom/common/context_counter.cc',
441+
'atom/common/context_counter.h',
440442
'atom/common/crash_reporter/crash_reporter.cc',
441443
'atom/common/crash_reporter/crash_reporter.h',
442444
'atom/common/crash_reporter/crash_reporter_linux.cc',

lib/renderer/api/remote.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const callbacksRegistry = new CallbacksRegistry()
1212
const remoteObjectCache = v8Util.createIDWeakMap()
1313

1414
// An unique ID that can represent current context.
15-
const contextId = v8Util.getContextId()
15+
const contextId = v8Util.getHiddenValue(global, 'contextId')
1616

1717
// Notify the main process when current context is going to be released.
1818
// Note that when the renderer process is destroyed, the message may not be

0 commit comments

Comments
 (0)