| 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/browser_event_router.h" | 5 #include "chrome/browser/extensions/browser_event_router.h" |
| 6 | 6 |
| 7 #include "base/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
| 8 #include "base/values.h" | 8 #include "base/values.h" |
| 9 #include "chrome/browser/extensions/api/extension_action/extension_page_actions_
api_constants.h" | 9 #include "chrome/browser/extensions/api/extension_action/extension_page_actions_
api_constants.h" |
| 10 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" | 10 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 if (service) { | 159 if (service) { |
| 160 service->window_event_router()->OnActiveWindowChanged( | 160 service->window_event_router()->OnActiveWindowChanged( |
| 161 browser ? browser->extension_window_controller() : NULL); | 161 browser ? browser->extension_window_controller() : NULL); |
| 162 } | 162 } |
| 163 } | 163 } |
| 164 | 164 |
| 165 void BrowserEventRouter::TabCreatedAt(WebContents* contents, | 165 void BrowserEventRouter::TabCreatedAt(WebContents* contents, |
| 166 int index, | 166 int index, |
| 167 bool active) { | 167 bool active) { |
| 168 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); | 168 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); |
| 169 DispatchEventWithTab(profile, "", events::kOnTabCreated, contents, active, | 169 const EventListenerMap::ListenerList& listeners( |
| 170 EventRouter::USER_GESTURE_NOT_ENABLED); | 170 ExtensionSystem::Get(profile)->event_router()-> |
| 171 listeners().GetEventListenersByName(events::kOnTabCreated)); |
| 172 for (EventListenerMap::ListenerList::const_iterator it = listeners.begin(); |
| 173 it != listeners.end(); |
| 174 ++it) { |
| 175 scoped_ptr<ListValue> args(new ListValue()); |
| 176 DictionaryValue* tab_value = ExtensionTabUtil::CreateTabValue( |
| 177 contents, |
| 178 profile->GetExtensionService()->extensions()->GetByID( |
| 179 (*it)->extension_id)); |
| 180 args->Append(tab_value); |
| 181 tab_value->SetBoolean(tab_keys::kSelectedKey, active); |
| 182 DispatchEventToExtension(profile, (*it)->extension_id, |
| 183 events::kOnTabCreated, args.Pass(), |
| 184 EventRouter::USER_GESTURE_NOT_ENABLED); |
| 185 } |
| 171 | 186 |
| 172 RegisterForTabNotifications(contents); | 187 RegisterForTabNotifications(contents); |
| 173 } | 188 } |
| 174 | 189 |
| 175 void BrowserEventRouter::TabInsertedAt(TabContents* contents, | 190 void BrowserEventRouter::TabInsertedAt(TabContents* contents, |
| 176 int index, | 191 int index, |
| 177 bool active) { | 192 bool active) { |
| 178 // If tab is new, send created event. | 193 // If tab is new, send created event. |
| 179 int tab_id = ExtensionTabUtil::GetTabId(contents->web_contents()); | 194 int tab_id = ExtensionTabUtil::GetTabId(contents->web_contents()); |
| 180 if (!GetTabEntry(contents->web_contents())) { | 195 if (!GetTabEntry(contents->web_contents())) { |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 371 scoped_ptr<ListValue> event_args, | 386 scoped_ptr<ListValue> event_args, |
| 372 scoped_ptr<ListValue> cross_incognito_args) { | 387 scoped_ptr<ListValue> cross_incognito_args) { |
| 373 if (!profile_->IsSameProfile(profile) || !profile->GetExtensionEventRouter()) | 388 if (!profile_->IsSameProfile(profile) || !profile->GetExtensionEventRouter()) |
| 374 return; | 389 return; |
| 375 | 390 |
| 376 profile->GetExtensionEventRouter()->DispatchEventsToRenderersAcrossIncognito( | 391 profile->GetExtensionEventRouter()->DispatchEventsToRenderersAcrossIncognito( |
| 377 event_name, event_args.Pass(), profile, cross_incognito_args.Pass(), | 392 event_name, event_args.Pass(), profile, cross_incognito_args.Pass(), |
| 378 GURL()); | 393 GURL()); |
| 379 } | 394 } |
| 380 | 395 |
| 381 void BrowserEventRouter::DispatchEventWithTab( | |
| 382 Profile* profile, | |
| 383 const std::string& extension_id, | |
| 384 const char* event_name, | |
| 385 const WebContents* web_contents, | |
| 386 bool active, | |
| 387 EventRouter::UserGestureState user_gesture, | |
| 388 scoped_ptr<ListValue> event_args) { | |
| 389 if (!profile_->IsSameProfile(profile)) | |
| 390 return; | |
| 391 | |
| 392 if (!extension_id.empty()) { | |
| 393 event_args->Append(ExtensionTabUtil::CreateTabValueActive( | |
| 394 web_contents, | |
| 395 active, | |
| 396 profile->GetExtensionService()->extensions()->GetByID(extension_id))); | |
| 397 DispatchEventToExtension(profile, extension_id, event_name, | |
| 398 event_args.Pass(), user_gesture); | |
| 399 } else { | |
| 400 const EventListenerMap::ListenerList& listeners( | |
| 401 ExtensionSystem::Get(profile)->event_router()-> | |
| 402 listeners().GetEventListenersByName(event_name)); | |
| 403 | |
| 404 for (EventListenerMap::ListenerList::const_iterator it = listeners.begin(); | |
| 405 it != listeners.end(); | |
| 406 ++it) { | |
| 407 scoped_ptr<ListValue> args(event_args->DeepCopy()); | |
| 408 args->Append(ExtensionTabUtil::CreateTabValueActive( | |
| 409 web_contents, | |
| 410 active, | |
| 411 profile->GetExtensionService()->extensions()->GetByID( | |
| 412 (*it)->extension_id))); | |
| 413 DispatchEventToExtension(profile, (*it)->extension_id, event_name, | |
| 414 args.Pass(), user_gesture); | |
| 415 } | |
| 416 } | |
| 417 } | |
| 418 | |
| 419 void BrowserEventRouter::DispatchSimpleBrowserEvent( | 396 void BrowserEventRouter::DispatchSimpleBrowserEvent( |
| 420 Profile* profile, const int window_id, const char* event_name) { | 397 Profile* profile, const int window_id, const char* event_name) { |
| 421 if (!profile_->IsSameProfile(profile)) | 398 if (!profile_->IsSameProfile(profile)) |
| 422 return; | 399 return; |
| 423 | 400 |
| 424 scoped_ptr<ListValue> args(new ListValue()); | 401 scoped_ptr<ListValue> args(new ListValue()); |
| 425 args->Append(Value::CreateIntegerValue(window_id)); | 402 args->Append(Value::CreateIntegerValue(window_id)); |
| 426 | 403 |
| 427 DispatchEvent(profile, event_name, args.Pass(), | 404 DispatchEvent(profile, event_name, args.Pass(), |
| 428 EventRouter::USER_GESTURE_UNKNOWN); | 405 EventRouter::USER_GESTURE_UNKNOWN); |
| 429 } | 406 } |
| 430 | 407 |
| 431 void BrowserEventRouter::DispatchTabUpdatedEvent( | 408 void BrowserEventRouter::DispatchTabUpdatedEvent( |
| 432 WebContents* contents, DictionaryValue* changed_properties) { | 409 WebContents* contents, DictionaryValue* changed_properties) { |
| 433 DCHECK(changed_properties); | 410 DCHECK(changed_properties); |
| 434 DCHECK(contents); | 411 DCHECK(contents); |
| 435 | 412 |
| 436 // The state of the tab (as seen from the extension point of view) has | 413 // The state of the tab (as seen from the extension point of view) has |
| 437 // changed. Send a notification to the extension. | 414 // changed. Send a notification to the extension. |
| 438 scoped_ptr<ListValue> args(new ListValue()); | 415 scoped_ptr<ListValue> args_base(new ListValue()); |
| 439 | 416 |
| 440 // First arg: The id of the tab that changed. | 417 // First arg: The id of the tab that changed. |
| 441 args->Append(Value::CreateIntegerValue(ExtensionTabUtil::GetTabId(contents))); | 418 args_base->AppendInteger(ExtensionTabUtil::GetTabId(contents)); |
| 442 | 419 |
| 443 // Second arg: An object containing the changes to the tab state. | 420 // Second arg: An object containing the changes to the tab state. |
| 444 args->Append(changed_properties); | 421 args_base->Append(changed_properties); |
| 445 | 422 |
| 446 // Third arg: An object containing the state of the tab. | 423 // Third arg: An object containing the state of the tab. |
| 447 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); | 424 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); |
| 448 | 425 |
| 449 DispatchEventWithTab(profile, "", events::kOnTabUpdated, contents, true, | 426 const EventListenerMap::ListenerList& listeners( |
| 450 EventRouter::USER_GESTURE_UNKNOWN, args.Pass()); | 427 ExtensionSystem::Get(profile)->event_router()-> |
| 428 listeners().GetEventListenersByName(events::kOnTabUpdated)); |
| 429 for (EventListenerMap::ListenerList::const_iterator it = listeners.begin(); |
| 430 it != listeners.end(); |
| 431 ++it) { |
| 432 scoped_ptr<ListValue> args(args_base->DeepCopy()); |
| 433 DictionaryValue* tab_value = ExtensionTabUtil::CreateTabValue( |
| 434 contents, |
| 435 profile->GetExtensionService()->extensions()->GetByID( |
| 436 (*it)->extension_id)); |
| 437 args->Append(tab_value); |
| 438 DispatchEventToExtension(profile, (*it)->extension_id, |
| 439 events::kOnTabUpdated, args.Pass(), |
| 440 EventRouter::USER_GESTURE_UNKNOWN); |
| 441 } |
| 451 } | 442 } |
| 452 | 443 |
| 453 BrowserEventRouter::TabEntry* BrowserEventRouter::GetTabEntry( | 444 BrowserEventRouter::TabEntry* BrowserEventRouter::GetTabEntry( |
| 454 const WebContents* contents) { | 445 const WebContents* contents) { |
| 455 int tab_id = ExtensionTabUtil::GetTabId(contents); | 446 int tab_id = ExtensionTabUtil::GetTabId(contents); |
| 456 std::map<int, TabEntry>::iterator i = tab_entries_.find(tab_id); | 447 std::map<int, TabEntry>::iterator i = tab_entries_.find(tab_id); |
| 457 if (tab_entries_.end() == i) | 448 if (tab_entries_.end() == i) |
| 458 return NULL; | 449 return NULL; |
| 459 return &i->second; | 450 return &i->second; |
| 460 } | 451 } |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 590 break; | 581 break; |
| 591 case ExtensionAction::TYPE_PAGE: | 582 case ExtensionAction::TYPE_PAGE: |
| 592 event_name = "pageAction.onClicked"; | 583 event_name = "pageAction.onClicked"; |
| 593 break; | 584 break; |
| 594 case ExtensionAction::TYPE_SCRIPT_BADGE: | 585 case ExtensionAction::TYPE_SCRIPT_BADGE: |
| 595 event_name = "scriptBadge.onClicked"; | 586 event_name = "scriptBadge.onClicked"; |
| 596 break; | 587 break; |
| 597 } | 588 } |
| 598 | 589 |
| 599 if (event_name) { | 590 if (event_name) { |
| 600 DispatchEventWithTab(profile, | 591 scoped_ptr<ListValue> args(new ListValue()); |
| 601 extension_action.extension_id(), | 592 DictionaryValue* tab_value = ExtensionTabUtil::CreateTabValue( |
| 602 event_name, | 593 tab_contents->web_contents(), |
| 603 tab_contents->web_contents(), | 594 ExtensionTabUtil::INCLUDE_PRIVACY_SENSITIVE_FIELDS); |
| 604 true, | 595 args->Append(tab_value); |
| 605 EventRouter::USER_GESTURE_ENABLED); | 596 |
| 597 DispatchEventToExtension(profile, |
| 598 extension_action.extension_id(), |
| 599 event_name, |
| 600 args.Pass(), |
| 601 EventRouter::USER_GESTURE_ENABLED); |
| 606 } | 602 } |
| 607 } | 603 } |
| 608 | 604 |
| 609 } // namespace extensions | 605 } // namespace extensions |
| OLD | NEW |