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/extension_event_router.h" | 5 #include "chrome/browser/extensions/extension_event_router.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/values.h" | 9 #include "base/values.h" |
10 #include "chrome/browser/extensions/extension_devtools_manager.h" | 10 #include "chrome/browser/extensions/extension_devtools_manager.h" |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 } | 61 } |
62 }; | 62 }; |
63 | 63 |
64 struct ExtensionEventRouter::ExtensionEvent { | 64 struct ExtensionEventRouter::ExtensionEvent { |
65 std::string extension_id; | 65 std::string extension_id; |
66 std::string event_name; | 66 std::string event_name; |
67 std::string event_args; | 67 std::string event_args; |
68 GURL event_url; | 68 GURL event_url; |
69 Profile* restrict_to_profile; | 69 Profile* restrict_to_profile; |
70 std::string cross_incognito_args; | 70 std::string cross_incognito_args; |
| 71 bool user_caused; |
71 | 72 |
72 ExtensionEvent(const std::string& extension_id, | 73 ExtensionEvent(const std::string& extension_id, |
73 const std::string& event_name, | 74 const std::string& event_name, |
74 const std::string& event_args, | 75 const std::string& event_args, |
75 const GURL& event_url, | 76 const GURL& event_url, |
76 Profile* restrict_to_profile, | 77 Profile* restrict_to_profile, |
77 const std::string& cross_incognito_args) | 78 const std::string& cross_incognito_args, |
| 79 bool user_caused) |
78 : extension_id(extension_id), | 80 : extension_id(extension_id), |
79 event_name(event_name), | 81 event_name(event_name), |
80 event_args(event_args), | 82 event_args(event_args), |
81 event_url(event_url), | 83 event_url(event_url), |
82 restrict_to_profile(restrict_to_profile), | 84 restrict_to_profile(restrict_to_profile), |
83 cross_incognito_args(cross_incognito_args) {} | 85 cross_incognito_args(cross_incognito_args), |
| 86 user_caused(user_caused) {} |
84 }; | 87 }; |
85 | 88 |
86 // static | 89 // static |
87 void ExtensionEventRouter::DispatchEvent(IPC::Message::Sender* ipc_sender, | 90 void ExtensionEventRouter::DispatchEvent(IPC::Message::Sender* ipc_sender, |
88 const std::string& extension_id, | 91 const std::string& extension_id, |
89 const std::string& event_name, | 92 const std::string& event_name, |
90 const std::string& event_args, | 93 const std::string& event_args, |
91 const GURL& event_url) { | 94 const GURL& event_url, |
| 95 bool user_caused) { |
92 ListValue args; | 96 ListValue args; |
93 args.Set(0, Value::CreateStringValue(event_name)); | 97 args.Set(0, Value::CreateStringValue(event_name)); |
94 args.Set(1, Value::CreateStringValue(event_args)); | 98 args.Set(1, Value::CreateStringValue(event_args)); |
95 ipc_sender->Send(new ExtensionMsg_MessageInvoke(MSG_ROUTING_CONTROL, | 99 ipc_sender->Send(new ExtensionMsg_MessageInvoke(MSG_ROUTING_CONTROL, |
96 extension_id, kDispatchEvent, args, event_url)); | 100 extension_id, kDispatchEvent, args, event_url, user_caused)); |
97 } | 101 } |
98 | 102 |
99 ExtensionEventRouter::ExtensionEventRouter(Profile* profile) | 103 ExtensionEventRouter::ExtensionEventRouter(Profile* profile) |
100 : profile_(profile), | 104 : profile_(profile), |
101 extension_devtools_manager_(profile->GetExtensionDevToolsManager()) { | 105 extension_devtools_manager_(profile->GetExtensionDevToolsManager()) { |
102 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 106 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
103 content::NotificationService::AllSources()); | 107 content::NotificationService::AllSources()); |
104 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | 108 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
105 content::NotificationService::AllSources()); | 109 content::NotificationService::AllSources()); |
106 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING, | 110 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING, |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 if (listener->extension_id == extension_id) | 177 if (listener->extension_id == extension_id) |
174 return true; | 178 return true; |
175 } | 179 } |
176 return false; | 180 return false; |
177 } | 181 } |
178 | 182 |
179 void ExtensionEventRouter::DispatchEventToRenderers( | 183 void ExtensionEventRouter::DispatchEventToRenderers( |
180 const std::string& event_name, | 184 const std::string& event_name, |
181 const std::string& event_args, | 185 const std::string& event_args, |
182 Profile* restrict_to_profile, | 186 Profile* restrict_to_profile, |
183 const GURL& event_url) { | 187 const GURL& event_url, |
| 188 bool user_caused) { |
184 linked_ptr<ExtensionEvent> event( | 189 linked_ptr<ExtensionEvent> event( |
185 new ExtensionEvent("", event_name, event_args, event_url, | 190 new ExtensionEvent("", event_name, event_args, event_url, |
186 restrict_to_profile, "")); | 191 restrict_to_profile, "", user_caused)); |
187 DispatchEventImpl(event, false); | 192 DispatchEventImpl(event, false); |
188 } | 193 } |
189 | 194 |
190 void ExtensionEventRouter::DispatchEventToExtension( | 195 void ExtensionEventRouter::DispatchEventToExtension( |
191 const std::string& extension_id, | 196 const std::string& extension_id, |
192 const std::string& event_name, | 197 const std::string& event_name, |
193 const std::string& event_args, | 198 const std::string& event_args, |
194 Profile* restrict_to_profile, | 199 Profile* restrict_to_profile, |
195 const GURL& event_url) { | 200 const GURL& event_url, |
| 201 bool user_caused) { |
196 DCHECK(!extension_id.empty()); | 202 DCHECK(!extension_id.empty()); |
197 linked_ptr<ExtensionEvent> event( | 203 linked_ptr<ExtensionEvent> event( |
198 new ExtensionEvent(extension_id, event_name, event_args, event_url, | 204 new ExtensionEvent(extension_id, event_name, event_args, event_url, |
199 restrict_to_profile, "")); | 205 restrict_to_profile, "", user_caused)); |
200 DispatchEventImpl(event, false); | 206 DispatchEventImpl(event, false); |
201 } | 207 } |
202 | 208 |
203 void ExtensionEventRouter::DispatchEventsToRenderersAcrossIncognito( | 209 void ExtensionEventRouter::DispatchEventsToRenderersAcrossIncognito( |
204 const std::string& event_name, | 210 const std::string& event_name, |
205 const std::string& event_args, | 211 const std::string& event_args, |
206 Profile* restrict_to_profile, | 212 Profile* restrict_to_profile, |
207 const std::string& cross_incognito_args, | 213 const std::string& cross_incognito_args, |
208 const GURL& event_url) { | 214 const GURL& event_url, |
| 215 bool user_caused) { |
209 linked_ptr<ExtensionEvent> event( | 216 linked_ptr<ExtensionEvent> event( |
210 new ExtensionEvent("", event_name, event_args, event_url, | 217 new ExtensionEvent("", event_name, event_args, event_url, |
211 restrict_to_profile, cross_incognito_args)); | 218 restrict_to_profile, cross_incognito_args, |
| 219 user_caused)); |
212 DispatchEventImpl(event, false); | 220 DispatchEventImpl(event, false); |
213 } | 221 } |
214 | 222 |
215 bool ExtensionEventRouter::CanDispatchEventNow( | 223 bool ExtensionEventRouter::CanDispatchEventNow( |
216 const std::string& extension_id) { | 224 const std::string& extension_id) { |
217 if (extension_id.empty()) | 225 if (extension_id.empty()) |
218 // TODO(mpcomplete): We need to test this per-extension, rather than | 226 // TODO(mpcomplete): We need to test this per-extension, rather than |
219 // globally. | 227 // globally. |
220 return true; | 228 return true; |
221 | 229 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 // Is this event from a different profile than the renderer (ie, an | 296 // Is this event from a different profile than the renderer (ie, an |
289 // incognito tab event sent to a normal process, or vice versa). | 297 // incognito tab event sent to a normal process, or vice versa). |
290 bool cross_incognito = event->restrict_to_profile && | 298 bool cross_incognito = event->restrict_to_profile && |
291 listener_profile != event->restrict_to_profile; | 299 listener_profile != event->restrict_to_profile; |
292 // Send the event with different arguments to extensions that can't | 300 // Send the event with different arguments to extensions that can't |
293 // cross incognito, if necessary. | 301 // cross incognito, if necessary. |
294 if (cross_incognito && !service->CanCrossIncognito(extension)) { | 302 if (cross_incognito && !service->CanCrossIncognito(extension)) { |
295 if (!event->cross_incognito_args.empty()) { | 303 if (!event->cross_incognito_args.empty()) { |
296 DispatchEvent(listener->process, listener->extension_id, | 304 DispatchEvent(listener->process, listener->extension_id, |
297 event->event_name, event->cross_incognito_args, | 305 event->event_name, event->cross_incognito_args, |
298 event->event_url); | 306 event->event_url, event->user_caused); |
299 IncrementInFlightEvents(extension); | 307 IncrementInFlightEvents(extension); |
300 } | 308 } |
301 continue; | 309 continue; |
302 } | 310 } |
303 | 311 |
304 DispatchEvent(listener->process, listener->extension_id, | 312 DispatchEvent(listener->process, listener->extension_id, |
305 event->event_name, event->event_args, event->event_url); | 313 event->event_name, event->event_args, |
| 314 event->event_url, event->user_caused); |
306 IncrementInFlightEvents(extension); | 315 IncrementInFlightEvents(extension); |
307 } | 316 } |
308 } | 317 } |
309 | 318 |
310 void ExtensionEventRouter::IncrementInFlightEvents(const Extension* extension) { | 319 void ExtensionEventRouter::IncrementInFlightEvents(const Extension* extension) { |
311 if (!extension->background_page_persists()) | 320 if (!extension->background_page_persists()) |
312 in_flight_events_[extension->id()]++; | 321 in_flight_events_[extension->id()]++; |
313 } | 322 } |
314 | 323 |
315 void ExtensionEventRouter::OnExtensionEventAck( | 324 void ExtensionEventRouter::OnExtensionEventAck( |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
386 ExtensionHost* eh = content::Details<ExtensionHost>(details).ptr(); | 395 ExtensionHost* eh = content::Details<ExtensionHost>(details).ptr(); |
387 DispatchPendingEvents(eh->extension_id()); | 396 DispatchPendingEvents(eh->extension_id()); |
388 break; | 397 break; |
389 } | 398 } |
390 // TODO(tessamac): if background page crashed/failed clear queue. | 399 // TODO(tessamac): if background page crashed/failed clear queue. |
391 default: | 400 default: |
392 NOTREACHED(); | 401 NOTREACHED(); |
393 return; | 402 return; |
394 } | 403 } |
395 } | 404 } |
OLD | NEW |