Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(458)

Side by Side Diff: extensions/renderer/dispatcher.cc

Issue 2495213007: [Extensions] Use a separate IPC message for extension events (Closed)
Patch Set: more Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/dispatcher.h" 5 #include "extensions/renderer/dispatcher.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <utility> 10 #include <utility>
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 #include "extensions/renderer/static_v8_external_one_byte_string_resource.h" 93 #include "extensions/renderer/static_v8_external_one_byte_string_resource.h"
94 #include "extensions/renderer/test_features_native_handler.h" 94 #include "extensions/renderer/test_features_native_handler.h"
95 #include "extensions/renderer/test_native_handler.h" 95 #include "extensions/renderer/test_native_handler.h"
96 #include "extensions/renderer/user_gestures_native_handler.h" 96 #include "extensions/renderer/user_gestures_native_handler.h"
97 #include "extensions/renderer/utils_native_handler.h" 97 #include "extensions/renderer/utils_native_handler.h"
98 #include "extensions/renderer/v8_context_native_handler.h" 98 #include "extensions/renderer/v8_context_native_handler.h"
99 #include "extensions/renderer/v8_helpers.h" 99 #include "extensions/renderer/v8_helpers.h"
100 #include "extensions/renderer/wake_event_page.h" 100 #include "extensions/renderer/wake_event_page.h"
101 #include "extensions/renderer/worker_script_context_set.h" 101 #include "extensions/renderer/worker_script_context_set.h"
102 #include "extensions/renderer/worker_thread_dispatcher.h" 102 #include "extensions/renderer/worker_thread_dispatcher.h"
103 #include "gin/converter.h"
103 #include "grit/extensions_renderer_resources.h" 104 #include "grit/extensions_renderer_resources.h"
104 #include "mojo/public/js/constants.h" 105 #include "mojo/public/js/constants.h"
105 #include "third_party/WebKit/public/platform/WebString.h" 106 #include "third_party/WebKit/public/platform/WebString.h"
106 #include "third_party/WebKit/public/platform/WebURLRequest.h" 107 #include "third_party/WebKit/public/platform/WebURLRequest.h"
107 #include "third_party/WebKit/public/web/WebCustomElement.h" 108 #include "third_party/WebKit/public/web/WebCustomElement.h"
108 #include "third_party/WebKit/public/web/WebDataSource.h" 109 #include "third_party/WebKit/public/web/WebDataSource.h"
109 #include "third_party/WebKit/public/web/WebDocument.h" 110 #include "third_party/WebKit/public/web/WebDocument.h"
110 #include "third_party/WebKit/public/web/WebFrame.h" 111 #include "third_party/WebKit/public/web/WebFrame.h"
111 #include "third_party/WebKit/public/web/WebLocalFrame.h" 112 #include "third_party/WebKit/public/web/WebLocalFrame.h"
112 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h" 113 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 } 198 }
198 199
199 void GetChrome(const v8::FunctionCallbackInfo<v8::Value>& args) { 200 void GetChrome(const v8::FunctionCallbackInfo<v8::Value>& args) {
200 args.GetReturnValue().Set(GetOrCreateChrome(context())); 201 args.GetReturnValue().Set(GetOrCreateChrome(context()));
201 } 202 }
202 }; 203 };
203 204
204 base::LazyInstance<WorkerScriptContextSet> g_worker_script_context_set = 205 base::LazyInstance<WorkerScriptContextSet> g_worker_script_context_set =
205 LAZY_INSTANCE_INITIALIZER; 206 LAZY_INSTANCE_INITIALIZER;
206 207
208 // Dispatches the event with the given name, arguments, and filtering info in
209 // the given |context|.
210 void DispatchEventInContext(const std::string& event_name,
211 const base::ListValue* event_args,
212 const base::DictionaryValue* filtering_info,
213 ScriptContext* context) {
214 v8::HandleScope handle_scope(context->isolate());
215 v8::Context::Scope context_scope(context->v8_context());
216
217 std::vector<v8::Local<v8::Value>> arguments;
218 arguments.push_back(gin::StringToSymbol(context->isolate(), event_name));
219
220 {
221 std::unique_ptr<content::V8ValueConverter> converter(
222 content::V8ValueConverter::create());
223 arguments.push_back(
224 converter->ToV8Value(event_args, context->v8_context()));
225 if (!filtering_info->empty()) {
226 arguments.push_back(
227 converter->ToV8Value(filtering_info, context->v8_context()));
228 }
229 }
230
231 context->module_system()->CallModuleMethodSafe(
232 kEventBindings, kEventDispatchFunction, arguments.size(),
233 arguments.data());
234 }
235
207 } // namespace 236 } // namespace
208 237
209 // Note that we can't use Blink public APIs in the constructor becase Blink 238 // Note that we can't use Blink public APIs in the constructor becase Blink
210 // is not initialized at the point we create Dispatcher. 239 // is not initialized at the point we create Dispatcher.
211 Dispatcher::Dispatcher(DispatcherDelegate* delegate) 240 Dispatcher::Dispatcher(DispatcherDelegate* delegate)
212 : delegate_(delegate), 241 : delegate_(delegate),
213 content_watcher_(new ContentWatcher()), 242 content_watcher_(new ContentWatcher()),
214 source_map_(&ResourceBundle::GetSharedInstance()), 243 source_map_(&ResourceBundle::GetSharedInstance()),
215 v8_schema_registry_(new V8SchemaRegistry), 244 v8_schema_registry_(new V8SchemaRegistry),
216 user_script_set_manager_observer_(this), 245 user_script_set_manager_observer_(this),
(...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after
616 // |frame_helper| and |render_frame| might be dead by now. 645 // |frame_helper| and |render_frame| might be dead by now.
617 } 646 }
618 647
619 void Dispatcher::OnExtensionResponse(int request_id, 648 void Dispatcher::OnExtensionResponse(int request_id,
620 bool success, 649 bool success,
621 const base::ListValue& response, 650 const base::ListValue& response,
622 const std::string& error) { 651 const std::string& error) {
623 request_sender_->HandleResponse(request_id, success, response, error); 652 request_sender_->HandleResponse(request_id, success, response, error);
624 } 653 }
625 654
626 void Dispatcher::DispatchEvent(const std::string& extension_id, 655 void Dispatcher::DispatchEvent(
627 const std::string& event_name) const { 656 const std::string& extension_id,
628 base::ListValue args; 657 const std::string& event_name,
629 args.Set(0, new base::StringValue(event_name)); 658 const base::ListValue& event_args,
630 args.Set(1, new base::ListValue()); 659 const base::DictionaryValue& filtering_info) const {
660 script_context_set_->ForEach(extension_id, nullptr,
661 base::Bind(&DispatchEventInContext, event_name,
662 &event_args, &filtering_info));
631 663
632 // Needed for Windows compilation, since kEventBindings is declared extern. 664 // Reset the idle handler each time there's any activity like event or message
633 const char* local_event_bindings = kEventBindings; 665 // dispatch.
634 script_context_set_->ForEach( 666 // TODO(devlin): It's likely this is totally wrong. See
635 extension_id, base::Bind(&CallModuleMethod, local_event_bindings, 667 // https://groups.google.com/a/chromium.org/forum/#!msg/scheduler-dev/iTRVbcmm pAs/pfqyUyEeAAAJ
636 kEventDispatchFunction, &args)); 668 if (set_idle_notifications_) {
669 RenderThread::Get()->ScheduleIdleHandler(
670 kInitialExtensionIdleHandlerDelayMs);
671 }
637 } 672 }
638 673
639 void Dispatcher::InvokeModuleSystemMethod(content::RenderFrame* render_frame, 674 void Dispatcher::InvokeModuleSystemMethod(content::RenderFrame* render_frame,
640 const std::string& extension_id, 675 const std::string& extension_id,
641 const std::string& module_name, 676 const std::string& module_name,
642 const std::string& function_name, 677 const std::string& function_name,
643 const base::ListValue& args, 678 const base::ListValue& args) {
644 bool user_gesture) {
645 std::unique_ptr<WebScopedUserGesture> web_user_gesture;
646 if (user_gesture) {
647 blink::WebLocalFrame* web_frame =
648 render_frame ? render_frame->GetWebFrame() : nullptr;
649 web_user_gesture.reset(new WebScopedUserGesture(web_frame));
650 }
651
652 script_context_set_->ForEach( 679 script_context_set_->ForEach(
653 extension_id, render_frame, 680 extension_id, render_frame,
654 base::Bind(&CallModuleMethod, module_name, function_name, &args)); 681 base::Bind(&CallModuleMethod, module_name, function_name, &args));
655 682
656 // Reset the idle handler each time there's any activity like event or message 683 // Reset the idle handler each time there's any activity like event or message
657 // dispatch, for which Invoke is the chokepoint. 684 // dispatch.
685 // TODO(devlin): It's likely this is totally wrong. See
686 // https://groups.google.com/a/chromium.org/forum/#!msg/scheduler-dev/iTRVbcmm pAs/pfqyUyEeAAAJ
658 if (set_idle_notifications_) { 687 if (set_idle_notifications_) {
659 RenderThread::Get()->ScheduleIdleHandler( 688 RenderThread::Get()->ScheduleIdleHandler(
660 kInitialExtensionIdleHandlerDelayMs); 689 kInitialExtensionIdleHandlerDelayMs);
661 } 690 }
662
663 // Tell the browser process when an event has been dispatched with a lazy
664 // background page active.
665 const Extension* extension =
666 RendererExtensionRegistry::Get()->GetByID(extension_id);
667 if (extension && BackgroundInfo::HasLazyBackgroundPage(extension) &&
668 module_name == kEventBindings &&
669 function_name == kEventDispatchFunction) {
670 content::RenderFrame* background_frame =
671 ExtensionFrameHelper::GetBackgroundPageFrame(extension_id);
672 if (background_frame) {
673 int message_id;
674 args.GetInteger(3, &message_id);
Devlin 2016/11/16 20:52:55 (Note: This is where it used to be used)
675 background_frame->Send(new ExtensionHostMsg_EventAck(
676 background_frame->GetRoutingID(), message_id));
677 }
678 }
679 } 691 }
680 692
681 // static 693 // static
682 std::vector<std::pair<std::string, int> > Dispatcher::GetJsResources() { 694 std::vector<std::pair<std::string, int> > Dispatcher::GetJsResources() {
683 std::vector<std::pair<std::string, int> > resources; 695 std::vector<std::pair<std::string, int> > resources;
684 696
685 // Libraries. 697 // Libraries.
686 resources.push_back(std::make_pair("appView", IDR_APP_VIEW_JS)); 698 resources.push_back(std::make_pair("appView", IDR_APP_VIEW_JS));
687 resources.push_back(std::make_pair("entryIdManager", IDR_ENTRY_ID_MANAGER)); 699 resources.push_back(std::make_pair("entryIdManager", IDR_ENTRY_ID_MANAGER));
688 resources.push_back(std::make_pair(kEventBindings, IDR_EVENT_BINDINGS_JS)); 700 resources.push_back(std::make_pair(kEventBindings, IDR_EVENT_BINDINGS_JS));
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
916 bool Dispatcher::OnControlMessageReceived(const IPC::Message& message) { 928 bool Dispatcher::OnControlMessageReceived(const IPC::Message& message) {
917 bool handled = true; 929 bool handled = true;
918 IPC_BEGIN_MESSAGE_MAP(Dispatcher, message) 930 IPC_BEGIN_MESSAGE_MAP(Dispatcher, message)
919 IPC_MESSAGE_HANDLER(ExtensionMsg_ActivateExtension, OnActivateExtension) 931 IPC_MESSAGE_HANDLER(ExtensionMsg_ActivateExtension, OnActivateExtension)
920 IPC_MESSAGE_HANDLER(ExtensionMsg_CancelSuspend, OnCancelSuspend) 932 IPC_MESSAGE_HANDLER(ExtensionMsg_CancelSuspend, OnCancelSuspend)
921 IPC_MESSAGE_HANDLER(ExtensionMsg_DeliverMessage, OnDeliverMessage) 933 IPC_MESSAGE_HANDLER(ExtensionMsg_DeliverMessage, OnDeliverMessage)
922 IPC_MESSAGE_HANDLER(ExtensionMsg_DispatchOnConnect, OnDispatchOnConnect) 934 IPC_MESSAGE_HANDLER(ExtensionMsg_DispatchOnConnect, OnDispatchOnConnect)
923 IPC_MESSAGE_HANDLER(ExtensionMsg_DispatchOnDisconnect, OnDispatchOnDisconnect) 935 IPC_MESSAGE_HANDLER(ExtensionMsg_DispatchOnDisconnect, OnDispatchOnDisconnect)
924 IPC_MESSAGE_HANDLER(ExtensionMsg_Loaded, OnLoaded) 936 IPC_MESSAGE_HANDLER(ExtensionMsg_Loaded, OnLoaded)
925 IPC_MESSAGE_HANDLER(ExtensionMsg_MessageInvoke, OnMessageInvoke) 937 IPC_MESSAGE_HANDLER(ExtensionMsg_MessageInvoke, OnMessageInvoke)
938 IPC_MESSAGE_HANDLER(ExtensionMsg_DispatchEvent, OnDispatchEvent)
926 IPC_MESSAGE_HANDLER(ExtensionMsg_SetSessionInfo, OnSetSessionInfo) 939 IPC_MESSAGE_HANDLER(ExtensionMsg_SetSessionInfo, OnSetSessionInfo)
927 IPC_MESSAGE_HANDLER(ExtensionMsg_SetScriptingWhitelist, 940 IPC_MESSAGE_HANDLER(ExtensionMsg_SetScriptingWhitelist,
928 OnSetScriptingWhitelist) 941 OnSetScriptingWhitelist)
929 IPC_MESSAGE_HANDLER(ExtensionMsg_SetSystemFont, OnSetSystemFont) 942 IPC_MESSAGE_HANDLER(ExtensionMsg_SetSystemFont, OnSetSystemFont)
930 IPC_MESSAGE_HANDLER(ExtensionMsg_SetWebViewPartitionID, 943 IPC_MESSAGE_HANDLER(ExtensionMsg_SetWebViewPartitionID,
931 OnSetWebViewPartitionID) 944 OnSetWebViewPartitionID)
932 IPC_MESSAGE_HANDLER(ExtensionMsg_ShouldSuspend, OnShouldSuspend) 945 IPC_MESSAGE_HANDLER(ExtensionMsg_ShouldSuspend, OnShouldSuspend)
933 IPC_MESSAGE_HANDLER(ExtensionMsg_Suspend, OnSuspend) 946 IPC_MESSAGE_HANDLER(ExtensionMsg_Suspend, OnSuspend)
934 IPC_MESSAGE_HANDLER(ExtensionMsg_TransferBlobs, OnTransferBlobs) 947 IPC_MESSAGE_HANDLER(ExtensionMsg_TransferBlobs, OnTransferBlobs)
935 IPC_MESSAGE_HANDLER(ExtensionMsg_Unloaded, OnUnloaded) 948 IPC_MESSAGE_HANDLER(ExtensionMsg_Unloaded, OnUnloaded)
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
1006 DOMActivityLogger::AttachToWorld(DOMActivityLogger::kMainWorldId, 1019 DOMActivityLogger::AttachToWorld(DOMActivityLogger::kMainWorldId,
1007 extension_id); 1020 extension_id);
1008 } 1021 }
1009 1022
1010 InitOriginPermissions(extension); 1023 InitOriginPermissions(extension);
1011 1024
1012 UpdateActiveExtensions(); 1025 UpdateActiveExtensions();
1013 } 1026 }
1014 1027
1015 void Dispatcher::OnCancelSuspend(const std::string& extension_id) { 1028 void Dispatcher::OnCancelSuspend(const std::string& extension_id) {
1016 DispatchEvent(extension_id, kOnSuspendCanceledEvent); 1029 DispatchEvent(extension_id, kOnSuspendCanceledEvent, base::ListValue(),
1030 base::DictionaryValue());
1017 } 1031 }
1018 1032
1019 void Dispatcher::OnDeliverMessage(int target_port_id, 1033 void Dispatcher::OnDeliverMessage(int target_port_id,
1020 int source_tab_id, 1034 int source_tab_id,
1021 const Message& message) { 1035 const Message& message) {
1022 std::unique_ptr<RequestSender::ScopedTabID> scoped_tab_id; 1036 std::unique_ptr<RequestSender::ScopedTabID> scoped_tab_id;
1023 if (source_tab_id != -1) { 1037 if (source_tab_id != -1) {
1024 scoped_tab_id.reset( 1038 scoped_tab_id.reset(
1025 new RequestSender::ScopedTabID(request_sender(), source_tab_id)); 1039 new RequestSender::ScopedTabID(request_sender(), source_tab_id));
1026 } 1040 }
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
1084 1098
1085 // Update the available bindings for all contexts. These may have changed if 1099 // Update the available bindings for all contexts. These may have changed if
1086 // an externally_connectable extension was loaded that can connect to an 1100 // an externally_connectable extension was loaded that can connect to an
1087 // open webpage. 1101 // open webpage.
1088 UpdateBindings(""); 1102 UpdateBindings("");
1089 } 1103 }
1090 1104
1091 void Dispatcher::OnMessageInvoke(const std::string& extension_id, 1105 void Dispatcher::OnMessageInvoke(const std::string& extension_id,
1092 const std::string& module_name, 1106 const std::string& module_name,
1093 const std::string& function_name, 1107 const std::string& function_name,
1094 const base::ListValue& args, 1108 const base::ListValue& args) {
1095 bool user_gesture) { 1109 InvokeModuleSystemMethod(NULL, extension_id, module_name, function_name,
lazyboy 2016/11/16 20:21:02 nullptr
Devlin 2016/11/16 20:52:55 Done.
1096 InvokeModuleSystemMethod( 1110 args);
1097 NULL, extension_id, module_name, function_name, args, user_gesture); 1111 }
1112
1113 void Dispatcher::OnDispatchEvent(
1114 const ExtensionMsg_DispatchEvent_Params& params,
1115 const base::ListValue& event_args) {
1116 std::unique_ptr<WebScopedUserGesture> web_user_gesture;
1117 if (params.is_user_gesture)
1118 web_user_gesture.reset(new WebScopedUserGesture(nullptr));
1119
1120 DispatchEvent(params.extension_id, params.event_name, event_args,
1121 params.filtering_info);
1122
1123 // Tell the browser process when an event has been dispatched with a lazy
1124 // background page active.
1125 const Extension* extension =
1126 RendererExtensionRegistry::Get()->GetByID(params.extension_id);
1127 if (extension && BackgroundInfo::HasLazyBackgroundPage(extension)) {
1128 content::RenderFrame* background_frame =
1129 ExtensionFrameHelper::GetBackgroundPageFrame(params.extension_id);
1130 if (background_frame) {
1131 background_frame->Send(new ExtensionHostMsg_EventAck(
1132 background_frame->GetRoutingID(), params.event_id));
1133 }
1134 }
1098 } 1135 }
1099 1136
1100 void Dispatcher::OnSetSessionInfo(version_info::Channel channel, 1137 void Dispatcher::OnSetSessionInfo(version_info::Channel channel,
1101 FeatureSessionType session_type) { 1138 FeatureSessionType session_type) {
1102 SetCurrentChannel(channel); 1139 SetCurrentChannel(channel);
1103 SetCurrentFeatureSessionType(session_type); 1140 SetCurrentFeatureSessionType(session_type);
1104 1141
1105 if (feature_util::ExtensionServiceWorkersEnabled()) { 1142 if (feature_util::ExtensionServiceWorkersEnabled()) {
1106 // chrome-extension: resources should be allowed to register ServiceWorkers. 1143 // chrome-extension: resources should be allowed to register ServiceWorkers.
1107 blink::WebSecurityPolicy::registerURLSchemeAsAllowingServiceWorkers( 1144 blink::WebSecurityPolicy::registerURLSchemeAsAllowingServiceWorkers(
(...skipping 23 matching lines...) Expand all
1131 RenderThread::Get()->Send( 1168 RenderThread::Get()->Send(
1132 new ExtensionHostMsg_ShouldSuspendAck(extension_id, sequence_id)); 1169 new ExtensionHostMsg_ShouldSuspendAck(extension_id, sequence_id));
1133 } 1170 }
1134 1171
1135 void Dispatcher::OnSuspend(const std::string& extension_id) { 1172 void Dispatcher::OnSuspend(const std::string& extension_id) {
1136 // Dispatch the suspend event. This doesn't go through the standard event 1173 // Dispatch the suspend event. This doesn't go through the standard event
1137 // dispatch machinery because it requires special handling. We need to let 1174 // dispatch machinery because it requires special handling. We need to let
1138 // the browser know when we are starting and stopping the event dispatch, so 1175 // the browser know when we are starting and stopping the event dispatch, so
1139 // that it still considers the extension idle despite any activity the suspend 1176 // that it still considers the extension idle despite any activity the suspend
1140 // event creates. 1177 // event creates.
1141 DispatchEvent(extension_id, kOnSuspendEvent); 1178 DispatchEvent(extension_id, kOnSuspendEvent, base::ListValue(),
1179 base::DictionaryValue());
1142 RenderThread::Get()->Send(new ExtensionHostMsg_SuspendAck(extension_id)); 1180 RenderThread::Get()->Send(new ExtensionHostMsg_SuspendAck(extension_id));
1143 } 1181 }
1144 1182
1145 void Dispatcher::OnTransferBlobs(const std::vector<std::string>& blob_uuids) { 1183 void Dispatcher::OnTransferBlobs(const std::vector<std::string>& blob_uuids) {
1146 RenderThread::Get()->Send(new ExtensionHostMsg_TransferBlobsAck(blob_uuids)); 1184 RenderThread::Get()->Send(new ExtensionHostMsg_TransferBlobsAck(blob_uuids));
1147 } 1185 }
1148 1186
1149 void Dispatcher::OnUnloaded(const std::string& id) { 1187 void Dispatcher::OnUnloaded(const std::string& id) {
1150 // See comment in OnLoaded for why it would be nice, but perhaps incorrect, 1188 // See comment in OnLoaded for why it would be nice, but perhaps incorrect,
1151 // to CHECK here rather than guarding. 1189 // to CHECK here rather than guarding.
(...skipping 498 matching lines...) Expand 10 before | Expand all | Expand 10 after
1650 // The "guestViewDeny" module must always be loaded last. It registers 1688 // The "guestViewDeny" module must always be loaded last. It registers
1651 // error-providing custom elements for the GuestView types that are not 1689 // error-providing custom elements for the GuestView types that are not
1652 // available, and thus all of those types must have been checked and loaded 1690 // available, and thus all of those types must have been checked and loaded
1653 // (or not loaded) beforehand. 1691 // (or not loaded) beforehand.
1654 if (context_type == Feature::BLESSED_EXTENSION_CONTEXT) { 1692 if (context_type == Feature::BLESSED_EXTENSION_CONTEXT) {
1655 module_system->Require("guestViewDeny"); 1693 module_system->Require("guestViewDeny");
1656 } 1694 }
1657 } 1695 }
1658 1696
1659 } // namespace extensions 1697 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698