diff options
author | mpcomplete@chromium.org <mpcomplete@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-12-05 03:22:48 +0000 |
---|---|---|
committer | mpcomplete@chromium.org <mpcomplete@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-12-05 03:22:48 +0000 |
commit | 6e850920ee154c82047f1c0ac21e78433c8e3017 (patch) | |
tree | d91b0921ca58a097885c158313e337573cccbdb0 /chrome/browser/extensions/browser_event_router.cc | |
parent | b66771cf74ae096828c7ff680e821caf917fbe2c (diff) | |
download | chromium_src-6e850920ee154c82047f1c0ac21e78433c8e3017.zip chromium_src-6e850920ee154c82047f1c0ac21e78433c8e3017.tar.gz chromium_src-6e850920ee154c82047f1c0ac21e78433c8e3017.tar.bz2 |
Add a more generic API for dispatching events from extension EventRouter.
Also fixes a bug where chrome.tabs.oncreated.addlistener is called twice for
event pages.
The new API allows the caller to specify a callback which gives them a chance
to tailor the event args for each extension and Profile.
BUG=163246,162543
Review URL: https://chromiumcodereview.appspot.com/11316338
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@171141 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/extensions/browser_event_router.cc')
-rw-r--r-- | chrome/browser/extensions/browser_event_router.cc | 85 |
1 files changed, 37 insertions, 48 deletions
diff --git a/chrome/browser/extensions/browser_event_router.cc b/chrome/browser/extensions/browser_event_router.cc index 48a429f..9783e7f 100644 --- a/chrome/browser/extensions/browser_event_router.cc +++ b/chrome/browser/extensions/browser_event_router.cc @@ -154,27 +154,29 @@ void BrowserEventRouter::OnBrowserSetLastActive(Browser* browser) { } } +static void WillDispatchTabCreatedEvent(WebContents* contents, + bool active, + Profile* profile, + const Extension* extension, + ListValue* event_args) { + DictionaryValue* tab_value = ExtensionTabUtil::CreateTabValue( + contents, extension); + event_args->Clear(); + event_args->Append(tab_value); + tab_value->SetBoolean(tab_keys::kSelectedKey, active); +} + void BrowserEventRouter::TabCreatedAt(WebContents* contents, int index, bool active) { Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); - const EventListenerMap::ListenerList& listeners( - ExtensionSystem::Get(profile)->event_router()-> - listeners().GetEventListenersByName(events::kOnTabCreated)); - for (EventListenerMap::ListenerList::const_iterator it = listeners.begin(); - it != listeners.end(); - ++it) { - scoped_ptr<ListValue> args(new ListValue()); - DictionaryValue* tab_value = ExtensionTabUtil::CreateTabValue( - contents, - profile->GetExtensionService()->extensions()->GetByID( - (*it)->extension_id)); - args->Append(tab_value); - tab_value->SetBoolean(tab_keys::kSelectedKey, active); - DispatchEventToExtension(profile, (*it)->extension_id, - events::kOnTabCreated, args.Pass(), - EventRouter::USER_GESTURE_NOT_ENABLED); - } + scoped_ptr<ListValue> args(new ListValue()); + scoped_ptr<Event> event(new Event(events::kOnTabCreated, args.Pass())); + event->restrict_to_profile = profile; + event->user_gesture = EventRouter::USER_GESTURE_NOT_ENABLED; + event->will_dispatch_callback = + base::Bind(&WillDispatchTabCreatedEvent, contents, active); + ExtensionSystem::Get(profile)->event_router()->BroadcastEvent(event.Pass()); RegisterForTabNotifications(contents); } @@ -380,20 +382,6 @@ void BrowserEventRouter::DispatchEventToExtension( profile, GURL(), user_gesture); } -void BrowserEventRouter::DispatchEventsAcrossIncognito( - Profile* profile, - const char* event_name, - scoped_ptr<ListValue> event_args, - scoped_ptr<ListValue> cross_incognito_args) { - if (!profile_->IsSameProfile(profile) || - !extensions::ExtensionSystem::Get(profile)->event_router()) - return; - - extensions::ExtensionSystem::Get(profile)->event_router()-> - DispatchEventsToRenderersAcrossIncognito(event_name, event_args.Pass(), - profile, cross_incognito_args.Pass(), GURL()); -} - void BrowserEventRouter::DispatchSimpleBrowserEvent( Profile* profile, const int window_id, const char* event_name) { if (!profile_->IsSameProfile(profile)) @@ -406,6 +394,16 @@ void BrowserEventRouter::DispatchSimpleBrowserEvent( EventRouter::USER_GESTURE_UNKNOWN); } +static void WillDispatchTabUpdatedEvent(WebContents* contents, + Profile* profile, + const Extension* extension, + ListValue* event_args) { + DictionaryValue* tab_value = ExtensionTabUtil::CreateTabValue( + contents, extension); + // Overwrite the third arg with our tab value as seen by this extension. + event_args->Set(2, tab_value); +} + void BrowserEventRouter::DispatchTabUpdatedEvent( WebContents* contents, DictionaryValue* changed_properties) { DCHECK(changed_properties); @@ -421,25 +419,16 @@ void BrowserEventRouter::DispatchTabUpdatedEvent( // Second arg: An object containing the changes to the tab state. args_base->Append(changed_properties); - // Third arg: An object containing the state of the tab. + // Third arg: An object containing the state of the tab. Filled in by + // WillDispatchTabUpdatedEvent. Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); - const EventListenerMap::ListenerList& listeners( - ExtensionSystem::Get(profile)->event_router()-> - listeners().GetEventListenersByName(events::kOnTabUpdated)); - for (EventListenerMap::ListenerList::const_iterator it = listeners.begin(); - it != listeners.end(); - ++it) { - scoped_ptr<ListValue> args(args_base->DeepCopy()); - DictionaryValue* tab_value = ExtensionTabUtil::CreateTabValue( - contents, - profile->GetExtensionService()->extensions()->GetByID( - (*it)->extension_id)); - args->Append(tab_value); - DispatchEventToExtension(profile, (*it)->extension_id, - events::kOnTabUpdated, args.Pass(), - EventRouter::USER_GESTURE_UNKNOWN); - } + scoped_ptr<Event> event(new Event(events::kOnTabUpdated, args_base.Pass())); + event->restrict_to_profile = profile; + event->user_gesture = EventRouter::USER_GESTURE_NOT_ENABLED; + event->will_dispatch_callback = + base::Bind(&WillDispatchTabUpdatedEvent, contents); + ExtensionSystem::Get(profile)->event_router()->BroadcastEvent(event.Pass()); } BrowserEventRouter::TabEntry* BrowserEventRouter::GetTabEntry( |