Chromium Code Reviews| 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 "base/prefs/pref_service.h" | 7 #include "base/prefs/pref_service.h" |
| 8 #include "chrome/browser/chrome_notification_types.h" | 8 #include "chrome/browser/chrome_notification_types.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/ui/browser_finder.h" | 10 #include "chrome/browser/ui/browser_finder.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_details.h" | 14 #include "content/public/browser/notification_details.h" |
| 15 #include "content/public/browser/notification_service.h" | 15 #include "content/public/browser/notification_service.h" |
| 16 #include "content/public/browser/notification_types.h" | 16 #include "content/public/browser/notification_types.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::WebContentsObserver(web_contents), |
| 26 zoom_percent_(100), | 26 zoom_percent_(100), |
| 27 last_zoom_id_(0), | |
| 27 observer_(NULL), | 28 observer_(NULL), |
| 28 browser_context_(web_contents->GetBrowserContext()) { | 29 browser_context_(web_contents->GetBrowserContext()) { |
| 29 Profile* profile = | 30 Profile* profile = |
| 30 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 31 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 31 default_zoom_level_.Init(prefs::kDefaultZoomLevel, profile->GetPrefs(), | 32 default_zoom_level_.Init( |
| 32 base::Bind(&ZoomController::UpdateState, | 33 prefs::kDefaultZoomLevel, profile->GetPrefs(), |
| 33 base::Unretained(this), | 34 base::Bind(static_cast<void(ZoomController::*)( |
| 34 std::string())); | 35 const std::string&)>(&ZoomController::UpdateState), |
| 36 base::Unretained(this), | |
| 37 std::string())); | |
| 35 | 38 |
| 36 zoom_subscription_ = content::HostZoomMap::GetForBrowserContext( | 39 zoom_subscription_ = content::HostZoomMap::GetForBrowserContext( |
| 37 browser_context_)->AddZoomLevelChangedCallback( | 40 browser_context_)->AddZoomLevelChangedCallback( |
| 38 base::Bind(&ZoomController::OnZoomLevelChanged, | 41 base::Bind(&ZoomController::OnZoomLevelChanged, |
| 39 base::Unretained(this))); | 42 base::Unretained(this))); |
| 40 | 43 |
| 41 UpdateState(std::string()); | 44 UpdateState(std::string()); |
| 42 } | 45 } |
| 43 | 46 |
| 44 ZoomController::~ZoomController() {} | 47 ZoomController::~ZoomController() {} |
| 45 | 48 |
| 46 bool ZoomController::IsAtDefaultZoom() const { | 49 bool ZoomController::IsAtDefaultZoom() const { |
| 47 return content::ZoomValuesEqual(web_contents()->GetZoomLevel(), | 50 return content::ZoomValuesEqual(web_contents()->GetZoomLevel(), |
| 48 default_zoom_level_.GetValue()); | 51 default_zoom_level_.GetValue()); |
| 49 } | 52 } |
| 50 | 53 |
| 51 int ZoomController::GetResourceForZoomLevel() const { | 54 int ZoomController::GetResourceForZoomLevel() const { |
| 52 if (IsAtDefaultZoom()) | 55 if (IsAtDefaultZoom()) |
| 53 return IDR_ZOOM_NORMAL; | 56 return IDR_ZOOM_NORMAL; |
| 54 double zoom = web_contents()->GetZoomLevel(); | 57 double zoom = web_contents()->GetZoomLevel(); |
| 55 return zoom > default_zoom_level_.GetValue() ? IDR_ZOOM_PLUS : IDR_ZOOM_MINUS; | 58 return zoom > default_zoom_level_.GetValue() ? IDR_ZOOM_PLUS : IDR_ZOOM_MINUS; |
| 56 } | 59 } |
| 57 | 60 |
| 61 const extensions::Extension* ZoomController::GetExtensionById( | |
| 62 int zoom_id) const { | |
| 63 if (last_zoom_id_) { | |
| 64 ZoomExtensionMap::const_iterator it = | |
| 65 zoom_extension_map_.find(last_zoom_id_); | |
| 66 if (it != zoom_extension_map_.end()) { | |
| 67 return it->second.get(); | |
| 68 } | |
| 69 } | |
| 70 return NULL; | |
| 71 } | |
| 72 | |
| 73 int ZoomController::last_zoom_id() const { | |
|
Fady Samuel
2014/04/07 20:01:14
I'm really not a fan of exposing ZoomIds.
paulmeyer
2014/04/07 20:56:10
Okay, will work on getting rid of all zoom ID stuf
| |
| 74 return last_zoom_id_; | |
| 75 } | |
| 76 | |
| 77 int ZoomController::SetZoomLevelByExtension( | |
| 78 double zoom_level, | |
| 79 scoped_refptr<const extensions::Extension> extension) { | |
| 80 int zoom_id = web_contents()->SetZoomLevel(zoom_level); | |
| 81 if (zoom_id) { | |
| 82 zoom_extension_map_.insert(std::make_pair(zoom_id, extension)); | |
| 83 } | |
| 84 return zoom_id; | |
| 85 } | |
| 86 | |
| 58 void ZoomController::DidNavigateMainFrame( | 87 void ZoomController::DidNavigateMainFrame( |
| 59 const content::LoadCommittedDetails& details, | 88 const content::LoadCommittedDetails& details, |
| 60 const content::FrameNavigateParams& params) { | 89 const content::FrameNavigateParams& params) { |
| 61 // If the main frame's content has changed, the new page may have a different | 90 // If the main frame's content has changed, the new page may have a different |
| 62 // zoom level from the old one. | 91 // zoom level from the old one. |
| 63 UpdateState(std::string()); | 92 UpdateState(std::string()); |
| 64 } | 93 } |
| 65 | 94 |
| 66 void ZoomController::OnZoomLevelChanged( | 95 void ZoomController::OnZoomLevelChanged( |
| 67 const content::HostZoomMap::ZoomLevelChange& change) { | 96 const content::HostZoomMap::ZoomLevelChange& change) { |
| 68 UpdateState(change.host); | 97 UpdateState(change.host, |
| 98 change.zoom_id, | |
| 99 change.mode == content::HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM); | |
| 69 } | 100 } |
| 70 | 101 |
| 71 void ZoomController::UpdateState(const std::string& host) { | 102 void ZoomController::UpdateState(const std::string& host) { |
| 103 UpdateState(host, 0, false); | |
| 104 } | |
| 105 | |
| 106 void ZoomController::UpdateState(const std::string& host, | |
| 107 int zoom_id, | |
| 108 bool is_temporary_zoom) { | |
| 109 // Remove old zoom extension map entries and update |last_zoom_id_|. | |
| 110 if (last_zoom_id_) { | |
| 111 ZoomExtensionMap::iterator it = zoom_extension_map_.find(last_zoom_id_); | |
| 112 if (it != zoom_extension_map_.end()) { | |
| 113 zoom_extension_map_.erase(it); | |
| 114 } | |
| 115 } | |
| 116 last_zoom_id_ = zoom_id; | |
| 117 | |
| 118 if (zoom_id) { | |
| 119 content::NotificationService::current()->Notify( | |
| 120 chrome::NOTIFICATION_TAB_ZOOM_CHANGE_COMPLETE, | |
| 121 content::Source<ZoomController>(this), | |
| 122 content::Details<int>(&zoom_id)); | |
| 123 } | |
| 124 | |
| 72 // If |host| is empty, all observers should be updated. | 125 // If |host| is empty, all observers should be updated. |
| 73 if (!host.empty()) { | 126 if (!host.empty()) { |
| 74 // Use the navigation entry's URL instead of the WebContents' so virtual | 127 // Use the navigation entry's URL instead of the WebContents' so virtual |
| 75 // URLs work (e.g. chrome://settings). http://crbug.com/153950 | 128 // URLs work (e.g. chrome://settings). http://crbug.com/153950 |
| 76 content::NavigationEntry* entry = | 129 content::NavigationEntry* entry = |
| 77 web_contents()->GetController().GetLastCommittedEntry(); | 130 web_contents()->GetController().GetLastCommittedEntry(); |
| 78 if (!entry || | 131 if (!entry || |
| 79 host != net::GetHostOrSpecFromURL(entry->GetURL())) { | 132 host != net::GetHostOrSpecFromURL(entry->GetURL())) { |
| 80 return; | 133 return; |
| 81 } | 134 } |
| 82 } | 135 } |
| 83 | |
| 84 bool dummy; | 136 bool dummy; |
| 85 zoom_percent_ = web_contents()->GetZoomPercent(&dummy, &dummy); | 137 zoom_percent_ = web_contents()->GetZoomPercent(&dummy, &dummy); |
| 86 | 138 |
| 87 if (observer_) | 139 if (observer_) { |
| 88 observer_->OnZoomChanged(web_contents(), !host.empty()); | 140 bool can_show_bubble = |
| 141 web_contents()->GetZoomMode() != content::kZoomModeDefault || | |
|
Fady Samuel
2014/04/07 20:01:14
This needs some explanation.
paulmeyer
2014/04/07 20:56:10
Done.
| |
| 142 (!host.empty() && !is_temporary_zoom); | |
| 143 observer_->OnZoomChanged(web_contents(), can_show_bubble); | |
| 144 } | |
| 89 } | 145 } |
| OLD | NEW |