OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/api/tabs/tabs_event_router.h" | 5 #include "chrome/browser/extensions/api/tabs/tabs_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/chrome_notification_types.h" | 9 #include "chrome/browser/chrome_notification_types.h" |
10 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" | 10 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
142 | 142 |
143 // Observing NOTIFICATION_WEB_CONTENTS_DESTROYED is necessary because it's | 143 // Observing NOTIFICATION_WEB_CONTENTS_DESTROYED is necessary because it's |
144 // possible for tabs to be created, detached and then destroyed without | 144 // possible for tabs to be created, detached and then destroyed without |
145 // ever having been re-attached and closed. This happens in the case of | 145 // ever having been re-attached and closed. This happens in the case of |
146 // a devtools WebContents that is opened in window, docked, then closed. | 146 // a devtools WebContents that is opened in window, docked, then closed. |
147 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | 147 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
148 content::Source<WebContents>(contents)); | 148 content::Source<WebContents>(contents)); |
149 | 149 |
150 registrar_.Add(this, chrome::NOTIFICATION_FAVICON_UPDATED, | 150 registrar_.Add(this, chrome::NOTIFICATION_FAVICON_UPDATED, |
151 content::Source<WebContents>(contents)); | 151 content::Source<WebContents>(contents)); |
152 | |
153 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_ZOOM_CHANGE, | |
154 content::Source<WebContents>(contents)); | |
152 } | 155 } |
153 | 156 |
154 void TabsEventRouter::UnregisterForTabNotifications(WebContents* contents) { | 157 void TabsEventRouter::UnregisterForTabNotifications(WebContents* contents) { |
155 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 158 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
156 content::Source<NavigationController>(&contents->GetController())); | 159 content::Source<NavigationController>(&contents->GetController())); |
157 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | 160 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
158 content::Source<WebContents>(contents)); | 161 content::Source<WebContents>(contents)); |
159 registrar_.Remove(this, chrome::NOTIFICATION_FAVICON_UPDATED, | 162 registrar_.Remove(this, chrome::NOTIFICATION_FAVICON_UPDATED, |
160 content::Source<WebContents>(contents)); | 163 content::Source<WebContents>(contents)); |
164 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_ZOOM_CHANGE, | |
165 content::Source<WebContents>(contents)); | |
161 } | 166 } |
162 | 167 |
163 void TabsEventRouter::OnBrowserRemoved(Browser* browser) { | 168 void TabsEventRouter::OnBrowserRemoved(Browser* browser) { |
164 if (!profile_->IsSameProfile(browser->profile())) | 169 if (!profile_->IsSameProfile(browser->profile())) |
165 return; | 170 return; |
166 | 171 |
167 // Stop listening to TabStripModel events for this browser. | 172 // Stop listening to TabStripModel events for this browser. |
168 browser->tab_strip_model()->RemoveObserver(this); | 173 browser->tab_strip_model()->RemoveObserver(this); |
169 } | 174 } |
170 | 175 |
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
466 new Event(tabs::OnUpdated::kEventName, args_base.Pass())); | 471 new Event(tabs::OnUpdated::kEventName, args_base.Pass())); |
467 event->restrict_to_browser_context = profile; | 472 event->restrict_to_browser_context = profile; |
468 event->user_gesture = EventRouter::USER_GESTURE_NOT_ENABLED; | 473 event->user_gesture = EventRouter::USER_GESTURE_NOT_ENABLED; |
469 event->will_dispatch_callback = | 474 event->will_dispatch_callback = |
470 base::Bind(&WillDispatchTabUpdatedEvent, | 475 base::Bind(&WillDispatchTabUpdatedEvent, |
471 contents, | 476 contents, |
472 changed_properties.get()); | 477 changed_properties.get()); |
473 ExtensionSystem::Get(profile)->event_router()->BroadcastEvent(event.Pass()); | 478 ExtensionSystem::Get(profile)->event_router()->BroadcastEvent(event.Pass()); |
474 } | 479 } |
475 | 480 |
481 void TabsEventRouter::DispatchTabZoomChangeEvent( | |
482 WebContents* contents, | |
483 double old_zoom_factor, | |
484 double new_zoom_factor, | |
485 base::DictionaryValue* zoom_settings) { | |
486 DCHECK(contents); | |
487 scoped_ptr<base::ListValue> args(new base::ListValue); | |
488 int tab_id = ExtensionTabUtil::GetTabId(contents); | |
489 if (tab_id >= 0) { | |
Fady Samuel
2014/04/07 20:01:14
Early exist preferred.
if (tab_id < 0)
return;
paulmeyer
2014/04/07 20:56:10
Done.
| |
490 args->AppendInteger(tab_id); | |
491 args->AppendDouble(old_zoom_factor); | |
492 args->AppendDouble(new_zoom_factor); | |
493 args->Append(zoom_settings); | |
494 | |
495 Profile* profile = Profile::FromBrowserContext( | |
496 contents->GetBrowserContext()); | |
497 DispatchEvent(profile, tabs::OnZoomChange::kEventName, args.Pass(), | |
498 EventRouter::USER_GESTURE_UNKNOWN); | |
499 } | |
500 } | |
501 | |
476 TabsEventRouter::TabEntry* TabsEventRouter::GetTabEntry( | 502 TabsEventRouter::TabEntry* TabsEventRouter::GetTabEntry( |
477 const WebContents* contents) { | 503 const WebContents* contents) { |
478 int tab_id = ExtensionTabUtil::GetTabId(contents); | 504 int tab_id = ExtensionTabUtil::GetTabId(contents); |
479 std::map<int, TabEntry>::iterator i = tab_entries_.find(tab_id); | 505 std::map<int, TabEntry>::iterator i = tab_entries_.find(tab_id); |
480 if (tab_entries_.end() == i) | 506 if (tab_entries_.end() == i) |
481 return NULL; | 507 return NULL; |
482 return &i->second; | 508 return &i->second; |
483 } | 509 } |
484 | 510 |
485 void TabsEventRouter::Observe(int type, | 511 void TabsEventRouter::Observe(int type, |
486 const content::NotificationSource& source, | 512 const content::NotificationSource& source, |
487 const content::NotificationDetails& details) { | 513 const content::NotificationDetails& details) { |
488 if (type == content::NOTIFICATION_NAV_ENTRY_COMMITTED) { | 514 if (type == content::NOTIFICATION_NAV_ENTRY_COMMITTED) { |
489 NavigationController* source_controller = | 515 NavigationController* source_controller = |
490 content::Source<NavigationController>(source).ptr(); | 516 content::Source<NavigationController>(source).ptr(); |
491 TabUpdated(source_controller->GetWebContents(), true); | 517 TabUpdated(source_controller->GetWebContents(), true); |
492 } else if (type == content::NOTIFICATION_WEB_CONTENTS_DESTROYED) { | 518 } else if (type == content::NOTIFICATION_WEB_CONTENTS_DESTROYED) { |
493 // Tab was destroyed after being detached (without being re-attached). | 519 // Tab was destroyed after being detached (without being re-attached). |
494 WebContents* contents = content::Source<WebContents>(source).ptr(); | 520 WebContents* contents = content::Source<WebContents>(source).ptr(); |
495 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 521 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
496 content::Source<NavigationController>(&contents->GetController())); | 522 content::Source<NavigationController>(&contents->GetController())); |
497 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | 523 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
498 content::Source<WebContents>(contents)); | 524 content::Source<WebContents>(contents)); |
499 registrar_.Remove(this, chrome::NOTIFICATION_FAVICON_UPDATED, | 525 registrar_.Remove(this, chrome::NOTIFICATION_FAVICON_UPDATED, |
500 content::Source<WebContents>(contents)); | 526 content::Source<WebContents>(contents)); |
501 } else if (type == chrome::NOTIFICATION_FAVICON_UPDATED) { | 527 } else if (type == chrome::NOTIFICATION_FAVICON_UPDATED) { |
502 bool icon_url_changed = *content::Details<bool>(details).ptr(); | 528 bool icon_url_changed = *content::Details<bool>(details).ptr(); |
503 if (icon_url_changed) | 529 if (icon_url_changed) |
504 FaviconUrlUpdated(content::Source<WebContents>(source).ptr()); | 530 FaviconUrlUpdated(content::Source<WebContents>(source).ptr()); |
531 } else if (type == content::NOTIFICATION_WEB_CONTENTS_ZOOM_CHANGE) { | |
532 content::ZoomChangeDetails zoom_change_details = | |
533 *content::Details<content::ZoomChangeDetails>(details).ptr(); | |
534 double old_zoom_factor = | |
535 content::ZoomLevelToZoomFactor(zoom_change_details.old_zoom_level); | |
536 double new_zoom_factor = | |
537 content::ZoomLevelToZoomFactor(zoom_change_details.new_zoom_level); | |
538 base::DictionaryValue* zoom_settings = new base::DictionaryValue(); | |
539 ZoomModeToZoomSettings(zoom_change_details.zoom_mode, zoom_settings); | |
540 DispatchTabZoomChangeEvent(content::Source<WebContents>(source).ptr(), | |
541 old_zoom_factor, new_zoom_factor, zoom_settings); | |
505 } else { | 542 } else { |
506 NOTREACHED(); | 543 NOTREACHED(); |
507 } | 544 } |
508 } | 545 } |
509 | 546 |
510 void TabsEventRouter::TabChangedAt(WebContents* contents, | 547 void TabsEventRouter::TabChangedAt(WebContents* contents, |
511 int index, | 548 int index, |
512 TabChangeType change_type) { | 549 TabChangeType change_type) { |
513 TabUpdated(contents, false); | 550 TabUpdated(contents, false); |
514 } | 551 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
548 if (ExtensionTabUtil::GetTabStripModel(contents, &tab_strip, &tab_index)) { | 585 if (ExtensionTabUtil::GetTabStripModel(contents, &tab_strip, &tab_index)) { |
549 scoped_ptr<base::DictionaryValue> changed_properties( | 586 scoped_ptr<base::DictionaryValue> changed_properties( |
550 new base::DictionaryValue()); | 587 new base::DictionaryValue()); |
551 changed_properties->SetBoolean(tabs_constants::kPinnedKey, | 588 changed_properties->SetBoolean(tabs_constants::kPinnedKey, |
552 tab_strip->IsTabPinned(tab_index)); | 589 tab_strip->IsTabPinned(tab_index)); |
553 DispatchTabUpdatedEvent(contents, changed_properties.Pass()); | 590 DispatchTabUpdatedEvent(contents, changed_properties.Pass()); |
554 } | 591 } |
555 } | 592 } |
556 | 593 |
557 } // namespace extensions | 594 } // namespace extensions |
OLD | NEW |