OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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_writer.h" | 7 #include "base/json/json_writer.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
9 #include "chrome/browser/browser.h" | 9 #include "chrome/browser/browser.h" |
10 #include "chrome/browser/profile.h" | 10 #include "chrome/browser/profile.h" |
11 #include "chrome/browser/extensions/extension_event_names.h" | 11 #include "chrome/browser/extensions/extension_event_names.h" |
12 #include "chrome/browser/extensions/extension_message_service.h" | 12 #include "chrome/browser/extensions/extension_message_service.h" |
13 #include "chrome/browser/extensions/extension_tabs_module_constants.h" | 13 #include "chrome/browser/extensions/extension_tabs_module_constants.h" |
14 #include "chrome/browser/extensions/extension_page_actions_module_constants.h" | 14 #include "chrome/browser/extensions/extension_page_actions_module_constants.h" |
15 #include "chrome/browser/tab_contents/navigation_entry.h" | 15 #include "chrome/browser/tab_contents/navigation_entry.h" |
16 #include "chrome/browser/tab_contents/tab_contents.h" | 16 #include "chrome/browser/tab_contents/tab_contents.h" |
17 #include "chrome/common/extensions/extension.h" | 17 #include "chrome/common/extensions/extension.h" |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 } | 93 } |
94 } | 94 } |
95 | 95 |
96 static void DispatchSimpleBrowserEvent(Profile* profile, | 96 static void DispatchSimpleBrowserEvent(Profile* profile, |
97 const int window_id, | 97 const int window_id, |
98 const char* event_name) { | 98 const char* event_name) { |
99 ListValue args; | 99 ListValue args; |
100 args.Append(Value::CreateIntegerValue(window_id)); | 100 args.Append(Value::CreateIntegerValue(window_id)); |
101 | 101 |
102 std::string json_args; | 102 std::string json_args; |
103 JSONWriter::Write(&args, false, &json_args); | 103 base::JSONWriter::Write(&args, false, &json_args); |
104 | 104 |
105 DispatchEvent(profile, event_name, json_args); | 105 DispatchEvent(profile, event_name, json_args); |
106 } | 106 } |
107 | 107 |
108 void ExtensionBrowserEventRouter::Init() { | 108 void ExtensionBrowserEventRouter::Init() { |
109 if (initialized_) | 109 if (initialized_) |
110 return; | 110 return; |
111 | 111 |
112 BrowserList::AddObserver(this); | 112 BrowserList::AddObserver(this); |
113 | 113 |
(...skipping 13 matching lines...) Expand all Loading... |
127 } | 127 } |
128 | 128 |
129 void ExtensionBrowserEventRouter::OnBrowserWindowReady(const Browser* browser) { | 129 void ExtensionBrowserEventRouter::OnBrowserWindowReady(const Browser* browser) { |
130 ListValue args; | 130 ListValue args; |
131 | 131 |
132 DictionaryValue* window_dictionary = ExtensionTabUtil::CreateWindowValue( | 132 DictionaryValue* window_dictionary = ExtensionTabUtil::CreateWindowValue( |
133 browser, false); | 133 browser, false); |
134 args.Append(window_dictionary); | 134 args.Append(window_dictionary); |
135 | 135 |
136 std::string json_args; | 136 std::string json_args; |
137 JSONWriter::Write(&args, false, &json_args); | 137 base::JSONWriter::Write(&args, false, &json_args); |
138 | 138 |
139 DispatchEvent(browser->profile(), events::kOnWindowCreated, json_args); | 139 DispatchEvent(browser->profile(), events::kOnWindowCreated, json_args); |
140 } | 140 } |
141 | 141 |
142 void ExtensionBrowserEventRouter::OnBrowserRemoving(const Browser* browser) { | 142 void ExtensionBrowserEventRouter::OnBrowserRemoving(const Browser* browser) { |
143 // Stop listening to TabStripModel events for this browser. | 143 // Stop listening to TabStripModel events for this browser. |
144 browser->tabstrip_model()->RemoveObserver(this); | 144 browser->tabstrip_model()->RemoveObserver(this); |
145 | 145 |
146 registrar_.Remove(this, NotificationType::BROWSER_WINDOW_READY, | 146 registrar_.Remove(this, NotificationType::BROWSER_WINDOW_READY, |
147 Source<const Browser>(browser)); | 147 Source<const Browser>(browser)); |
148 | 148 |
149 DispatchSimpleBrowserEvent(browser->profile(), | 149 DispatchSimpleBrowserEvent(browser->profile(), |
150 ExtensionTabUtil::GetWindowId(browser), | 150 ExtensionTabUtil::GetWindowId(browser), |
151 events::kOnWindowRemoved); | 151 events::kOnWindowRemoved); |
152 } | 152 } |
153 | 153 |
154 void ExtensionBrowserEventRouter::OnBrowserSetLastActive( | 154 void ExtensionBrowserEventRouter::OnBrowserSetLastActive( |
155 const Browser* browser) { | 155 const Browser* browser) { |
156 DispatchSimpleBrowserEvent(browser->profile(), | 156 DispatchSimpleBrowserEvent(browser->profile(), |
157 ExtensionTabUtil::GetWindowId(browser), | 157 ExtensionTabUtil::GetWindowId(browser), |
158 events::kOnWindowFocusedChanged); | 158 events::kOnWindowFocusedChanged); |
159 } | 159 } |
160 | 160 |
161 void ExtensionBrowserEventRouter::TabCreatedAt(TabContents* contents, | 161 void ExtensionBrowserEventRouter::TabCreatedAt(TabContents* contents, |
162 int index, | 162 int index, |
163 bool foreground) { | 163 bool foreground) { |
164 ListValue args; | 164 ListValue args; |
165 args.Append(ExtensionTabUtil::CreateTabValue(contents)); | 165 args.Append(ExtensionTabUtil::CreateTabValue(contents)); |
166 std::string json_args; | 166 std::string json_args; |
167 JSONWriter::Write(&args, false, &json_args); | 167 base::JSONWriter::Write(&args, false, &json_args); |
168 | 168 |
169 DispatchEvent(contents->profile(), events::kOnTabCreated, json_args); | 169 DispatchEvent(contents->profile(), events::kOnTabCreated, json_args); |
170 | 170 |
171 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, | 171 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, |
172 Source<NavigationController>(&contents->controller())); | 172 Source<NavigationController>(&contents->controller())); |
173 | 173 |
174 // Observing TAB_CONTENTS_DESTROYED is necessary because it's | 174 // Observing TAB_CONTENTS_DESTROYED is necessary because it's |
175 // possible for tabs to be created, detached and then destroyed without | 175 // possible for tabs to be created, detached and then destroyed without |
176 // ever having been re-attached and closed. This happens in the case of | 176 // ever having been re-attached and closed. This happens in the case of |
177 // a devtools TabContents that is opened in window, docked, then closed. | 177 // a devtools TabContents that is opened in window, docked, then closed. |
(...skipping 17 matching lines...) Expand all Loading... |
195 args.Append(Value::CreateIntegerValue(tab_id)); | 195 args.Append(Value::CreateIntegerValue(tab_id)); |
196 | 196 |
197 DictionaryValue* object_args = new DictionaryValue(); | 197 DictionaryValue* object_args = new DictionaryValue(); |
198 object_args->Set(tab_keys::kNewWindowIdKey, Value::CreateIntegerValue( | 198 object_args->Set(tab_keys::kNewWindowIdKey, Value::CreateIntegerValue( |
199 ExtensionTabUtil::GetWindowIdOfTab(contents))); | 199 ExtensionTabUtil::GetWindowIdOfTab(contents))); |
200 object_args->Set(tab_keys::kNewPositionKey, Value::CreateIntegerValue( | 200 object_args->Set(tab_keys::kNewPositionKey, Value::CreateIntegerValue( |
201 index)); | 201 index)); |
202 args.Append(object_args); | 202 args.Append(object_args); |
203 | 203 |
204 std::string json_args; | 204 std::string json_args; |
205 JSONWriter::Write(&args, false, &json_args); | 205 base::JSONWriter::Write(&args, false, &json_args); |
206 | 206 |
207 DispatchEvent(contents->profile(), events::kOnTabAttached, json_args); | 207 DispatchEvent(contents->profile(), events::kOnTabAttached, json_args); |
208 } | 208 } |
209 | 209 |
210 void ExtensionBrowserEventRouter::TabDetachedAt(TabContents* contents, | 210 void ExtensionBrowserEventRouter::TabDetachedAt(TabContents* contents, |
211 int index) { | 211 int index) { |
212 int tab_id = ExtensionTabUtil::GetTabId(contents); | 212 int tab_id = ExtensionTabUtil::GetTabId(contents); |
213 if (tab_entries_.find(tab_id) == tab_entries_.end()) { | 213 if (tab_entries_.find(tab_id) == tab_entries_.end()) { |
214 // The tab was removed. Don't send detach event. | 214 // The tab was removed. Don't send detach event. |
215 return; | 215 return; |
216 } | 216 } |
217 | 217 |
218 ListValue args; | 218 ListValue args; |
219 args.Append(Value::CreateIntegerValue(tab_id)); | 219 args.Append(Value::CreateIntegerValue(tab_id)); |
220 | 220 |
221 DictionaryValue* object_args = new DictionaryValue(); | 221 DictionaryValue* object_args = new DictionaryValue(); |
222 object_args->Set(tab_keys::kOldWindowIdKey, Value::CreateIntegerValue( | 222 object_args->Set(tab_keys::kOldWindowIdKey, Value::CreateIntegerValue( |
223 ExtensionTabUtil::GetWindowIdOfTab(contents))); | 223 ExtensionTabUtil::GetWindowIdOfTab(contents))); |
224 object_args->Set(tab_keys::kOldPositionKey, Value::CreateIntegerValue( | 224 object_args->Set(tab_keys::kOldPositionKey, Value::CreateIntegerValue( |
225 index)); | 225 index)); |
226 args.Append(object_args); | 226 args.Append(object_args); |
227 | 227 |
228 std::string json_args; | 228 std::string json_args; |
229 JSONWriter::Write(&args, false, &json_args); | 229 base::JSONWriter::Write(&args, false, &json_args); |
230 | 230 |
231 DispatchEvent(contents->profile(), events::kOnTabDetached, json_args); | 231 DispatchEvent(contents->profile(), events::kOnTabDetached, json_args); |
232 } | 232 } |
233 | 233 |
234 void ExtensionBrowserEventRouter::TabClosingAt(TabContents* contents, | 234 void ExtensionBrowserEventRouter::TabClosingAt(TabContents* contents, |
235 int index) { | 235 int index) { |
236 int tab_id = ExtensionTabUtil::GetTabId(contents); | 236 int tab_id = ExtensionTabUtil::GetTabId(contents); |
237 | 237 |
238 ListValue args; | 238 ListValue args; |
239 args.Append(Value::CreateIntegerValue(tab_id)); | 239 args.Append(Value::CreateIntegerValue(tab_id)); |
240 | 240 |
241 std::string json_args; | 241 std::string json_args; |
242 JSONWriter::Write(&args, false, &json_args); | 242 base::JSONWriter::Write(&args, false, &json_args); |
243 | 243 |
244 DispatchEvent(contents->profile(), events::kOnTabRemoved, json_args); | 244 DispatchEvent(contents->profile(), events::kOnTabRemoved, json_args); |
245 | 245 |
246 int removed_count = tab_entries_.erase(tab_id); | 246 int removed_count = tab_entries_.erase(tab_id); |
247 DCHECK_GT(removed_count, 0); | 247 DCHECK_GT(removed_count, 0); |
248 | 248 |
249 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED, | 249 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED, |
250 Source<NavigationController>(&contents->controller())); | 250 Source<NavigationController>(&contents->controller())); |
251 registrar_.Remove(this, NotificationType::TAB_CONTENTS_DESTROYED, | 251 registrar_.Remove(this, NotificationType::TAB_CONTENTS_DESTROYED, |
252 Source<TabContents>(contents)); | 252 Source<TabContents>(contents)); |
253 } | 253 } |
254 | 254 |
255 void ExtensionBrowserEventRouter::TabSelectedAt(TabContents* old_contents, | 255 void ExtensionBrowserEventRouter::TabSelectedAt(TabContents* old_contents, |
256 TabContents* new_contents, | 256 TabContents* new_contents, |
257 int index, | 257 int index, |
258 bool user_gesture) { | 258 bool user_gesture) { |
259 ListValue args; | 259 ListValue args; |
260 args.Append(Value::CreateIntegerValue( | 260 args.Append(Value::CreateIntegerValue( |
261 ExtensionTabUtil::GetTabId(new_contents))); | 261 ExtensionTabUtil::GetTabId(new_contents))); |
262 | 262 |
263 DictionaryValue* object_args = new DictionaryValue(); | 263 DictionaryValue* object_args = new DictionaryValue(); |
264 object_args->Set(tab_keys::kWindowIdKey, Value::CreateIntegerValue( | 264 object_args->Set(tab_keys::kWindowIdKey, Value::CreateIntegerValue( |
265 ExtensionTabUtil::GetWindowIdOfTab(new_contents))); | 265 ExtensionTabUtil::GetWindowIdOfTab(new_contents))); |
266 args.Append(object_args); | 266 args.Append(object_args); |
267 | 267 |
268 std::string json_args; | 268 std::string json_args; |
269 JSONWriter::Write(&args, false, &json_args); | 269 base::JSONWriter::Write(&args, false, &json_args); |
270 | 270 |
271 DispatchEvent(new_contents->profile(), events::kOnTabSelectionChanged, | 271 DispatchEvent(new_contents->profile(), events::kOnTabSelectionChanged, |
272 json_args); | 272 json_args); |
273 } | 273 } |
274 | 274 |
275 void ExtensionBrowserEventRouter::TabMoved(TabContents* contents, | 275 void ExtensionBrowserEventRouter::TabMoved(TabContents* contents, |
276 int from_index, | 276 int from_index, |
277 int to_index, | 277 int to_index, |
278 bool pinned_state_changed) { | 278 bool pinned_state_changed) { |
279 ListValue args; | 279 ListValue args; |
280 args.Append(Value::CreateIntegerValue(ExtensionTabUtil::GetTabId(contents))); | 280 args.Append(Value::CreateIntegerValue(ExtensionTabUtil::GetTabId(contents))); |
281 | 281 |
282 DictionaryValue* object_args = new DictionaryValue(); | 282 DictionaryValue* object_args = new DictionaryValue(); |
283 object_args->Set(tab_keys::kWindowIdKey, Value::CreateIntegerValue( | 283 object_args->Set(tab_keys::kWindowIdKey, Value::CreateIntegerValue( |
284 ExtensionTabUtil::GetWindowIdOfTab(contents))); | 284 ExtensionTabUtil::GetWindowIdOfTab(contents))); |
285 object_args->Set(tab_keys::kFromIndexKey, Value::CreateIntegerValue( | 285 object_args->Set(tab_keys::kFromIndexKey, Value::CreateIntegerValue( |
286 from_index)); | 286 from_index)); |
287 object_args->Set(tab_keys::kToIndexKey, Value::CreateIntegerValue( | 287 object_args->Set(tab_keys::kToIndexKey, Value::CreateIntegerValue( |
288 to_index)); | 288 to_index)); |
289 args.Append(object_args); | 289 args.Append(object_args); |
290 | 290 |
291 std::string json_args; | 291 std::string json_args; |
292 JSONWriter::Write(&args, false, &json_args); | 292 base::JSONWriter::Write(&args, false, &json_args); |
293 | 293 |
294 DispatchEvent(contents->profile(), events::kOnTabMoved, json_args); | 294 DispatchEvent(contents->profile(), events::kOnTabMoved, json_args); |
295 } | 295 } |
296 | 296 |
297 void ExtensionBrowserEventRouter::TabUpdated(TabContents* contents, | 297 void ExtensionBrowserEventRouter::TabUpdated(TabContents* contents, |
298 bool did_navigate) { | 298 bool did_navigate) { |
299 int tab_id = ExtensionTabUtil::GetTabId(contents); | 299 int tab_id = ExtensionTabUtil::GetTabId(contents); |
300 std::map<int, TabEntry>::iterator i = tab_entries_.find(tab_id); | 300 std::map<int, TabEntry>::iterator i = tab_entries_.find(tab_id); |
301 DCHECK(tab_entries_.end() != i); | 301 DCHECK(tab_entries_.end() != i); |
302 TabEntry& entry = i->second; | 302 TabEntry& entry = i->second; |
303 | 303 |
304 DictionaryValue* changed_properties = NULL; | 304 DictionaryValue* changed_properties = NULL; |
305 if (did_navigate) | 305 if (did_navigate) |
306 changed_properties = entry.DidNavigate(contents); | 306 changed_properties = entry.DidNavigate(contents); |
307 else | 307 else |
308 changed_properties = entry.UpdateLoadState(contents); | 308 changed_properties = entry.UpdateLoadState(contents); |
309 | 309 |
310 if (changed_properties) { | 310 if (changed_properties) { |
311 // The state of the tab (as seen from the extension point of view) has | 311 // The state of the tab (as seen from the extension point of view) has |
312 // changed. Send a notification to the extension. | 312 // changed. Send a notification to the extension. |
313 ListValue args; | 313 ListValue args; |
314 args.Append(Value::CreateIntegerValue(tab_id)); | 314 args.Append(Value::CreateIntegerValue(tab_id)); |
315 args.Append(changed_properties); | 315 args.Append(changed_properties); |
316 | 316 |
317 std::string json_args; | 317 std::string json_args; |
318 JSONWriter::Write(&args, false, &json_args); | 318 base::JSONWriter::Write(&args, false, &json_args); |
319 | 319 |
320 DispatchEvent(contents->profile(), events::kOnTabUpdated, json_args); | 320 DispatchEvent(contents->profile(), events::kOnTabUpdated, json_args); |
321 } | 321 } |
322 } | 322 } |
323 | 323 |
324 void ExtensionBrowserEventRouter::Observe(NotificationType type, | 324 void ExtensionBrowserEventRouter::Observe(NotificationType type, |
325 const NotificationSource& source, | 325 const NotificationSource& source, |
326 const NotificationDetails& details) { | 326 const NotificationDetails& details) { |
327 if (type == NotificationType::NAV_ENTRY_COMMITTED) { | 327 if (type == NotificationType::NAV_ENTRY_COMMITTED) { |
328 NavigationController* source_controller = | 328 NavigationController* source_controller = |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 ListValue args; | 361 ListValue args; |
362 args.Append(Value::CreateStringValue(page_action_id)); | 362 args.Append(Value::CreateStringValue(page_action_id)); |
363 | 363 |
364 DictionaryValue* data = new DictionaryValue(); | 364 DictionaryValue* data = new DictionaryValue(); |
365 data->Set(tab_keys::kTabIdKey, Value::CreateIntegerValue(tab_id)); | 365 data->Set(tab_keys::kTabIdKey, Value::CreateIntegerValue(tab_id)); |
366 data->Set(tab_keys::kTabUrlKey, Value::CreateStringValue(url)); | 366 data->Set(tab_keys::kTabUrlKey, Value::CreateStringValue(url)); |
367 data->Set(page_action_keys::kButtonKey, Value::CreateIntegerValue(button)); | 367 data->Set(page_action_keys::kButtonKey, Value::CreateIntegerValue(button)); |
368 args.Append(data); | 368 args.Append(data); |
369 | 369 |
370 std::string json_args; | 370 std::string json_args; |
371 JSONWriter::Write(&args, false, &json_args); | 371 base::JSONWriter::Write(&args, false, &json_args); |
372 | 372 |
373 std::string event_name = std::string("pageAction/") + extension_id; | 373 std::string event_name = std::string("pageAction/") + extension_id; |
374 DispatchEvent(profile, event_name.c_str(), json_args); | 374 DispatchEvent(profile, event_name.c_str(), json_args); |
375 } | 375 } |
376 | 376 |
377 void ExtensionBrowserEventRouter::BrowserActionExecuted( | 377 void ExtensionBrowserEventRouter::BrowserActionExecuted( |
378 Profile* profile, const std::string& extension_id, Browser* browser) { | 378 Profile* profile, const std::string& extension_id, Browser* browser) { |
379 TabContents* tab_contents = NULL; | 379 TabContents* tab_contents = NULL; |
380 int tab_id = 0; | 380 int tab_id = 0; |
381 if (!ExtensionTabUtil::GetDefaultTab(browser, &tab_contents, &tab_id)) | 381 if (!ExtensionTabUtil::GetDefaultTab(browser, &tab_contents, &tab_id)) |
382 return; | 382 return; |
383 | 383 |
384 ListValue args; | 384 ListValue args; |
385 args.Append(ExtensionTabUtil::CreateTabValue(tab_contents)); | 385 args.Append(ExtensionTabUtil::CreateTabValue(tab_contents)); |
386 std::string json_args; | 386 std::string json_args; |
387 JSONWriter::Write(&args, false, &json_args); | 387 base::JSONWriter::Write(&args, false, &json_args); |
388 | 388 |
389 std::string event_name = std::string("browserAction/") + extension_id; | 389 std::string event_name = std::string("browserAction/") + extension_id; |
390 DispatchEvent(profile, event_name.c_str(), json_args); | 390 DispatchEvent(profile, event_name.c_str(), json_args); |
391 } | 391 } |
OLD | NEW |