OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/renderer/messaging_bindings.h" | 5 #include "extensions/renderer/messaging_bindings.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 #include "content/public/renderer/render_frame.h" | 21 #include "content/public/renderer/render_frame.h" |
22 #include "content/public/renderer/render_thread.h" | 22 #include "content/public/renderer/render_thread.h" |
23 #include "extensions/common/api/messaging/message.h" | 23 #include "extensions/common/api/messaging/message.h" |
24 #include "extensions/common/extension_messages.h" | 24 #include "extensions/common/extension_messages.h" |
25 #include "extensions/common/manifest_handlers/externally_connectable.h" | 25 #include "extensions/common/manifest_handlers/externally_connectable.h" |
26 #include "extensions/renderer/dispatcher.h" | 26 #include "extensions/renderer/dispatcher.h" |
27 #include "extensions/renderer/event_bindings.h" | 27 #include "extensions/renderer/event_bindings.h" |
28 #include "extensions/renderer/object_backed_native_handler.h" | 28 #include "extensions/renderer/object_backed_native_handler.h" |
29 #include "extensions/renderer/script_context.h" | 29 #include "extensions/renderer/script_context.h" |
30 #include "extensions/renderer/script_context_set.h" | 30 #include "extensions/renderer/script_context_set.h" |
| 31 #include "extensions/renderer/v8_maybe_helpers.h" |
31 #include "third_party/WebKit/public/web/WebDocument.h" | 32 #include "third_party/WebKit/public/web/WebDocument.h" |
32 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 33 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
33 #include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h" | 34 #include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h" |
34 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" | 35 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" |
35 #include "third_party/WebKit/public/web/WebScopedWindowFocusAllowedIndicator.h" | 36 #include "third_party/WebKit/public/web/WebScopedWindowFocusAllowedIndicator.h" |
36 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h" | 37 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h" |
37 #include "v8/include/v8.h" | 38 #include "v8/include/v8.h" |
38 | 39 |
39 // Message passing API example (in a content script): | 40 // Message passing API example (in a content script): |
40 // var extension = | 41 // var extension = |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 | 180 |
180 // Sends a message along the given channel. | 181 // Sends a message along the given channel. |
181 void PostMessage(const v8::FunctionCallbackInfo<v8::Value>& args) { | 182 void PostMessage(const v8::FunctionCallbackInfo<v8::Value>& args) { |
182 content::RenderFrame* renderframe = context()->GetRenderFrame(); | 183 content::RenderFrame* renderframe = context()->GetRenderFrame(); |
183 if (!renderframe) | 184 if (!renderframe) |
184 return; | 185 return; |
185 | 186 |
186 // Arguments are (int32 port_id, string message). | 187 // Arguments are (int32 port_id, string message). |
187 CHECK(args.Length() == 2 && args[0]->IsInt32() && args[1]->IsString()); | 188 CHECK(args.Length() == 2 && args[0]->IsInt32() && args[1]->IsString()); |
188 | 189 |
189 int port_id = args[0]->Int32Value(); | 190 int port_id = args[0].As<v8::Int32>()->Value(); |
190 if (!HasPortData(port_id)) { | 191 if (!HasPortData(port_id)) { |
191 args.GetIsolate()->ThrowException(v8::Exception::Error( | 192 v8::Local<v8::String> error_message = |
192 v8::String::NewFromUtf8(args.GetIsolate(), kPortClosedError))); | 193 ToV8String(args.GetIsolate(), kPortClosedError); |
| 194 args.GetIsolate()->ThrowException(v8::Exception::Error(error_message)); |
193 return; | 195 return; |
194 } | 196 } |
195 | 197 |
196 renderframe->Send(new ExtensionHostMsg_PostMessage( | 198 renderframe->Send(new ExtensionHostMsg_PostMessage( |
197 renderframe->GetRoutingID(), port_id, | 199 renderframe->GetRoutingID(), port_id, |
198 Message(*v8::String::Utf8Value(args[1]), | 200 Message(*v8::String::Utf8Value(args[1]), |
199 blink::WebUserGestureIndicator::isProcessingUserGesture()))); | 201 blink::WebUserGestureIndicator::isProcessingUserGesture()))); |
200 } | 202 } |
201 | 203 |
202 // Forcefully disconnects a port. | 204 // Forcefully disconnects a port. |
203 void CloseChannel(const v8::FunctionCallbackInfo<v8::Value>& args) { | 205 void CloseChannel(const v8::FunctionCallbackInfo<v8::Value>& args) { |
204 // Arguments are (int32 port_id, boolean notify_browser). | 206 // Arguments are (int32 port_id, boolean notify_browser). |
205 CHECK_EQ(2, args.Length()); | 207 CHECK_EQ(2, args.Length()); |
206 CHECK(args[0]->IsInt32()); | 208 CHECK(args[0]->IsInt32()); |
207 CHECK(args[1]->IsBoolean()); | 209 CHECK(args[1]->IsBoolean()); |
208 | 210 |
209 int port_id = args[0]->Int32Value(); | 211 int port_id = args[0].As<v8::Int32>()->Value(); |
210 if (!HasPortData(port_id)) | 212 if (!HasPortData(port_id)) |
211 return; | 213 return; |
212 | 214 |
213 // Send via the RenderThread because the RenderFrame might be closing. | 215 // Send via the RenderThread because the RenderFrame might be closing. |
214 bool notify_browser = args[1]->BooleanValue(); | 216 bool notify_browser = args[1].As<v8::Boolean>()->Value(); |
215 if (notify_browser) { | 217 if (notify_browser) { |
216 content::RenderThread::Get()->Send( | 218 content::RenderThread::Get()->Send( |
217 new ExtensionHostMsg_CloseChannel(port_id, std::string())); | 219 new ExtensionHostMsg_CloseChannel(port_id, std::string())); |
218 } | 220 } |
219 | 221 |
220 ClearPortDataAndNotifyDispatcher(port_id); | 222 ClearPortDataAndNotifyDispatcher(port_id); |
221 } | 223 } |
222 | 224 |
223 // A new port has been created for a context. This occurs both when script | 225 // A new port has been created for a context. This occurs both when script |
224 // opens a connection, and when a connection is opened to this script. | 226 // opens a connection, and when a connection is opened to this script. |
225 void PortAddRef(const v8::FunctionCallbackInfo<v8::Value>& args) { | 227 void PortAddRef(const v8::FunctionCallbackInfo<v8::Value>& args) { |
226 // Arguments are (int32 port_id). | 228 // Arguments are (int32 port_id). |
227 CHECK_EQ(1, args.Length()); | 229 CHECK_EQ(1, args.Length()); |
228 CHECK(args[0]->IsInt32()); | 230 CHECK(args[0]->IsInt32()); |
229 | 231 |
230 int port_id = args[0]->Int32Value(); | 232 int port_id = args[0].As<v8::Int32>()->Value(); |
231 ++GetPortData(port_id).ref_count; | 233 ++GetPortData(port_id).ref_count; |
232 } | 234 } |
233 | 235 |
234 // The frame a port lived in has been destroyed. When there are no more | 236 // The frame a port lived in has been destroyed. When there are no more |
235 // frames with a reference to a given port, we will disconnect it and notify | 237 // frames with a reference to a given port, we will disconnect it and notify |
236 // the other end of the channel. | 238 // the other end of the channel. |
237 void PortRelease(const v8::FunctionCallbackInfo<v8::Value>& args) { | 239 void PortRelease(const v8::FunctionCallbackInfo<v8::Value>& args) { |
238 // Arguments are (int32 port_id). | 240 // Arguments are (int32 port_id). |
239 CHECK(args.Length() == 1 && args[0]->IsInt32()); | 241 CHECK(args.Length() == 1 && args[0]->IsInt32()); |
240 ReleasePort(args[0]->Int32Value()); | 242 ReleasePort(args[0].As<v8::Int32>()->Value()); |
241 } | 243 } |
242 | 244 |
243 // Implementation of both the PortRelease native handler call, and callback | 245 // Implementation of both the PortRelease native handler call, and callback |
244 // when contexts are invalidated to release their ports. | 246 // when contexts are invalidated to release their ports. |
245 void ReleasePort(int port_id) { | 247 void ReleasePort(int port_id) { |
246 if (HasPortData(port_id) && --GetPortData(port_id).ref_count == 0) { | 248 if (HasPortData(port_id) && --GetPortData(port_id).ref_count == 0) { |
247 // Send via the RenderThread because the RenderFrame might be closing. | 249 // Send via the RenderThread because the RenderFrame might be closing. |
248 content::RenderThread::Get()->Send( | 250 content::RenderThread::Get()->Send( |
249 new ExtensionHostMsg_CloseChannel(port_id, std::string())); | 251 new ExtensionHostMsg_CloseChannel(port_id, std::string())); |
250 ClearPortDataAndNotifyDispatcher(port_id); | 252 ClearPortDataAndNotifyDispatcher(port_id); |
251 } | 253 } |
252 } | 254 } |
253 | 255 |
254 // void BindToGC(object, callback, port_id) | 256 // void BindToGC(object, callback, port_id) |
255 // | 257 // |
256 // Binds |callback| to be invoked *sometime after* |object| is garbage | 258 // Binds |callback| to be invoked *sometime after* |object| is garbage |
257 // collected. We don't call the method re-entrantly so as to avoid executing | 259 // collected. We don't call the method re-entrantly so as to avoid executing |
258 // JS in some bizarro undefined mid-GC state, nor do we then call into the | 260 // JS in some bizarro undefined mid-GC state, nor do we then call into the |
259 // script context if it's been invalidated. | 261 // script context if it's been invalidated. |
260 // | 262 // |
261 // If the script context *is* invalidated in the meantime, as a slight hack, | 263 // If the script context *is* invalidated in the meantime, as a slight hack, |
262 // release the port with ID |port_id| if it's >= 0. | 264 // release the port with ID |port_id| if it's >= 0. |
263 void BindToGC(const v8::FunctionCallbackInfo<v8::Value>& args) { | 265 void BindToGC(const v8::FunctionCallbackInfo<v8::Value>& args) { |
264 CHECK(args.Length() == 3 && args[0]->IsObject() && args[1]->IsFunction() && | 266 CHECK(args.Length() == 3 && args[0]->IsObject() && args[1]->IsFunction() && |
265 args[2]->IsInt32()); | 267 args[2]->IsInt32()); |
266 int port_id = args[2]->Int32Value(); | 268 int port_id = args[2].As<v8::Int32>()->Value(); |
267 base::Closure fallback = base::Bind(&base::DoNothing); | 269 base::Closure fallback = base::Bind(&base::DoNothing); |
268 if (port_id >= 0) { | 270 if (port_id >= 0) { |
269 fallback = base::Bind(&ExtensionImpl::ReleasePort, | 271 fallback = base::Bind(&ExtensionImpl::ReleasePort, |
270 weak_ptr_factory_.GetWeakPtr(), port_id); | 272 weak_ptr_factory_.GetWeakPtr(), port_id); |
271 } | 273 } |
272 // Destroys itself when the object is GC'd or context is invalidated. | 274 // Destroys itself when the object is GC'd or context is invalidated. |
273 new GCCallback(context(), args[0].As<v8::Object>(), | 275 new GCCallback(context(), args[0].As<v8::Object>(), |
274 args[1].As<v8::Function>(), fallback); | 276 args[1].As<v8::Function>(), fallback); |
275 } | 277 } |
276 | 278 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 v8::Local<v8::Value> tls_channel_id_value = v8::Undefined(isolate); | 311 v8::Local<v8::Value> tls_channel_id_value = v8::Undefined(isolate); |
310 v8::Local<v8::Value> guest_process_id = v8::Undefined(isolate); | 312 v8::Local<v8::Value> guest_process_id = v8::Undefined(isolate); |
311 | 313 |
312 if (extension) { | 314 if (extension) { |
313 if (!source->tab.empty() && !extension->is_platform_app()) | 315 if (!source->tab.empty() && !extension->is_platform_app()) |
314 tab = converter->ToV8Value(&source->tab, script_context->v8_context()); | 316 tab = converter->ToV8Value(&source->tab, script_context->v8_context()); |
315 | 317 |
316 ExternallyConnectableInfo* externally_connectable = | 318 ExternallyConnectableInfo* externally_connectable = |
317 ExternallyConnectableInfo::Get(extension); | 319 ExternallyConnectableInfo::Get(extension); |
318 if (externally_connectable && | 320 if (externally_connectable && |
319 externally_connectable->accepts_tls_channel_id) { | 321 externally_connectable->accepts_tls_channel_id && |
320 tls_channel_id_value = v8::String::NewFromUtf8(isolate, | 322 tls_channel_id.size() < v8::String::kMaxLength) { |
321 tls_channel_id.c_str(), | 323 tls_channel_id_value = |
322 v8::String::kNormalString, | 324 ToV8String(isolate, tls_channel_id.c_str()); |
323 tls_channel_id.size()); | |
324 } | 325 } |
325 | 326 |
326 if (info.guest_process_id != content::ChildProcessHost::kInvalidUniqueID) | 327 if (info.guest_process_id != content::ChildProcessHost::kInvalidUniqueID) |
327 guest_process_id = v8::Integer::New(isolate, info.guest_process_id); | 328 guest_process_id = v8::Integer::New(isolate, info.guest_process_id); |
328 } | 329 } |
329 | 330 |
| 331 if (channel_name.size() >= v8::String::kMaxLength || |
| 332 info.source_id.size() >= v8::String::kMaxLength || |
| 333 target_extension_id.size() >= v8::String::kMaxLength || |
| 334 source_url_spec.size() >= v8::String::kMaxLength) { |
| 335 LOG(ERROR) << "Failed to execute dispatchOnConnect."; |
| 336 return; |
| 337 } |
| 338 |
330 v8::Local<v8::Value> arguments[] = { | 339 v8::Local<v8::Value> arguments[] = { |
331 // portId | 340 // portId |
332 v8::Integer::New(isolate, target_port_id), | 341 v8::Integer::New(isolate, target_port_id), |
333 // channelName | 342 // channelName |
334 v8::String::NewFromUtf8(isolate, channel_name.c_str(), | 343 ToV8String(isolate, channel_name.c_str()), |
335 v8::String::kNormalString, channel_name.size()), | |
336 // sourceTab | 344 // sourceTab |
337 tab, | 345 tab, |
338 // source_frame_id | 346 // source_frame_id |
339 v8::Integer::New(isolate, source->frame_id), | 347 v8::Integer::New(isolate, source->frame_id), |
340 // guestProcessId | 348 // guestProcessId |
341 guest_process_id, | 349 guest_process_id, |
342 // sourceExtensionId | 350 // sourceExtensionId |
343 v8::String::NewFromUtf8(isolate, info.source_id.c_str(), | 351 ToV8String(isolate, info.source_id.c_str()), |
344 v8::String::kNormalString, info.source_id.size()), | |
345 // targetExtensionId | 352 // targetExtensionId |
346 v8::String::NewFromUtf8(isolate, target_extension_id.c_str(), | 353 ToV8String(isolate, target_extension_id.c_str()), |
347 v8::String::kNormalString, | |
348 target_extension_id.size()), | |
349 // sourceUrl | 354 // sourceUrl |
350 v8::String::NewFromUtf8(isolate, source_url_spec.c_str(), | 355 ToV8String(isolate, source_url_spec.c_str()), |
351 v8::String::kNormalString, | |
352 source_url_spec.size()), | |
353 // tlsChannelId | 356 // tlsChannelId |
354 tls_channel_id_value, | 357 tls_channel_id_value, |
355 }; | 358 }; |
356 | 359 |
357 v8::Local<v8::Value> retval = | 360 v8::Local<v8::Value> retval = |
358 script_context->module_system()->CallModuleMethod( | 361 script_context->module_system()->CallModuleMethod( |
359 "messaging", "dispatchOnConnect", arraysize(arguments), arguments); | 362 "messaging", "dispatchOnConnect", arraysize(arguments), arguments); |
360 | 363 |
361 if (!retval.IsEmpty()) { | 364 if (!retval.IsEmpty()) { |
362 CHECK(retval->IsBoolean()); | 365 CHECK(retval->IsBoolean()); |
363 *port_created |= retval->BooleanValue(); | 366 *port_created |= retval.As<v8::Boolean>()->Value(); |
364 } else { | 367 } else { |
365 LOG(ERROR) << "Empty return value from dispatchOnConnect."; | 368 LOG(ERROR) << "Empty return value from dispatchOnConnect."; |
366 } | 369 } |
367 } | 370 } |
368 | 371 |
369 void DeliverMessageToScriptContext(const Message& message, | 372 void DeliverMessageToScriptContext(const Message& message, |
370 int target_port_id, | 373 int target_port_id, |
371 ScriptContext* script_context) { | 374 ScriptContext* script_context) { |
372 v8::Isolate* isolate = script_context->isolate(); | 375 v8::Isolate* isolate = script_context->isolate(); |
373 v8::HandleScope handle_scope(isolate); | 376 v8::HandleScope handle_scope(isolate); |
374 | 377 |
375 // Check to see whether the context has this port before bothering to create | 378 // Check to see whether the context has this port before bothering to create |
376 // the message. | 379 // the message. |
377 v8::Local<v8::Value> port_id_handle = | 380 v8::Local<v8::Value> port_id_handle = |
378 v8::Integer::New(isolate, target_port_id); | 381 v8::Integer::New(isolate, target_port_id); |
379 v8::Local<v8::Value> has_port = | 382 v8::Local<v8::Value> has_port = |
380 script_context->module_system()->CallModuleMethod("messaging", "hasPort", | 383 script_context->module_system()->CallModuleMethod("messaging", "hasPort", |
381 1, &port_id_handle); | 384 1, &port_id_handle); |
382 | 385 |
383 CHECK(!has_port.IsEmpty()); | 386 CHECK(!has_port.IsEmpty()); |
384 if (!has_port->BooleanValue()) | 387 if (!has_port->IsBoolean() || !has_port.As<v8::Boolean>()->Value()) |
385 return; | 388 return; |
386 | 389 |
| 390 if (message.data.size() >= v8::String::kMaxLength) |
| 391 return; |
387 std::vector<v8::Local<v8::Value>> arguments; | 392 std::vector<v8::Local<v8::Value>> arguments; |
388 arguments.push_back(v8::String::NewFromUtf8(isolate, | 393 arguments.push_back(ToV8String(isolate, message.data.c_str())); |
389 message.data.c_str(), | |
390 v8::String::kNormalString, | |
391 message.data.size())); | |
392 arguments.push_back(port_id_handle); | 394 arguments.push_back(port_id_handle); |
393 | 395 |
394 scoped_ptr<blink::WebScopedUserGesture> web_user_gesture; | 396 scoped_ptr<blink::WebScopedUserGesture> web_user_gesture; |
395 scoped_ptr<blink::WebScopedWindowFocusAllowedIndicator> allow_window_focus; | 397 scoped_ptr<blink::WebScopedWindowFocusAllowedIndicator> allow_window_focus; |
396 if (message.user_gesture) { | 398 if (message.user_gesture) { |
397 web_user_gesture.reset(new blink::WebScopedUserGesture); | 399 web_user_gesture.reset(new blink::WebScopedUserGesture); |
398 | 400 |
399 if (script_context->web_frame()) { | 401 if (script_context->web_frame()) { |
400 blink::WebDocument document = script_context->web_frame()->document(); | 402 blink::WebDocument document = script_context->web_frame()->document(); |
401 allow_window_focus.reset(new blink::WebScopedWindowFocusAllowedIndicator( | 403 allow_window_focus.reset(new blink::WebScopedWindowFocusAllowedIndicator( |
402 &document)); | 404 &document)); |
403 } | 405 } |
404 } | 406 } |
405 | 407 |
406 script_context->module_system()->CallModuleMethod( | 408 script_context->module_system()->CallModuleMethod( |
407 "messaging", "dispatchOnMessage", &arguments); | 409 "messaging", "dispatchOnMessage", &arguments); |
408 } | 410 } |
409 | 411 |
410 void DispatchOnDisconnectToScriptContext(int port_id, | 412 void DispatchOnDisconnectToScriptContext(int port_id, |
411 const std::string& error_message, | 413 const std::string& error_message, |
412 ScriptContext* script_context) { | 414 ScriptContext* script_context) { |
413 v8::Isolate* isolate = script_context->isolate(); | 415 v8::Isolate* isolate = script_context->isolate(); |
414 v8::HandleScope handle_scope(isolate); | 416 v8::HandleScope handle_scope(isolate); |
415 | 417 |
416 std::vector<v8::Local<v8::Value>> arguments; | 418 std::vector<v8::Local<v8::Value>> arguments; |
417 arguments.push_back(v8::Integer::New(isolate, port_id)); | 419 arguments.push_back(v8::Integer::New(isolate, port_id)); |
418 if (!error_message.empty()) { | 420 if (!error_message.empty()) { |
419 arguments.push_back( | 421 arguments.push_back( |
420 v8::String::NewFromUtf8(isolate, error_message.c_str())); | 422 ToV8String(isolate, error_message.c_str())); |
421 } else { | 423 } else { |
422 arguments.push_back(v8::Null(isolate)); | 424 arguments.push_back(v8::Null(isolate)); |
423 } | 425 } |
424 | 426 |
425 script_context->module_system()->CallModuleMethod( | 427 script_context->module_system()->CallModuleMethod( |
426 "messaging", "dispatchOnDisconnect", &arguments); | 428 "messaging", "dispatchOnDisconnect", &arguments); |
427 } | 429 } |
428 | 430 |
429 } // namespace | 431 } // namespace |
430 | 432 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
484 // TODO(robwu): ScriptContextSet.ForEach should accept RenderFrame*. | 486 // TODO(robwu): ScriptContextSet.ForEach should accept RenderFrame*. |
485 content::RenderView* restrict_to_render_view = | 487 content::RenderView* restrict_to_render_view = |
486 restrict_to_render_frame ? restrict_to_render_frame->GetRenderView() | 488 restrict_to_render_frame ? restrict_to_render_frame->GetRenderView() |
487 : NULL; | 489 : NULL; |
488 context_set.ForEach( | 490 context_set.ForEach( |
489 restrict_to_render_view, | 491 restrict_to_render_view, |
490 base::Bind(&DispatchOnDisconnectToScriptContext, port_id, error_message)); | 492 base::Bind(&DispatchOnDisconnectToScriptContext, port_id, error_message)); |
491 } | 493 } |
492 | 494 |
493 } // namespace extensions | 495 } // namespace extensions |
OLD | NEW |