| 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 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 332 to_index)); | 332 to_index)); |
| 333 args->Append(object_args); | 333 args->Append(object_args); |
| 334 | 334 |
| 335 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); | 335 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); |
| 336 DispatchEvent(profile, events::kOnTabMoved, args.Pass(), | 336 DispatchEvent(profile, events::kOnTabMoved, args.Pass(), |
| 337 EventRouter::USER_GESTURE_UNKNOWN); | 337 EventRouter::USER_GESTURE_UNKNOWN); |
| 338 } | 338 } |
| 339 | 339 |
| 340 void BrowserEventRouter::TabUpdated(WebContents* contents, bool did_navigate) { | 340 void BrowserEventRouter::TabUpdated(WebContents* contents, bool did_navigate) { |
| 341 TabEntry* entry = GetTabEntry(contents); | 341 TabEntry* entry = GetTabEntry(contents); |
| 342 scoped_ptr<DictionaryValue> changed_properties; | 342 DictionaryValue* changed_properties = NULL; |
| 343 | 343 |
| 344 DCHECK(entry); | 344 DCHECK(entry); |
| 345 | 345 |
| 346 if (did_navigate) | 346 if (did_navigate) |
| 347 changed_properties.reset(entry->DidNavigate(contents)); | 347 changed_properties = entry->DidNavigate(contents); |
| 348 else | 348 else |
| 349 changed_properties.reset(entry->UpdateLoadState(contents)); | 349 changed_properties = entry->UpdateLoadState(contents); |
| 350 | 350 |
| 351 if (changed_properties) | 351 if (changed_properties) |
| 352 DispatchTabUpdatedEvent(contents, changed_properties.Pass()); | 352 DispatchTabUpdatedEvent(contents, changed_properties); |
| 353 } | 353 } |
| 354 | 354 |
| 355 void BrowserEventRouter::DispatchEvent( | 355 void BrowserEventRouter::DispatchEvent( |
| 356 Profile* profile, | 356 Profile* profile, |
| 357 const char* event_name, | 357 const char* event_name, |
| 358 scoped_ptr<ListValue> args, | 358 scoped_ptr<ListValue> args, |
| 359 EventRouter::UserGestureState user_gesture) { | 359 EventRouter::UserGestureState user_gesture) { |
| 360 if (!profile_->IsSameProfile(profile) || | 360 if (!profile_->IsSameProfile(profile) || |
| 361 !extensions::ExtensionSystem::Get(profile)->event_router()) | 361 !extensions::ExtensionSystem::Get(profile)->event_router()) |
| 362 return; | 362 return; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 389 if (!profile_->IsSameProfile(profile)) | 389 if (!profile_->IsSameProfile(profile)) |
| 390 return; | 390 return; |
| 391 | 391 |
| 392 scoped_ptr<ListValue> args(new ListValue()); | 392 scoped_ptr<ListValue> args(new ListValue()); |
| 393 args->Append(Value::CreateIntegerValue(window_id)); | 393 args->Append(Value::CreateIntegerValue(window_id)); |
| 394 | 394 |
| 395 DispatchEvent(profile, event_name, args.Pass(), | 395 DispatchEvent(profile, event_name, args.Pass(), |
| 396 EventRouter::USER_GESTURE_UNKNOWN); | 396 EventRouter::USER_GESTURE_UNKNOWN); |
| 397 } | 397 } |
| 398 | 398 |
| 399 static void WillDispatchTabUpdatedEvent( | 399 static void WillDispatchTabUpdatedEvent(WebContents* contents, |
| 400 WebContents* contents, | 400 Profile* profile, |
| 401 const DictionaryValue* changed_properties, | 401 const Extension* extension, |
| 402 Profile* profile, | 402 ListValue* event_args) { |
| 403 const Extension* extension, | |
| 404 ListValue* event_args) { | |
| 405 // Overwrite the second argument with the appropriate properties dictionary, | |
| 406 // depending on extension permissions. | |
| 407 DictionaryValue* properties_value = changed_properties->DeepCopy(); | |
| 408 ExtensionTabUtil::ScrubTabValueForExtension(contents, extension, | |
| 409 properties_value); | |
| 410 event_args->Set(1, properties_value); | |
| 411 | |
| 412 // Overwrite the third arg with our tab value as seen by this extension. | |
| 413 DictionaryValue* tab_value = ExtensionTabUtil::CreateTabValue( | 403 DictionaryValue* tab_value = ExtensionTabUtil::CreateTabValue( |
| 414 contents, extension); | 404 contents, extension); |
| 405 // Overwrite the third arg with our tab value as seen by this extension. |
| 415 event_args->Set(2, tab_value); | 406 event_args->Set(2, tab_value); |
| 416 } | 407 } |
| 417 | 408 |
| 418 void BrowserEventRouter::DispatchTabUpdatedEvent( | 409 void BrowserEventRouter::DispatchTabUpdatedEvent( |
| 419 WebContents* contents, scoped_ptr<DictionaryValue> changed_properties) { | 410 WebContents* contents, DictionaryValue* changed_properties) { |
| 420 DCHECK(changed_properties); | 411 DCHECK(changed_properties); |
| 421 DCHECK(contents); | 412 DCHECK(contents); |
| 422 | 413 |
| 423 // The state of the tab (as seen from the extension point of view) has | 414 // The state of the tab (as seen from the extension point of view) has |
| 424 // changed. Send a notification to the extension. | 415 // changed. Send a notification to the extension. |
| 425 scoped_ptr<ListValue> args_base(new ListValue()); | 416 scoped_ptr<ListValue> args_base(new ListValue()); |
| 426 | 417 |
| 427 // First arg: The id of the tab that changed. | 418 // First arg: The id of the tab that changed. |
| 428 args_base->AppendInteger(ExtensionTabUtil::GetTabId(contents)); | 419 args_base->AppendInteger(ExtensionTabUtil::GetTabId(contents)); |
| 429 | 420 |
| 430 // Second arg: An object containing the changes to the tab state. Filled in | 421 // Second arg: An object containing the changes to the tab state. |
| 431 // by WillDispatchTabUpdatedEvent as a copy of changed_properties, if the | 422 args_base->Append(changed_properties); |
| 432 // extension has the tabs permission. | |
| 433 | 423 |
| 434 // Third arg: An object containing the state of the tab. Filled in by | 424 // Third arg: An object containing the state of the tab. Filled in by |
| 435 // WillDispatchTabUpdatedEvent. | 425 // WillDispatchTabUpdatedEvent. |
| 436 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); | 426 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); |
| 437 | 427 |
| 438 scoped_ptr<Event> event(new Event(events::kOnTabUpdated, args_base.Pass())); | 428 scoped_ptr<Event> event(new Event(events::kOnTabUpdated, args_base.Pass())); |
| 439 event->restrict_to_profile = profile; | 429 event->restrict_to_profile = profile; |
| 440 event->user_gesture = EventRouter::USER_GESTURE_NOT_ENABLED; | 430 event->user_gesture = EventRouter::USER_GESTURE_NOT_ENABLED; |
| 441 event->will_dispatch_callback = | 431 event->will_dispatch_callback = |
| 442 base::Bind(&WillDispatchTabUpdatedEvent, | 432 base::Bind(&WillDispatchTabUpdatedEvent, contents); |
| 443 contents, changed_properties.get()); | |
| 444 ExtensionSystem::Get(profile)->event_router()->BroadcastEvent(event.Pass()); | 433 ExtensionSystem::Get(profile)->event_router()->BroadcastEvent(event.Pass()); |
| 445 } | 434 } |
| 446 | 435 |
| 447 BrowserEventRouter::TabEntry* BrowserEventRouter::GetTabEntry( | 436 BrowserEventRouter::TabEntry* BrowserEventRouter::GetTabEntry( |
| 448 const WebContents* contents) { | 437 const WebContents* contents) { |
| 449 int tab_id = ExtensionTabUtil::GetTabId(contents); | 438 int tab_id = ExtensionTabUtil::GetTabId(contents); |
| 450 std::map<int, TabEntry>::iterator i = tab_entries_.find(tab_id); | 439 std::map<int, TabEntry>::iterator i = tab_entries_.find(tab_id); |
| 451 if (tab_entries_.end() == i) | 440 if (tab_entries_.end() == i) |
| 452 return NULL; | 441 return NULL; |
| 453 return &i->second; | 442 return &i->second; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 505 RegisterForTabNotifications(new_contents); | 494 RegisterForTabNotifications(new_contents); |
| 506 } | 495 } |
| 507 } | 496 } |
| 508 | 497 |
| 509 void BrowserEventRouter::TabPinnedStateChanged(WebContents* contents, | 498 void BrowserEventRouter::TabPinnedStateChanged(WebContents* contents, |
| 510 int index) { | 499 int index) { |
| 511 TabStripModel* tab_strip = NULL; | 500 TabStripModel* tab_strip = NULL; |
| 512 int tab_index; | 501 int tab_index; |
| 513 | 502 |
| 514 if (ExtensionTabUtil::GetTabStripModel(contents, &tab_strip, &tab_index)) { | 503 if (ExtensionTabUtil::GetTabStripModel(contents, &tab_strip, &tab_index)) { |
| 515 scoped_ptr<DictionaryValue> changed_properties(new DictionaryValue()); | 504 DictionaryValue* changed_properties = new DictionaryValue(); |
| 516 changed_properties->SetBoolean(tab_keys::kPinnedKey, | 505 changed_properties->SetBoolean(tab_keys::kPinnedKey, |
| 517 tab_strip->IsTabPinned(tab_index)); | 506 tab_strip->IsTabPinned(tab_index)); |
| 518 DispatchTabUpdatedEvent(contents, changed_properties.Pass()); | 507 DispatchTabUpdatedEvent(contents, changed_properties); |
| 519 } | 508 } |
| 520 } | 509 } |
| 521 | 510 |
| 522 void BrowserEventRouter::TabStripEmpty() {} | 511 void BrowserEventRouter::TabStripEmpty() {} |
| 523 | 512 |
| 524 void BrowserEventRouter::DispatchOldPageActionEvent( | 513 void BrowserEventRouter::DispatchOldPageActionEvent( |
| 525 Profile* profile, | 514 Profile* profile, |
| 526 const std::string& extension_id, | 515 const std::string& extension_id, |
| 527 const std::string& page_action_id, | 516 const std::string& page_action_id, |
| 528 int tab_id, | 517 int tab_id, |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 595 event_name = "scriptBadge.onClicked"; | 584 event_name = "scriptBadge.onClicked"; |
| 596 break; | 585 break; |
| 597 case Extension::ActionInfo::TYPE_SYSTEM_INDICATOR: | 586 case Extension::ActionInfo::TYPE_SYSTEM_INDICATOR: |
| 598 // The System Indicator handles its own clicks. | 587 // The System Indicator handles its own clicks. |
| 599 break; | 588 break; |
| 600 } | 589 } |
| 601 | 590 |
| 602 if (event_name) { | 591 if (event_name) { |
| 603 scoped_ptr<ListValue> args(new ListValue()); | 592 scoped_ptr<ListValue> args(new ListValue()); |
| 604 DictionaryValue* tab_value = ExtensionTabUtil::CreateTabValue( | 593 DictionaryValue* tab_value = ExtensionTabUtil::CreateTabValue( |
| 605 web_contents); | 594 web_contents, |
| 595 ExtensionTabUtil::INCLUDE_PRIVACY_SENSITIVE_FIELDS); |
| 606 args->Append(tab_value); | 596 args->Append(tab_value); |
| 607 | 597 |
| 608 DispatchEventToExtension(profile, | 598 DispatchEventToExtension(profile, |
| 609 extension_action.extension_id(), | 599 extension_action.extension_id(), |
| 610 event_name, | 600 event_name, |
| 611 args.Pass(), | 601 args.Pass(), |
| 612 EventRouter::USER_GESTURE_ENABLED); | 602 EventRouter::USER_GESTURE_ENABLED); |
| 613 } | 603 } |
| 614 } | 604 } |
| 615 | 605 |
| 616 } // namespace extensions | 606 } // namespace extensions |
| OLD | NEW |