| 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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 // Init() can happen after the browser is running, so catch up with any | 82 // Init() can happen after the browser is running, so catch up with any |
| 83 // windows that already exist. | 83 // windows that already exist. |
| 84 for (BrowserList::const_iterator iter = BrowserList::begin(); | 84 for (BrowserList::const_iterator iter = BrowserList::begin(); |
| 85 iter != BrowserList::end(); ++iter) { | 85 iter != BrowserList::end(); ++iter) { |
| 86 RegisterForBrowserNotifications(*iter); | 86 RegisterForBrowserNotifications(*iter); |
| 87 | 87 |
| 88 // Also catch up our internal bookkeeping of tab entries. | 88 // Also catch up our internal bookkeeping of tab entries. |
| 89 Browser* browser = *iter; | 89 Browser* browser = *iter; |
| 90 if (browser->tab_strip_model()) { | 90 if (browser->tab_strip_model()) { |
| 91 for (int i = 0; i < browser->tab_strip_model()->count(); ++i) { | 91 for (int i = 0; i < browser->tab_strip_model()->count(); ++i) { |
| 92 WebContents* contents = | 92 WebContents* contents = chrome::GetWebContentsAt(browser, i); |
| 93 chrome::GetTabContentsAt(browser, i)->web_contents(); | |
| 94 int tab_id = ExtensionTabUtil::GetTabId(contents); | 93 int tab_id = ExtensionTabUtil::GetTabId(contents); |
| 95 tab_entries_[tab_id] = TabEntry(); | 94 tab_entries_[tab_id] = TabEntry(); |
| 96 } | 95 } |
| 97 } | 96 } |
| 98 } | 97 } |
| 99 | 98 |
| 100 initialized_ = true; | 99 initialized_ = true; |
| 101 } | 100 } |
| 102 | 101 |
| 103 BrowserEventRouter::BrowserEventRouter(Profile* profile) | 102 BrowserEventRouter::BrowserEventRouter(Profile* profile) |
| (...skipping 10 matching lines...) Expand all Loading... |
| 114 RegisterForBrowserNotifications(browser); | 113 RegisterForBrowserNotifications(browser); |
| 115 } | 114 } |
| 116 | 115 |
| 117 void BrowserEventRouter::RegisterForBrowserNotifications(Browser* browser) { | 116 void BrowserEventRouter::RegisterForBrowserNotifications(Browser* browser) { |
| 118 if (!profile_->IsSameProfile(browser->profile())) | 117 if (!profile_->IsSameProfile(browser->profile())) |
| 119 return; | 118 return; |
| 120 // Start listening to TabStripModel events for this browser. | 119 // Start listening to TabStripModel events for this browser. |
| 121 browser->tab_strip_model()->AddObserver(this); | 120 browser->tab_strip_model()->AddObserver(this); |
| 122 | 121 |
| 123 for (int i = 0; i < browser->tab_strip_model()->count(); ++i) { | 122 for (int i = 0; i < browser->tab_strip_model()->count(); ++i) { |
| 124 RegisterForTabNotifications( | 123 RegisterForTabNotifications(chrome::GetWebContentsAt(browser, i)); |
| 125 chrome::GetTabContentsAt(browser, i)->web_contents()); | |
| 126 } | 124 } |
| 127 } | 125 } |
| 128 | 126 |
| 129 void BrowserEventRouter::RegisterForTabNotifications(WebContents* contents) { | 127 void BrowserEventRouter::RegisterForTabNotifications(WebContents* contents) { |
| 130 registrar_.Add( | 128 registrar_.Add( |
| 131 this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 129 this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
| 132 content::Source<NavigationController>(&contents->GetController())); | 130 content::Source<NavigationController>(&contents->GetController())); |
| 133 | 131 |
| 134 // Observing NOTIFICATION_WEB_CONTENTS_DESTROYED is necessary because it's | 132 // Observing NOTIFICATION_WEB_CONTENTS_DESTROYED is necessary because it's |
| 135 // possible for tabs to be created, detached and then destroyed without | 133 // possible for tabs to be created, detached and then destroyed without |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 | 286 |
| 289 void BrowserEventRouter::TabSelectionChanged( | 287 void BrowserEventRouter::TabSelectionChanged( |
| 290 TabStripModel* tab_strip_model, | 288 TabStripModel* tab_strip_model, |
| 291 const TabStripSelectionModel& old_model) { | 289 const TabStripSelectionModel& old_model) { |
| 292 TabStripSelectionModel::SelectedIndices new_selection = | 290 TabStripSelectionModel::SelectedIndices new_selection = |
| 293 tab_strip_model->selection_model().selected_indices(); | 291 tab_strip_model->selection_model().selected_indices(); |
| 294 ListValue* all = new ListValue(); | 292 ListValue* all = new ListValue(); |
| 295 | 293 |
| 296 for (size_t i = 0; i < new_selection.size(); ++i) { | 294 for (size_t i = 0; i < new_selection.size(); ++i) { |
| 297 int index = new_selection[i]; | 295 int index = new_selection[i]; |
| 298 TabContents* contents = tab_strip_model->GetTabContentsAt(index); | 296 WebContents* contents = tab_strip_model->GetWebContentsAt(index); |
| 299 if (!contents) | 297 if (!contents) |
| 300 break; | 298 break; |
| 301 int tab_id = ExtensionTabUtil::GetTabId(contents->web_contents()); | 299 int tab_id = ExtensionTabUtil::GetTabId(contents); |
| 302 all->Append(Value::CreateIntegerValue(tab_id)); | 300 all->Append(Value::CreateIntegerValue(tab_id)); |
| 303 } | 301 } |
| 304 | 302 |
| 305 scoped_ptr<ListValue> args(new ListValue()); | 303 scoped_ptr<ListValue> args(new ListValue()); |
| 306 DictionaryValue* select_info = new DictionaryValue(); | 304 DictionaryValue* select_info = new DictionaryValue(); |
| 307 | 305 |
| 308 select_info->Set(tab_keys::kWindowIdKey, Value::CreateIntegerValue( | 306 select_info->Set(tab_keys::kWindowIdKey, Value::CreateIntegerValue( |
| 309 ExtensionTabUtil::GetWindowIdOfTabStripModel(tab_strip_model))); | 307 ExtensionTabUtil::GetWindowIdOfTabStripModel(tab_strip_model))); |
| 310 | 308 |
| 311 select_info->Set(tab_keys::kTabIdsKey, all); | 309 select_info->Set(tab_keys::kTabIdsKey, all); |
| (...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 517 args->Append(data); | 515 args->Append(data); |
| 518 | 516 |
| 519 DispatchEventToExtension(profile, extension_id, "pageActions", args.Pass(), | 517 DispatchEventToExtension(profile, extension_id, "pageActions", args.Pass(), |
| 520 EventRouter::USER_GESTURE_ENABLED); | 518 EventRouter::USER_GESTURE_ENABLED); |
| 521 } | 519 } |
| 522 | 520 |
| 523 void BrowserEventRouter::BrowserActionExecuted( | 521 void BrowserEventRouter::BrowserActionExecuted( |
| 524 const ExtensionAction& browser_action, | 522 const ExtensionAction& browser_action, |
| 525 Browser* browser) { | 523 Browser* browser) { |
| 526 Profile* profile = browser->profile(); | 524 Profile* profile = browser->profile(); |
| 527 TabContents* tab_contents = NULL; | 525 WebContents* web_contents = NULL; |
| 528 int tab_id = 0; | 526 int tab_id = 0; |
| 529 if (!ExtensionTabUtil::GetDefaultTab(browser, &tab_contents, &tab_id)) | 527 if (!ExtensionTabUtil::GetDefaultTab(browser, &web_contents, &tab_id)) |
| 530 return; | 528 return; |
| 531 ExtensionActionExecuted(profile, browser_action, tab_contents); | 529 ExtensionActionExecuted(profile, browser_action, web_contents); |
| 532 } | 530 } |
| 533 | 531 |
| 534 void BrowserEventRouter::PageActionExecuted(Profile* profile, | 532 void BrowserEventRouter::PageActionExecuted(Profile* profile, |
| 535 const ExtensionAction& page_action, | 533 const ExtensionAction& page_action, |
| 536 int tab_id, | 534 int tab_id, |
| 537 const std::string& url, | 535 const std::string& url, |
| 538 int button) { | 536 int button) { |
| 539 DispatchOldPageActionEvent(profile, page_action.extension_id(), | 537 DispatchOldPageActionEvent(profile, page_action.extension_id(), |
| 540 page_action.id(), tab_id, url, button); | 538 page_action.id(), tab_id, url, button); |
| 541 TabContents* tab_contents = NULL; | 539 WebContents* web_contents = NULL; |
| 542 if (!ExtensionTabUtil::GetTabById(tab_id, profile, profile->IsOffTheRecord(), | 540 if (!ExtensionTabUtil::GetTabById(tab_id, profile, profile->IsOffTheRecord(), |
| 543 NULL, NULL, &tab_contents, NULL)) { | 541 NULL, NULL, &web_contents, NULL)) { |
| 544 return; | 542 return; |
| 545 } | 543 } |
| 546 ExtensionActionExecuted(profile, page_action, tab_contents); | 544 ExtensionActionExecuted(profile, page_action, web_contents); |
| 547 } | 545 } |
| 548 | 546 |
| 549 void BrowserEventRouter::ScriptBadgeExecuted( | 547 void BrowserEventRouter::ScriptBadgeExecuted( |
| 550 Profile* profile, | 548 Profile* profile, |
| 551 const ExtensionAction& script_badge, | 549 const ExtensionAction& script_badge, |
| 552 int tab_id) { | 550 int tab_id) { |
| 553 TabContents* tab_contents = NULL; | 551 WebContents* web_contents = NULL; |
| 554 if (!ExtensionTabUtil::GetTabById(tab_id, profile, profile->IsOffTheRecord(), | 552 if (!ExtensionTabUtil::GetTabById(tab_id, profile, profile->IsOffTheRecord(), |
| 555 NULL, NULL, &tab_contents, NULL)) { | 553 NULL, NULL, &web_contents, NULL)) { |
| 556 return; | 554 return; |
| 557 } | 555 } |
| 558 ExtensionActionExecuted(profile, script_badge, tab_contents); | 556 ExtensionActionExecuted(profile, script_badge, web_contents); |
| 559 } | 557 } |
| 560 | 558 |
| 561 void BrowserEventRouter::CommandExecuted(Profile* profile, | 559 void BrowserEventRouter::CommandExecuted(Profile* profile, |
| 562 const std::string& extension_id, | 560 const std::string& extension_id, |
| 563 const std::string& command) { | 561 const std::string& command) { |
| 564 scoped_ptr<ListValue> args(new ListValue()); | 562 scoped_ptr<ListValue> args(new ListValue()); |
| 565 args->Append(Value::CreateStringValue(command)); | 563 args->Append(Value::CreateStringValue(command)); |
| 566 | 564 |
| 567 DispatchEventToExtension(profile, | 565 DispatchEventToExtension(profile, |
| 568 extension_id, | 566 extension_id, |
| 569 "commands.onCommand", | 567 "commands.onCommand", |
| 570 args.Pass(), | 568 args.Pass(), |
| 571 EventRouter::USER_GESTURE_ENABLED); | 569 EventRouter::USER_GESTURE_ENABLED); |
| 572 } | 570 } |
| 573 | 571 |
| 574 void BrowserEventRouter::ExtensionActionExecuted( | 572 void BrowserEventRouter::ExtensionActionExecuted( |
| 575 Profile* profile, | 573 Profile* profile, |
| 576 const ExtensionAction& extension_action, | 574 const ExtensionAction& extension_action, |
| 577 TabContents* tab_contents) { | 575 WebContents* web_contents) { |
| 578 const char* event_name = NULL; | 576 const char* event_name = NULL; |
| 579 switch (extension_action.action_type()) { | 577 switch (extension_action.action_type()) { |
| 580 case Extension::ActionInfo::TYPE_BROWSER: | 578 case Extension::ActionInfo::TYPE_BROWSER: |
| 581 event_name = "browserAction.onClicked"; | 579 event_name = "browserAction.onClicked"; |
| 582 break; | 580 break; |
| 583 case Extension::ActionInfo::TYPE_PAGE: | 581 case Extension::ActionInfo::TYPE_PAGE: |
| 584 event_name = "pageAction.onClicked"; | 582 event_name = "pageAction.onClicked"; |
| 585 break; | 583 break; |
| 586 case Extension::ActionInfo::TYPE_SCRIPT_BADGE: | 584 case Extension::ActionInfo::TYPE_SCRIPT_BADGE: |
| 587 event_name = "scriptBadge.onClicked"; | 585 event_name = "scriptBadge.onClicked"; |
| 588 break; | 586 break; |
| 589 } | 587 } |
| 590 | 588 |
| 591 if (event_name) { | 589 if (event_name) { |
| 592 scoped_ptr<ListValue> args(new ListValue()); | 590 scoped_ptr<ListValue> args(new ListValue()); |
| 593 DictionaryValue* tab_value = ExtensionTabUtil::CreateTabValue( | 591 DictionaryValue* tab_value = ExtensionTabUtil::CreateTabValue( |
| 594 tab_contents->web_contents(), | 592 web_contents, |
| 595 ExtensionTabUtil::INCLUDE_PRIVACY_SENSITIVE_FIELDS); | 593 ExtensionTabUtil::INCLUDE_PRIVACY_SENSITIVE_FIELDS); |
| 596 args->Append(tab_value); | 594 args->Append(tab_value); |
| 597 | 595 |
| 598 DispatchEventToExtension(profile, | 596 DispatchEventToExtension(profile, |
| 599 extension_action.extension_id(), | 597 extension_action.extension_id(), |
| 600 event_name, | 598 event_name, |
| 601 args.Pass(), | 599 args.Pass(), |
| 602 EventRouter::USER_GESTURE_ENABLED); | 600 EventRouter::USER_GESTURE_ENABLED); |
| 603 } | 601 } |
| 604 } | 602 } |
| 605 | 603 |
| 606 } // namespace extensions | 604 } // namespace extensions |
| OLD | NEW |