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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
// Copyright 2014 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 "extensions/renderer/messaging_bindings.h"
#include <stdint.h>
#include <map>
#include <string>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/callback.h"
#include "base/callback_helpers.h"
#include "base/lazy_instance.h"
#include "base/metrics/histogram_macros.h"
#include "base/values.h"
#include "content/public/renderer/render_frame.h"
#include "extensions/common/api/messaging/message.h"
#include "extensions/common/api/messaging/port_id.h"
#include "extensions/common/extension_messages.h"
#include "extensions/renderer/extension_frame_helper.h"
#include "extensions/renderer/extension_port.h"
#include "extensions/renderer/gc_callback.h"
#include "extensions/renderer/messaging_util.h"
#include "extensions/renderer/script_context.h"
#include "extensions/renderer/script_context_set.h"
#include "extensions/renderer/v8_helpers.h"
#include "gin/converter.h"
#include "third_party/blink/public/web/web_user_gesture_indicator.h"
#include "v8/include/v8.h"
// Message passing API example (in a content script):
// var port = runtime.connect();
// port.postMessage('Can you hear me now?');
// port.onmessage.addListener(function(msg, port) {
// alert('response=' + msg);
// port.postMessage('I got your reponse');
// });
namespace extensions {
using v8_helpers::ToV8String;
namespace {
// A global map between ScriptContext and MessagingBindings.
base::LazyInstance<std::map<ScriptContext*, MessagingBindings*>>::
DestructorAtExit g_messaging_map = LAZY_INSTANCE_INITIALIZER;
} // namespace
MessagingBindings::MessagingBindings(ScriptContext* context)
: ObjectBackedNativeHandler(context),
weak_ptr_factory_(this) {
g_messaging_map.Get()[context] = this;
}
MessagingBindings::~MessagingBindings() {
g_messaging_map.Get().erase(context());
if (num_extension_ports_ > 0) {
UMA_HISTOGRAM_COUNTS_1000(
"Extensions.Messaging.ExtensionPortsCreated.Total", next_js_id_);
}
}
void MessagingBindings::AddRoutes() {
RouteHandlerFunction(
"CloseChannel",
base::Bind(&MessagingBindings::CloseChannel, base::Unretained(this)));
RouteHandlerFunction(
"PostMessage",
base::Bind(&MessagingBindings::PostMessage, base::Unretained(this)));
// TODO(fsamuel, kalman): Move BindToGC out of messaging natives.
RouteHandlerFunction("BindToGC", base::Bind(&MessagingBindings::BindToGC,
base::Unretained(this)));
RouteHandlerFunction("OpenChannelToExtension", "runtime.connect",
base::Bind(&MessagingBindings::OpenChannelToExtension,
base::Unretained(this)));
RouteHandlerFunction("OpenChannelToNativeApp", "runtime.connectNative",
base::Bind(&MessagingBindings::OpenChannelToNativeApp,
base::Unretained(this)));
RouteHandlerFunction(
"OpenChannelToTab",
base::Bind(&MessagingBindings::OpenChannelToTab, base::Unretained(this)));
}
// static
MessagingBindings* MessagingBindings::ForContext(ScriptContext* context) {
return g_messaging_map.Get()[context];
}
ExtensionPort* MessagingBindings::GetPortWithId(const PortId& id) {
for (const auto& key_value : ports_) {
if (key_value.second->id() == id)
return key_value.second.get();
}
return nullptr;
}
ExtensionPort* MessagingBindings::CreateNewPortWithId(const PortId& id) {
int js_id = GetNextJsId();
auto port = std::make_unique<ExtensionPort>(context(), id, js_id);
return ports_.insert(std::make_pair(js_id, std::move(port)))
.first->second.get();
}
void MessagingBindings::PostMessage(
const v8::FunctionCallbackInfo<v8::Value>& args) {
// Arguments are (int32_t port_id, string message).
CHECK(args.Length() == 2);
CHECK(args[0]->IsInt32());
CHECK(args[1]->IsString());
int js_port_id = args[0].As<v8::Int32>()->Value();
auto iter = ports_.find(js_port_id);
if (iter == ports_.end())
return;
ExtensionPort& port = *iter->second;
std::string error;
std::unique_ptr<Message> message = messaging_util::MessageFromJSONString(
args[1].As<v8::String>(), &error, context()->web_frame());
if (!message) {
args.GetReturnValue().Set(gin::StringToV8(args.GetIsolate(), error));
return;
}
port.PostExtensionMessage(std::move(message));
}
void MessagingBindings::CloseChannel(
const v8::FunctionCallbackInfo<v8::Value>& args) {
// Arguments are (int32_t port_id, bool force_close).
CHECK_EQ(2, args.Length());
CHECK(args[0]->IsInt32());
CHECK(args[1]->IsBoolean());
int js_port_id = args[0].As<v8::Int32>()->Value();
bool force_close = args[1].As<v8::Boolean>()->Value();
ClosePort(js_port_id, force_close);
}
void MessagingBindings::BindToGC(
const v8::FunctionCallbackInfo<v8::Value>& args) {
CHECK(args.Length() == 3);
CHECK(args[0]->IsObject());
CHECK(args[1]->IsFunction());
CHECK(args[2]->IsInt32());
int js_port_id = args[2].As<v8::Int32>()->Value();
base::Closure fallback = base::DoNothing();
if (js_port_id >= 0) {
// TODO(robwu): Falling back to closing the port shouldn't be needed. If
// the script context is destroyed, then the frame has navigated. But that
// is already detected by the browser, so this logic is redundant. Remove
// this fallback (and move BindToGC out of messaging because it is also
// used in other places that have nothing to do with messaging...).
fallback = base::Bind(&MessagingBindings::ClosePort,
weak_ptr_factory_.GetWeakPtr(), js_port_id,
false /* force_close */);
}
// Destroys itself when the object is GC'd or context is invalidated.
new GCCallback(context(), args[0].As<v8::Object>(),
args[1].As<v8::Function>(), fallback);
}
void MessagingBindings::OpenChannelToExtension(
const v8::FunctionCallbackInfo<v8::Value>& args) {
content::RenderFrame* render_frame = context()->GetRenderFrame();
if (!render_frame)
return;
// The Javascript code should validate/fill the arguments.
CHECK_EQ(args.Length(), 3);
CHECK(args[0]->IsString());
CHECK(args[1]->IsString());
CHECK(args[2]->IsBoolean());
int js_id = GetNextJsId();
PortId port_id(context()->context_id(), js_id, true);
ports_[js_id] = std::make_unique<ExtensionPort>(context(), port_id, js_id);
ExtensionMsg_ExternalConnectionInfo info;
// For messaging APIs, hosted apps should be considered a web page so hide
// its extension ID.
const Extension* extension = context()->extension();
if (extension && !extension->is_hosted_app())
info.source_id = extension->id();
v8::Isolate* isolate = args.GetIsolate();
info.target_id = *v8::String::Utf8Value(isolate, args[0]);
info.source_url = context()->url();
std::string channel_name = *v8::String::Utf8Value(isolate, args[1]);
// TODO(devlin): Why is this not part of info?
bool include_tls_channel_id =
args.Length() > 2 ? args[2]->BooleanValue() : false;
{
SCOPED_UMA_HISTOGRAM_TIMER(
"Extensions.Messaging.SetPortIdTime.Extension");
render_frame->Send(new ExtensionHostMsg_OpenChannelToExtension(
render_frame->GetRoutingID(), info, channel_name,
include_tls_channel_id, port_id));
}
++num_extension_ports_;
args.GetReturnValue().Set(static_cast<int32_t>(js_id));
}
void MessagingBindings::OpenChannelToNativeApp(
const v8::FunctionCallbackInfo<v8::Value>& args) {
// The Javascript code should validate/fill the arguments.
CHECK_EQ(args.Length(), 1);
CHECK(args[0]->IsString());
// This should be checked by our function routing code.
CHECK(context()->GetAvailability("runtime.connectNative").is_available());
content::RenderFrame* render_frame = context()->GetRenderFrame();
if (!render_frame)
return;
std::string native_app_name =
*v8::String::Utf8Value(args.GetIsolate(), args[0]);
int js_id = GetNextJsId();
PortId port_id(context()->context_id(), js_id, true);
ports_[js_id] = std::make_unique<ExtensionPort>(context(), port_id, js_id);
{
SCOPED_UMA_HISTOGRAM_TIMER(
"Extensions.Messaging.SetPortIdTime.NativeApp");
render_frame->Send(new ExtensionHostMsg_OpenChannelToNativeApp(
render_frame->GetRoutingID(), native_app_name, port_id));
}
args.GetReturnValue().Set(static_cast<int32_t>(js_id));
}
void MessagingBindings::OpenChannelToTab(
const v8::FunctionCallbackInfo<v8::Value>& args) {
content::RenderFrame* render_frame = context()->GetRenderFrame();
if (!render_frame)
return;
// tabs_custom_bindings.js unwraps arguments to tabs.connect/sendMessage and
// passes them to OpenChannelToTab, in the following order:
// - |tab_id| - Positive number that specifies the destination of the channel.
// - |frame_id| - Target frame(s) in the tab where onConnect is dispatched:
// -1 for all frames, 0 for the main frame, >0 for a child frame.
// - |extension_id| - ID of the initiating extension.
// - |channel_name| - A user-defined channel name.
CHECK(args.Length() == 4);
CHECK(args[0]->IsInt32());
CHECK(args[1]->IsInt32());
CHECK(args[2]->IsString());
CHECK(args[3]->IsString());
int js_id = GetNextJsId();
PortId port_id(context()->context_id(), js_id, true);
ports_[js_id] = std::make_unique<ExtensionPort>(context(), port_id, js_id);
ExtensionMsg_TabTargetConnectionInfo info;
info.tab_id = args[0]->Int32Value();
info.frame_id = args[1]->Int32Value();
// TODO(devlin): Why is this not part of info?
v8::Isolate* isolate = args.GetIsolate();
std::string extension_id = *v8::String::Utf8Value(isolate, args[2]);
std::string channel_name = *v8::String::Utf8Value(isolate, args[3]);
ExtensionFrameHelper* frame_helper = ExtensionFrameHelper::Get(render_frame);
DCHECK(frame_helper);
{
SCOPED_UMA_HISTOGRAM_TIMER("Extensions.Messaging.SetPortIdTime.Tab");
render_frame->Send(new ExtensionHostMsg_OpenChannelToTab(
render_frame->GetRoutingID(), info, extension_id, channel_name,
port_id));
}
args.GetReturnValue().Set(static_cast<int32_t>(js_id));
}
void MessagingBindings::ClosePort(int js_port_id, bool force_close) {
// TODO(robwu): Merge this logic with CloseChannel once the TODO in BindToGC
// has been addressed.
auto iter = ports_.find(js_port_id);
if (iter != ports_.end()) {
std::unique_ptr<ExtensionPort> port = std::move(iter->second);
ports_.erase(iter);
port->Close(force_close);
}
}
int MessagingBindings::GetNextJsId() {
return next_js_id_++;
}
} // namespace extensions
|