| 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" |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
| 13 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/values.h" | 15 #include "base/values.h" |
| 16 #include "content/public/child/v8_value_converter.h" | 16 #include "content/public/child/v8_value_converter.h" |
| 17 #include "content/public/common/child_process_host.h" | 17 #include "content/public/common/child_process_host.h" |
| 18 #include "content/public/renderer/render_frame.h" | 18 #include "content/public/renderer/render_frame.h" |
| 19 #include "content/public/renderer/render_thread.h" | 19 #include "content/public/renderer/render_thread.h" |
| 20 #include "extensions/common/api/messaging/message.h" | 20 #include "extensions/common/api/messaging/message.h" |
| 21 #include "extensions/common/extension_messages.h" | 21 #include "extensions/common/extension_messages.h" |
| 22 #include "extensions/common/guest_view/guest_view_constants.h" | 22 #include "extensions/common/guest_view/guest_view_constants.h" |
| 23 #include "extensions/common/manifest_handlers/externally_connectable.h" | 23 #include "extensions/common/manifest_handlers/externally_connectable.h" |
| 24 #include "extensions/renderer/dispatcher.h" | 24 #include "extensions/renderer/dispatcher.h" |
| 25 #include "extensions/renderer/event_bindings.h" | 25 #include "extensions/renderer/event_bindings.h" |
| 26 #include "extensions/renderer/object_backed_native_handler.h" | 26 #include "extensions/renderer/object_backed_native_handler.h" |
| 27 #include "extensions/renderer/script_context.h" | 27 #include "extensions/renderer/script_context.h" |
| 28 #include "extensions/renderer/script_context_set.h" | 28 #include "extensions/renderer/script_context_set.h" |
| 29 #include "third_party/WebKit/public/web/WebDocument.h" |
| 29 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 30 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 30 #include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h" | 31 #include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h" |
| 31 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" | 32 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" |
| 32 #include "third_party/WebKit/public/web/WebScopedWindowFocusAllowedIndicator.h" | 33 #include "third_party/WebKit/public/web/WebScopedWindowFocusAllowedIndicator.h" |
| 33 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h" | 34 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h" |
| 34 #include "v8/include/v8.h" | 35 #include "v8/include/v8.h" |
| 35 | 36 |
| 36 // Message passing API example (in a content script): | 37 // Message passing API example (in a content script): |
| 37 // var extension = | 38 // var extension = |
| 38 // new chrome.Extension('00123456789abcdef0123456789abcdef0123456'); | 39 // new chrome.Extension('00123456789abcdef0123456789abcdef0123456'); |
| (...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 "messaging", "dispatchOnConnect", arraysize(arguments), arguments); | 328 "messaging", "dispatchOnConnect", arraysize(arguments), arguments); |
| 328 | 329 |
| 329 if (!retval.IsEmpty()) { | 330 if (!retval.IsEmpty()) { |
| 330 CHECK(retval->IsBoolean()); | 331 CHECK(retval->IsBoolean()); |
| 331 *port_created |= retval->BooleanValue(); | 332 *port_created |= retval->BooleanValue(); |
| 332 } else { | 333 } else { |
| 333 LOG(ERROR) << "Empty return value from dispatchOnConnect."; | 334 LOG(ERROR) << "Empty return value from dispatchOnConnect."; |
| 334 } | 335 } |
| 335 } | 336 } |
| 336 | 337 |
| 337 void DeliverMessageToScriptContext(const std::string& message_data, | 338 void DeliverMessageToScriptContext(const Message& message, |
| 338 int target_port_id, | 339 int target_port_id, |
| 339 ScriptContext* script_context) { | 340 ScriptContext* script_context) { |
| 340 v8::Isolate* isolate = script_context->isolate(); | 341 v8::Isolate* isolate = script_context->isolate(); |
| 341 v8::HandleScope handle_scope(isolate); | 342 v8::HandleScope handle_scope(isolate); |
| 342 | 343 |
| 343 // Check to see whether the context has this port before bothering to create | 344 // Check to see whether the context has this port before bothering to create |
| 344 // the message. | 345 // the message. |
| 345 v8::Handle<v8::Value> port_id_handle = | 346 v8::Handle<v8::Value> port_id_handle = |
| 346 v8::Integer::New(isolate, target_port_id); | 347 v8::Integer::New(isolate, target_port_id); |
| 347 v8::Handle<v8::Value> has_port = | 348 v8::Handle<v8::Value> has_port = |
| 348 script_context->module_system()->CallModuleMethod( | 349 script_context->module_system()->CallModuleMethod( |
| 349 "messaging", "hasPort", 1, &port_id_handle); | 350 "messaging", "hasPort", 1, &port_id_handle); |
| 350 | 351 |
| 351 CHECK(!has_port.IsEmpty()); | 352 CHECK(!has_port.IsEmpty()); |
| 352 if (!has_port->BooleanValue()) | 353 if (!has_port->BooleanValue()) |
| 353 return; | 354 return; |
| 354 | 355 |
| 355 std::vector<v8::Handle<v8::Value> > arguments; | 356 std::vector<v8::Handle<v8::Value> > arguments; |
| 356 arguments.push_back(v8::String::NewFromUtf8(isolate, | 357 arguments.push_back(v8::String::NewFromUtf8(isolate, |
| 357 message_data.c_str(), | 358 message.data.c_str(), |
| 358 v8::String::kNormalString, | 359 v8::String::kNormalString, |
| 359 message_data.size())); | 360 message.data.size())); |
| 360 arguments.push_back(port_id_handle); | 361 arguments.push_back(port_id_handle); |
| 362 |
| 363 scoped_ptr<blink::WebScopedUserGesture> web_user_gesture; |
| 364 scoped_ptr<blink::WebScopedWindowFocusAllowedIndicator> allow_window_focus; |
| 365 if (message.user_gesture) { |
| 366 web_user_gesture.reset(new blink::WebScopedUserGesture); |
| 367 |
| 368 if (script_context->web_frame()) { |
| 369 blink::WebDocument document = script_context->web_frame()->document(); |
| 370 allow_window_focus.reset(new blink::WebScopedWindowFocusAllowedIndicator( |
| 371 &document)); |
| 372 } |
| 373 } |
| 374 |
| 361 script_context->module_system()->CallModuleMethod( | 375 script_context->module_system()->CallModuleMethod( |
| 362 "messaging", "dispatchOnMessage", &arguments); | 376 "messaging", "dispatchOnMessage", &arguments); |
| 363 } | 377 } |
| 364 | 378 |
| 365 void DispatchOnDisconnectToScriptContext(int port_id, | 379 void DispatchOnDisconnectToScriptContext(int port_id, |
| 366 const std::string& error_message, | 380 const std::string& error_message, |
| 367 ScriptContext* script_context) { | 381 ScriptContext* script_context) { |
| 368 v8::Isolate* isolate = script_context->isolate(); | 382 v8::Isolate* isolate = script_context->isolate(); |
| 369 v8::HandleScope handle_scope(isolate); | 383 v8::HandleScope handle_scope(isolate); |
| 370 | 384 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 target_port_id, kReceivingEndDoesntExistError)); | 428 target_port_id, kReceivingEndDoesntExistError)); |
| 415 } | 429 } |
| 416 } | 430 } |
| 417 | 431 |
| 418 // static | 432 // static |
| 419 void MessagingBindings::DeliverMessage( | 433 void MessagingBindings::DeliverMessage( |
| 420 const ScriptContextSet& context_set, | 434 const ScriptContextSet& context_set, |
| 421 int target_port_id, | 435 int target_port_id, |
| 422 const Message& message, | 436 const Message& message, |
| 423 content::RenderFrame* restrict_to_render_frame) { | 437 content::RenderFrame* restrict_to_render_frame) { |
| 424 scoped_ptr<blink::WebScopedUserGesture> web_user_gesture; | |
| 425 scoped_ptr<blink::WebScopedWindowFocusAllowedIndicator> allow_window_focus; | |
| 426 if (message.user_gesture) { | |
| 427 web_user_gesture.reset(new blink::WebScopedUserGesture); | |
| 428 allow_window_focus.reset(new blink::WebScopedWindowFocusAllowedIndicator); | |
| 429 } | |
| 430 | |
| 431 // TODO(robwu): ScriptContextSet.ForEach should accept RenderFrame*. | 438 // TODO(robwu): ScriptContextSet.ForEach should accept RenderFrame*. |
| 432 content::RenderView* restrict_to_render_view = | 439 content::RenderView* restrict_to_render_view = |
| 433 restrict_to_render_frame ? restrict_to_render_frame->GetRenderView() | 440 restrict_to_render_frame ? restrict_to_render_frame->GetRenderView() |
| 434 : NULL; | 441 : NULL; |
| 435 context_set.ForEach( | 442 context_set.ForEach( |
| 436 restrict_to_render_view, | 443 restrict_to_render_view, |
| 437 base::Bind(&DeliverMessageToScriptContext, message.data, target_port_id)); | 444 base::Bind(&DeliverMessageToScriptContext, message, target_port_id)); |
| 438 } | 445 } |
| 439 | 446 |
| 440 // static | 447 // static |
| 441 void MessagingBindings::DispatchOnDisconnect( | 448 void MessagingBindings::DispatchOnDisconnect( |
| 442 const ScriptContextSet& context_set, | 449 const ScriptContextSet& context_set, |
| 443 int port_id, | 450 int port_id, |
| 444 const std::string& error_message, | 451 const std::string& error_message, |
| 445 content::RenderFrame* restrict_to_render_frame) { | 452 content::RenderFrame* restrict_to_render_frame) { |
| 446 // TODO(robwu): ScriptContextSet.ForEach should accept RenderFrame*. | 453 // TODO(robwu): ScriptContextSet.ForEach should accept RenderFrame*. |
| 447 content::RenderView* restrict_to_render_view = | 454 content::RenderView* restrict_to_render_view = |
| 448 restrict_to_render_frame ? restrict_to_render_frame->GetRenderView() | 455 restrict_to_render_frame ? restrict_to_render_frame->GetRenderView() |
| 449 : NULL; | 456 : NULL; |
| 450 context_set.ForEach( | 457 context_set.ForEach( |
| 451 restrict_to_render_view, | 458 restrict_to_render_view, |
| 452 base::Bind(&DispatchOnDisconnectToScriptContext, port_id, error_message)); | 459 base::Bind(&DispatchOnDisconnectToScriptContext, port_id, error_message)); |
| 453 } | 460 } |
| 454 | 461 |
| 455 } // namespace extensions | 462 } // namespace extensions |
| OLD | NEW |