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

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: replace missing extension_system include Created 8 years 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 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 SystemInfoEventRouter::GetInstance()->RemoveEventListener(event_name); 229 SystemInfoEventRouter::GetInstance()->RemoveEventListener(event_name);
230 } 230 }
231 231
232 void EventRouter::AddLazyEventListener(const std::string& event_name, 232 void EventRouter::AddLazyEventListener(const std::string& event_name,
233 const std::string& extension_id) { 233 const std::string& extension_id) {
234 scoped_ptr<EventListener> listener(new EventListener( 234 scoped_ptr<EventListener> listener(new EventListener(
235 event_name, extension_id, NULL, scoped_ptr<DictionaryValue>())); 235 event_name, extension_id, NULL, scoped_ptr<DictionaryValue>()));
236 bool is_new = listeners_.AddListener(listener.Pass()); 236 bool is_new = listeners_.AddListener(listener.Pass());
237 237
238 if (is_new) { 238 if (is_new) {
239 ExtensionPrefs* prefs = 239 ExtensionPrefs* prefs = extensions::ExtensionSystem::Get(profile_)->
240 profile_->GetExtensionService()->extension_prefs(); 240 extension_service()->extension_prefs();
241 std::set<std::string> events = prefs->GetRegisteredEvents(extension_id); 241 std::set<std::string> events = prefs->GetRegisteredEvents(extension_id);
242 bool prefs_is_new = events.insert(event_name).second; 242 bool prefs_is_new = events.insert(event_name).second;
243 if (prefs_is_new) 243 if (prefs_is_new)
244 prefs->SetRegisteredEvents(extension_id, events); 244 prefs->SetRegisteredEvents(extension_id, events);
245 } 245 }
246 } 246 }
247 247
248 void EventRouter::RemoveLazyEventListener(const std::string& event_name, 248 void EventRouter::RemoveLazyEventListener(const std::string& event_name,
249 const std::string& extension_id) { 249 const std::string& extension_id) {
250 EventListener listener(event_name, extension_id, NULL, 250 EventListener listener(event_name, extension_id, NULL,
251 scoped_ptr<DictionaryValue>()); 251 scoped_ptr<DictionaryValue>());
252 bool did_exist = listeners_.RemoveListener(&listener); 252 bool did_exist = listeners_.RemoveListener(&listener);
253 253
254 if (did_exist) { 254 if (did_exist) {
255 ExtensionPrefs* prefs = 255 ExtensionPrefs* prefs = extensions::ExtensionSystem::Get(profile_)->
256 profile_->GetExtensionService()->extension_prefs(); 256 extension_service()->extension_prefs();
257 std::set<std::string> events = prefs->GetRegisteredEvents(extension_id); 257 std::set<std::string> events = prefs->GetRegisteredEvents(extension_id);
258 bool prefs_did_exist = events.erase(event_name) > 0; 258 bool prefs_did_exist = events.erase(event_name) > 0;
259 DCHECK(prefs_did_exist); 259 DCHECK(prefs_did_exist);
260 prefs->SetRegisteredEvents(extension_id, events); 260 prefs->SetRegisteredEvents(extension_id, events);
261 } 261 }
262 } 262 }
263 263
264 void EventRouter::AddFilteredEventListener(const std::string& event_name, 264 void EventRouter::AddFilteredEventListener(const std::string& event_name,
265 content::RenderProcessHost* process, 265 content::RenderProcessHost* process,
266 const std::string& extension_id, 266 const std::string& extension_id,
267 const base::DictionaryValue& filter, 267 const base::DictionaryValue& filter,
268 bool add_lazy_listener) { 268 bool add_lazy_listener) {
269 listeners_.AddListener(scoped_ptr<EventListener>(new EventListener( 269 listeners_.AddListener(scoped_ptr<EventListener>(new EventListener(
270 event_name, extension_id, process, 270 event_name, extension_id, process,
271 scoped_ptr<DictionaryValue>(filter.DeepCopy())))); 271 scoped_ptr<DictionaryValue>(filter.DeepCopy()))));
272 272
273 if (add_lazy_listener) { 273 if (add_lazy_listener) {
274 bool added = listeners_.AddListener(scoped_ptr<EventListener>( 274 bool added = listeners_.AddListener(scoped_ptr<EventListener>(
275 new EventListener(event_name, extension_id, NULL, 275 new EventListener(event_name, extension_id, NULL,
276 scoped_ptr<DictionaryValue>(filter.DeepCopy())))); 276 scoped_ptr<DictionaryValue>(filter.DeepCopy()))));
277 277
278 if (added) { 278 if (added) {
279 ExtensionPrefs* prefs = 279 ExtensionPrefs* prefs = extensions::ExtensionSystem::Get(profile_)->
280 profile_->GetExtensionService()->extension_prefs(); 280 extension_service()->extension_prefs();
281 prefs->AddFilterToEvent(event_name, extension_id, &filter); 281 prefs->AddFilterToEvent(event_name, extension_id, &filter);
282 } 282 }
283 } 283 }
284 } 284 }
285 285
286 void EventRouter::RemoveFilteredEventListener( 286 void EventRouter::RemoveFilteredEventListener(
287 const std::string& event_name, 287 const std::string& event_name,
288 content::RenderProcessHost* process, 288 content::RenderProcessHost* process,
289 const std::string& extension_id, 289 const std::string& extension_id,
290 const base::DictionaryValue& filter, 290 const base::DictionaryValue& filter,
291 bool remove_lazy_listener) { 291 bool remove_lazy_listener) {
292 EventListener listener(event_name, extension_id, process, 292 EventListener listener(event_name, extension_id, process,
293 scoped_ptr<DictionaryValue>(filter.DeepCopy())); 293 scoped_ptr<DictionaryValue>(filter.DeepCopy()));
294 294
295 listeners_.RemoveListener(&listener); 295 listeners_.RemoveListener(&listener);
296 296
297 if (remove_lazy_listener) { 297 if (remove_lazy_listener) {
298 listener.process = NULL; 298 listener.process = NULL;
299 bool removed = listeners_.RemoveListener(&listener); 299 bool removed = listeners_.RemoveListener(&listener);
300 300
301 if (removed) { 301 if (removed) {
302 ExtensionPrefs* prefs = 302 ExtensionPrefs* prefs = extensions::ExtensionSystem::Get(profile_)->
303 profile_->GetExtensionService()->extension_prefs(); 303 extension_service()->extension_prefs();
304 prefs->RemoveFilterFromEvent(event_name, extension_id, &filter); 304 prefs->RemoveFilterFromEvent(event_name, extension_id, &filter);
305 } 305 }
306 } 306 }
307 } 307 }
308 308
309 bool EventRouter::HasEventListener(const std::string& event_name) { 309 bool EventRouter::HasEventListener(const std::string& event_name) {
310 return listeners_.HasListenerForEvent(event_name); 310 return listeners_.HasListenerForEvent(event_name);
311 } 311 }
312 312
313 bool EventRouter::ExtensionHasEventListener(const std::string& extension_id, 313 bool EventRouter::ExtensionHasEventListener(const std::string& extension_id,
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
447 } 447 }
448 } 448 }
449 } 449 }
450 } 450 }
451 } 451 }
452 452
453 void EventRouter::DispatchLazyEvent( 453 void EventRouter::DispatchLazyEvent(
454 const std::string& extension_id, 454 const std::string& extension_id,
455 const linked_ptr<Event>& event, 455 const linked_ptr<Event>& event,
456 std::set<EventDispatchIdentifier>* already_dispatched) { 456 std::set<EventDispatchIdentifier>* already_dispatched) {
457 ExtensionService* service = profile_->GetExtensionService(); 457 ExtensionService* service =
458 extensions::ExtensionSystem::Get(profile_)->extension_service();
458 // Check both the original and the incognito profile to see if we 459 // Check both the original and the incognito profile to see if we
459 // should load a lazy bg page to handle the event. The latter case 460 // should load a lazy bg page to handle the event. The latter case
460 // occurs in the case of split-mode extensions. 461 // occurs in the case of split-mode extensions.
461 const Extension* extension = service->extensions()->GetByID(extension_id); 462 const Extension* extension = service->extensions()->GetByID(extension_id);
462 if (extension) { 463 if (extension) {
463 if (MaybeLoadLazyBackgroundPageToDispatchEvent( 464 if (MaybeLoadLazyBackgroundPageToDispatchEvent(
464 profile_, extension, event)) { 465 profile_, extension, event)) {
465 already_dispatched->insert(std::make_pair(profile_, extension_id)); 466 already_dispatched->insert(std::make_pair(profile_, extension_id));
466 } 467 }
467 468
468 if (profile_->HasOffTheRecordProfile() && 469 if (profile_->HasOffTheRecordProfile() &&
469 extension->incognito_split_mode()) { 470 extension->incognito_split_mode()) {
470 if (MaybeLoadLazyBackgroundPageToDispatchEvent( 471 if (MaybeLoadLazyBackgroundPageToDispatchEvent(
471 profile_->GetOffTheRecordProfile(), extension, event)) { 472 profile_->GetOffTheRecordProfile(), extension, event)) {
472 already_dispatched->insert( 473 already_dispatched->insert(
473 std::make_pair(profile_->GetOffTheRecordProfile(), extension_id)); 474 std::make_pair(profile_->GetOffTheRecordProfile(), extension_id));
474 } 475 }
475 } 476 }
476 } 477 }
477 } 478 }
478 479
479 void EventRouter::DispatchEventToProcess(const std::string& extension_id, 480 void EventRouter::DispatchEventToProcess(const std::string& extension_id,
480 content::RenderProcessHost* process, 481 content::RenderProcessHost* process,
481 const linked_ptr<Event>& event) { 482 const linked_ptr<Event>& event) {
482 ExtensionService* service = profile_->GetExtensionService(); 483 ExtensionService* service =
484 extensions::ExtensionSystem::Get(profile_)->extension_service();
483 const Extension* extension = service->extensions()->GetByID(extension_id); 485 const Extension* extension = service->extensions()->GetByID(extension_id);
484 486
485 // The extension could have been removed, but we do not unregister it until 487 // The extension could have been removed, but we do not unregister it until
486 // the extension process is unloaded. 488 // the extension process is unloaded.
487 if (!extension) 489 if (!extension)
488 return; 490 return;
489 491
490 Profile* listener_profile = Profile::FromBrowserContext( 492 Profile* listener_profile = Profile::FromBrowserContext(
491 process->GetBrowserContext()); 493 process->GetBrowserContext());
492 ProcessMap* process_map = 494 ProcessMap* process_map = extensions::ExtensionSystem::Get(listener_profile)->
493 listener_profile->GetExtensionService()->process_map(); 495 extension_service()->process_map();
494 // If the event is privileged, only send to extension processes. Otherwise, 496 // If the event is privileged, only send to extension processes. Otherwise,
495 // it's OK to send to normal renderers (e.g., for content scripts). 497 // it's OK to send to normal renderers (e.g., for content scripts).
496 if (ExtensionAPI::GetSharedInstance()->IsPrivileged(event->event_name) && 498 if (ExtensionAPI::GetSharedInstance()->IsPrivileged(event->event_name) &&
497 !process_map->Contains(extension->id(), process->GetID())) { 499 !process_map->Contains(extension->id(), process->GetID())) {
498 return; 500 return;
499 } 501 }
500 502
501 ListValue* event_args = NULL; 503 ListValue* event_args = NULL;
502 if (!CanDispatchEventToProfile(listener_profile, extension, 504 if (!CanDispatchEventToProfile(listener_profile, extension,
503 event, &event_args)) { 505 event, &event_args)) {
(...skipping 12 matching lines...) Expand all
516 const linked_ptr<Event>& event, 518 const linked_ptr<Event>& event,
517 ListValue** event_args) { 519 ListValue** event_args) {
518 if (event_args) 520 if (event_args)
519 *event_args = event->event_args.get(); 521 *event_args = event->event_args.get();
520 522
521 // Is this event from a different profile than the renderer (ie, an 523 // Is this event from a different profile than the renderer (ie, an
522 // incognito tab event sent to a normal process, or vice versa). 524 // incognito tab event sent to a normal process, or vice versa).
523 bool cross_incognito = 525 bool cross_incognito =
524 event->restrict_to_profile && profile != event->restrict_to_profile; 526 event->restrict_to_profile && profile != event->restrict_to_profile;
525 if (cross_incognito && 527 if (cross_incognito &&
526 !profile->GetExtensionService()->CanCrossIncognito(extension)) { 528 !extensions::ExtensionSystem::Get(profile)->extension_service()->
529 CanCrossIncognito(extension)) {
527 if (!event->cross_incognito_args.get()) 530 if (!event->cross_incognito_args.get())
528 return false; 531 return false;
529 // Send the event with different arguments to extensions that can't 532 // Send the event with different arguments to extensions that can't
530 // cross incognito. 533 // cross incognito.
531 if (event_args) 534 if (event_args)
532 *event_args = event->cross_incognito_args.get(); 535 *event_args = event->cross_incognito_args.get();
533 } 536 }
534 537
535 return true; 538 return true;
536 } 539 }
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
619 LazyBackgroundTaskQueue* queue = 622 LazyBackgroundTaskQueue* queue =
620 ExtensionSystem::Get(profile_)->lazy_background_task_queue(); 623 ExtensionSystem::Get(profile_)->lazy_background_task_queue();
621 queue->AddPendingTask(profile_, extension->id(), 624 queue->AddPendingTask(profile_, extension->id(),
622 base::Bind(&DoNothing)); 625 base::Bind(&DoNothing));
623 } 626 }
624 } 627 }
625 case chrome::NOTIFICATION_EXTENSION_LOADED: { 628 case chrome::NOTIFICATION_EXTENSION_LOADED: {
626 // Add all registered lazy listeners to our cache. 629 // Add all registered lazy listeners to our cache.
627 const Extension* extension = 630 const Extension* extension =
628 content::Details<const Extension>(details).ptr(); 631 content::Details<const Extension>(details).ptr();
629 ExtensionPrefs* prefs = 632 ExtensionPrefs* prefs = extensions::ExtensionSystem::Get(profile_)->
630 profile_->GetExtensionService()->extension_prefs(); 633 extension_service()->extension_prefs();
631 std::set<std::string> registered_events = 634 std::set<std::string> registered_events =
632 prefs->GetRegisteredEvents(extension->id()); 635 prefs->GetRegisteredEvents(extension->id());
633 listeners_.LoadUnfilteredLazyListeners(extension->id(), 636 listeners_.LoadUnfilteredLazyListeners(extension->id(),
634 registered_events); 637 registered_events);
635 const DictionaryValue* filtered_events = 638 const DictionaryValue* filtered_events =
636 prefs->GetFilteredEvents(extension->id()); 639 prefs->GetFilteredEvents(extension->id());
637 if (filtered_events) 640 if (filtered_events)
638 listeners_.LoadFilteredLazyListeners(extension->id(), *filtered_events); 641 listeners_.LoadFilteredLazyListeners(extension->id(), *filtered_events);
639 642
640 if (dispatch_chrome_updated_event_) { 643 if (dispatch_chrome_updated_event_) {
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
705 info(info) {} 708 info(info) {}
706 709
707 Event::~Event() {} 710 Event::~Event() {}
708 711
709 EventListenerInfo::EventListenerInfo(const std::string& event_name, 712 EventListenerInfo::EventListenerInfo(const std::string& event_name,
710 const std::string& extension_id) 713 const std::string& extension_id)
711 : event_name(event_name), 714 : event_name(event_name),
712 extension_id(extension_id) {} 715 extension_id(extension_id) {}
713 716
714 } // namespace extensions 717 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/crx_installer_browsertest.cc ('k') | chrome/browser/extensions/extension_apitest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698