| 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/ui/zoom/zoom_controller.h" | 5 #include "chrome/browser/ui/zoom/zoom_controller.h" |
| 6 | 6 |
| 7 #include "chrome/browser/prefs/pref_service.h" | 7 #include "chrome/browser/prefs/pref_service.h" |
| 8 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/ui/browser_finder.h" | 9 #include "chrome/browser/ui/browser_finder.h" |
| 10 #include "chrome/common/chrome_notification_types.h" | 10 #include "chrome/common/chrome_notification_types.h" |
| 11 #include "chrome/common/pref_names.h" | 11 #include "chrome/common/pref_names.h" |
| 12 #include "content/public/browser/host_zoom_map.h" | 12 #include "content/public/browser/host_zoom_map.h" |
| 13 #include "content/public/browser/navigation_entry.h" | 13 #include "content/public/browser/navigation_entry.h" |
| 14 #include "content/public/browser/notification_types.h" | 14 #include "content/public/browser/notification_types.h" |
| 15 #include "content/public/browser/notification_details.h" | 15 #include "content/public/browser/notification_details.h" |
| 16 #include "content/public/browser/notification_service.h" | 16 #include "content/public/browser/notification_service.h" |
| 17 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
| 18 #include "content/public/common/page_zoom.h" | 18 #include "content/public/common/page_zoom.h" |
| 19 #include "grit/theme_resources.h" | 19 #include "grit/theme_resources.h" |
| 20 #include "net/base/net_util.h" | 20 #include "net/base/net_util.h" |
| 21 | 21 |
| 22 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ZoomController); | 22 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ZoomController); |
| 23 | 23 |
| 24 ZoomController::ZoomController(content::WebContents* web_contents) | 24 ZoomController::ZoomController(content::WebContents* web_contents) |
| 25 : content::WebContentsObserver(web_contents), | 25 : content::HostZoomMap::Observer(content::HostZoomMap::GetForBrowserContext( |
| 26 web_contents->GetBrowserContext())), |
| 27 content::WebContentsObserver(web_contents), |
| 26 zoom_percent_(100), | 28 zoom_percent_(100), |
| 27 observer_(NULL) { | 29 observer_(NULL) { |
| 28 Profile* profile = | 30 Profile* profile = |
| 29 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 31 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 30 default_zoom_level_.Init(prefs::kDefaultZoomLevel, profile->GetPrefs(), | 32 default_zoom_level_.Init(prefs::kDefaultZoomLevel, profile->GetPrefs(), |
| 31 base::Bind(&ZoomController::UpdateState, | 33 base::Bind(&ZoomController::UpdateState, |
| 32 base::Unretained(this), | 34 base::Unretained(this), |
| 33 std::string())); | 35 std::string())); |
| 34 | 36 |
| 35 content::HostZoomMap* zoom_map = | |
| 36 content::HostZoomMap::GetForBrowserContext(profile); | |
| 37 registrar_.Add(this, content::NOTIFICATION_ZOOM_LEVEL_CHANGED, | |
| 38 content::Source<content::HostZoomMap>(zoom_map)); | |
| 39 | |
| 40 UpdateState(std::string()); | 37 UpdateState(std::string()); |
| 41 } | 38 } |
| 42 | 39 |
| 43 ZoomController::~ZoomController() {} | 40 ZoomController::~ZoomController() {} |
| 44 | 41 |
| 45 bool ZoomController::IsAtDefaultZoom() const { | 42 bool ZoomController::IsAtDefaultZoom() const { |
| 46 if (!web_contents()) | 43 if (!web_contents()) |
| 47 return true; | 44 return true; |
| 48 | 45 |
| 49 return content::ZoomValuesEqual(web_contents()->GetZoomLevel(), | 46 return content::ZoomValuesEqual(web_contents()->GetZoomLevel(), |
| 50 default_zoom_level_.GetValue()); | 47 default_zoom_level_.GetValue()); |
| 51 } | 48 } |
| 52 | 49 |
| 53 int ZoomController::GetResourceForZoomLevel() const { | 50 int ZoomController::GetResourceForZoomLevel() const { |
| 54 if (!web_contents()) | 51 if (!web_contents()) |
| 55 return IDR_ZOOM_MINUS; | 52 return IDR_ZOOM_MINUS; |
| 56 | 53 |
| 57 DCHECK(!IsAtDefaultZoom()); | 54 DCHECK(!IsAtDefaultZoom()); |
| 58 double zoom = web_contents()->GetZoomLevel(); | 55 double zoom = web_contents()->GetZoomLevel(); |
| 59 return zoom > default_zoom_level_.GetValue() ? IDR_ZOOM_PLUS : IDR_ZOOM_MINUS; | 56 return zoom > default_zoom_level_.GetValue() ? IDR_ZOOM_PLUS : IDR_ZOOM_MINUS; |
| 60 } | 57 } |
| 61 | 58 |
| 59 void ZoomController::OnZoomLevelChanged(const std::string& host) { |
| 60 UpdateState(host); |
| 61 } |
| 62 |
| 62 void ZoomController::DidNavigateMainFrame( | 63 void ZoomController::DidNavigateMainFrame( |
| 63 const content::LoadCommittedDetails& details, | 64 const content::LoadCommittedDetails& details, |
| 64 const content::FrameNavigateParams& params) { | 65 const content::FrameNavigateParams& params) { |
| 65 // If the main frame's content has changed, the new page may have a different | 66 // If the main frame's content has changed, the new page may have a different |
| 66 // zoom level from the old one. | 67 // zoom level from the old one. |
| 67 UpdateState(std::string()); | 68 UpdateState(std::string()); |
| 68 } | 69 } |
| 69 | 70 |
| 70 void ZoomController::Observe(int type, | |
| 71 const content::NotificationSource& source, | |
| 72 const content::NotificationDetails& details) { | |
| 73 DCHECK_EQ(content::NOTIFICATION_ZOOM_LEVEL_CHANGED, type); | |
| 74 UpdateState(*content::Details<std::string>(details).ptr()); | |
| 75 } | |
| 76 | |
| 77 void ZoomController::UpdateState(const std::string& host) { | 71 void ZoomController::UpdateState(const std::string& host) { |
| 78 // TODO(dbeam): I'm not totally sure why this is happening, and there's been a | 72 // TODO(dbeam): I'm not totally sure why this is happening, and there's been a |
| 79 // bit of effort to understand with no tangible results yet. It's possible | 73 // bit of effort to understand with no tangible results yet. It's possible |
| 80 // that WebContents is NULL as it's being destroyed or some other random | 74 // that WebContents is NULL as it's being destroyed or some other random |
| 81 // reason that I haven't found yet. Applying band-aid. http://crbug.com/144879 | 75 // reason that I haven't found yet. Applying band-aid. http://crbug.com/144879 |
| 82 if (!web_contents()) | 76 if (!web_contents()) |
| 83 return; | 77 return; |
| 84 | 78 |
| 85 // If |host| is empty, all observers should be updated. | 79 // If |host| is empty, all observers should be updated. |
| 86 if (!host.empty()) { | 80 if (!host.empty()) { |
| 87 // Use the active navigation entry's URL instead of the WebContents' so | 81 // Use the active navigation entry's URL instead of the WebContents' so |
| 88 // virtual URLs work (e.g. chrome://settings). http://crbug.com/153950 | 82 // virtual URLs work (e.g. chrome://settings). http://crbug.com/153950 |
| 89 content::NavigationEntry* active_entry = | 83 content::NavigationEntry* active_entry = |
| 90 web_contents()->GetController().GetActiveEntry(); | 84 web_contents()->GetController().GetActiveEntry(); |
| 91 if (!active_entry || | 85 if (!active_entry || |
| 92 host != net::GetHostOrSpecFromURL(active_entry->GetURL())) { | 86 host != net::GetHostOrSpecFromURL(active_entry->GetURL())) { |
| 93 return; | 87 return; |
| 94 } | 88 } |
| 95 } | 89 } |
| 96 | 90 |
| 97 bool dummy; | 91 bool dummy; |
| 98 zoom_percent_ = web_contents()->GetZoomPercent(&dummy, &dummy); | 92 zoom_percent_ = web_contents()->GetZoomPercent(&dummy, &dummy); |
| 99 | 93 |
| 100 if (observer_) | 94 if (observer_) |
| 101 observer_->OnZoomChanged(web_contents(), !host.empty()); | 95 observer_->OnZoomChanged(web_contents(), !host.empty()); |
| 102 } | 96 } |
| OLD | NEW |