| 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 DictionaryValue* changed_properties = NULL; | 342 scoped_ptr<DictionaryValue> changed_properties; |
| 343 | 343 |
| 344 DCHECK(entry); | 344 DCHECK(entry); |
| 345 | 345 |
| 346 if (did_navigate) | 346 if (did_navigate) |
| 347 changed_properties = entry->DidNavigate(contents); | 347 changed_properties.reset(entry->DidNavigate(contents)); |
| 348 else | 348 else |
| 349 changed_properties = entry->UpdateLoadState(contents); | 349 changed_properties.reset(entry->UpdateLoadState(contents)); |
| 350 | 350 |
| 351 if (changed_properties) | 351 if (changed_properties) |
| 352 DispatchTabUpdatedEvent(contents, changed_properties); | 352 DispatchTabUpdatedEvent(contents, changed_properties.Pass()); |
| 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(WebContents* contents, | 399 static void WillDispatchTabUpdatedEvent( |
| 400 Profile* profile, | 400 WebContents* contents, |
| 401 const Extension* extension, | 401 const DictionaryValue* changed_properties, |
| 402 ListValue* event_args) { | 402 Profile* profile, |
| 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. |
| 403 DictionaryValue* tab_value = ExtensionTabUtil::CreateTabValue( | 413 DictionaryValue* tab_value = ExtensionTabUtil::CreateTabValue( |
| 404 contents, extension); | 414 contents, extension); |
| 405 // Overwrite the third arg with our tab value as seen by this extension. | |
| 406 event_args->Set(2, tab_value); | 415 event_args->Set(2, tab_value); |
| 407 } | 416 } |
| 408 | 417 |
| 409 void BrowserEventRouter::DispatchTabUpdatedEvent( | 418 void BrowserEventRouter::DispatchTabUpdatedEvent( |
| 410 WebContents* contents, DictionaryValue* changed_properties) { | 419 WebContents* contents, scoped_ptr<DictionaryValue> changed_properties) { |
| 411 DCHECK(changed_properties); | 420 DCHECK(changed_properties); |
| 412 DCHECK(contents); | 421 DCHECK(contents); |
| 413 | 422 |
| 414 // The state of the tab (as seen from the extension point of view) has | 423 // The state of the tab (as seen from the extension point of view) has |
| 415 // changed. Send a notification to the extension. | 424 // changed. Send a notification to the extension. |
| 416 scoped_ptr<ListValue> args_base(new ListValue()); | 425 scoped_ptr<ListValue> args_base(new ListValue()); |
| 417 | 426 |
| 418 // First arg: The id of the tab that changed. | 427 // First arg: The id of the tab that changed. |
| 419 args_base->AppendInteger(ExtensionTabUtil::GetTabId(contents)); | 428 args_base->AppendInteger(ExtensionTabUtil::GetTabId(contents)); |
| 420 | 429 |
| 421 // Second arg: An object containing the changes to the tab state. | 430 // Second arg: An object containing the changes to the tab state. Filled in |
| 422 args_base->Append(changed_properties); | 431 // by WillDispatchTabUpdatedEvent as a copy of changed_properties, if the |
| 432 // extension has the tabs permission. |
| 423 | 433 |
| 424 // Third arg: An object containing the state of the tab. Filled in by | 434 // Third arg: An object containing the state of the tab. Filled in by |
| 425 // WillDispatchTabUpdatedEvent. | 435 // WillDispatchTabUpdatedEvent. |
| 426 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); | 436 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); |
| 427 | 437 |
| 428 scoped_ptr<Event> event(new Event(events::kOnTabUpdated, args_base.Pass())); | 438 scoped_ptr<Event> event(new Event(events::kOnTabUpdated, args_base.Pass())); |
| 429 event->restrict_to_profile = profile; | 439 event->restrict_to_profile = profile; |
| 430 event->user_gesture = EventRouter::USER_GESTURE_NOT_ENABLED; | 440 event->user_gesture = EventRouter::USER_GESTURE_NOT_ENABLED; |
| 431 event->will_dispatch_callback = | 441 event->will_dispatch_callback = |
| 432 base::Bind(&WillDispatchTabUpdatedEvent, contents); | 442 base::Bind(&WillDispatchTabUpdatedEvent, |
| 443 contents, changed_properties.get()); |
| 433 ExtensionSystem::Get(profile)->event_router()->BroadcastEvent(event.Pass()); | 444 ExtensionSystem::Get(profile)->event_router()->BroadcastEvent(event.Pass()); |
| 434 } | 445 } |
| 435 | 446 |
| 436 BrowserEventRouter::TabEntry* BrowserEventRouter::GetTabEntry( | 447 BrowserEventRouter::TabEntry* BrowserEventRouter::GetTabEntry( |
| 437 const WebContents* contents) { | 448 const WebContents* contents) { |
| 438 int tab_id = ExtensionTabUtil::GetTabId(contents); | 449 int tab_id = ExtensionTabUtil::GetTabId(contents); |
| 439 std::map<int, TabEntry>::iterator i = tab_entries_.find(tab_id); | 450 std::map<int, TabEntry>::iterator i = tab_entries_.find(tab_id); |
| 440 if (tab_entries_.end() == i) | 451 if (tab_entries_.end() == i) |
| 441 return NULL; | 452 return NULL; |
| 442 return &i->second; | 453 return &i->second; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 474 TabClosingAt(tab_strip_model, old_contents, index); | 485 TabClosingAt(tab_strip_model, old_contents, index); |
| 475 TabInsertedAt(new_contents, index, tab_strip_model->active_index() == index); | 486 TabInsertedAt(new_contents, index, tab_strip_model->active_index() == index); |
| 476 } | 487 } |
| 477 | 488 |
| 478 void BrowserEventRouter::TabPinnedStateChanged(WebContents* contents, | 489 void BrowserEventRouter::TabPinnedStateChanged(WebContents* contents, |
| 479 int index) { | 490 int index) { |
| 480 TabStripModel* tab_strip = NULL; | 491 TabStripModel* tab_strip = NULL; |
| 481 int tab_index; | 492 int tab_index; |
| 482 | 493 |
| 483 if (ExtensionTabUtil::GetTabStripModel(contents, &tab_strip, &tab_index)) { | 494 if (ExtensionTabUtil::GetTabStripModel(contents, &tab_strip, &tab_index)) { |
| 484 DictionaryValue* changed_properties = new DictionaryValue(); | 495 scoped_ptr<DictionaryValue> changed_properties(new DictionaryValue()); |
| 485 changed_properties->SetBoolean(tab_keys::kPinnedKey, | 496 changed_properties->SetBoolean(tab_keys::kPinnedKey, |
| 486 tab_strip->IsTabPinned(tab_index)); | 497 tab_strip->IsTabPinned(tab_index)); |
| 487 DispatchTabUpdatedEvent(contents, changed_properties); | 498 DispatchTabUpdatedEvent(contents, changed_properties.Pass()); |
| 488 } | 499 } |
| 489 } | 500 } |
| 490 | 501 |
| 491 void BrowserEventRouter::TabStripEmpty() {} | 502 void BrowserEventRouter::TabStripEmpty() {} |
| 492 | 503 |
| 493 void BrowserEventRouter::DispatchOldPageActionEvent( | 504 void BrowserEventRouter::DispatchOldPageActionEvent( |
| 494 Profile* profile, | 505 Profile* profile, |
| 495 const std::string& extension_id, | 506 const std::string& extension_id, |
| 496 const std::string& page_action_id, | 507 const std::string& page_action_id, |
| 497 int tab_id, | 508 int tab_id, |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 564 event_name = "scriptBadge.onClicked"; | 575 event_name = "scriptBadge.onClicked"; |
| 565 break; | 576 break; |
| 566 case Extension::ActionInfo::TYPE_SYSTEM_INDICATOR: | 577 case Extension::ActionInfo::TYPE_SYSTEM_INDICATOR: |
| 567 // The System Indicator handles its own clicks. | 578 // The System Indicator handles its own clicks. |
| 568 break; | 579 break; |
| 569 } | 580 } |
| 570 | 581 |
| 571 if (event_name) { | 582 if (event_name) { |
| 572 scoped_ptr<ListValue> args(new ListValue()); | 583 scoped_ptr<ListValue> args(new ListValue()); |
| 573 DictionaryValue* tab_value = ExtensionTabUtil::CreateTabValue( | 584 DictionaryValue* tab_value = ExtensionTabUtil::CreateTabValue( |
| 574 web_contents, | 585 web_contents); |
| 575 ExtensionTabUtil::INCLUDE_PRIVACY_SENSITIVE_FIELDS); | |
| 576 args->Append(tab_value); | 586 args->Append(tab_value); |
| 577 | 587 |
| 578 DispatchEventToExtension(profile, | 588 DispatchEventToExtension(profile, |
| 579 extension_action.extension_id(), | 589 extension_action.extension_id(), |
| 580 event_name, | 590 event_name, |
| 581 args.Pass(), | 591 args.Pass(), |
| 582 EventRouter::USER_GESTURE_ENABLED); | 592 EventRouter::USER_GESTURE_ENABLED); |
| 583 } | 593 } |
| 584 } | 594 } |
| 585 | 595 |
| 586 } // namespace extensions | 596 } // namespace extensions |
| OLD | NEW |