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 "chrome/browser/extensions/event_router.h" | 5 #include "chrome/browser/extensions/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.h" | 11 #include "base/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 "base/version.h" | 14 #include "base/version.h" |
15 #include "chrome/browser/browser_process.h" | 15 #include "chrome/browser/browser_process.h" |
16 #include "chrome/browser/extensions/activity_log.h" | |
16 #include "chrome/browser/extensions/api/runtime/runtime_api.h" | 17 #include "chrome/browser/extensions/api/runtime/runtime_api.h" |
17 #include "chrome/browser/extensions/api/web_request/web_request_api.h" | 18 #include "chrome/browser/extensions/api/web_request/web_request_api.h" |
18 #include "chrome/browser/extensions/event_names.h" | 19 #include "chrome/browser/extensions/event_names.h" |
19 #include "chrome/browser/extensions/extension_host.h" | 20 #include "chrome/browser/extensions/extension_host.h" |
20 #include "chrome/browser/extensions/extension_prefs.h" | 21 #include "chrome/browser/extensions/extension_prefs.h" |
21 #include "chrome/browser/extensions/extension_process_manager.h" | 22 #include "chrome/browser/extensions/extension_process_manager.h" |
22 #include "chrome/browser/extensions/extension_service.h" | 23 #include "chrome/browser/extensions/extension_service.h" |
23 #include "chrome/browser/extensions/extension_system.h" | 24 #include "chrome/browser/extensions/extension_system.h" |
24 #include "chrome/browser/extensions/lazy_background_task_queue.h" | 25 #include "chrome/browser/extensions/lazy_background_task_queue.h" |
25 #include "chrome/browser/extensions/process_map.h" | 26 #include "chrome/browser/extensions/process_map.h" |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
79 bool operator<(const ListenerProcess& that) const { | 80 bool operator<(const ListenerProcess& that) const { |
80 if (process < that.process) | 81 if (process < that.process) |
81 return true; | 82 return true; |
82 if (process == that.process && extension_id < that.extension_id) | 83 if (process == that.process && extension_id < that.extension_id) |
83 return true; | 84 return true; |
84 return false; | 85 return false; |
85 } | 86 } |
86 }; | 87 }; |
87 | 88 |
88 // static | 89 // static |
90 void EventRouter::LogExtensionEventMessage(Profile* profile, | |
91 const std::string& extension_id, | |
92 const std::string& event_name, | |
93 scoped_ptr<ListValue> event_args) { | |
94 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
95 BrowserThread::PostTask(BrowserThread::UI, | |
96 FROM_HERE, | |
97 base::Bind(&LogExtensionEventMessage, | |
98 profile, | |
99 extension_id, | |
100 event_name, | |
101 Passed(event_args.Pass()))); | |
102 } else { | |
103 const Extension* extension = extensions::ExtensionSystem::Get(profile) | |
104 ->extension_service()->extensions()->GetByID(extension_id); | |
Eric Dingle
2013/01/24 16:16:34
I *think* that the first -> should be up on the pr
mvrable
2013/01/24 18:12:31
I think you are right. I did a quick search and f
| |
105 extensions::ActivityLog::GetInstance(profile) | |
Eric Dingle
2013/01/24 16:16:34
I would format this as:
extensions::ActivityL
mvrable
2013/01/24 18:12:31
Done.
| |
106 ->LogEventAction(extension, event_name, event_args.get(), ""); | |
107 } | |
108 } | |
109 | |
110 // static | |
89 void EventRouter::DispatchExtensionMessage(IPC::Sender* ipc_sender, | 111 void EventRouter::DispatchExtensionMessage(IPC::Sender* ipc_sender, |
112 Profile* profile, | |
90 const std::string& extension_id, | 113 const std::string& extension_id, |
91 const std::string& event_name, | 114 const std::string& event_name, |
92 ListValue* event_args, | 115 ListValue* event_args, |
93 const GURL& event_url, | 116 const GURL& event_url, |
94 UserGestureState user_gesture, | 117 UserGestureState user_gesture, |
95 const EventFilteringInfo& info) { | 118 const EventFilteringInfo& info) { |
119 LogExtensionEventMessage(profile, extension_id, event_name, | |
120 scoped_ptr<ListValue>(event_args->DeepCopy())); | |
Eric Dingle
2013/01/24 16:16:34
You should indent this line +4 to be aligned with
mvrable
2013/01/24 18:12:31
Done.
| |
121 | |
96 ListValue args; | 122 ListValue args; |
97 args.Set(0, Value::CreateStringValue(event_name)); | 123 args.Set(0, Value::CreateStringValue(event_name)); |
98 args.Set(1, event_args); | 124 args.Set(1, event_args); |
99 args.Set(2, info.AsValue().release()); | 125 args.Set(2, info.AsValue().release()); |
100 ipc_sender->Send(new ExtensionMsg_MessageInvoke(MSG_ROUTING_CONTROL, | 126 ipc_sender->Send(new ExtensionMsg_MessageInvoke(MSG_ROUTING_CONTROL, |
101 extension_id, kDispatchEvent, args, event_url, | 127 extension_id, kDispatchEvent, args, event_url, |
102 user_gesture == USER_GESTURE_ENABLED)); | 128 user_gesture == USER_GESTURE_ENABLED)); |
103 | 129 |
104 // DispatchExtensionMessage does _not_ take ownership of event_args, so we | 130 // DispatchExtensionMessage does _not_ take ownership of event_args, so we |
105 // must ensure that the destruction of args does not attempt to free it. | 131 // must ensure that the destruction of args does not attempt to free it. |
106 Value* removed_event_args = NULL; | 132 Value* removed_event_args = NULL; |
107 args.Remove(1, &removed_event_args); | 133 args.Remove(1, &removed_event_args); |
108 } | 134 } |
109 | 135 |
110 // static | 136 // static |
111 void EventRouter::DispatchEvent(IPC::Sender* ipc_sender, | 137 void EventRouter::DispatchEvent(IPC::Sender* ipc_sender, |
138 Profile* profile, | |
112 const std::string& extension_id, | 139 const std::string& extension_id, |
113 const std::string& event_name, | 140 const std::string& event_name, |
114 scoped_ptr<ListValue> event_args, | 141 scoped_ptr<ListValue> event_args, |
115 const GURL& event_url, | 142 const GURL& event_url, |
116 UserGestureState user_gesture, | 143 UserGestureState user_gesture, |
117 const EventFilteringInfo& info) { | 144 const EventFilteringInfo& info) { |
118 DispatchExtensionMessage(ipc_sender, extension_id, event_name, | 145 DispatchExtensionMessage(ipc_sender, profile, extension_id, event_name, |
119 event_args.get(), event_url, user_gesture, info); | 146 event_args.get(), event_url, user_gesture, info); |
120 } | 147 } |
121 | 148 |
122 EventRouter::EventRouter(Profile* profile, ExtensionPrefs* extension_prefs) | 149 EventRouter::EventRouter(Profile* profile, ExtensionPrefs* extension_prefs) |
123 : profile_(profile), | 150 : profile_(profile), |
124 listeners_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | 151 listeners_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
152 activity_logger_(ActivityLog::GetInstance(profile)), | |
125 dispatch_chrome_updated_event_(false) { | 153 dispatch_chrome_updated_event_(false) { |
126 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 154 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
127 content::NotificationService::AllSources()); | 155 content::NotificationService::AllSources()); |
128 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | 156 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
129 content::NotificationService::AllSources()); | 157 content::NotificationService::AllSources()); |
130 registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY, | 158 registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY, |
131 content::Source<Profile>(profile_)); | 159 content::Source<Profile>(profile_)); |
132 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_ENABLED, | 160 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_ENABLED, |
133 content::Source<Profile>(profile_)); | 161 content::Source<Profile>(profile_)); |
134 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, | 162 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
183 | 211 |
184 void EventRouter::OnListenerAdded(const EventListener* listener) { | 212 void EventRouter::OnListenerAdded(const EventListener* listener) { |
185 const std::string& event_name = listener->event_name; | 213 const std::string& event_name = listener->event_name; |
186 const EventListenerInfo details(event_name, listener->extension_id); | 214 const EventListenerInfo details(event_name, listener->extension_id); |
187 ObserverMap::iterator observer = observers_.find(event_name); | 215 ObserverMap::iterator observer = observers_.find(event_name); |
188 if (observer != observers_.end()) | 216 if (observer != observers_.end()) |
189 observer->second->OnListenerAdded(details); | 217 observer->second->OnListenerAdded(details); |
190 | 218 |
191 if (SystemInfoEventRouter::IsSystemInfoEvent(event_name)) | 219 if (SystemInfoEventRouter::IsSystemInfoEvent(event_name)) |
192 SystemInfoEventRouter::GetInstance()->AddEventListener(event_name); | 220 SystemInfoEventRouter::GetInstance()->AddEventListener(event_name); |
221 | |
222 const Extension* extension = extensions::ExtensionSystem::Get(profile_)-> | |
223 extension_service()->GetExtensionById(listener->extension_id, | |
224 ExtensionService::INCLUDE_ENABLED); | |
225 if (extension) { | |
226 scoped_ptr<ListValue> args(new ListValue()); | |
227 if (listener->filter) | |
228 args->Append(listener->filter->DeepCopy()); | |
229 activity_logger_->LogAPIAction(extension, | |
230 event_name + ".addListener", | |
231 args.get(), | |
232 ""); | |
233 } | |
193 } | 234 } |
194 | 235 |
195 void EventRouter::OnListenerRemoved(const EventListener* listener) { | 236 void EventRouter::OnListenerRemoved(const EventListener* listener) { |
196 const std::string& event_name = listener->event_name; | 237 const std::string& event_name = listener->event_name; |
197 const EventListenerInfo details(event_name, listener->extension_id); | 238 const EventListenerInfo details(event_name, listener->extension_id); |
198 ObserverMap::iterator observer = observers_.find(event_name); | 239 ObserverMap::iterator observer = observers_.find(event_name); |
199 if (observer != observers_.end()) | 240 if (observer != observers_.end()) |
200 observer->second->OnListenerRemoved(details); | 241 observer->second->OnListenerRemoved(details); |
201 | 242 |
202 BrowserThread::PostTask( | 243 BrowserThread::PostTask( |
203 BrowserThread::IO, FROM_HERE, | 244 BrowserThread::IO, FROM_HERE, |
204 base::Bind(&NotifyEventListenerRemovedOnIOThread, | 245 base::Bind(&NotifyEventListenerRemovedOnIOThread, |
205 profile_, listener->extension_id, event_name)); | 246 profile_, listener->extension_id, event_name)); |
206 | 247 |
207 if (SystemInfoEventRouter::IsSystemInfoEvent(event_name)) | 248 if (SystemInfoEventRouter::IsSystemInfoEvent(event_name)) |
208 SystemInfoEventRouter::GetInstance()->RemoveEventListener(event_name); | 249 SystemInfoEventRouter::GetInstance()->RemoveEventListener(event_name); |
250 | |
251 const Extension* extension = extensions::ExtensionSystem::Get(profile_)-> | |
252 extension_service()->GetExtensionById(listener->extension_id, | |
253 ExtensionService::INCLUDE_ENABLED); | |
254 if (extension) { | |
255 scoped_ptr<ListValue> args(new ListValue()); | |
256 activity_logger_->LogAPIAction(extension, | |
257 event_name + ".removeListener", | |
258 args.get(), | |
259 ""); | |
260 } | |
209 } | 261 } |
210 | 262 |
211 void EventRouter::AddLazyEventListener(const std::string& event_name, | 263 void EventRouter::AddLazyEventListener(const std::string& event_name, |
212 const std::string& extension_id) { | 264 const std::string& extension_id) { |
213 scoped_ptr<EventListener> listener(new EventListener( | 265 scoped_ptr<EventListener> listener(new EventListener( |
214 event_name, extension_id, NULL, scoped_ptr<DictionaryValue>())); | 266 event_name, extension_id, NULL, scoped_ptr<DictionaryValue>())); |
215 bool is_new = listeners_.AddListener(listener.Pass()); | 267 bool is_new = listeners_.AddListener(listener.Pass()); |
216 | 268 |
217 if (is_new) { | 269 if (is_new) { |
218 ExtensionPrefs* prefs = extensions::ExtensionSystem::Get(profile_)-> | 270 ExtensionPrefs* prefs = extensions::ExtensionSystem::Get(profile_)-> |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
419 } | 471 } |
420 | 472 |
421 if (!CanDispatchEventToProfile(listener_profile, extension, event)) | 473 if (!CanDispatchEventToProfile(listener_profile, extension, event)) |
422 return; | 474 return; |
423 | 475 |
424 if (!event->will_dispatch_callback.is_null()) { | 476 if (!event->will_dispatch_callback.is_null()) { |
425 event->will_dispatch_callback.Run(listener_profile, extension, | 477 event->will_dispatch_callback.Run(listener_profile, extension, |
426 event->event_args.get()); | 478 event->event_args.get()); |
427 } | 479 } |
428 | 480 |
429 DispatchExtensionMessage(process, extension_id, | 481 DispatchExtensionMessage(process, listener_profile, extension_id, |
430 event->event_name, event->event_args.get(), | 482 event->event_name, event->event_args.get(), |
431 event->event_url, event->user_gesture, | 483 event->event_url, event->user_gesture, |
432 event->filter_info); | 484 event->filter_info); |
433 IncrementInFlightEvents(listener_profile, extension); | 485 IncrementInFlightEvents(listener_profile, extension); |
434 } | 486 } |
435 | 487 |
436 bool EventRouter::CanDispatchEventToProfile(Profile* profile, | 488 bool EventRouter::CanDispatchEventToProfile(Profile* profile, |
437 const Extension* extension, | 489 const Extension* extension, |
438 const linked_ptr<Event>& event) { | 490 const linked_ptr<Event>& event) { |
439 // Is this event from a different profile than the renderer (ie, an | 491 // Is this event from a different profile than the renderer (ie, an |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
648 copy->will_dispatch_callback = will_dispatch_callback; | 700 copy->will_dispatch_callback = will_dispatch_callback; |
649 return copy; | 701 return copy; |
650 } | 702 } |
651 | 703 |
652 EventListenerInfo::EventListenerInfo(const std::string& event_name, | 704 EventListenerInfo::EventListenerInfo(const std::string& event_name, |
653 const std::string& extension_id) | 705 const std::string& extension_id) |
654 : event_name(event_name), | 706 : event_name(event_name), |
655 extension_id(extension_id) {} | 707 extension_id(extension_id) {} |
656 | 708 |
657 } // namespace extensions | 709 } // namespace extensions |
OLD | NEW |