forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
js_channel_bindings.cc
140 lines (112 loc) · 4.47 KB
/
js_channel_bindings.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromecast/renderer/js_channel_bindings.h"
#include "chromecast/renderer/native_bindings_helper.h"
#include "content/public/renderer/render_frame.h"
#include "content/public/renderer/render_thread.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "third_party/blink/public/web/blink.h"
#include "third_party/blink/public/web/web_local_frame.h"
namespace chromecast {
// These are defined to be a set of objects that provide a postMessage(string)
// method. In turn they forward messages over their mojo channel to the browser.
JsChannelBindings::JsChannelBindings(
content::RenderFrame* render_frame,
mojo::PendingReceiver<mojom::JsChannelClient> receiver)
: content::RenderFrameObserver(render_frame),
receiver_(this, std::move(receiver)) {}
JsChannelBindings::~JsChannelBindings() = default;
// static
void JsChannelBindings::Create(content::RenderFrame* render_frame) {
content::RenderThread* render_thread = content::RenderThread::Get();
// First, get a connection to the main service for our process.
mojo::PendingRemote<mojom::JsChannelBindingProvider> pending_remote;
render_thread->BindHostReceiver(
pending_remote.InitWithNewPipeAndPassReceiver());
mojo::Remote<mojom::JsChannelBindingProvider> provider(
std::move(pending_remote));
mojo::PendingRemote<mojom::JsChannelClient> client;
// This deletes itself when the RenderFrame is destroyed.
new JsChannelBindings(render_frame, client.InitWithNewPipeAndPassReceiver());
// Tell the browser that we are ready to receive pipes.
provider->Register(render_frame->GetRoutingID(), std::move(client));
}
void JsChannelBindings::DidClearWindowObject() {
for (auto& e : channels_)
Install(e.first);
}
void JsChannelBindings::OnDestruct() {
delete this;
}
void JsChannelBindings::DidCreateScriptContext(v8::Local<v8::Context> context,
int32_t world_id) {
// World 0 is the "main" world that page JS uses. Non-zero IDs indicate worker
// threads or context scripts.
if (world_id == 0 && !did_create_script_context_) {
did_create_script_context_ = true;
for (auto& channel : channels_)
Install(channel.first);
}
}
void JsChannelBindings::Install(const std::string& channel) {
blink::WebLocalFrame* web_frame = render_frame()->GetWebFrame();
if (!web_frame)
return;
v8::Isolate* isolate = blink::MainThreadIsolate();
if (!isolate)
return;
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> context = web_frame->MainWorldScriptContext();
if (context.IsEmpty())
return;
v8::Context::Scope context_scope(context);
auto container = EnsureObjectExists(isolate, context->Global(), channel);
InstallBinding(isolate, container, "postMessage", &JsChannelBindings::Func,
base::Unretained(this), channel);
}
void JsChannelBindings::Func(const std::string& channel,
v8::Local<v8::Value> message) {
for (auto& e : channels_) {
if (e.first == channel) {
v8::String::Utf8Value utf8(blink::MainThreadIsolate(), message);
e.second->PostMessage(*utf8);
break;
}
}
}
void JsChannelBindings::CreateChannel(
const std::string& channel,
mojo::PendingRemote<mojom::JsChannel> pipe) {
channels_.push_back(
std::make_pair(channel, mojo::Remote<mojom::JsChannel>(std::move(pipe))));
if (did_create_script_context_)
Install(channel);
}
void JsChannelBindings::RemoveChannel(const std::string& channel) {
for (auto iter = channels_.begin(); iter != channels_.end(); ++iter) {
if (iter->first == channel) {
channels_.erase(iter);
break;
}
}
if (!did_create_script_context_)
return;
// Remove V8 object.
blink::WebLocalFrame* web_frame = render_frame()->GetWebFrame();
if (!web_frame)
return;
v8::Isolate* isolate = blink::MainThreadIsolate();
if (!isolate)
return;
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> context = web_frame->MainWorldScriptContext();
if (context.IsEmpty())
return;
v8::Context::Scope context_scope(context);
auto result = context->Global()->Set(
context, gin::StringToSymbol(isolate, channel), v8::Local<v8::Object>());
if (result.IsNothing() || !result.FromJust())
VLOG(1) << "Failed to remove binding for method " << channel;
}
} // namespace chromecast