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/extensions/api/tabs/tabs_event_router.h" | |
| 10 #include "chrome/browser/extensions/api/tabs/tabs_windows_api.h" | |
| 9 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/ui/browser_finder.h" | 12 #include "chrome/browser/ui/browser_finder.h" |
| 13 #include "chrome/browser/ui/zoom/zoom_event_manager.h" | |
| 11 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
| 12 #include "content/public/browser/host_zoom_map.h" | 15 #include "content/public/browser/host_zoom_map.h" |
| 13 #include "content/public/browser/navigation_entry.h" | 16 #include "content/public/browser/navigation_entry.h" |
| 14 #include "content/public/browser/notification_details.h" | 17 #include "content/public/browser/notification_details.h" |
| 15 #include "content/public/browser/notification_service.h" | 18 #include "content/public/browser/notification_service.h" |
| 16 #include "content/public/browser/notification_types.h" | 19 #include "content/public/browser/notification_types.h" |
| 20 #include "content/public/browser/render_process_host.h" | |
| 21 #include "content/public/browser/render_view_host.h" | |
| 17 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
| 18 #include "content/public/common/page_zoom.h" | 23 #include "content/public/common/page_zoom.h" |
| 24 #include "extensions/common/extension.h" | |
| 19 #include "grit/theme_resources.h" | 25 #include "grit/theme_resources.h" |
| 20 #include "net/base/net_util.h" | 26 #include "net/base/net_util.h" |
| 21 | 27 |
| 22 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ZoomController); | 28 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ZoomController); |
| 23 | 29 |
| 24 ZoomController::ZoomController(content::WebContents* web_contents) | 30 ZoomController::ZoomController(content::WebContents* web_contents) |
| 25 : content::WebContentsObserver(web_contents), | 31 : content::WebContentsObserver(web_contents), |
| 26 zoom_percent_(100), | 32 zoom_mode_(kZoomModeDefault), |
| 27 observer_(NULL), | |
| 28 browser_context_(web_contents->GetBrowserContext()) { | 33 browser_context_(web_contents->GetBrowserContext()) { |
| 29 Profile* profile = | 34 Profile* profile = |
| 30 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 35 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 31 default_zoom_level_.Init(prefs::kDefaultZoomLevel, profile->GetPrefs(), | 36 default_zoom_level_.Init( |
| 32 base::Bind(&ZoomController::UpdateState, | 37 prefs::kDefaultZoomLevel, |
| 33 base::Unretained(this), | 38 profile->GetPrefs(), |
| 34 std::string())); | 39 base::Bind( |
| 40 &ZoomController::UpdateState, base::Unretained(this), std::string())); | |
| 41 zoom_level_ = default_zoom_level_.GetValue(); | |
| 35 | 42 |
| 36 zoom_subscription_ = content::HostZoomMap::GetForBrowserContext( | 43 zoom_subscription_ = content::HostZoomMap::GetForBrowserContext( |
| 37 browser_context_)->AddZoomLevelChangedCallback( | 44 browser_context_)->AddZoomLevelChangedCallback( |
| 38 base::Bind(&ZoomController::OnZoomLevelChanged, | 45 base::Bind(&ZoomController::OnZoomLevelChanged, |
| 39 base::Unretained(this))); | 46 base::Unretained(this))); |
| 40 | 47 |
| 48 AddObserver( | |
| 49 extensions::TabsWindowsAPI::Get(browser_context_)->tabs_event_router()); | |
| 50 | |
| 41 UpdateState(std::string()); | 51 UpdateState(std::string()); |
| 42 } | 52 } |
| 43 | 53 |
| 44 ZoomController::~ZoomController() {} | 54 ZoomController::~ZoomController() {} |
| 45 | 55 |
| 46 bool ZoomController::IsAtDefaultZoom() const { | 56 bool ZoomController::IsAtDefaultZoom() const { |
| 47 return content::ZoomValuesEqual( | 57 return content::ZoomValuesEqual(GetZoomLevel(), |
| 48 content::HostZoomMap::GetZoomLevel(web_contents()), | 58 default_zoom_level_.GetValue()); |
| 49 default_zoom_level_.GetValue()); | |
| 50 } | 59 } |
| 51 | 60 |
| 52 int ZoomController::GetResourceForZoomLevel() const { | 61 int ZoomController::GetResourceForZoomLevel() const { |
| 53 if (IsAtDefaultZoom()) | 62 if (IsAtDefaultZoom()) |
| 54 return IDR_ZOOM_NORMAL; | 63 return IDR_ZOOM_NORMAL; |
| 55 double zoom = content::HostZoomMap::GetZoomLevel(web_contents()); | 64 return GetZoomLevel() > default_zoom_level_.GetValue() ? IDR_ZOOM_PLUS |
| 56 return zoom > default_zoom_level_.GetValue() ? IDR_ZOOM_PLUS : IDR_ZOOM_MINUS; | 65 : IDR_ZOOM_MINUS; |
| 66 } | |
| 67 | |
| 68 double ZoomController::GetZoomLevel() const { | |
| 69 return zoom_mode_ == kZoomModeManual | |
| 70 ? zoom_level_ | |
| 71 : content::HostZoomMap::GetZoomLevel(web_contents()); | |
| 72 } | |
| 73 | |
| 74 int ZoomController::GetZoomPercent() const { | |
| 75 double zoom_factor = content::ZoomLevelToZoomFactor(GetZoomLevel()); | |
| 76 return static_cast<int>(zoom_factor * 100 + 0.5); | |
| 77 } | |
| 78 | |
| 79 bool ZoomController::SetZoomLevel(double zoom_level) { | |
| 80 // An extension did not initiate this zoom change. | |
| 81 return SetZoomLevelByExtension(zoom_level, | |
| 82 scoped_refptr<const extensions::Extension>()); | |
| 83 } | |
| 84 | |
| 85 bool ZoomController::SetZoomLevelByExtension( | |
| 86 double zoom_level, | |
| 87 scoped_refptr<const extensions::Extension> extension) { | |
| 88 // Cannot zoom in disabled mode. | |
| 89 if (zoom_mode_ == kZoomModeDisabled) | |
| 90 return false; | |
| 91 | |
| 92 // Store extension data so that |extension| can be attributed when the zoom | |
| 93 // change completes. We expect that by the time this function returns that | |
| 94 // any observers that require this information will have requested it. | |
| 95 last_extension_ = extension; | |
| 96 | |
| 97 if (zoom_mode_ == kZoomModeManual) { | |
| 98 double old_zoom_level = zoom_level_; | |
| 99 zoom_level_ = zoom_level; | |
| 100 | |
| 101 // TODO(wjmaclean) Do we care about filling in host/scheme here? | |
| 102 content::HostZoomMap::ZoomLevelChange change; | |
| 103 change.mode = content::HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM; | |
| 104 change.zoom_level = zoom_level; | |
| 105 ZoomEventManager::GetForBrowserContext(web_contents()->GetBrowserContext()) | |
|
Devlin
2014/06/23 22:56:08
nit: not your code, but since we have a private |b
wjmaclean
2014/06/24 15:38:16
Done.
| |
| 106 ->OnZoomLevelChanged(change); | |
| 107 | |
| 108 FOR_EACH_OBSERVER( | |
| 109 ZoomObserver, | |
| 110 observers_, | |
| 111 OnZoomChanged(web_contents(), true /* can_show_bubble */)); | |
| 112 FOR_EACH_OBSERVER( | |
| 113 ZoomObserver, | |
| 114 observers_, | |
| 115 OnZoomChangeInitiated( | |
| 116 web_contents(), old_zoom_level, zoom_level, zoom_mode_)); | |
| 117 | |
| 118 last_extension_ = NULL; | |
| 119 return true; | |
| 120 } | |
| 121 | |
| 122 // Do not actually rescale the page in manual mode. | |
| 123 content::HostZoomMap* zoom_map = content::HostZoomMap::GetForBrowserContext( | |
| 124 web_contents()->GetBrowserContext()); | |
| 125 DCHECK(zoom_map); | |
| 126 double old_zoom_level = GetZoomLevel(); | |
| 127 int render_process_id = web_contents()->GetRenderProcessHost()->GetID(); | |
| 128 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID(); | |
| 129 if (zoom_mode_ == kZoomModeIsolated || | |
| 130 zoom_map->UsesTemporaryZoomLevel(render_process_id, render_view_id)) { | |
| 131 zoom_map->SetTemporaryZoomLevel( | |
| 132 render_process_id, render_view_id, zoom_level); | |
| 133 } else { | |
| 134 content::NavigationEntry* entry = | |
| 135 web_contents()->GetController().GetLastCommittedEntry(); | |
| 136 // We allow empty |host| values for data urls. | |
| 137 std::string host = | |
| 138 net::GetHostOrSpecFromURL(entry ? entry->GetURL() : GURL::EmptyGURL()); | |
| 139 zoom_map->SetZoomLevelForHost(host, zoom_level); | |
| 140 } | |
| 141 FOR_EACH_OBSERVER( | |
| 142 ZoomObserver, | |
| 143 observers_, | |
| 144 OnZoomChangeInitiated( | |
| 145 web_contents(), old_zoom_level, zoom_level, zoom_mode_)); | |
| 146 | |
| 147 last_extension_ = NULL; | |
| 148 return true; | |
| 149 } | |
| 150 | |
| 151 void ZoomController::SetZoomMode(ZoomMode new_mode) { | |
| 152 if (new_mode == zoom_mode_) | |
| 153 return; | |
| 154 | |
| 155 content::HostZoomMap* zoom_map = content::HostZoomMap::GetForBrowserContext( | |
| 156 web_contents()->GetBrowserContext()); | |
| 157 DCHECK(zoom_map); | |
| 158 int render_process_id = web_contents()->GetRenderProcessHost()->GetID(); | |
| 159 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID(); | |
| 160 double original_zoom_level = GetZoomLevel(); | |
| 161 | |
| 162 switch (new_mode) { | |
| 163 case kZoomModeDefault: { | |
| 164 content::NavigationEntry* entry = | |
| 165 web_contents()->GetController().GetLastCommittedEntry(); | |
| 166 GURL url; | |
| 167 if (entry) | |
| 168 url = entry->GetURL(); | |
| 169 std::string host = net::GetHostOrSpecFromURL(url); | |
| 170 | |
| 171 if (zoom_map->HasZoomLevel(url.scheme(), host)) { | |
| 172 // If there are other tabs with the same origin, then set this tab's | |
| 173 // zoom level to match theirs. The temporary zoom level will be cleared | |
| 174 // below, but this call will make sure this tab re-draws at the correct | |
| 175 // zoom level. | |
| 176 double origin_zoom_level = | |
| 177 zoom_map->GetZoomLevelForHostAndScheme(url.scheme(), host); | |
| 178 zoom_map->SetTemporaryZoomLevel( | |
| 179 render_process_id, render_view_id, origin_zoom_level); | |
| 180 } else { | |
| 181 // The host will need a level prior to removing the temporary level. | |
| 182 // We don't want the zoom level to change just because we entered | |
| 183 // default mode. | |
| 184 zoom_map->SetZoomLevelForHost(host, original_zoom_level); | |
| 185 } | |
| 186 // Remove per-tab zoom data for this tab. | |
| 187 zoom_map->ClearTemporaryZoomLevel(render_process_id, render_view_id); | |
| 188 break; | |
| 189 } | |
| 190 case kZoomModeIsolated: { | |
| 191 // Unless the zoom mode was |kZoomModeDisabled| before this call, the page | |
| 192 // needs an initial isolated zoom back to the same level it was at in the | |
| 193 // other mode. | |
| 194 if (zoom_mode_ != kZoomModeDisabled) { | |
| 195 zoom_map->SetTemporaryZoomLevel( | |
| 196 render_process_id, render_view_id, original_zoom_level); | |
| 197 } | |
| 198 break; | |
| 199 } | |
| 200 case kZoomModeManual: { | |
| 201 // Unless the zoom mode was |kZoomModeDisabled| before this call, the page | |
| 202 // needs to be resized to the default zoom before calling SetZoomLevel() | |
| 203 // so that the page can be resized manually to the same zoom as before. | |
| 204 if (zoom_mode_ != kZoomModeDisabled) { | |
| 205 zoom_map->SetTemporaryZoomLevel(render_process_id, render_view_id, 0); | |
| 206 zoom_level_ = original_zoom_level; | |
| 207 } | |
| 208 break; | |
| 209 } | |
| 210 case kZoomModeDisabled: { | |
| 211 // The page needs to be zoomed back to default before disabling the zoom | |
| 212 zoom_map->SetTemporaryZoomLevel(render_process_id, render_view_id, 0); | |
| 213 break; | |
| 214 } | |
| 215 } | |
| 216 | |
| 217 zoom_mode_ = new_mode; | |
| 57 } | 218 } |
| 58 | 219 |
| 59 void ZoomController::DidNavigateMainFrame( | 220 void ZoomController::DidNavigateMainFrame( |
| 60 const content::LoadCommittedDetails& details, | 221 const content::LoadCommittedDetails& details, |
| 61 const content::FrameNavigateParams& params) { | 222 const content::FrameNavigateParams& params) { |
| 62 // If the main frame's content has changed, the new page may have a different | 223 // If the main frame's content has changed, the new page may have a different |
| 63 // zoom level from the old one. | 224 // zoom level from the old one. |
| 64 UpdateState(std::string()); | 225 UpdateState(std::string()); |
| 65 } | 226 } |
| 66 | 227 |
| 67 void ZoomController::OnZoomLevelChanged( | 228 void ZoomController::OnZoomLevelChanged( |
| 68 const content::HostZoomMap::ZoomLevelChange& change) { | 229 const content::HostZoomMap::ZoomLevelChange& change) { |
| 69 UpdateState(change.host); | 230 UpdateStateIncludingTemporary( |
| 231 change.host, | |
| 232 change.mode == content::HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM); | |
| 70 } | 233 } |
| 71 | 234 |
| 72 void ZoomController::UpdateState(const std::string& host) { | 235 void ZoomController::UpdateState(const std::string& host) { |
| 236 UpdateStateIncludingTemporary(host, false); | |
| 237 } | |
| 238 | |
| 239 void ZoomController::UpdateStateIncludingTemporary(const std::string& host, | |
| 240 bool is_temporary_zoom) { | |
| 73 // If |host| is empty, all observers should be updated. | 241 // If |host| is empty, all observers should be updated. |
| 74 if (!host.empty()) { | 242 if (!host.empty()) { |
| 75 // Use the navigation entry's URL instead of the WebContents' so virtual | 243 // Use the navigation entry's URL instead of the WebContents' so virtual |
| 76 // URLs work (e.g. chrome://settings). http://crbug.com/153950 | 244 // URLs work (e.g. chrome://settings). http://crbug.com/153950 |
| 77 content::NavigationEntry* entry = | 245 content::NavigationEntry* entry = |
| 78 web_contents()->GetController().GetLastCommittedEntry(); | 246 web_contents()->GetController().GetLastCommittedEntry(); |
| 79 if (!entry || | 247 if (!entry || |
| 80 host != net::GetHostOrSpecFromURL(entry->GetURL())) { | 248 host != net::GetHostOrSpecFromURL(entry->GetURL())) { |
| 81 return; | 249 return; |
| 82 } | 250 } |
| 83 } | 251 } |
| 84 | 252 |
| 85 bool dummy; | 253 // The zoom bubble can be shown for all normal, per-origin zoom changes |
| 86 zoom_percent_ = web_contents()->GetZoomPercent(&dummy, &dummy); | 254 // (where the host will not be empty and the zoom is not temporary), or any |
| 87 | 255 // special zoom changes (where the zoom mode will not be "default"). |
| 88 if (observer_) | 256 bool can_show_bubble = |
| 89 observer_->OnZoomChanged(web_contents(), !host.empty()); | 257 zoom_mode_ != kZoomModeDefault || (!host.empty() && !is_temporary_zoom); |
| 258 FOR_EACH_OBSERVER(ZoomObserver, | |
| 259 observers_, | |
| 260 OnZoomChanged(web_contents(), can_show_bubble)); | |
| 90 } | 261 } |
| OLD | NEW |