| 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 11 matching lines...) Expand all Loading... |
| 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/WebScopedMicrotaskSuppression.h" | 28 #include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h" |
| 29 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" | 29 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" |
| 30 #include "third_party/WebKit/public/web/WebScopedWindowFocusAllowedIndicator.h" | 30 #include "third_party/WebKit/public/web/WebScopedWindowFocusAllowedIndicator.h" |
| 31 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h" | 31 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h" |
| 32 #include "third_party/WebKit/public/web/WebUserGestureToken.h" | |
| 33 #include "v8/include/v8.h" | 32 #include "v8/include/v8.h" |
| 34 | 33 |
| 35 // Message passing API example (in a content script): | 34 // Message passing API example (in a content script): |
| 36 // var extension = | 35 // var extension = |
| 37 // new chrome.Extension('00123456789abcdef0123456789abcdef0123456'); | 36 // new chrome.Extension('00123456789abcdef0123456789abcdef0123456'); |
| 38 // var port = runtime.connect(); | 37 // var port = runtime.connect(); |
| 39 // port.postMessage('Can you hear me now?'); | 38 // port.postMessage('Can you hear me now?'); |
| 40 // port.onmessage.addListener(function(msg, port) { | 39 // port.onmessage.addListener(function(msg, port) { |
| 41 // alert('response=' + msg); | 40 // alert('response=' + msg); |
| 42 // port.postMessage('I got your reponse'); | 41 // port.postMessage('I got your reponse'); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 } | 97 } |
| 99 | 98 |
| 100 virtual ~ExtensionImpl() {} | 99 virtual ~ExtensionImpl() {} |
| 101 | 100 |
| 102 private: | 101 private: |
| 103 void ClearPortDataAndNotifyDispatcher(int port_id) { | 102 void ClearPortDataAndNotifyDispatcher(int port_id) { |
| 104 ClearPortData(port_id); | 103 ClearPortData(port_id); |
| 105 dispatcher_->ClearPortData(port_id); | 104 dispatcher_->ClearPortData(port_id); |
| 106 } | 105 } |
| 107 | 106 |
| 108 bool ShouldForwardUserGesture() { | |
| 109 return blink::WebUserGestureIndicator::isProcessingUserGesture() && | |
| 110 !blink::WebUserGestureIndicator::currentUserGestureToken() | |
| 111 .wasForwarded(); | |
| 112 } | |
| 113 | |
| 114 // Sends a message along the given channel. | 107 // Sends a message along the given channel. |
| 115 void PostMessage(const v8::FunctionCallbackInfo<v8::Value>& args) { | 108 void PostMessage(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 116 content::RenderView* renderview = context()->GetRenderView(); | 109 content::RenderView* renderview = context()->GetRenderView(); |
| 117 if (!renderview) | 110 if (!renderview) |
| 118 return; | 111 return; |
| 119 | 112 |
| 120 // Arguments are (int32 port_id, string message). | 113 // Arguments are (int32 port_id, string message). |
| 121 CHECK(args.Length() == 2 && args[0]->IsInt32() && args[1]->IsString()); | 114 CHECK(args.Length() == 2 && args[0]->IsInt32() && args[1]->IsString()); |
| 122 | 115 |
| 123 int port_id = args[0]->Int32Value(); | 116 int port_id = args[0]->Int32Value(); |
| 124 if (!HasPortData(port_id)) { | 117 if (!HasPortData(port_id)) { |
| 125 args.GetIsolate()->ThrowException(v8::Exception::Error( | 118 args.GetIsolate()->ThrowException(v8::Exception::Error( |
| 126 v8::String::NewFromUtf8(args.GetIsolate(), kPortClosedError))); | 119 v8::String::NewFromUtf8(args.GetIsolate(), kPortClosedError))); |
| 127 return; | 120 return; |
| 128 } | 121 } |
| 129 | 122 |
| 130 renderview->Send(new ExtensionHostMsg_PostMessage( | 123 renderview->Send(new ExtensionHostMsg_PostMessage( |
| 131 renderview->GetRoutingID(), | 124 renderview->GetRoutingID(), port_id, |
| 132 port_id, | 125 Message(*v8::String::Utf8Value(args[1]), |
| 133 Message(*v8::String::Utf8Value(args[1]), ShouldForwardUserGesture()))); | 126 blink::WebUserGestureIndicator::isProcessingUserGesture()))); |
| 134 } | 127 } |
| 135 | 128 |
| 136 // Forcefully disconnects a port. | 129 // Forcefully disconnects a port. |
| 137 void CloseChannel(const v8::FunctionCallbackInfo<v8::Value>& args) { | 130 void CloseChannel(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 138 // Arguments are (int32 port_id, boolean notify_browser). | 131 // Arguments are (int32 port_id, boolean notify_browser). |
| 139 CHECK_EQ(2, args.Length()); | 132 CHECK_EQ(2, args.Length()); |
| 140 CHECK(args[0]->IsInt32()); | 133 CHECK(args[0]->IsInt32()); |
| 141 CHECK(args[1]->IsBoolean()); | 134 CHECK(args[1]->IsBoolean()); |
| 142 | 135 |
| 143 int port_id = args[0]->Int32Value(); | 136 int port_id = args[0]->Int32Value(); |
| (...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 354 // static | 347 // static |
| 355 void MessagingBindings::DeliverMessage( | 348 void MessagingBindings::DeliverMessage( |
| 356 const ScriptContextSet::ContextSet& contexts, | 349 const ScriptContextSet::ContextSet& contexts, |
| 357 int target_port_id, | 350 int target_port_id, |
| 358 const Message& message, | 351 const Message& message, |
| 359 content::RenderView* restrict_to_render_view) { | 352 content::RenderView* restrict_to_render_view) { |
| 360 scoped_ptr<blink::WebScopedUserGesture> web_user_gesture; | 353 scoped_ptr<blink::WebScopedUserGesture> web_user_gesture; |
| 361 scoped_ptr<blink::WebScopedWindowFocusAllowedIndicator> allow_window_focus; | 354 scoped_ptr<blink::WebScopedWindowFocusAllowedIndicator> allow_window_focus; |
| 362 if (message.user_gesture) { | 355 if (message.user_gesture) { |
| 363 web_user_gesture.reset(new blink::WebScopedUserGesture); | 356 web_user_gesture.reset(new blink::WebScopedUserGesture); |
| 364 blink::WebUserGestureIndicator::currentUserGestureToken().setForwarded(); | |
| 365 allow_window_focus.reset(new blink::WebScopedWindowFocusAllowedIndicator); | 357 allow_window_focus.reset(new blink::WebScopedWindowFocusAllowedIndicator); |
| 366 } | 358 } |
| 367 | 359 |
| 368 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 360 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 369 v8::HandleScope handle_scope(isolate); | 361 v8::HandleScope handle_scope(isolate); |
| 370 | 362 |
| 371 // TODO(kalman): pass in the full ScriptContextSet; call ForEach. | 363 // TODO(kalman): pass in the full ScriptContextSet; call ForEach. |
| 372 for (ScriptContextSet::ContextSet::const_iterator it = contexts.begin(); | 364 for (ScriptContextSet::ContextSet::const_iterator it = contexts.begin(); |
| 373 it != contexts.end(); | 365 it != contexts.end(); |
| 374 ++it) { | 366 ++it) { |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 432 v8::String::NewFromUtf8(isolate, error_message.c_str())); | 424 v8::String::NewFromUtf8(isolate, error_message.c_str())); |
| 433 } else { | 425 } else { |
| 434 arguments.push_back(v8::Null(isolate)); | 426 arguments.push_back(v8::Null(isolate)); |
| 435 } | 427 } |
| 436 (*it)->module_system()->CallModuleMethod( | 428 (*it)->module_system()->CallModuleMethod( |
| 437 "messaging", "dispatchOnDisconnect", &arguments); | 429 "messaging", "dispatchOnDisconnect", &arguments); |
| 438 } | 430 } |
| 439 } | 431 } |
| 440 | 432 |
| 441 } // namespace extensions | 433 } // namespace extensions |
| OLD | NEW |