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