| 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/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "chrome/browser/chrome_notification_types.h" | 14 #include "chrome/browser/chrome_notification_types.h" |
| 15 #include "chrome/browser/extensions/extension_host.h" | 15 #include "chrome/browser/extensions/extension_host.h" |
| 16 #include "chrome/browser/extensions/extension_util.h" | 16 #include "chrome/browser/extensions/extension_util.h" |
| 17 #include "chrome/common/extensions/extension_messages.h" | 17 #include "chrome/common/extensions/extension_messages.h" |
| 18 #include "content/public/browser/notification_service.h" | 18 #include "content/public/browser/notification_service.h" |
| 19 #include "content/public/browser/render_process_host.h" | 19 #include "content/public/browser/render_process_host.h" |
| 20 #include "extensions/browser/api_activity_monitor.h" |
| 20 #include "extensions/browser/extension_prefs.h" | 21 #include "extensions/browser/extension_prefs.h" |
| 21 #include "extensions/browser/extension_registry.h" | 22 #include "extensions/browser/extension_registry.h" |
| 22 #include "extensions/browser/extension_system.h" | 23 #include "extensions/browser/extension_system.h" |
| 23 #include "extensions/browser/extensions_browser_client.h" | 24 #include "extensions/browser/extensions_browser_client.h" |
| 24 #include "extensions/browser/lazy_background_task_queue.h" | 25 #include "extensions/browser/lazy_background_task_queue.h" |
| 25 #include "extensions/browser/process_manager.h" | 26 #include "extensions/browser/process_manager.h" |
| 26 #include "extensions/browser/process_map.h" | 27 #include "extensions/browser/process_map.h" |
| 27 #include "extensions/common/extension.h" | 28 #include "extensions/common/extension.h" |
| 28 #include "extensions/common/extension_api.h" | 29 #include "extensions/common/extension_api.h" |
| 29 #include "extensions/common/extension_urls.h" | 30 #include "extensions/common/extension_urls.h" |
| 30 #include "extensions/common/manifest_handlers/background_info.h" | 31 #include "extensions/common/manifest_handlers/background_info.h" |
| 31 #include "extensions/common/manifest_handlers/incognito_info.h" | 32 #include "extensions/common/manifest_handlers/incognito_info.h" |
| 32 | 33 |
| 33 using base::DictionaryValue; | 34 using base::DictionaryValue; |
| 34 using base::ListValue; | 35 using base::ListValue; |
| 35 using content::BrowserContext; | 36 using content::BrowserContext; |
| 36 using content::BrowserThread; | 37 using content::BrowserThread; |
| 37 | 38 |
| 38 namespace extensions { | 39 namespace extensions { |
| 39 | 40 |
| 40 namespace { | 41 namespace { |
| 41 | 42 |
| 42 void DoNothing(ExtensionHost* host) {} | 43 void DoNothing(ExtensionHost* host) {} |
| 43 | 44 |
| 44 // A dictionary of event names to lists of filters that this extension has | 45 // A dictionary of event names to lists of filters that this extension has |
| 45 // registered from its lazy background page. | 46 // registered from its lazy background page. |
| 46 const char kFilteredEvents[] = "filtered_events"; | 47 const char kFilteredEvents[] = "filtered_events"; |
| 47 | 48 |
| 49 // Sends a notification about an event to the API activity monitor on the |
| 50 // UI thread. Can be called from any thread. |
| 51 void NotifyApiEventDispatched(void* browser_context_id, |
| 52 const std::string& extension_id, |
| 53 const std::string& event_name, |
| 54 scoped_ptr<ListValue> args) { |
| 55 // The ApiActivityMonitor can only be accessed from the UI thread. |
| 56 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 57 BrowserThread::PostTask( |
| 58 BrowserThread::UI, |
| 59 FROM_HERE, |
| 60 base::Bind(&NotifyApiEventDispatched, |
| 61 browser_context_id, |
| 62 extension_id, |
| 63 event_name, |
| 64 base::Passed(&args))); |
| 65 return; |
| 66 } |
| 67 |
| 68 // Notify the ApiActivityMonitor about the event dispatch. |
| 69 BrowserContext* context = static_cast<BrowserContext*>(browser_context_id); |
| 70 if (!ExtensionsBrowserClient::Get()->IsValidContext(context)) |
| 71 return; |
| 72 ApiActivityMonitor* monitor = |
| 73 ExtensionsBrowserClient::Get()->GetApiActivityMonitor(context); |
| 74 if (monitor) |
| 75 monitor->OnApiEventDispatched(extension_id, event_name, args.Pass()); |
| 76 } |
| 77 |
| 48 } // namespace | 78 } // namespace |
| 49 | 79 |
| 50 const char EventRouter::kRegisteredEvents[] = "events"; | 80 const char EventRouter::kRegisteredEvents[] = "events"; |
| 51 | 81 |
| 52 struct EventRouter::ListenerProcess { | 82 struct EventRouter::ListenerProcess { |
| 53 content::RenderProcessHost* process; | 83 content::RenderProcessHost* process; |
| 54 std::string extension_id; | 84 std::string extension_id; |
| 55 | 85 |
| 56 ListenerProcess(content::RenderProcessHost* process, | 86 ListenerProcess(content::RenderProcessHost* process, |
| 57 const std::string& extension_id) | 87 const std::string& extension_id) |
| 58 : process(process), extension_id(extension_id) {} | 88 : process(process), extension_id(extension_id) {} |
| 59 | 89 |
| 60 bool operator<(const ListenerProcess& that) const { | 90 bool operator<(const ListenerProcess& that) const { |
| 61 if (process < that.process) | 91 if (process < that.process) |
| 62 return true; | 92 return true; |
| 63 if (process == that.process && extension_id < that.extension_id) | 93 if (process == that.process && extension_id < that.extension_id) |
| 64 return true; | 94 return true; |
| 65 return false; | 95 return false; |
| 66 } | 96 } |
| 67 }; | 97 }; |
| 68 | 98 |
| 69 // static | 99 // static |
| 70 void EventRouter::NotifyExtensionDispatchObserverOnUIThread( | |
| 71 void* browser_context_id, | |
| 72 scoped_ptr<EventDispatchInfo> details) { | |
| 73 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
| 74 BrowserThread::PostTask( | |
| 75 BrowserThread::UI, | |
| 76 FROM_HERE, | |
| 77 base::Bind(&NotifyExtensionDispatchObserverOnUIThread, | |
| 78 browser_context_id, base::Passed(&details))); | |
| 79 } else { | |
| 80 BrowserContext* context = | |
| 81 reinterpret_cast<BrowserContext*>(browser_context_id); | |
| 82 if (!ExtensionsBrowserClient::Get()->IsValidContext(context)) | |
| 83 return; | |
| 84 ExtensionSystem* extension_system = ExtensionSystem::Get(context); | |
| 85 EventRouter* event_router = extension_system->event_router(); | |
| 86 if (!event_router) | |
| 87 return; | |
| 88 if (event_router->event_dispatch_observer_) { | |
| 89 event_router->event_dispatch_observer_->OnWillDispatchEvent( | |
| 90 details.Pass()); | |
| 91 } | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 // static | |
| 96 void EventRouter::DispatchExtensionMessage(IPC::Sender* ipc_sender, | 100 void EventRouter::DispatchExtensionMessage(IPC::Sender* ipc_sender, |
| 97 void* browser_context_id, | 101 void* browser_context_id, |
| 98 const std::string& extension_id, | 102 const std::string& extension_id, |
| 99 const std::string& event_name, | 103 const std::string& event_name, |
| 100 ListValue* event_args, | 104 ListValue* event_args, |
| 101 UserGestureState user_gesture, | 105 UserGestureState user_gesture, |
| 102 const EventFilteringInfo& info) { | 106 const EventFilteringInfo& info) { |
| 103 NotifyExtensionDispatchObserverOnUIThread( | 107 NotifyApiEventDispatched(browser_context_id, |
| 104 browser_context_id, | 108 extension_id, |
| 105 make_scoped_ptr(new EventDispatchInfo( | 109 event_name, |
| 106 extension_id, | 110 make_scoped_ptr(event_args->DeepCopy())); |
| 107 event_name, | |
| 108 make_scoped_ptr(event_args->DeepCopy())))); | |
| 109 | 111 |
| 110 ListValue args; | 112 ListValue args; |
| 111 args.Set(0, new base::StringValue(event_name)); | 113 args.Set(0, new base::StringValue(event_name)); |
| 112 args.Set(1, event_args); | 114 args.Set(1, event_args); |
| 113 args.Set(2, info.AsValue().release()); | 115 args.Set(2, info.AsValue().release()); |
| 114 ipc_sender->Send(new ExtensionMsg_MessageInvoke( | 116 ipc_sender->Send(new ExtensionMsg_MessageInvoke( |
| 115 MSG_ROUTING_CONTROL, | 117 MSG_ROUTING_CONTROL, |
| 116 extension_id, | 118 extension_id, |
| 117 kEventBindings, | 119 kEventBindings, |
| 118 "dispatchEvent", | 120 "dispatchEvent", |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 FROM_HERE, | 155 FROM_HERE, |
| 154 base::Bind(&EventRouter::IncrementInFlightEventsOnUI, | 156 base::Bind(&EventRouter::IncrementInFlightEventsOnUI, |
| 155 browser_context_id, | 157 browser_context_id, |
| 156 extension_id)); | 158 extension_id)); |
| 157 } | 159 } |
| 158 | 160 |
| 159 EventRouter::EventRouter(BrowserContext* browser_context, | 161 EventRouter::EventRouter(BrowserContext* browser_context, |
| 160 ExtensionPrefs* extension_prefs) | 162 ExtensionPrefs* extension_prefs) |
| 161 : browser_context_(browser_context), | 163 : browser_context_(browser_context), |
| 162 extension_prefs_(extension_prefs), | 164 extension_prefs_(extension_prefs), |
| 163 listeners_(this), | 165 listeners_(this) { |
| 164 event_dispatch_observer_(NULL) { | |
| 165 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 166 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| 166 content::NotificationService::AllSources()); | 167 content::NotificationService::AllSources()); |
| 167 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | 168 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 168 content::NotificationService::AllSources()); | 169 content::NotificationService::AllSources()); |
| 169 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_ENABLED, | 170 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_ENABLED, |
| 170 content::Source<BrowserContext>(browser_context_)); | 171 content::Source<BrowserContext>(browser_context_)); |
| 171 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, | 172 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
| 172 content::Source<BrowserContext>(browser_context_)); | 173 content::Source<BrowserContext>(browser_context_)); |
| 173 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 174 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 174 content::Source<BrowserContext>(browser_context_)); | 175 content::Source<BrowserContext>(browser_context_)); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 202 std::vector<ObserverMap::iterator> iters_to_remove; | 203 std::vector<ObserverMap::iterator> iters_to_remove; |
| 203 for (ObserverMap::iterator iter = observers_.begin(); | 204 for (ObserverMap::iterator iter = observers_.begin(); |
| 204 iter != observers_.end(); ++iter) { | 205 iter != observers_.end(); ++iter) { |
| 205 if (iter->second == observer) | 206 if (iter->second == observer) |
| 206 iters_to_remove.push_back(iter); | 207 iters_to_remove.push_back(iter); |
| 207 } | 208 } |
| 208 for (size_t i = 0; i < iters_to_remove.size(); ++i) | 209 for (size_t i = 0; i < iters_to_remove.size(); ++i) |
| 209 observers_.erase(iters_to_remove[i]); | 210 observers_.erase(iters_to_remove[i]); |
| 210 } | 211 } |
| 211 | 212 |
| 212 void EventRouter::SetEventDispatchObserver(EventDispatchObserver* observer) { | |
| 213 CHECK(!event_dispatch_observer_); | |
| 214 event_dispatch_observer_ = observer; | |
| 215 } | |
| 216 | |
| 217 void EventRouter::OnListenerAdded(const EventListener* listener) { | 213 void EventRouter::OnListenerAdded(const EventListener* listener) { |
| 218 const EventListenerInfo details( | 214 const EventListenerInfo details( |
| 219 listener->event_name, | 215 listener->event_name, |
| 220 listener->extension_id, | 216 listener->extension_id, |
| 221 listener->process ? listener->process->GetBrowserContext() : NULL); | 217 listener->process ? listener->process->GetBrowserContext() : NULL); |
| 222 std::string base_event_name = GetBaseEventName(listener->event_name); | 218 std::string base_event_name = GetBaseEventName(listener->event_name); |
| 223 ObserverMap::iterator observer = observers_.find(base_event_name); | 219 ObserverMap::iterator observer = observers_.find(base_event_name); |
| 224 if (observer != observers_.end()) | 220 if (observer != observers_.end()) |
| 225 observer->second->OnListenerAdded(details); | 221 observer->second->OnListenerAdded(details); |
| 226 } | 222 } |
| (...skipping 539 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 766 return copy; | 762 return copy; |
| 767 } | 763 } |
| 768 | 764 |
| 769 EventListenerInfo::EventListenerInfo(const std::string& event_name, | 765 EventListenerInfo::EventListenerInfo(const std::string& event_name, |
| 770 const std::string& extension_id, | 766 const std::string& extension_id, |
| 771 content::BrowserContext* browser_context) | 767 content::BrowserContext* browser_context) |
| 772 : event_name(event_name), | 768 : event_name(event_name), |
| 773 extension_id(extension_id), | 769 extension_id(extension_id), |
| 774 browser_context(browser_context) {} | 770 browser_context(browser_context) {} |
| 775 | 771 |
| 776 EventDispatchInfo::EventDispatchInfo(const std::string& extension_id, | |
| 777 const std::string& event_name, | |
| 778 scoped_ptr<ListValue> event_args) | |
| 779 : extension_id(extension_id), | |
| 780 event_name(event_name), | |
| 781 event_args(event_args.Pass()) {} | |
| 782 | |
| 783 EventDispatchInfo::~EventDispatchInfo() {} | |
| 784 | |
| 785 } // namespace extensions | 772 } // namespace extensions |
| OLD | NEW |