| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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" |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 tab_keys::kStatusValueLoading); | 54 tab_keys::kStatusValueLoading); |
| 55 | 55 |
| 56 if (contents->GetURL() != url_) { | 56 if (contents->GetURL() != url_) { |
| 57 url_ = contents->GetURL(); | 57 url_ = contents->GetURL(); |
| 58 changed_properties->SetString(tab_keys::kUrlKey, url_.spec()); | 58 changed_properties->SetString(tab_keys::kUrlKey, url_.spec()); |
| 59 } | 59 } |
| 60 | 60 |
| 61 return changed_properties; | 61 return changed_properties; |
| 62 } | 62 } |
| 63 | 63 |
| 64 ExtensionBrowserEventRouter* ExtensionBrowserEventRouter::GetInstance() { | |
| 65 return Singleton<ExtensionBrowserEventRouter>::get(); | |
| 66 } | |
| 67 | |
| 68 static void DispatchEvent(Profile* profile, | 64 static void DispatchEvent(Profile* profile, |
| 69 const char* event_name, | 65 const char* event_name, |
| 70 const std::string& json_args) { | 66 const std::string& json_args) { |
| 71 if (profile->GetExtensionEventRouter()) { | 67 if (profile->GetExtensionEventRouter()) { |
| 72 profile->GetExtensionEventRouter()->DispatchEventToRenderers( | 68 profile->GetExtensionEventRouter()->DispatchEventToRenderers( |
| 73 event_name, json_args, profile, GURL()); | 69 event_name, json_args, profile, GURL()); |
| 74 } | 70 } |
| 75 } | 71 } |
| 76 | 72 |
| 77 static void DispatchEventToExtension(Profile* profile, | 73 static void DispatchEventToExtension(Profile* profile, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 104 const char* event_name) { | 100 const char* event_name) { |
| 105 ListValue args; | 101 ListValue args; |
| 106 args.Append(Value::CreateIntegerValue(window_id)); | 102 args.Append(Value::CreateIntegerValue(window_id)); |
| 107 | 103 |
| 108 std::string json_args; | 104 std::string json_args; |
| 109 base::JSONWriter::Write(&args, false, &json_args); | 105 base::JSONWriter::Write(&args, false, &json_args); |
| 110 | 106 |
| 111 DispatchEvent(profile, event_name, json_args); | 107 DispatchEvent(profile, event_name, json_args); |
| 112 } | 108 } |
| 113 | 109 |
| 114 void ExtensionBrowserEventRouter::Init(Profile* profile) { | 110 void ExtensionBrowserEventRouter::Init() { |
| 115 if (initialized_) | 111 if (initialized_) |
| 116 return; | 112 return; |
| 117 DCHECK(!profile->IsOffTheRecord()); | |
| 118 profile_ = profile; | |
| 119 BrowserList::AddObserver(this); | 113 BrowserList::AddObserver(this); |
| 120 #if defined(TOOLKIT_VIEWS) | 114 #if defined(TOOLKIT_VIEWS) |
| 121 views::FocusManager::GetWidgetFocusManager()->AddFocusChangeListener(this); | 115 views::FocusManager::GetWidgetFocusManager()->AddFocusChangeListener(this); |
| 122 #elif defined(TOOLKIT_GTK) | 116 #elif defined(TOOLKIT_GTK) |
| 123 ActiveWindowWatcherX::AddObserver(this); | 117 ActiveWindowWatcherX::AddObserver(this); |
| 124 #elif defined(OS_MACOSX) | 118 #elif defined(OS_MACOSX) |
| 125 // Needed for when no suitable window can be passed to an extension as the | 119 // Needed for when no suitable window can be passed to an extension as the |
| 126 // currently focused window. | 120 // currently focused window. |
| 127 registrar_.Add(this, NotificationType::NO_KEY_WINDOW, | 121 registrar_.Add(this, NotificationType::NO_KEY_WINDOW, |
| 128 NotificationService::AllSources()); | 122 NotificationService::AllSources()); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 141 TabContents* contents = browser->GetTabContentsAt(i); | 135 TabContents* contents = browser->GetTabContentsAt(i); |
| 142 int tab_id = ExtensionTabUtil::GetTabId(contents); | 136 int tab_id = ExtensionTabUtil::GetTabId(contents); |
| 143 tab_entries_[tab_id] = TabEntry(); | 137 tab_entries_[tab_id] = TabEntry(); |
| 144 } | 138 } |
| 145 } | 139 } |
| 146 } | 140 } |
| 147 | 141 |
| 148 initialized_ = true; | 142 initialized_ = true; |
| 149 } | 143 } |
| 150 | 144 |
| 151 ExtensionBrowserEventRouter::ExtensionBrowserEventRouter() | 145 ExtensionBrowserEventRouter::ExtensionBrowserEventRouter(Profile* profile) |
| 152 : initialized_(false), | 146 : initialized_(false), |
| 153 focused_window_id_(extension_misc::kUnknownWindowId), | 147 focused_window_id_(extension_misc::kUnknownWindowId), |
| 154 profile_(NULL) { } | 148 profile_(profile) { |
| 149 DCHECK(!profile->IsOffTheRecord()); |
| 150 } |
| 155 | 151 |
| 156 ExtensionBrowserEventRouter::~ExtensionBrowserEventRouter() {} | 152 ExtensionBrowserEventRouter::~ExtensionBrowserEventRouter() { |
| 153 BrowserList::RemoveObserver(this); |
| 154 #if defined(TOOLKIT_VIEWS) |
| 155 views::FocusManager::GetWidgetFocusManager()->RemoveFocusChangeListener(this); |
| 156 #elif defined(TOOLKIT_GTK) |
| 157 ActiveWindowWatcherX::RemoveObserver(this); |
| 158 #endif |
| 159 } |
| 157 | 160 |
| 158 void ExtensionBrowserEventRouter::OnBrowserAdded(const Browser* browser) { | 161 void ExtensionBrowserEventRouter::OnBrowserAdded(const Browser* browser) { |
| 159 RegisterForBrowserNotifications(browser); | 162 RegisterForBrowserNotifications(browser); |
| 160 } | 163 } |
| 161 | 164 |
| 162 void ExtensionBrowserEventRouter::RegisterForBrowserNotifications( | 165 void ExtensionBrowserEventRouter::RegisterForBrowserNotifications( |
| 163 const Browser* browser) { | 166 const Browser* browser) { |
| 164 // Start listening to TabStripModel events for this browser. | 167 // Start listening to TabStripModel events for this browser. |
| 165 browser->tabstrip_model()->AddObserver(this); | 168 browser->tabstrip_model()->AddObserver(this); |
| 166 | 169 |
| (...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 534 | 537 |
| 535 void ExtensionBrowserEventRouter::BrowserActionExecuted( | 538 void ExtensionBrowserEventRouter::BrowserActionExecuted( |
| 536 Profile* profile, const std::string& extension_id, Browser* browser) { | 539 Profile* profile, const std::string& extension_id, Browser* browser) { |
| 537 TabContentsWrapper* tab_contents = NULL; | 540 TabContentsWrapper* tab_contents = NULL; |
| 538 int tab_id = 0; | 541 int tab_id = 0; |
| 539 if (!ExtensionTabUtil::GetDefaultTab(browser, &tab_contents, &tab_id)) | 542 if (!ExtensionTabUtil::GetDefaultTab(browser, &tab_contents, &tab_id)) |
| 540 return; | 543 return; |
| 541 DispatchEventWithTab(profile, extension_id, "browserAction.onClicked", | 544 DispatchEventWithTab(profile, extension_id, "browserAction.onClicked", |
| 542 tab_contents->tab_contents()); | 545 tab_contents->tab_contents()); |
| 543 } | 546 } |
| OLD | NEW |