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/browser_event_router.h" | 5 #include "chrome/browser/extensions/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/api/tabs/tabs_constants.h" | 10 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" |
11 #include "chrome/browser/extensions/api/tabs/tabs_windows_api.h" | 11 #include "chrome/browser/extensions/api/tabs/tabs_windows_api.h" |
12 #include "chrome/browser/extensions/api/tabs/windows_event_router.h" | 12 #include "chrome/browser/extensions/api/tabs/windows_event_router.h" |
13 #include "chrome/browser/extensions/event_names.h" | 13 #include "chrome/browser/extensions/event_names.h" |
14 #include "chrome/browser/extensions/extension_action.h" | 14 #include "chrome/browser/extensions/extension_action.h" |
15 #include "chrome/browser/extensions/extension_service.h" | 15 #include "chrome/browser/extensions/extension_service.h" |
16 #include "chrome/browser/extensions/extension_system.h" | 16 #include "chrome/browser/extensions/extension_system.h" |
17 #include "chrome/browser/extensions/extension_tab_util.h" | 17 #include "chrome/browser/extensions/extension_tab_util.h" |
18 #include "chrome/browser/extensions/window_controller.h" | 18 #include "chrome/browser/extensions/window_controller.h" |
19 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
20 #include "chrome/browser/ui/browser.h" | 20 #include "chrome/browser/ui/browser.h" |
21 #include "chrome/browser/ui/browser_iterator.h" | 21 #include "chrome/browser/ui/browser_iterator.h" |
22 #include "chrome/browser/ui/browser_list.h" | 22 #include "chrome/browser/ui/browser_list.h" |
23 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 23 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
24 #include "chrome/common/chrome_notification_types.h" | |
24 #include "chrome/common/extensions/api/extension_action/action_info.h" | 25 #include "chrome/common/extensions/api/extension_action/action_info.h" |
25 #include "chrome/common/extensions/extension_constants.h" | 26 #include "chrome/common/extensions/extension_constants.h" |
27 #include "content/public/browser/favicon_status.h" | |
26 #include "content/public/browser/navigation_controller.h" | 28 #include "content/public/browser/navigation_controller.h" |
29 #include "content/public/browser/navigation_entry.h" | |
27 #include "content/public/browser/notification_service.h" | 30 #include "content/public/browser/notification_service.h" |
28 #include "content/public/browser/notification_types.h" | 31 #include "content/public/browser/notification_types.h" |
29 #include "content/public/browser/web_contents.h" | 32 #include "content/public/browser/web_contents.h" |
30 | 33 |
31 namespace events = extensions::event_names; | 34 namespace events = extensions::event_names; |
32 namespace tab_keys = extensions::tabs_constants; | 35 namespace tab_keys = extensions::tabs_constants; |
33 namespace page_actions_keys = extension_page_actions_api_constants; | 36 namespace page_actions_keys = extension_page_actions_api_constants; |
34 | 37 |
35 using content::NavigationController; | 38 using content::NavigationController; |
36 using content::WebContents; | 39 using content::WebContents; |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
121 registrar_.Add( | 124 registrar_.Add( |
122 this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 125 this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
123 content::Source<NavigationController>(&contents->GetController())); | 126 content::Source<NavigationController>(&contents->GetController())); |
124 | 127 |
125 // Observing NOTIFICATION_WEB_CONTENTS_DESTROYED is necessary because it's | 128 // Observing NOTIFICATION_WEB_CONTENTS_DESTROYED is necessary because it's |
126 // possible for tabs to be created, detached and then destroyed without | 129 // possible for tabs to be created, detached and then destroyed without |
127 // ever having been re-attached and closed. This happens in the case of | 130 // ever having been re-attached and closed. This happens in the case of |
128 // a devtools WebContents that is opened in window, docked, then closed. | 131 // a devtools WebContents that is opened in window, docked, then closed. |
129 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | 132 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
130 content::Source<WebContents>(contents)); | 133 content::Source<WebContents>(contents)); |
134 | |
135 registrar_.Add(this, chrome::NOTIFICATION_FAVICON_UPDATED, | |
136 content::Source<WebContents>(contents)); | |
131 } | 137 } |
132 | 138 |
133 void BrowserEventRouter::UnregisterForTabNotifications(WebContents* contents) { | 139 void BrowserEventRouter::UnregisterForTabNotifications(WebContents* contents) { |
134 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 140 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
135 content::Source<NavigationController>(&contents->GetController())); | 141 content::Source<NavigationController>(&contents->GetController())); |
136 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | 142 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
137 content::Source<WebContents>(contents)); | 143 content::Source<WebContents>(contents)); |
144 registrar_.Remove(this, chrome::NOTIFICATION_FAVICON_UPDATED, | |
145 content::Source<WebContents>(contents)); | |
138 } | 146 } |
139 | 147 |
140 void BrowserEventRouter::OnBrowserRemoved(Browser* browser) { | 148 void BrowserEventRouter::OnBrowserRemoved(Browser* browser) { |
141 if (!profile_->IsSameProfile(browser->profile())) | 149 if (!profile_->IsSameProfile(browser->profile())) |
142 return; | 150 return; |
143 | 151 |
144 // Stop listening to TabStripModel events for this browser. | 152 // Stop listening to TabStripModel events for this browser. |
145 browser->tab_strip_model()->RemoveObserver(this); | 153 browser->tab_strip_model()->RemoveObserver(this); |
146 } | 154 } |
147 | 155 |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
345 | 353 |
346 if (did_navigate) | 354 if (did_navigate) |
347 changed_properties.reset(entry->DidNavigate(contents)); | 355 changed_properties.reset(entry->DidNavigate(contents)); |
348 else | 356 else |
349 changed_properties.reset(entry->UpdateLoadState(contents)); | 357 changed_properties.reset(entry->UpdateLoadState(contents)); |
350 | 358 |
351 if (changed_properties) | 359 if (changed_properties) |
352 DispatchTabUpdatedEvent(contents, changed_properties.Pass()); | 360 DispatchTabUpdatedEvent(contents, changed_properties.Pass()); |
353 } | 361 } |
354 | 362 |
363 void BrowserEventRouter::FaviconUrlUpdated( | |
364 WebContents* contents, | |
365 const bool* icon_url_changed) { | |
asargent_no_longer_on_chrome
2013/03/13 20:28:30
style nit: If you can make it fit under 80 chars,
Timo Reimann
2013/03/13 21:36:26
Done.
| |
366 if (!icon_url_changed || !*icon_url_changed) | |
367 return; | |
368 content::NavigationEntry* entry = | |
369 contents->GetController().GetActiveEntry(); | |
370 if (!entry || !entry->GetFavicon().valid) | |
371 return; | |
372 scoped_ptr<DictionaryValue> changed_properties(new DictionaryValue()); | |
373 changed_properties->SetString(tab_keys::kFaviconUrlKey, | |
374 entry->GetFavicon().url.spec()); | |
asargent_no_longer_on_chrome
2013/03/13 20:28:30
Is it possible that the favicon url could be inval
Timo Reimann
2013/03/13 21:36:26
I'm not perfectly sure on this one. Peter probably
| |
375 DispatchTabUpdatedEvent(contents, changed_properties.Pass()); | |
376 } | |
377 | |
355 void BrowserEventRouter::DispatchEvent( | 378 void BrowserEventRouter::DispatchEvent( |
356 Profile* profile, | 379 Profile* profile, |
357 const char* event_name, | 380 const char* event_name, |
358 scoped_ptr<ListValue> args, | 381 scoped_ptr<ListValue> args, |
359 EventRouter::UserGestureState user_gesture) { | 382 EventRouter::UserGestureState user_gesture) { |
360 if (!profile_->IsSameProfile(profile) || | 383 if (!profile_->IsSameProfile(profile) || |
361 !extensions::ExtensionSystem::Get(profile)->event_router()) | 384 !extensions::ExtensionSystem::Get(profile)->event_router()) |
362 return; | 385 return; |
363 | 386 |
364 scoped_ptr<Event> event(new Event(event_name, args.Pass())); | 387 scoped_ptr<Event> event(new Event(event_name, args.Pass())); |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
460 NavigationController* source_controller = | 483 NavigationController* source_controller = |
461 content::Source<NavigationController>(source).ptr(); | 484 content::Source<NavigationController>(source).ptr(); |
462 TabUpdated(source_controller->GetWebContents(), true); | 485 TabUpdated(source_controller->GetWebContents(), true); |
463 } else if (type == content::NOTIFICATION_WEB_CONTENTS_DESTROYED) { | 486 } else if (type == content::NOTIFICATION_WEB_CONTENTS_DESTROYED) { |
464 // Tab was destroyed after being detached (without being re-attached). | 487 // Tab was destroyed after being detached (without being re-attached). |
465 WebContents* contents = content::Source<WebContents>(source).ptr(); | 488 WebContents* contents = content::Source<WebContents>(source).ptr(); |
466 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 489 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
467 content::Source<NavigationController>(&contents->GetController())); | 490 content::Source<NavigationController>(&contents->GetController())); |
468 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | 491 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
469 content::Source<WebContents>(contents)); | 492 content::Source<WebContents>(contents)); |
493 registrar_.Remove(this, chrome::NOTIFICATION_FAVICON_UPDATED, | |
494 content::Source<WebContents>(contents)); | |
495 } else if (type == chrome::NOTIFICATION_FAVICON_UPDATED) { | |
496 WebContents* contents = content::Source<WebContents>(source).ptr(); | |
497 const bool* icon_url_changed = content::Details<bool>(details).ptr(); | |
498 FaviconUrlUpdated(contents, icon_url_changed); | |
470 } else { | 499 } else { |
471 NOTREACHED(); | 500 NOTREACHED(); |
472 } | 501 } |
473 } | 502 } |
474 | 503 |
475 void BrowserEventRouter::TabChangedAt(WebContents* contents, | 504 void BrowserEventRouter::TabChangedAt(WebContents* contents, |
476 int index, | 505 int index, |
477 TabChangeType change_type) { | 506 TabChangeType change_type) { |
478 TabUpdated(contents, false); | 507 TabUpdated(contents, false); |
479 } | 508 } |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
608 | 637 |
609 DispatchEventToExtension(profile, | 638 DispatchEventToExtension(profile, |
610 extension_action.extension_id(), | 639 extension_action.extension_id(), |
611 event_name, | 640 event_name, |
612 args.Pass(), | 641 args.Pass(), |
613 EventRouter::USER_GESTURE_ENABLED); | 642 EventRouter::USER_GESTURE_ENABLED); |
614 } | 643 } |
615 } | 644 } |
616 | 645 |
617 } // namespace extensions | 646 } // namespace extensions |
OLD | NEW |