Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(308)

Side by Side Diff: chrome/browser/extensions/event_router.cc

Issue 11365181: Remove GetExtensionService from Profile. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: tweaks Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 SystemInfoEventRouter::GetInstance()->RemoveEventListener(event_name); 225 SystemInfoEventRouter::GetInstance()->RemoveEventListener(event_name);
226 } 226 }
227 227
228 void EventRouter::AddLazyEventListener(const std::string& event_name, 228 void EventRouter::AddLazyEventListener(const std::string& event_name,
229 const std::string& extension_id) { 229 const std::string& extension_id) {
230 scoped_ptr<EventListener> listener(new EventListener( 230 scoped_ptr<EventListener> listener(new EventListener(
231 event_name, extension_id, NULL, scoped_ptr<DictionaryValue>())); 231 event_name, extension_id, NULL, scoped_ptr<DictionaryValue>()));
232 bool is_new = listeners_.AddListener(listener.Pass()); 232 bool is_new = listeners_.AddListener(listener.Pass());
233 233
234 if (is_new) { 234 if (is_new) {
235 ExtensionPrefs* prefs = 235 ExtensionPrefs* prefs = extensions::ExtensionSystem::Get(profile_)->
236 profile_->GetExtensionService()->extension_prefs(); 236 extension_service()->extension_prefs();
237 std::set<std::string> events = prefs->GetRegisteredEvents(extension_id); 237 std::set<std::string> events = prefs->GetRegisteredEvents(extension_id);
238 bool prefs_is_new = events.insert(event_name).second; 238 bool prefs_is_new = events.insert(event_name).second;
239 if (prefs_is_new) 239 if (prefs_is_new)
240 prefs->SetRegisteredEvents(extension_id, events); 240 prefs->SetRegisteredEvents(extension_id, events);
241 } 241 }
242 } 242 }
243 243
244 void EventRouter::RemoveLazyEventListener(const std::string& event_name, 244 void EventRouter::RemoveLazyEventListener(const std::string& event_name,
245 const std::string& extension_id) { 245 const std::string& extension_id) {
246 EventListener listener(event_name, extension_id, NULL, 246 EventListener listener(event_name, extension_id, NULL,
247 scoped_ptr<DictionaryValue>()); 247 scoped_ptr<DictionaryValue>());
248 bool did_exist = listeners_.RemoveListener(&listener); 248 bool did_exist = listeners_.RemoveListener(&listener);
249 249
250 if (did_exist) { 250 if (did_exist) {
251 ExtensionPrefs* prefs = 251 ExtensionPrefs* prefs = extensions::ExtensionSystem::Get(profile_)->
252 profile_->GetExtensionService()->extension_prefs(); 252 extension_service()->extension_prefs();
253 std::set<std::string> events = prefs->GetRegisteredEvents(extension_id); 253 std::set<std::string> events = prefs->GetRegisteredEvents(extension_id);
254 bool prefs_did_exist = events.erase(event_name) > 0; 254 bool prefs_did_exist = events.erase(event_name) > 0;
255 DCHECK(prefs_did_exist); 255 DCHECK(prefs_did_exist);
256 prefs->SetRegisteredEvents(extension_id, events); 256 prefs->SetRegisteredEvents(extension_id, events);
257 } 257 }
258 } 258 }
259 259
260 void EventRouter::AddFilteredEventListener(const std::string& event_name, 260 void EventRouter::AddFilteredEventListener(const std::string& event_name,
261 content::RenderProcessHost* process, 261 content::RenderProcessHost* process,
262 const std::string& extension_id, 262 const std::string& extension_id,
263 const base::DictionaryValue& filter, 263 const base::DictionaryValue& filter,
264 bool add_lazy_listener) { 264 bool add_lazy_listener) {
265 listeners_.AddListener(scoped_ptr<EventListener>(new EventListener( 265 listeners_.AddListener(scoped_ptr<EventListener>(new EventListener(
266 event_name, extension_id, process, 266 event_name, extension_id, process,
267 scoped_ptr<DictionaryValue>(filter.DeepCopy())))); 267 scoped_ptr<DictionaryValue>(filter.DeepCopy()))));
268 268
269 if (add_lazy_listener) { 269 if (add_lazy_listener) {
270 bool added = listeners_.AddListener(scoped_ptr<EventListener>( 270 bool added = listeners_.AddListener(scoped_ptr<EventListener>(
271 new EventListener(event_name, extension_id, NULL, 271 new EventListener(event_name, extension_id, NULL,
272 scoped_ptr<DictionaryValue>(filter.DeepCopy())))); 272 scoped_ptr<DictionaryValue>(filter.DeepCopy()))));
273 273
274 if (added) { 274 if (added) {
275 ExtensionPrefs* prefs = 275 ExtensionPrefs* prefs = extensions::ExtensionSystem::Get(profile_)->
276 profile_->GetExtensionService()->extension_prefs(); 276 extension_service()->extension_prefs();
277 prefs->AddFilterToEvent(event_name, extension_id, &filter); 277 prefs->AddFilterToEvent(event_name, extension_id, &filter);
278 } 278 }
279 } 279 }
280 } 280 }
281 281
282 void EventRouter::RemoveFilteredEventListener( 282 void EventRouter::RemoveFilteredEventListener(
283 const std::string& event_name, 283 const std::string& event_name,
284 content::RenderProcessHost* process, 284 content::RenderProcessHost* process,
285 const std::string& extension_id, 285 const std::string& extension_id,
286 const base::DictionaryValue& filter, 286 const base::DictionaryValue& filter,
287 bool remove_lazy_listener) { 287 bool remove_lazy_listener) {
288 EventListener listener(event_name, extension_id, process, 288 EventListener listener(event_name, extension_id, process,
289 scoped_ptr<DictionaryValue>(filter.DeepCopy())); 289 scoped_ptr<DictionaryValue>(filter.DeepCopy()));
290 290
291 listeners_.RemoveListener(&listener); 291 listeners_.RemoveListener(&listener);
292 292
293 if (remove_lazy_listener) { 293 if (remove_lazy_listener) {
294 listener.process = NULL; 294 listener.process = NULL;
295 bool removed = listeners_.RemoveListener(&listener); 295 bool removed = listeners_.RemoveListener(&listener);
296 296
297 if (removed) { 297 if (removed) {
298 ExtensionPrefs* prefs = 298 ExtensionPrefs* prefs = extensions::ExtensionSystem::Get(profile_)->
299 profile_->GetExtensionService()->extension_prefs(); 299 extension_service()->extension_prefs();
300 prefs->RemoveFilterFromEvent(event_name, extension_id, &filter); 300 prefs->RemoveFilterFromEvent(event_name, extension_id, &filter);
301 } 301 }
302 } 302 }
303 } 303 }
304 304
305 bool EventRouter::HasEventListener(const std::string& event_name) { 305 bool EventRouter::HasEventListener(const std::string& event_name) {
306 return listeners_.HasListenerForEvent(event_name); 306 return listeners_.HasListenerForEvent(event_name);
307 } 307 }
308 308
309 bool EventRouter::ExtensionHasEventListener(const std::string& extension_id, 309 bool EventRouter::ExtensionHasEventListener(const std::string& extension_id,
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
443 } 443 }
444 } 444 }
445 } 445 }
446 } 446 }
447 } 447 }
448 448
449 void EventRouter::DispatchLazyEvent( 449 void EventRouter::DispatchLazyEvent(
450 const std::string& extension_id, 450 const std::string& extension_id,
451 const linked_ptr<Event>& event, 451 const linked_ptr<Event>& event,
452 std::set<EventDispatchIdentifier>* already_dispatched) { 452 std::set<EventDispatchIdentifier>* already_dispatched) {
453 ExtensionService* service = profile_->GetExtensionService(); 453 ExtensionService* service =
454 extensions::ExtensionSystem::Get(profile_)->extension_service();
454 // Check both the original and the incognito profile to see if we 455 // Check both the original and the incognito profile to see if we
455 // should load a lazy bg page to handle the event. The latter case 456 // should load a lazy bg page to handle the event. The latter case
456 // occurs in the case of split-mode extensions. 457 // occurs in the case of split-mode extensions.
457 const Extension* extension = service->extensions()->GetByID(extension_id); 458 const Extension* extension = service->extensions()->GetByID(extension_id);
458 if (extension) { 459 if (extension) {
459 if (MaybeLoadLazyBackgroundPageToDispatchEvent( 460 if (MaybeLoadLazyBackgroundPageToDispatchEvent(
460 profile_, extension, event)) { 461 profile_, extension, event)) {
461 already_dispatched->insert(std::make_pair(profile_, extension_id)); 462 already_dispatched->insert(std::make_pair(profile_, extension_id));
462 } 463 }
463 464
464 if (profile_->HasOffTheRecordProfile() && 465 if (profile_->HasOffTheRecordProfile() &&
465 extension->incognito_split_mode()) { 466 extension->incognito_split_mode()) {
466 if (MaybeLoadLazyBackgroundPageToDispatchEvent( 467 if (MaybeLoadLazyBackgroundPageToDispatchEvent(
467 profile_->GetOffTheRecordProfile(), extension, event)) { 468 profile_->GetOffTheRecordProfile(), extension, event)) {
468 already_dispatched->insert( 469 already_dispatched->insert(
469 std::make_pair(profile_->GetOffTheRecordProfile(), extension_id)); 470 std::make_pair(profile_->GetOffTheRecordProfile(), extension_id));
470 } 471 }
471 } 472 }
472 } 473 }
473 } 474 }
474 475
475 void EventRouter::DispatchEventToProcess(const std::string& extension_id, 476 void EventRouter::DispatchEventToProcess(const std::string& extension_id,
476 content::RenderProcessHost* process, 477 content::RenderProcessHost* process,
477 const linked_ptr<Event>& event) { 478 const linked_ptr<Event>& event) {
478 ExtensionService* service = profile_->GetExtensionService(); 479 ExtensionService* service =
480 extensions::ExtensionSystem::Get(profile_)->extension_service();
479 const Extension* extension = service->extensions()->GetByID(extension_id); 481 const Extension* extension = service->extensions()->GetByID(extension_id);
480 482
481 // The extension could have been removed, but we do not unregister it until 483 // The extension could have been removed, but we do not unregister it until
482 // the extension process is unloaded. 484 // the extension process is unloaded.
483 if (!extension) 485 if (!extension)
484 return; 486 return;
485 487
486 Profile* listener_profile = Profile::FromBrowserContext( 488 Profile* listener_profile = Profile::FromBrowserContext(
487 process->GetBrowserContext()); 489 process->GetBrowserContext());
488 ProcessMap* process_map = 490 ProcessMap* process_map = extensions::ExtensionSystem::Get(listener_profile)->
489 listener_profile->GetExtensionService()->process_map(); 491 extension_service()->process_map();
490 // If the event is privileged, only send to extension processes. Otherwise, 492 // If the event is privileged, only send to extension processes. Otherwise,
491 // it's OK to send to normal renderers (e.g., for content scripts). 493 // it's OK to send to normal renderers (e.g., for content scripts).
492 if (ExtensionAPI::GetSharedInstance()->IsPrivileged(event->event_name) && 494 if (ExtensionAPI::GetSharedInstance()->IsPrivileged(event->event_name) &&
493 !process_map->Contains(extension->id(), process->GetID())) { 495 !process_map->Contains(extension->id(), process->GetID())) {
494 return; 496 return;
495 } 497 }
496 498
497 ListValue* event_args = NULL; 499 ListValue* event_args = NULL;
498 if (!CanDispatchEventToProfile(listener_profile, extension, 500 if (!CanDispatchEventToProfile(listener_profile, extension,
499 event, &event_args)) { 501 event, &event_args)) {
(...skipping 12 matching lines...) Expand all
512 const linked_ptr<Event>& event, 514 const linked_ptr<Event>& event,
513 ListValue** event_args) { 515 ListValue** event_args) {
514 if (event_args) 516 if (event_args)
515 *event_args = event->event_args.get(); 517 *event_args = event->event_args.get();
516 518
517 // Is this event from a different profile than the renderer (ie, an 519 // Is this event from a different profile than the renderer (ie, an
518 // incognito tab event sent to a normal process, or vice versa). 520 // incognito tab event sent to a normal process, or vice versa).
519 bool cross_incognito = 521 bool cross_incognito =
520 event->restrict_to_profile && profile != event->restrict_to_profile; 522 event->restrict_to_profile && profile != event->restrict_to_profile;
521 if (cross_incognito && 523 if (cross_incognito &&
522 !profile->GetExtensionService()->CanCrossIncognito(extension)) { 524 !extensions::ExtensionSystem::Get(profile)->extension_service()->
525 CanCrossIncognito(extension)) {
523 if (!event->cross_incognito_args.get()) 526 if (!event->cross_incognito_args.get())
524 return false; 527 return false;
525 // Send the event with different arguments to extensions that can't 528 // Send the event with different arguments to extensions that can't
526 // cross incognito. 529 // cross incognito.
527 if (event_args) 530 if (event_args)
528 *event_args = event->cross_incognito_args.get(); 531 *event_args = event->cross_incognito_args.get();
529 } 532 }
530 533
531 return true; 534 return true;
532 } 535 }
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
603 } 606 }
604 case chrome::NOTIFICATION_EXTENSIONS_READY: { 607 case chrome::NOTIFICATION_EXTENSIONS_READY: {
605 // We're done restarting Chrome after an update. 608 // We're done restarting Chrome after an update.
606 dispatch_chrome_updated_event_ = false; 609 dispatch_chrome_updated_event_ = false;
607 break; 610 break;
608 } 611 }
609 case chrome::NOTIFICATION_EXTENSION_LOADED: { 612 case chrome::NOTIFICATION_EXTENSION_LOADED: {
610 // Add all registered lazy listeners to our cache. 613 // Add all registered lazy listeners to our cache.
611 const Extension* extension = 614 const Extension* extension =
612 content::Details<const Extension>(details).ptr(); 615 content::Details<const Extension>(details).ptr();
613 ExtensionPrefs* prefs = 616 ExtensionPrefs* prefs = extensions::ExtensionSystem::Get(profile_)->
614 profile_->GetExtensionService()->extension_prefs(); 617 extension_service()->extension_prefs();
615 std::set<std::string> registered_events = 618 std::set<std::string> registered_events =
616 prefs->GetRegisteredEvents(extension->id()); 619 prefs->GetRegisteredEvents(extension->id());
617 listeners_.LoadUnfilteredLazyListeners(extension->id(), 620 listeners_.LoadUnfilteredLazyListeners(extension->id(),
618 registered_events); 621 registered_events);
619 const DictionaryValue* filtered_events = 622 const DictionaryValue* filtered_events =
620 prefs->GetFilteredEvents(extension->id()); 623 prefs->GetFilteredEvents(extension->id());
621 if (filtered_events) 624 if (filtered_events)
622 listeners_.LoadFilteredLazyListeners(extension->id(), *filtered_events); 625 listeners_.LoadFilteredLazyListeners(extension->id(), *filtered_events);
623 626
624 if (dispatch_chrome_updated_event_) { 627 if (dispatch_chrome_updated_event_) {
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
689 info(info) {} 692 info(info) {}
690 693
691 Event::~Event() {} 694 Event::~Event() {}
692 695
693 EventListenerInfo::EventListenerInfo(const std::string& event_name, 696 EventListenerInfo::EventListenerInfo(const std::string& event_name,
694 const std::string& extension_id) 697 const std::string& extension_id)
695 : event_name(event_name), 698 : event_name(event_name),
696 extension_id(extension_id) {} 699 extension_id(extension_id) {}
697 700
698 } // namespace extensions 701 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698