| 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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 return false; | 60 return false; |
| 61 } | 61 } |
| 62 }; | 62 }; |
| 63 | 63 |
| 64 struct ExtensionEventRouter::ExtensionEvent { | 64 struct ExtensionEventRouter::ExtensionEvent { |
| 65 std::string event_name; | 65 std::string event_name; |
| 66 std::string event_args; | 66 std::string event_args; |
| 67 GURL event_url; | 67 GURL event_url; |
| 68 Profile* restrict_to_profile; | 68 Profile* restrict_to_profile; |
| 69 std::string cross_incognito_args; | 69 std::string cross_incognito_args; |
| 70 UserGesture user_gesture; |
| 70 | 71 |
| 71 ExtensionEvent(const std::string& event_name, | 72 ExtensionEvent(const std::string& event_name, |
| 72 const std::string& event_args, | 73 const std::string& event_args, |
| 73 const GURL& event_url, | 74 const GURL& event_url, |
| 74 Profile* restrict_to_profile, | 75 Profile* restrict_to_profile, |
| 75 const std::string& cross_incognito_args) | 76 const std::string& cross_incognito_args, |
| 77 UserGesture user_gesture) |
| 76 : event_name(event_name), | 78 : event_name(event_name), |
| 77 event_args(event_args), | 79 event_args(event_args), |
| 78 event_url(event_url), | 80 event_url(event_url), |
| 79 restrict_to_profile(restrict_to_profile), | 81 restrict_to_profile(restrict_to_profile), |
| 80 cross_incognito_args(cross_incognito_args) {} | 82 cross_incognito_args(cross_incognito_args), |
| 83 user_gesture(user_gesture) {} |
| 81 }; | 84 }; |
| 82 | 85 |
| 83 // static | 86 // static |
| 84 void ExtensionEventRouter::DispatchEvent(IPC::Message::Sender* ipc_sender, | 87 void ExtensionEventRouter::DispatchEvent(IPC::Message::Sender* ipc_sender, |
| 85 const std::string& extension_id, | 88 const std::string& extension_id, |
| 86 const std::string& event_name, | 89 const std::string& event_name, |
| 87 const std::string& event_args, | 90 const std::string& event_args, |
| 88 const GURL& event_url) { | 91 const GURL& event_url, |
| 92 UserGesture user_gesture) { |
| 89 ListValue args; | 93 ListValue args; |
| 90 args.Set(0, Value::CreateStringValue(event_name)); | 94 args.Set(0, Value::CreateStringValue(event_name)); |
| 91 args.Set(1, Value::CreateStringValue(event_args)); | 95 args.Set(1, Value::CreateStringValue(event_args)); |
| 92 ipc_sender->Send(new ExtensionMsg_MessageInvoke(MSG_ROUTING_CONTROL, | 96 ipc_sender->Send(new ExtensionMsg_MessageInvoke(MSG_ROUTING_CONTROL, |
| 93 extension_id, kDispatchEvent, args, event_url)); | 97 extension_id, kDispatchEvent, args, event_url, |
| 98 user_gesture == kIsUserGesture)); |
| 94 } | 99 } |
| 95 | 100 |
| 96 ExtensionEventRouter::ExtensionEventRouter(Profile* profile) | 101 ExtensionEventRouter::ExtensionEventRouter(Profile* profile) |
| 97 : profile_(profile), | 102 : profile_(profile), |
| 98 extension_devtools_manager_(profile->GetExtensionDevToolsManager()) { | 103 extension_devtools_manager_(profile->GetExtensionDevToolsManager()) { |
| 99 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 104 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| 100 content::NotificationService::AllSources()); | 105 content::NotificationService::AllSources()); |
| 101 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | 106 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 102 content::NotificationService::AllSources()); | 107 content::NotificationService::AllSources()); |
| 103 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING, | 108 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING, |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 return false; | 223 return false; |
| 219 } | 224 } |
| 220 | 225 |
| 221 void ExtensionEventRouter::DispatchEventToRenderers( | 226 void ExtensionEventRouter::DispatchEventToRenderers( |
| 222 const std::string& event_name, | 227 const std::string& event_name, |
| 223 const std::string& event_args, | 228 const std::string& event_args, |
| 224 Profile* restrict_to_profile, | 229 Profile* restrict_to_profile, |
| 225 const GURL& event_url) { | 230 const GURL& event_url) { |
| 226 linked_ptr<ExtensionEvent> event( | 231 linked_ptr<ExtensionEvent> event( |
| 227 new ExtensionEvent(event_name, event_args, event_url, | 232 new ExtensionEvent(event_name, event_args, event_url, |
| 228 restrict_to_profile, "")); | 233 restrict_to_profile, "", kUnknownUserGesture)); |
| 229 DispatchEventImpl("", event, false); | 234 DispatchEventImpl("", event, false); |
| 230 } | 235 } |
| 231 | 236 |
| 232 void ExtensionEventRouter::DispatchEventToExtension( | 237 void ExtensionEventRouter::DispatchEventToExtension( |
| 233 const std::string& extension_id, | 238 const std::string& extension_id, |
| 234 const std::string& event_name, | 239 const std::string& event_name, |
| 235 const std::string& event_args, | 240 const std::string& event_args, |
| 236 Profile* restrict_to_profile, | 241 Profile* restrict_to_profile, |
| 237 const GURL& event_url) { | 242 const GURL& event_url) { |
| 238 DCHECK(!extension_id.empty()); | 243 DCHECK(!extension_id.empty()); |
| 239 linked_ptr<ExtensionEvent> event( | 244 linked_ptr<ExtensionEvent> event( |
| 240 new ExtensionEvent(event_name, event_args, event_url, | 245 new ExtensionEvent(event_name, event_args, event_url, |
| 241 restrict_to_profile, "")); | 246 restrict_to_profile, "", kUnknownUserGesture)); |
| 242 DispatchEventImpl(extension_id, event, false); | 247 DispatchEventImpl(extension_id, event, false); |
| 243 } | 248 } |
| 244 | 249 |
| 250 void ExtensionEventRouter::DispatchEventToExtension( |
| 251 const std::string& extension_id, |
| 252 const std::string& event_name, |
| 253 const std::string& event_args, |
| 254 Profile* restrict_to_profile, |
| 255 const GURL& event_url, |
| 256 UserGesture user_gesture) { |
| 257 DCHECK(!extension_id.empty()); |
| 258 linked_ptr<ExtensionEvent> event( |
| 259 new ExtensionEvent(event_name, event_args, event_url, |
| 260 restrict_to_profile, "", user_gesture)); |
| 261 DispatchEventImpl(extension_id, event, false); |
| 262 } |
| 263 |
| 245 void ExtensionEventRouter::DispatchEventsToRenderersAcrossIncognito( | 264 void ExtensionEventRouter::DispatchEventsToRenderersAcrossIncognito( |
| 246 const std::string& event_name, | 265 const std::string& event_name, |
| 247 const std::string& event_args, | 266 const std::string& event_args, |
| 248 Profile* restrict_to_profile, | 267 Profile* restrict_to_profile, |
| 249 const std::string& cross_incognito_args, | 268 const std::string& cross_incognito_args, |
| 250 const GURL& event_url) { | 269 const GURL& event_url) { |
| 251 linked_ptr<ExtensionEvent> event( | 270 linked_ptr<ExtensionEvent> event( |
| 252 new ExtensionEvent(event_name, event_args, event_url, | 271 new ExtensionEvent(event_name, event_args, event_url, |
| 253 restrict_to_profile, cross_incognito_args)); | 272 restrict_to_profile, cross_incognito_args, |
| 273 kUnknownUserGesture)); |
| 254 DispatchEventImpl("", event, false); | 274 DispatchEventImpl("", event, false); |
| 255 } | 275 } |
| 256 | 276 |
| 257 bool ExtensionEventRouter::CanDispatchEventNow(const Extension* extension) { | 277 bool ExtensionEventRouter::CanDispatchEventNow(const Extension* extension) { |
| 258 DCHECK(extension); | 278 DCHECK(extension); |
| 259 if (extension->has_background_page() && | 279 if (extension->has_background_page() && |
| 260 !extension->background_page_persists()) { | 280 !extension->background_page_persists()) { |
| 261 ExtensionProcessManager* pm = profile_->GetExtensionProcessManager(); | 281 ExtensionProcessManager* pm = profile_->GetExtensionProcessManager(); |
| 262 | 282 |
| 263 // TODO(mpcomplete): this is incorrect. We need to check whether the page | 283 // TODO(mpcomplete): this is incorrect. We need to check whether the page |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 // Is this event from a different profile than the renderer (ie, an | 343 // Is this event from a different profile than the renderer (ie, an |
| 324 // incognito tab event sent to a normal process, or vice versa). | 344 // incognito tab event sent to a normal process, or vice versa). |
| 325 bool cross_incognito = event->restrict_to_profile && | 345 bool cross_incognito = event->restrict_to_profile && |
| 326 listener_profile != event->restrict_to_profile; | 346 listener_profile != event->restrict_to_profile; |
| 327 // Send the event with different arguments to extensions that can't | 347 // Send the event with different arguments to extensions that can't |
| 328 // cross incognito, if necessary. | 348 // cross incognito, if necessary. |
| 329 if (cross_incognito && !service->CanCrossIncognito(extension)) { | 349 if (cross_incognito && !service->CanCrossIncognito(extension)) { |
| 330 if (!event->cross_incognito_args.empty()) { | 350 if (!event->cross_incognito_args.empty()) { |
| 331 DispatchEvent(listener->process, listener->extension_id, | 351 DispatchEvent(listener->process, listener->extension_id, |
| 332 event->event_name, event->cross_incognito_args, | 352 event->event_name, event->cross_incognito_args, |
| 333 event->event_url); | 353 event->event_url, event->user_gesture); |
| 334 IncrementInFlightEvents(extension); | 354 IncrementInFlightEvents(extension); |
| 335 } | 355 } |
| 336 continue; | 356 continue; |
| 337 } | 357 } |
| 338 | 358 |
| 339 DispatchEvent(listener->process, listener->extension_id, | 359 DispatchEvent(listener->process, listener->extension_id, |
| 340 event->event_name, event->event_args, event->event_url); | 360 event->event_name, event->event_args, |
| 361 event->event_url, event->user_gesture); |
| 341 IncrementInFlightEvents(extension); | 362 IncrementInFlightEvents(extension); |
| 342 } | 363 } |
| 343 } | 364 } |
| 344 | 365 |
| 345 void ExtensionEventRouter::LoadLazyBackgroundPagesForEvent( | 366 void ExtensionEventRouter::LoadLazyBackgroundPagesForEvent( |
| 346 const std::string& extension_id, | 367 const std::string& extension_id, |
| 347 const linked_ptr<ExtensionEvent>& event) { | 368 const linked_ptr<ExtensionEvent>& event) { |
| 348 ExtensionService* service = profile_->GetExtensionService(); | 369 ExtensionService* service = profile_->GetExtensionService(); |
| 349 ExtensionProcessManager* pm = profile_->GetExtensionProcessManager(); | 370 ExtensionProcessManager* pm = profile_->GetExtensionProcessManager(); |
| 350 | 371 |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 490 extension->id(), kOnInstalledEvent, "[]", NULL, GURL()); | 511 extension->id(), kOnInstalledEvent, "[]", NULL, GURL()); |
| 491 break; | 512 break; |
| 492 } | 513 } |
| 493 | 514 |
| 494 // TODO(tessamac): if background page crashed/failed clear queue. | 515 // TODO(tessamac): if background page crashed/failed clear queue. |
| 495 default: | 516 default: |
| 496 NOTREACHED(); | 517 NOTREACHED(); |
| 497 return; | 518 return; |
| 498 } | 519 } |
| 499 } | 520 } |
| OLD | NEW |