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" |
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/profile.h" | 13 #include "chrome/browser/profile.h" |
14 #include "chrome/browser/tab_contents/navigation_entry.h" | 14 #include "chrome/browser/tab_contents/navigation_entry.h" |
15 #include "chrome/browser/tab_contents/tab_contents.h" | 15 #include "chrome/browser/tab_contents/tab_contents.h" |
| 16 #include "chrome/browser/tab_contents_wrapper.h" |
| 17 #include "chrome/browser/tab_contents_wrapper.h" |
16 #include "chrome/browser/tabs/tab_strip_model.h" | 18 #include "chrome/browser/tabs/tab_strip_model.h" |
17 #include "chrome/browser/ui/browser.h" | 19 #include "chrome/browser/ui/browser.h" |
18 #include "chrome/common/extensions/extension_constants.h" | 20 #include "chrome/common/extensions/extension_constants.h" |
19 #include "chrome/common/extensions/extension.h" | 21 #include "chrome/common/extensions/extension.h" |
20 #include "chrome/common/notification_service.h" | 22 #include "chrome/common/notification_service.h" |
21 | 23 |
22 namespace events = extension_event_names; | 24 namespace events = extension_event_names; |
23 namespace tab_keys = extension_tabs_module_constants; | 25 namespace tab_keys = extension_tabs_module_constants; |
24 namespace page_action_keys = extension_page_actions_module_constants; | 26 namespace page_action_keys = extension_page_actions_module_constants; |
25 | 27 |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 // Init() can happen after the browser is running, so catch up with any | 132 // Init() can happen after the browser is running, so catch up with any |
131 // windows that already exist. | 133 // windows that already exist. |
132 for (BrowserList::const_iterator iter = BrowserList::begin(); | 134 for (BrowserList::const_iterator iter = BrowserList::begin(); |
133 iter != BrowserList::end(); ++iter) { | 135 iter != BrowserList::end(); ++iter) { |
134 RegisterForBrowserNotifications(*iter); | 136 RegisterForBrowserNotifications(*iter); |
135 | 137 |
136 // Also catch up our internal bookkeeping of tab entries. | 138 // Also catch up our internal bookkeeping of tab entries. |
137 Browser* browser = *iter; | 139 Browser* browser = *iter; |
138 if (browser->tabstrip_model()) { | 140 if (browser->tabstrip_model()) { |
139 for (int i = 0; i < browser->tabstrip_model()->count(); ++i) { | 141 for (int i = 0; i < browser->tabstrip_model()->count(); ++i) { |
140 TabContents* contents = browser->tabstrip_model()->GetTabContentsAt(i); | 142 TabContents* contents = browser->GetTabContentsAt(i); |
141 int tab_id = ExtensionTabUtil::GetTabId(contents); | 143 int tab_id = ExtensionTabUtil::GetTabId(contents); |
142 tab_entries_[tab_id] = TabEntry(); | 144 tab_entries_[tab_id] = TabEntry(); |
143 } | 145 } |
144 } | 146 } |
145 } | 147 } |
146 | 148 |
147 initialized_ = true; | 149 initialized_ = true; |
148 } | 150 } |
149 | 151 |
150 ExtensionBrowserEventRouter::ExtensionBrowserEventRouter() | 152 ExtensionBrowserEventRouter::ExtensionBrowserEventRouter() |
(...skipping 13 matching lines...) Expand all Loading... |
164 browser->tabstrip_model()->AddObserver(this); | 166 browser->tabstrip_model()->AddObserver(this); |
165 | 167 |
166 // If this is a new window, it isn't ready at this point, so we register to be | 168 // If this is a new window, it isn't ready at this point, so we register to be |
167 // notified when it is. If this is an existing window, this is a no-op that we | 169 // notified when it is. If this is an existing window, this is a no-op that we |
168 // just do to reduce code complexity. | 170 // just do to reduce code complexity. |
169 registrar_.Add(this, NotificationType::BROWSER_WINDOW_READY, | 171 registrar_.Add(this, NotificationType::BROWSER_WINDOW_READY, |
170 Source<const Browser>(browser)); | 172 Source<const Browser>(browser)); |
171 | 173 |
172 if (browser->tabstrip_model()) { | 174 if (browser->tabstrip_model()) { |
173 for (int i = 0; i < browser->tabstrip_model()->count(); ++i) | 175 for (int i = 0; i < browser->tabstrip_model()->count(); ++i) |
174 RegisterForTabNotifications( | 176 RegisterForTabNotifications(browser->GetTabContentsAt(i)); |
175 browser->tabstrip_model()->GetTabContentsAt(i)); | |
176 } | 177 } |
177 } | 178 } |
178 | 179 |
179 void ExtensionBrowserEventRouter::RegisterForTabNotifications( | 180 void ExtensionBrowserEventRouter::RegisterForTabNotifications( |
180 TabContents* contents) { | 181 TabContents* contents) { |
181 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, | 182 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, |
182 Source<NavigationController>(&contents->controller())); | 183 Source<NavigationController>(&contents->controller())); |
183 | 184 |
184 // Observing TAB_CONTENTS_DESTROYED is necessary because it's | 185 // Observing TAB_CONTENTS_DESTROYED is necessary because it's |
185 // possible for tabs to be created, detached and then destroyed without | 186 // possible for tabs to be created, detached and then destroyed without |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 | 259 |
259 void ExtensionBrowserEventRouter::TabCreatedAt(TabContents* contents, | 260 void ExtensionBrowserEventRouter::TabCreatedAt(TabContents* contents, |
260 int index, | 261 int index, |
261 bool foreground) { | 262 bool foreground) { |
262 DispatchEventWithTab(contents->profile(), "", events::kOnTabCreated, | 263 DispatchEventWithTab(contents->profile(), "", events::kOnTabCreated, |
263 contents); | 264 contents); |
264 | 265 |
265 RegisterForTabNotifications(contents); | 266 RegisterForTabNotifications(contents); |
266 } | 267 } |
267 | 268 |
268 void ExtensionBrowserEventRouter::TabInsertedAt(TabContents* contents, | 269 void ExtensionBrowserEventRouter::TabInsertedAt(TabContentsWrapper* contents, |
269 int index, | 270 int index, |
270 bool foreground) { | 271 bool foreground) { |
271 // If tab is new, send created event. | 272 // If tab is new, send created event. |
272 int tab_id = ExtensionTabUtil::GetTabId(contents); | 273 int tab_id = ExtensionTabUtil::GetTabId(contents->tab_contents()); |
273 if (!GetTabEntry(contents)) { | 274 if (!GetTabEntry(contents->tab_contents())) { |
274 tab_entries_[tab_id] = TabEntry(); | 275 tab_entries_[tab_id] = TabEntry(); |
275 | 276 |
276 TabCreatedAt(contents, index, foreground); | 277 TabCreatedAt(contents->tab_contents(), index, foreground); |
277 return; | 278 return; |
278 } | 279 } |
279 | 280 |
280 ListValue args; | 281 ListValue args; |
281 args.Append(Value::CreateIntegerValue(tab_id)); | 282 args.Append(Value::CreateIntegerValue(tab_id)); |
282 | 283 |
283 DictionaryValue* object_args = new DictionaryValue(); | 284 DictionaryValue* object_args = new DictionaryValue(); |
284 object_args->Set(tab_keys::kNewWindowIdKey, Value::CreateIntegerValue( | 285 object_args->Set(tab_keys::kNewWindowIdKey, Value::CreateIntegerValue( |
285 ExtensionTabUtil::GetWindowIdOfTab(contents))); | 286 ExtensionTabUtil::GetWindowIdOfTab(contents->tab_contents()))); |
286 object_args->Set(tab_keys::kNewPositionKey, Value::CreateIntegerValue( | 287 object_args->Set(tab_keys::kNewPositionKey, Value::CreateIntegerValue( |
287 index)); | 288 index)); |
288 args.Append(object_args); | 289 args.Append(object_args); |
289 | 290 |
290 std::string json_args; | 291 std::string json_args; |
291 base::JSONWriter::Write(&args, false, &json_args); | 292 base::JSONWriter::Write(&args, false, &json_args); |
292 | 293 |
293 DispatchEvent(contents->profile(), events::kOnTabAttached, json_args); | 294 DispatchEvent(contents->profile(), events::kOnTabAttached, json_args); |
294 } | 295 } |
295 | 296 |
296 void ExtensionBrowserEventRouter::TabDetachedAt(TabContents* contents, | 297 void ExtensionBrowserEventRouter::TabDetachedAt(TabContentsWrapper* contents, |
297 int index) { | 298 int index) { |
298 if (!GetTabEntry(contents)) { | 299 if (!GetTabEntry(contents->tab_contents())) { |
299 // The tab was removed. Don't send detach event. | 300 // The tab was removed. Don't send detach event. |
300 return; | 301 return; |
301 } | 302 } |
302 | 303 |
303 ListValue args; | 304 ListValue args; |
304 args.Append(Value::CreateIntegerValue(ExtensionTabUtil::GetTabId(contents))); | 305 args.Append(Value::CreateIntegerValue( |
| 306 ExtensionTabUtil::GetTabId(contents->tab_contents()))); |
305 | 307 |
306 DictionaryValue* object_args = new DictionaryValue(); | 308 DictionaryValue* object_args = new DictionaryValue(); |
307 object_args->Set(tab_keys::kOldWindowIdKey, Value::CreateIntegerValue( | 309 object_args->Set(tab_keys::kOldWindowIdKey, Value::CreateIntegerValue( |
308 ExtensionTabUtil::GetWindowIdOfTab(contents))); | 310 ExtensionTabUtil::GetWindowIdOfTab(contents->tab_contents()))); |
309 object_args->Set(tab_keys::kOldPositionKey, Value::CreateIntegerValue( | 311 object_args->Set(tab_keys::kOldPositionKey, Value::CreateIntegerValue( |
310 index)); | 312 index)); |
311 args.Append(object_args); | 313 args.Append(object_args); |
312 | 314 |
313 std::string json_args; | 315 std::string json_args; |
314 base::JSONWriter::Write(&args, false, &json_args); | 316 base::JSONWriter::Write(&args, false, &json_args); |
315 | 317 |
316 DispatchEvent(contents->profile(), events::kOnTabDetached, json_args); | 318 DispatchEvent(contents->profile(), events::kOnTabDetached, json_args); |
317 } | 319 } |
318 | 320 |
319 void ExtensionBrowserEventRouter::TabClosingAt(TabStripModel* tab_strip_model, | 321 void ExtensionBrowserEventRouter::TabClosingAt(TabStripModel* tab_strip_model, |
320 TabContents* contents, | 322 TabContentsWrapper* contents, |
321 int index) { | 323 int index) { |
322 int tab_id = ExtensionTabUtil::GetTabId(contents); | 324 int tab_id = ExtensionTabUtil::GetTabId(contents->tab_contents()); |
323 | 325 |
324 ListValue args; | 326 ListValue args; |
325 args.Append(Value::CreateIntegerValue(tab_id)); | 327 args.Append(Value::CreateIntegerValue(tab_id)); |
326 | 328 |
327 DictionaryValue* object_args = new DictionaryValue(); | 329 DictionaryValue* object_args = new DictionaryValue(); |
328 object_args->SetBoolean(tab_keys::kWindowClosing, | 330 object_args->SetBoolean(tab_keys::kWindowClosing, |
329 tab_strip_model->closing_all()); | 331 tab_strip_model->closing_all()); |
330 args.Append(object_args); | 332 args.Append(object_args); |
331 | 333 |
332 std::string json_args; | 334 std::string json_args; |
333 base::JSONWriter::Write(&args, false, &json_args); | 335 base::JSONWriter::Write(&args, false, &json_args); |
334 | 336 |
335 DispatchEvent(contents->profile(), events::kOnTabRemoved, json_args); | 337 DispatchEvent(contents->profile(), events::kOnTabRemoved, json_args); |
336 | 338 |
337 int removed_count = tab_entries_.erase(tab_id); | 339 int removed_count = tab_entries_.erase(tab_id); |
338 DCHECK_GT(removed_count, 0); | 340 DCHECK_GT(removed_count, 0); |
339 | 341 |
340 UnregisterForTabNotifications(contents); | 342 UnregisterForTabNotifications(contents->tab_contents()); |
341 } | 343 } |
342 | 344 |
343 void ExtensionBrowserEventRouter::TabSelectedAt(TabContents* old_contents, | 345 void ExtensionBrowserEventRouter::TabSelectedAt( |
344 TabContents* new_contents, | 346 TabContentsWrapper* old_contents, |
345 int index, | 347 TabContentsWrapper* new_contents, |
346 bool user_gesture) { | 348 int index, |
| 349 bool user_gesture) { |
347 ListValue args; | 350 ListValue args; |
348 args.Append(Value::CreateIntegerValue( | 351 args.Append(Value::CreateIntegerValue( |
349 ExtensionTabUtil::GetTabId(new_contents))); | 352 ExtensionTabUtil::GetTabId(new_contents->tab_contents()))); |
350 | 353 |
351 DictionaryValue* object_args = new DictionaryValue(); | 354 DictionaryValue* object_args = new DictionaryValue(); |
352 object_args->Set(tab_keys::kWindowIdKey, Value::CreateIntegerValue( | 355 object_args->Set(tab_keys::kWindowIdKey, Value::CreateIntegerValue( |
353 ExtensionTabUtil::GetWindowIdOfTab(new_contents))); | 356 ExtensionTabUtil::GetWindowIdOfTab(new_contents->tab_contents()))); |
354 args.Append(object_args); | 357 args.Append(object_args); |
355 | 358 |
356 std::string json_args; | 359 std::string json_args; |
357 base::JSONWriter::Write(&args, false, &json_args); | 360 base::JSONWriter::Write(&args, false, &json_args); |
358 | 361 |
359 DispatchEvent(new_contents->profile(), events::kOnTabSelectionChanged, | 362 DispatchEvent(new_contents->profile(), events::kOnTabSelectionChanged, |
360 json_args); | 363 json_args); |
361 } | 364 } |
362 | 365 |
363 void ExtensionBrowserEventRouter::TabMoved(TabContents* contents, | 366 void ExtensionBrowserEventRouter::TabMoved(TabContentsWrapper* contents, |
364 int from_index, | 367 int from_index, |
365 int to_index) { | 368 int to_index) { |
366 ListValue args; | 369 ListValue args; |
367 args.Append(Value::CreateIntegerValue(ExtensionTabUtil::GetTabId(contents))); | 370 args.Append(Value::CreateIntegerValue( |
| 371 ExtensionTabUtil::GetTabId(contents->tab_contents()))); |
368 | 372 |
369 DictionaryValue* object_args = new DictionaryValue(); | 373 DictionaryValue* object_args = new DictionaryValue(); |
370 object_args->Set(tab_keys::kWindowIdKey, Value::CreateIntegerValue( | 374 object_args->Set(tab_keys::kWindowIdKey, Value::CreateIntegerValue( |
371 ExtensionTabUtil::GetWindowIdOfTab(contents))); | 375 ExtensionTabUtil::GetWindowIdOfTab(contents->tab_contents()))); |
372 object_args->Set(tab_keys::kFromIndexKey, Value::CreateIntegerValue( | 376 object_args->Set(tab_keys::kFromIndexKey, Value::CreateIntegerValue( |
373 from_index)); | 377 from_index)); |
374 object_args->Set(tab_keys::kToIndexKey, Value::CreateIntegerValue( | 378 object_args->Set(tab_keys::kToIndexKey, Value::CreateIntegerValue( |
375 to_index)); | 379 to_index)); |
376 args.Append(object_args); | 380 args.Append(object_args); |
377 | 381 |
378 std::string json_args; | 382 std::string json_args; |
379 base::JSONWriter::Write(&args, false, &json_args); | 383 base::JSONWriter::Write(&args, false, &json_args); |
380 | 384 |
381 DispatchEvent(contents->profile(), events::kOnTabMoved, json_args); | 385 DispatchEvent(contents->profile(), events::kOnTabMoved, json_args); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
449 OnBrowserWindowReady(browser); | 453 OnBrowserWindowReady(browser); |
450 #if defined(OS_MACOSX) | 454 #if defined(OS_MACOSX) |
451 } else if (type == NotificationType::NO_KEY_WINDOW) { | 455 } else if (type == NotificationType::NO_KEY_WINDOW) { |
452 OnBrowserSetLastActive(NULL); | 456 OnBrowserSetLastActive(NULL); |
453 #endif | 457 #endif |
454 } else { | 458 } else { |
455 NOTREACHED(); | 459 NOTREACHED(); |
456 } | 460 } |
457 } | 461 } |
458 | 462 |
459 void ExtensionBrowserEventRouter::TabChangedAt(TabContents* contents, | 463 void ExtensionBrowserEventRouter::TabChangedAt(TabContentsWrapper* contents, |
460 int index, | 464 int index, |
461 TabChangeType change_type) { | 465 TabChangeType change_type) { |
462 TabUpdated(contents, false); | 466 TabUpdated(contents->tab_contents(), false); |
463 } | 467 } |
464 | 468 |
465 void ExtensionBrowserEventRouter::TabReplacedAt(TabContents* old_contents, | 469 void ExtensionBrowserEventRouter::TabReplacedAt( |
466 TabContents* new_contents, | 470 TabContentsWrapper* old_contents, |
467 int index) { | 471 TabContentsWrapper* new_contents, |
468 UnregisterForTabNotifications(old_contents); | 472 int index) { |
469 RegisterForTabNotifications(new_contents); | 473 UnregisterForTabNotifications(old_contents->tab_contents()); |
| 474 RegisterForTabNotifications(new_contents->tab_contents()); |
470 } | 475 } |
471 | 476 |
472 void ExtensionBrowserEventRouter::TabPinnedStateChanged(TabContents* contents, | 477 void ExtensionBrowserEventRouter::TabPinnedStateChanged( |
473 int index) { | 478 TabContentsWrapper* contents, |
| 479 int index) { |
474 TabStripModel* tab_strip = NULL; | 480 TabStripModel* tab_strip = NULL; |
475 int tab_index; | 481 int tab_index; |
476 | 482 |
477 if (ExtensionTabUtil::GetTabStripModel(contents, &tab_strip, &tab_index)) { | 483 if (ExtensionTabUtil::GetTabStripModel( |
| 484 contents->tab_contents(), &tab_strip, &tab_index)) { |
478 DictionaryValue* changed_properties = new DictionaryValue(); | 485 DictionaryValue* changed_properties = new DictionaryValue(); |
479 changed_properties->SetBoolean(tab_keys::kPinnedKey, | 486 changed_properties->SetBoolean(tab_keys::kPinnedKey, |
480 tab_strip->IsTabPinned(tab_index)); | 487 tab_strip->IsTabPinned(tab_index)); |
481 DispatchTabUpdatedEvent(contents, changed_properties); | 488 DispatchTabUpdatedEvent(contents->tab_contents(), changed_properties); |
482 } | 489 } |
483 } | 490 } |
484 | 491 |
485 void ExtensionBrowserEventRouter::TabStripEmpty() {} | 492 void ExtensionBrowserEventRouter::TabStripEmpty() {} |
486 | 493 |
487 void ExtensionBrowserEventRouter::DispatchOldPageActionEvent( | 494 void ExtensionBrowserEventRouter::DispatchOldPageActionEvent( |
488 Profile* profile, | 495 Profile* profile, |
489 const std::string& extension_id, | 496 const std::string& extension_id, |
490 const std::string& page_action_id, | 497 const std::string& page_action_id, |
491 int tab_id, | 498 int tab_id, |
(...skipping 16 matching lines...) Expand all Loading... |
508 | 515 |
509 void ExtensionBrowserEventRouter::PageActionExecuted( | 516 void ExtensionBrowserEventRouter::PageActionExecuted( |
510 Profile* profile, | 517 Profile* profile, |
511 const std::string& extension_id, | 518 const std::string& extension_id, |
512 const std::string& page_action_id, | 519 const std::string& page_action_id, |
513 int tab_id, | 520 int tab_id, |
514 const std::string& url, | 521 const std::string& url, |
515 int button) { | 522 int button) { |
516 DispatchOldPageActionEvent(profile, extension_id, page_action_id, tab_id, url, | 523 DispatchOldPageActionEvent(profile, extension_id, page_action_id, tab_id, url, |
517 button); | 524 button); |
518 TabContents* tab_contents = NULL; | 525 TabContentsWrapper* tab_contents = NULL; |
519 if (!ExtensionTabUtil::GetTabById(tab_id, profile, profile->IsOffTheRecord(), | 526 if (!ExtensionTabUtil::GetTabById(tab_id, profile, profile->IsOffTheRecord(), |
520 NULL, NULL, &tab_contents, NULL)) { | 527 NULL, NULL, &tab_contents, NULL)) { |
521 return; | 528 return; |
522 } | 529 } |
523 DispatchEventWithTab(profile, extension_id, "pageAction.onClicked", | 530 DispatchEventWithTab(profile, extension_id, "pageAction.onClicked", |
524 tab_contents); | 531 tab_contents->tab_contents()); |
525 } | 532 } |
526 | 533 |
527 void ExtensionBrowserEventRouter::BrowserActionExecuted( | 534 void ExtensionBrowserEventRouter::BrowserActionExecuted( |
528 Profile* profile, const std::string& extension_id, Browser* browser) { | 535 Profile* profile, const std::string& extension_id, Browser* browser) { |
529 TabContents* tab_contents = NULL; | 536 TabContentsWrapper* tab_contents = NULL; |
530 int tab_id = 0; | 537 int tab_id = 0; |
531 if (!ExtensionTabUtil::GetDefaultTab(browser, &tab_contents, &tab_id)) | 538 if (!ExtensionTabUtil::GetDefaultTab(browser, &tab_contents, &tab_id)) |
532 return; | 539 return; |
533 DispatchEventWithTab(profile, extension_id, "browserAction.onClicked", | 540 DispatchEventWithTab(profile, extension_id, "browserAction.onClicked", |
534 tab_contents); | 541 tab_contents->tab_contents()); |
535 } | 542 } |
OLD | NEW |