| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/extension_browser_event_router.h" | 5 #include "chrome/browser/extensions/extension_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/extension_event_names.h" | 9 #include "chrome/browser/extensions/extension_event_names.h" |
| 10 #include "chrome/browser/extensions/extension_event_router.h" | 10 #include "chrome/browser/extensions/extension_event_router.h" |
| 11 #include "chrome/browser/extensions/extension_page_actions_module_constants.h" | 11 #include "chrome/browser/extensions/extension_page_actions_module_constants.h" |
| 12 #include "chrome/browser/extensions/extension_tabs_module_constants.h" | 12 #include "chrome/browser/extensions/extension_tabs_module_constants.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/tabs/tab_strip_model.h" | 14 #include "chrome/browser/tabs/tab_strip_model.h" |
| 15 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
| 16 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 16 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 17 #include "chrome/common/chrome_notification_types.h" |
| 17 #include "chrome/common/extensions/extension.h" | 18 #include "chrome/common/extensions/extension.h" |
| 18 #include "chrome/common/extensions/extension_constants.h" | 19 #include "chrome/common/extensions/extension_constants.h" |
| 19 #include "content/browser/tab_contents/navigation_entry.h" | 20 #include "content/browser/tab_contents/navigation_entry.h" |
| 20 #include "content/browser/tab_contents/tab_contents.h" | 21 #include "content/browser/tab_contents/tab_contents.h" |
| 21 #include "content/common/notification_service.h" | 22 #include "content/common/notification_service.h" |
| 22 | 23 |
| 23 namespace events = extension_event_names; | 24 namespace events = extension_event_names; |
| 24 namespace tab_keys = extension_tabs_module_constants; | 25 namespace tab_keys = extension_tabs_module_constants; |
| 25 namespace page_action_keys = extension_page_actions_module_constants; | 26 namespace page_action_keys = extension_page_actions_module_constants; |
| 26 | 27 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 if (initialized_) | 66 if (initialized_) |
| 66 return; | 67 return; |
| 67 BrowserList::AddObserver(this); | 68 BrowserList::AddObserver(this); |
| 68 #if defined(TOOLKIT_VIEWS) | 69 #if defined(TOOLKIT_VIEWS) |
| 69 views::FocusManager::GetWidgetFocusManager()->AddFocusChangeListener(this); | 70 views::FocusManager::GetWidgetFocusManager()->AddFocusChangeListener(this); |
| 70 #elif defined(TOOLKIT_GTK) | 71 #elif defined(TOOLKIT_GTK) |
| 71 ui::ActiveWindowWatcherX::AddObserver(this); | 72 ui::ActiveWindowWatcherX::AddObserver(this); |
| 72 #elif defined(OS_MACOSX) | 73 #elif defined(OS_MACOSX) |
| 73 // Needed for when no suitable window can be passed to an extension as the | 74 // Needed for when no suitable window can be passed to an extension as the |
| 74 // currently focused window. | 75 // currently focused window. |
| 75 registrar_.Add(this, NotificationType::NO_KEY_WINDOW, | 76 registrar_.Add(this, content::NOTIFICATION_NO_KEY_WINDOW, |
| 76 NotificationService::AllSources()); | 77 NotificationService::AllSources()); |
| 77 #endif | 78 #endif |
| 78 | 79 |
| 79 // Init() can happen after the browser is running, so catch up with any | 80 // Init() can happen after the browser is running, so catch up with any |
| 80 // windows that already exist. | 81 // windows that already exist. |
| 81 for (BrowserList::const_iterator iter = BrowserList::begin(); | 82 for (BrowserList::const_iterator iter = BrowserList::begin(); |
| 82 iter != BrowserList::end(); ++iter) { | 83 iter != BrowserList::end(); ++iter) { |
| 83 RegisterForBrowserNotifications(*iter); | 84 RegisterForBrowserNotifications(*iter); |
| 84 | 85 |
| 85 // Also catch up our internal bookkeeping of tab entries. | 86 // Also catch up our internal bookkeeping of tab entries. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 } | 121 } |
| 121 | 122 |
| 122 void ExtensionBrowserEventRouter::RegisterForBrowserNotifications( | 123 void ExtensionBrowserEventRouter::RegisterForBrowserNotifications( |
| 123 const Browser* browser) { | 124 const Browser* browser) { |
| 124 // Start listening to TabStripModel events for this browser. | 125 // Start listening to TabStripModel events for this browser. |
| 125 browser->tabstrip_model()->AddObserver(this); | 126 browser->tabstrip_model()->AddObserver(this); |
| 126 | 127 |
| 127 // If this is a new window, it isn't ready at this point, so we register to be | 128 // If this is a new window, it isn't ready at this point, so we register to be |
| 128 // notified when it is. If this is an existing window, this is a no-op that we | 129 // notified when it is. If this is an existing window, this is a no-op that we |
| 129 // just do to reduce code complexity. | 130 // just do to reduce code complexity. |
| 130 registrar_.Add(this, NotificationType::BROWSER_WINDOW_READY, | 131 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_WINDOW_READY, |
| 131 Source<const Browser>(browser)); | 132 Source<const Browser>(browser)); |
| 132 | 133 |
| 133 for (int i = 0; i < browser->tabstrip_model()->count(); ++i) | 134 for (int i = 0; i < browser->tabstrip_model()->count(); ++i) |
| 134 RegisterForTabNotifications(browser->GetTabContentsAt(i)); | 135 RegisterForTabNotifications(browser->GetTabContentsAt(i)); |
| 135 } | 136 } |
| 136 | 137 |
| 137 void ExtensionBrowserEventRouter::RegisterForTabNotifications( | 138 void ExtensionBrowserEventRouter::RegisterForTabNotifications( |
| 138 TabContents* contents) { | 139 TabContents* contents) { |
| 139 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, | 140 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
| 140 Source<NavigationController>(&contents->controller())); | 141 Source<NavigationController>(&contents->controller())); |
| 141 | 142 |
| 142 // Observing TAB_CONTENTS_DESTROYED is necessary because it's | 143 // Observing TAB_CONTENTS_DESTROYED is necessary because it's |
| 143 // possible for tabs to be created, detached and then destroyed without | 144 // possible for tabs to be created, detached and then destroyed without |
| 144 // ever having been re-attached and closed. This happens in the case of | 145 // ever having been re-attached and closed. This happens in the case of |
| 145 // a devtools TabContents that is opened in window, docked, then closed. | 146 // a devtools TabContents that is opened in window, docked, then closed. |
| 146 registrar_.Add(this, NotificationType::TAB_CONTENTS_DESTROYED, | 147 registrar_.Add(this, content::NOTIFICATION_TAB_CONTENTS_DESTROYED, |
| 147 Source<TabContents>(contents)); | 148 Source<TabContents>(contents)); |
| 148 } | 149 } |
| 149 | 150 |
| 150 void ExtensionBrowserEventRouter::UnregisterForTabNotifications( | 151 void ExtensionBrowserEventRouter::UnregisterForTabNotifications( |
| 151 TabContents* contents) { | 152 TabContents* contents) { |
| 152 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED, | 153 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
| 153 Source<NavigationController>(&contents->controller())); | 154 Source<NavigationController>(&contents->controller())); |
| 154 registrar_.Remove(this, NotificationType::TAB_CONTENTS_DESTROYED, | 155 registrar_.Remove(this, content::NOTIFICATION_TAB_CONTENTS_DESTROYED, |
| 155 Source<TabContents>(contents)); | 156 Source<TabContents>(contents)); |
| 156 } | 157 } |
| 157 | 158 |
| 158 void ExtensionBrowserEventRouter::OnBrowserWindowReady(const Browser* browser) { | 159 void ExtensionBrowserEventRouter::OnBrowserWindowReady(const Browser* browser) { |
| 159 ListValue args; | 160 ListValue args; |
| 160 | 161 |
| 161 DictionaryValue* window_dictionary = ExtensionTabUtil::CreateWindowValue( | 162 DictionaryValue* window_dictionary = ExtensionTabUtil::CreateWindowValue( |
| 162 browser, false); | 163 browser, false); |
| 163 args.Append(window_dictionary); | 164 args.Append(window_dictionary); |
| 164 | 165 |
| 165 std::string json_args; | 166 std::string json_args; |
| 166 base::JSONWriter::Write(&args, false, &json_args); | 167 base::JSONWriter::Write(&args, false, &json_args); |
| 167 | 168 |
| 168 DispatchEvent(browser->profile(), events::kOnWindowCreated, json_args); | 169 DispatchEvent(browser->profile(), events::kOnWindowCreated, json_args); |
| 169 } | 170 } |
| 170 | 171 |
| 171 void ExtensionBrowserEventRouter::OnBrowserRemoved(const Browser* browser) { | 172 void ExtensionBrowserEventRouter::OnBrowserRemoved(const Browser* browser) { |
| 172 if (!profile_->IsSameProfile(browser->profile())) | 173 if (!profile_->IsSameProfile(browser->profile())) |
| 173 return; | 174 return; |
| 174 | 175 |
| 175 // Stop listening to TabStripModel events for this browser. | 176 // Stop listening to TabStripModel events for this browser. |
| 176 browser->tabstrip_model()->RemoveObserver(this); | 177 browser->tabstrip_model()->RemoveObserver(this); |
| 177 | 178 |
| 178 registrar_.Remove(this, NotificationType::BROWSER_WINDOW_READY, | 179 registrar_.Remove(this, chrome::NOTIFICATION_BROWSER_WINDOW_READY, |
| 179 Source<const Browser>(browser)); | 180 Source<const Browser>(browser)); |
| 180 | 181 |
| 181 DispatchSimpleBrowserEvent(browser->profile(), | 182 DispatchSimpleBrowserEvent(browser->profile(), |
| 182 ExtensionTabUtil::GetWindowId(browser), | 183 ExtensionTabUtil::GetWindowId(browser), |
| 183 events::kOnWindowRemoved); | 184 events::kOnWindowRemoved); |
| 184 } | 185 } |
| 185 | 186 |
| 186 #if defined(TOOLKIT_VIEWS) | 187 #if defined(TOOLKIT_VIEWS) |
| 187 void ExtensionBrowserEventRouter::NativeFocusWillChange( | 188 void ExtensionBrowserEventRouter::NativeFocusWillChange( |
| 188 gfx::NativeView focused_before, | 189 gfx::NativeView focused_before, |
| (...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 480 | 481 |
| 481 ExtensionBrowserEventRouter::TabEntry* ExtensionBrowserEventRouter::GetTabEntry( | 482 ExtensionBrowserEventRouter::TabEntry* ExtensionBrowserEventRouter::GetTabEntry( |
| 482 const TabContents* contents) { | 483 const TabContents* contents) { |
| 483 int tab_id = ExtensionTabUtil::GetTabId(contents); | 484 int tab_id = ExtensionTabUtil::GetTabId(contents); |
| 484 std::map<int, TabEntry>::iterator i = tab_entries_.find(tab_id); | 485 std::map<int, TabEntry>::iterator i = tab_entries_.find(tab_id); |
| 485 if (tab_entries_.end() == i) | 486 if (tab_entries_.end() == i) |
| 486 return NULL; | 487 return NULL; |
| 487 return &i->second; | 488 return &i->second; |
| 488 } | 489 } |
| 489 | 490 |
| 490 void ExtensionBrowserEventRouter::Observe(NotificationType type, | 491 void ExtensionBrowserEventRouter::Observe(int type, |
| 491 const NotificationSource& source, | 492 const NotificationSource& source, |
| 492 const NotificationDetails& details) { | 493 const NotificationDetails& details) { |
| 493 if (type == NotificationType::NAV_ENTRY_COMMITTED) { | 494 if (type == content::NOTIFICATION_NAV_ENTRY_COMMITTED) { |
| 494 NavigationController* source_controller = | 495 NavigationController* source_controller = |
| 495 Source<NavigationController>(source).ptr(); | 496 Source<NavigationController>(source).ptr(); |
| 496 TabUpdated(source_controller->tab_contents(), true); | 497 TabUpdated(source_controller->tab_contents(), true); |
| 497 } else if (type == NotificationType::TAB_CONTENTS_DESTROYED) { | 498 } else if (type == content::NOTIFICATION_TAB_CONTENTS_DESTROYED) { |
| 498 // Tab was destroyed after being detached (without being re-attached). | 499 // Tab was destroyed after being detached (without being re-attached). |
| 499 TabContents* contents = Source<TabContents>(source).ptr(); | 500 TabContents* contents = Source<TabContents>(source).ptr(); |
| 500 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED, | 501 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
| 501 Source<NavigationController>(&contents->controller())); | 502 Source<NavigationController>(&contents->controller())); |
| 502 registrar_.Remove(this, NotificationType::TAB_CONTENTS_DESTROYED, | 503 registrar_.Remove(this, content::NOTIFICATION_TAB_CONTENTS_DESTROYED, |
| 503 Source<TabContents>(contents)); | 504 Source<TabContents>(contents)); |
| 504 } else if (type == NotificationType::BROWSER_WINDOW_READY) { | 505 } else if (type == chrome::NOTIFICATION_BROWSER_WINDOW_READY) { |
| 505 const Browser* browser = Source<const Browser>(source).ptr(); | 506 const Browser* browser = Source<const Browser>(source).ptr(); |
| 506 OnBrowserWindowReady(browser); | 507 OnBrowserWindowReady(browser); |
| 507 #if defined(OS_MACOSX) | 508 #if defined(OS_MACOSX) |
| 508 } else if (type == NotificationType::NO_KEY_WINDOW) { | 509 } else if (type == content::NOTIFICATION_NO_KEY_WINDOW) { |
| 509 OnBrowserSetLastActive(NULL); | 510 OnBrowserSetLastActive(NULL); |
| 510 #endif | 511 #endif |
| 511 } else { | 512 } else { |
| 512 NOTREACHED(); | 513 NOTREACHED(); |
| 513 } | 514 } |
| 514 } | 515 } |
| 515 | 516 |
| 516 void ExtensionBrowserEventRouter::TabChangedAt(TabContentsWrapper* contents, | 517 void ExtensionBrowserEventRouter::TabChangedAt(TabContentsWrapper* contents, |
| 517 int index, | 518 int index, |
| 518 TabChangeType change_type) { | 519 TabChangeType change_type) { |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 587 | 588 |
| 588 void ExtensionBrowserEventRouter::BrowserActionExecuted( | 589 void ExtensionBrowserEventRouter::BrowserActionExecuted( |
| 589 Profile* profile, const std::string& extension_id, Browser* browser) { | 590 Profile* profile, const std::string& extension_id, Browser* browser) { |
| 590 TabContentsWrapper* tab_contents = NULL; | 591 TabContentsWrapper* tab_contents = NULL; |
| 591 int tab_id = 0; | 592 int tab_id = 0; |
| 592 if (!ExtensionTabUtil::GetDefaultTab(browser, &tab_contents, &tab_id)) | 593 if (!ExtensionTabUtil::GetDefaultTab(browser, &tab_contents, &tab_id)) |
| 593 return; | 594 return; |
| 594 DispatchEventWithTab(profile, extension_id, "browserAction.onClicked", | 595 DispatchEventWithTab(profile, extension_id, "browserAction.onClicked", |
| 595 tab_contents->tab_contents(), true); | 596 tab_contents->tab_contents(), true); |
| 596 } | 597 } |
| OLD | NEW |