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/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/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/extension_event_names.h" | 10 #include "chrome/browser/extensions/extension_event_names.h" |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 | 119 |
120 ExtensionBrowserEventRouter::~ExtensionBrowserEventRouter() { | 120 ExtensionBrowserEventRouter::~ExtensionBrowserEventRouter() { |
121 BrowserList::RemoveObserver(this); | 121 BrowserList::RemoveObserver(this); |
122 #if defined(TOOLKIT_VIEWS) | 122 #if defined(TOOLKIT_VIEWS) |
123 views::WidgetFocusManager::GetInstance()->RemoveFocusChangeListener(this); | 123 views::WidgetFocusManager::GetInstance()->RemoveFocusChangeListener(this); |
124 #elif defined(TOOLKIT_GTK) | 124 #elif defined(TOOLKIT_GTK) |
125 ui::ActiveWindowWatcherX::RemoveObserver(this); | 125 ui::ActiveWindowWatcherX::RemoveObserver(this); |
126 #endif | 126 #endif |
127 } | 127 } |
128 | 128 |
129 void ExtensionBrowserEventRouter::OnBrowserAdded(const Browser* browser) { | 129 void ExtensionBrowserEventRouter::OnBrowserAdded(Browser* browser) { |
130 RegisterForBrowserNotifications(browser); | 130 RegisterForBrowserNotifications(browser); |
131 } | 131 } |
132 | 132 |
133 void ExtensionBrowserEventRouter::RegisterForBrowserNotifications( | 133 void ExtensionBrowserEventRouter::RegisterForBrowserNotifications( |
134 const Browser* browser) { | 134 Browser* browser) { |
135 if (!profile_->IsSameProfile(browser->profile())) | 135 if (!profile_->IsSameProfile(browser->profile())) |
136 return; | 136 return; |
137 // Start listening to TabStripModel events for this browser. | 137 // Start listening to TabStripModel events for this browser. |
138 browser->tab_strip_model()->AddObserver(this); | 138 browser->tab_strip_model()->AddObserver(this); |
139 | 139 |
140 // If this is a new window, it isn't ready at this point, so we register to be | 140 // If this is a new window, it isn't ready at this point, so we register to be |
141 // notified when it is. If this is an existing window, this is a no-op that we | 141 // notified when it is. If this is an existing window, this is a no-op that we |
142 // just do to reduce code complexity. | 142 // just do to reduce code complexity. |
143 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_WINDOW_READY, | 143 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_WINDOW_READY, |
144 content::Source<const Browser>(browser)); | 144 content::Source<Browser>(browser)); |
145 | 145 |
146 for (int i = 0; i < browser->tab_strip_model()->count(); ++i) { | 146 for (int i = 0; i < browser->tab_strip_model()->count(); ++i) { |
147 RegisterForTabNotifications( | 147 RegisterForTabNotifications( |
148 browser->GetTabContentsWrapperAt(i)->web_contents()); | 148 browser->GetTabContentsWrapperAt(i)->web_contents()); |
149 } | 149 } |
150 } | 150 } |
151 | 151 |
152 void ExtensionBrowserEventRouter::RegisterForTabNotifications( | 152 void ExtensionBrowserEventRouter::RegisterForTabNotifications( |
153 WebContents* contents) { | 153 WebContents* contents) { |
154 registrar_.Add( | 154 registrar_.Add( |
155 this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 155 this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
156 content::Source<NavigationController>(&contents->GetController())); | 156 content::Source<NavigationController>(&contents->GetController())); |
157 | 157 |
158 // Observing NOTIFICATION_WEB_CONTENTS_DESTROYED is necessary because it's | 158 // Observing NOTIFICATION_WEB_CONTENTS_DESTROYED is necessary because it's |
159 // possible for tabs to be created, detached and then destroyed without | 159 // possible for tabs to be created, detached and then destroyed without |
160 // ever having been re-attached and closed. This happens in the case of | 160 // ever having been re-attached and closed. This happens in the case of |
161 // a devtools WebContents that is opened in window, docked, then closed. | 161 // a devtools WebContents that is opened in window, docked, then closed. |
162 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | 162 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
163 content::Source<WebContents>(contents)); | 163 content::Source<WebContents>(contents)); |
164 } | 164 } |
165 | 165 |
166 void ExtensionBrowserEventRouter::UnregisterForTabNotifications( | 166 void ExtensionBrowserEventRouter::UnregisterForTabNotifications( |
167 WebContents* contents) { | 167 WebContents* contents) { |
168 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 168 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
169 content::Source<NavigationController>(&contents->GetController())); | 169 content::Source<NavigationController>(&contents->GetController())); |
170 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | 170 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
171 content::Source<WebContents>(contents)); | 171 content::Source<WebContents>(contents)); |
172 } | 172 } |
173 | 173 |
174 void ExtensionBrowserEventRouter::OnBrowserWindowReady(const Browser* browser) { | 174 void ExtensionBrowserEventRouter::OnBrowserWindowReady(Browser* browser) { |
175 ListValue args; | 175 ListValue args; |
176 | 176 |
177 DCHECK(browser->extension_window_controller()); | 177 DCHECK(browser->extension_window_controller()); |
178 DictionaryValue* window_dictionary = | 178 DictionaryValue* window_dictionary = |
179 browser->extension_window_controller()->CreateWindowValue(); | 179 browser->extension_window_controller()->CreateWindowValue(); |
180 args.Append(window_dictionary); | 180 args.Append(window_dictionary); |
181 | 181 |
182 std::string json_args; | 182 std::string json_args; |
183 base::JSONWriter::Write(&args, &json_args); | 183 base::JSONWriter::Write(&args, &json_args); |
184 | 184 |
185 DispatchEvent(browser->profile(), events::kOnWindowCreated, json_args); | 185 DispatchEvent(browser->profile(), events::kOnWindowCreated, json_args); |
186 } | 186 } |
187 | 187 |
188 void ExtensionBrowserEventRouter::OnBrowserRemoved(const Browser* browser) { | 188 void ExtensionBrowserEventRouter::OnBrowserRemoved(Browser* browser) { |
189 if (!profile_->IsSameProfile(browser->profile())) | 189 if (!profile_->IsSameProfile(browser->profile())) |
190 return; | 190 return; |
191 | 191 |
192 // Stop listening to TabStripModel events for this browser. | 192 // Stop listening to TabStripModel events for this browser. |
193 browser->tab_strip_model()->RemoveObserver(this); | 193 browser->tab_strip_model()->RemoveObserver(this); |
194 | 194 |
195 registrar_.Remove(this, chrome::NOTIFICATION_BROWSER_WINDOW_READY, | 195 registrar_.Remove(this, chrome::NOTIFICATION_BROWSER_WINDOW_READY, |
196 content::Source<const Browser>(browser)); | 196 content::Source<Browser>(browser)); |
197 | 197 |
198 DispatchSimpleBrowserEvent(browser->profile(), | 198 DispatchSimpleBrowserEvent(browser->profile(), |
199 ExtensionTabUtil::GetWindowId(browser), | 199 ExtensionTabUtil::GetWindowId(browser), |
200 events::kOnWindowRemoved); | 200 events::kOnWindowRemoved); |
201 } | 201 } |
202 | 202 |
203 #if defined(TOOLKIT_VIEWS) | 203 #if defined(TOOLKIT_VIEWS) |
204 void ExtensionBrowserEventRouter::OnNativeFocusChange( | 204 void ExtensionBrowserEventRouter::OnNativeFocusChange( |
205 gfx::NativeView focused_before, | 205 gfx::NativeView focused_before, |
206 gfx::NativeView focused_now) { | 206 gfx::NativeView focused_now) { |
207 if (!focused_now) | 207 if (!focused_now) |
208 OnBrowserSetLastActive(NULL); | 208 OnBrowserSetLastActive(NULL); |
209 } | 209 } |
210 #elif defined(TOOLKIT_GTK) | 210 #elif defined(TOOLKIT_GTK) |
211 void ExtensionBrowserEventRouter::ActiveWindowChanged( | 211 void ExtensionBrowserEventRouter::ActiveWindowChanged( |
212 GdkWindow* active_window) { | 212 GdkWindow* active_window) { |
213 if (!active_window) | 213 if (!active_window) |
214 OnBrowserSetLastActive(NULL); | 214 OnBrowserSetLastActive(NULL); |
215 } | 215 } |
216 #endif | 216 #endif |
217 | 217 |
218 void ExtensionBrowserEventRouter::OnBrowserSetLastActive( | 218 void ExtensionBrowserEventRouter::OnBrowserSetLastActive( |
219 const Browser* browser) { | 219 Browser* browser) { |
220 Profile* window_profile = NULL; | 220 Profile* window_profile = NULL; |
221 int window_id = extension_misc::kUnknownWindowId; | 221 int window_id = extension_misc::kUnknownWindowId; |
222 if (browser && profile_->IsSameProfile(browser->profile())) { | 222 if (browser && profile_->IsSameProfile(browser->profile())) { |
223 window_profile = browser->profile(); | 223 window_profile = browser->profile(); |
224 window_id = ExtensionTabUtil::GetWindowId(browser); | 224 window_id = ExtensionTabUtil::GetWindowId(browser); |
225 } | 225 } |
226 | 226 |
227 if (focused_window_id_ == window_id) | 227 if (focused_window_id_ == window_id) |
228 return; | 228 return; |
229 | 229 |
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
558 content::Source<NavigationController>(source).ptr(); | 558 content::Source<NavigationController>(source).ptr(); |
559 TabUpdated(source_controller->GetWebContents(), true); | 559 TabUpdated(source_controller->GetWebContents(), true); |
560 } else if (type == content::NOTIFICATION_WEB_CONTENTS_DESTROYED) { | 560 } else if (type == content::NOTIFICATION_WEB_CONTENTS_DESTROYED) { |
561 // Tab was destroyed after being detached (without being re-attached). | 561 // Tab was destroyed after being detached (without being re-attached). |
562 WebContents* contents = content::Source<WebContents>(source).ptr(); | 562 WebContents* contents = content::Source<WebContents>(source).ptr(); |
563 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 563 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
564 content::Source<NavigationController>(&contents->GetController())); | 564 content::Source<NavigationController>(&contents->GetController())); |
565 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | 565 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
566 content::Source<WebContents>(contents)); | 566 content::Source<WebContents>(contents)); |
567 } else if (type == chrome::NOTIFICATION_BROWSER_WINDOW_READY) { | 567 } else if (type == chrome::NOTIFICATION_BROWSER_WINDOW_READY) { |
568 const Browser* browser = content::Source<const Browser>(source).ptr(); | 568 Browser* browser = content::Source<Browser>(source).ptr(); |
569 OnBrowserWindowReady(browser); | 569 OnBrowserWindowReady(browser); |
570 #if defined(OS_MACOSX) | 570 #if defined(OS_MACOSX) |
571 } else if (type == content::NOTIFICATION_NO_KEY_WINDOW) { | 571 } else if (type == content::NOTIFICATION_NO_KEY_WINDOW) { |
572 OnBrowserSetLastActive(NULL); | 572 OnBrowserSetLastActive(NULL); |
573 #endif | 573 #endif |
574 } else { | 574 } else { |
575 NOTREACHED(); | 575 NOTREACHED(); |
576 } | 576 } |
577 } | 577 } |
578 | 578 |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
694 } | 694 } |
695 | 695 |
696 if (event_name) { | 696 if (event_name) { |
697 DispatchEventWithTab(profile, | 697 DispatchEventWithTab(profile, |
698 extension_id, | 698 extension_id, |
699 event_name, | 699 event_name, |
700 tab_contents->web_contents(), | 700 tab_contents->web_contents(), |
701 true); | 701 true); |
702 } | 702 } |
703 } | 703 } |
OLD | NEW |