| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/browser/event_router.h" | 5 #include "extensions/browser/event_router.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/lazy_instance.h" |
| 11 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 12 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
| 14 #include "base/synchronization/lock.h" |
| 13 #include "base/values.h" | 15 #include "base/values.h" |
| 14 #include "content/public/browser/notification_service.h" | 16 #include "content/public/browser/notification_service.h" |
| 15 #include "content/public/browser/render_process_host.h" | 17 #include "content/public/browser/render_process_host.h" |
| 16 #include "extensions/browser/api_activity_monitor.h" | 18 #include "extensions/browser/api_activity_monitor.h" |
| 17 #include "extensions/browser/extension_host.h" | 19 #include "extensions/browser/extension_host.h" |
| 18 #include "extensions/browser/extension_prefs.h" | 20 #include "extensions/browser/extension_prefs.h" |
| 19 #include "extensions/browser/extension_registry.h" | 21 #include "extensions/browser/extension_registry.h" |
| 20 #include "extensions/browser/extension_system.h" | 22 #include "extensions/browser/extension_system.h" |
| 21 #include "extensions/browser/extensions_browser_client.h" | 23 #include "extensions/browser/extensions_browser_client.h" |
| 22 #include "extensions/browser/lazy_background_task_queue.h" | 24 #include "extensions/browser/lazy_background_task_queue.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 41 namespace extensions { | 43 namespace extensions { |
| 42 | 44 |
| 43 namespace { | 45 namespace { |
| 44 | 46 |
| 45 void DoNothing(ExtensionHost* host) {} | 47 void DoNothing(ExtensionHost* host) {} |
| 46 | 48 |
| 47 // A dictionary of event names to lists of filters that this extension has | 49 // A dictionary of event names to lists of filters that this extension has |
| 48 // registered from its lazy background page. | 50 // registered from its lazy background page. |
| 49 const char kFilteredEvents[] = "filtered_events"; | 51 const char kFilteredEvents[] = "filtered_events"; |
| 50 | 52 |
| 51 // Sends a notification about an event to the API activity monitor on the | 53 // Sends a notification about an event to the API activity monitor and the |
| 52 // UI thread. Can be called from any thread. | 54 // ExtensionHost for |extension_id| on the UI thread. Can be called from any |
| 53 void NotifyApiEventDispatched(void* browser_context_id, | 55 // thread. |
| 54 const std::string& extension_id, | 56 void NotifyEventDispatched(void* browser_context_id, |
| 55 const std::string& event_name, | 57 const std::string& extension_id, |
| 56 scoped_ptr<ListValue> args) { | 58 const std::string& event_name, |
| 59 int message_id, |
| 60 scoped_ptr<ListValue> args) { |
| 57 // The ApiActivityMonitor can only be accessed from the UI thread. | 61 // The ApiActivityMonitor can only be accessed from the UI thread. |
| 58 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 62 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 59 BrowserThread::PostTask( | 63 BrowserThread::PostTask( |
| 60 BrowserThread::UI, | 64 BrowserThread::UI, FROM_HERE, |
| 61 FROM_HERE, | 65 base::Bind(&NotifyEventDispatched, browser_context_id, extension_id, |
| 62 base::Bind(&NotifyApiEventDispatched, | 66 event_name, message_id, base::Passed(&args))); |
| 63 browser_context_id, | |
| 64 extension_id, | |
| 65 event_name, | |
| 66 base::Passed(&args))); | |
| 67 return; | 67 return; |
| 68 } | 68 } |
| 69 | 69 |
| 70 // Notify the ApiActivityMonitor about the event dispatch. | 70 // Notify the ApiActivityMonitor about the event dispatch. |
| 71 BrowserContext* context = static_cast<BrowserContext*>(browser_context_id); | 71 BrowserContext* context = static_cast<BrowserContext*>(browser_context_id); |
| 72 if (!ExtensionsBrowserClient::Get()->IsValidContext(context)) | 72 if (!ExtensionsBrowserClient::Get()->IsValidContext(context)) |
| 73 return; | 73 return; |
| 74 ApiActivityMonitor* monitor = | 74 ApiActivityMonitor* monitor = |
| 75 ExtensionsBrowserClient::Get()->GetApiActivityMonitor(context); | 75 ExtensionsBrowserClient::Get()->GetApiActivityMonitor(context); |
| 76 if (monitor) | 76 if (monitor) |
| 77 monitor->OnApiEventDispatched(extension_id, event_name, args.Pass()); | 77 monitor->OnApiEventDispatched(extension_id, event_name, args.Pass()); |
| 78 |
| 79 ExtensionHost* host = |
| 80 ProcessManager::Get(context)->GetBackgroundHostForExtension(extension_id); |
| 81 if (host) |
| 82 host->OnMessageDispatched(event_name, message_id); |
| 78 } | 83 } |
| 79 | 84 |
| 85 // A global identifier used to distinguish extension messages that is |
| 86 // incremented every time a message is dispatched. |
| 87 int g_extension_message_id = 0; |
| 88 |
| 89 // Protects access to |g_extension_message_id|. |
| 90 base::LazyInstance<base::Lock>::Leaky g_message_id_lock = |
| 91 LAZY_INSTANCE_INITIALIZER; |
| 92 |
| 80 } // namespace | 93 } // namespace |
| 81 | 94 |
| 82 const char EventRouter::kRegisteredEvents[] = "events"; | 95 const char EventRouter::kRegisteredEvents[] = "events"; |
| 83 | 96 |
| 84 struct EventRouter::ListenerProcess { | 97 struct EventRouter::ListenerProcess { |
| 85 content::RenderProcessHost* process; | 98 content::RenderProcessHost* process; |
| 86 std::string extension_id; | 99 std::string extension_id; |
| 87 | 100 |
| 88 ListenerProcess(content::RenderProcessHost* process, | 101 ListenerProcess(content::RenderProcessHost* process, |
| 89 const std::string& extension_id) | 102 const std::string& extension_id) |
| 90 : process(process), extension_id(extension_id) {} | 103 : process(process), extension_id(extension_id) {} |
| 91 | 104 |
| 92 bool operator<(const ListenerProcess& that) const { | 105 bool operator<(const ListenerProcess& that) const { |
| 93 if (process < that.process) | 106 if (process < that.process) |
| 94 return true; | 107 return true; |
| 95 if (process == that.process && extension_id < that.extension_id) | 108 if (process == that.process && extension_id < that.extension_id) |
| 96 return true; | 109 return true; |
| 97 return false; | 110 return false; |
| 98 } | 111 } |
| 99 }; | 112 }; |
| 100 | 113 |
| 101 // static | 114 // static |
| 102 void EventRouter::DispatchExtensionMessage(IPC::Sender* ipc_sender, | 115 void EventRouter::DispatchExtensionMessage(IPC::Sender* ipc_sender, |
| 103 void* browser_context_id, | 116 void* browser_context_id, |
| 104 const std::string& extension_id, | 117 const std::string& extension_id, |
| 105 const std::string& event_name, | 118 const std::string& event_name, |
| 106 ListValue* event_args, | 119 ListValue* event_args, |
| 107 UserGestureState user_gesture, | 120 UserGestureState user_gesture, |
| 108 const EventFilteringInfo& info) { | 121 const EventFilteringInfo& info) { |
| 109 NotifyApiEventDispatched(browser_context_id, | 122 // Since this function can be called from any thread we need to protect access |
| 110 extension_id, | 123 // to |g_extension_message_id|. |
| 111 event_name, | 124 g_message_id_lock.Get().Acquire(); |
| 112 make_scoped_ptr(event_args->DeepCopy())); | 125 int message_id = g_extension_message_id++; |
| 126 g_message_id_lock.Get().Release(); |
| 127 |
| 128 NotifyEventDispatched(browser_context_id, extension_id, event_name, |
| 129 message_id, make_scoped_ptr(event_args->DeepCopy())); |
| 113 | 130 |
| 114 ListValue args; | 131 ListValue args; |
| 115 args.Set(0, new base::StringValue(event_name)); | 132 args.Set(0, new base::StringValue(event_name)); |
| 116 args.Set(1, event_args); | 133 args.Set(1, event_args); |
| 117 args.Set(2, info.AsValue().release()); | 134 args.Set(2, info.AsValue().release()); |
| 135 args.Set(3, new base::FundamentalValue(message_id)); |
| 118 ipc_sender->Send(new ExtensionMsg_MessageInvoke( | 136 ipc_sender->Send(new ExtensionMsg_MessageInvoke( |
| 119 MSG_ROUTING_CONTROL, | 137 MSG_ROUTING_CONTROL, |
| 120 extension_id, | 138 extension_id, |
| 121 kEventBindings, | 139 kEventBindings, |
| 122 "dispatchEvent", | 140 "dispatchEvent", |
| 123 args, | 141 args, |
| 124 user_gesture == USER_GESTURE_ENABLED)); | 142 user_gesture == USER_GESTURE_ENABLED)); |
| 125 | 143 |
| 126 // DispatchExtensionMessage does _not_ take ownership of event_args, so we | 144 // DispatchExtensionMessage does _not_ take ownership of event_args, so we |
| 127 // must ensure that the destruction of args does not attempt to free it. | 145 // must ensure that the destruction of args does not attempt to free it. |
| (...skipping 690 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 818 const std::string& extension_id, | 836 const std::string& extension_id, |
| 819 const GURL& listener_url, | 837 const GURL& listener_url, |
| 820 content::BrowserContext* browser_context) | 838 content::BrowserContext* browser_context) |
| 821 : event_name(event_name), | 839 : event_name(event_name), |
| 822 extension_id(extension_id), | 840 extension_id(extension_id), |
| 823 listener_url(listener_url), | 841 listener_url(listener_url), |
| 824 browser_context(browser_context) { | 842 browser_context(browser_context) { |
| 825 } | 843 } |
| 826 | 844 |
| 827 } // namespace extensions | 845 } // namespace extensions |
| OLD | NEW |