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), |
|
sky
2014/06/24 16:23:42
Can you initialize zoom_level_ too (I realize you
wjmaclean
2014/06/24 18:04:07
Sure!
| |
| 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( | |
|
sky
2014/06/24 16:23:42
Is this the common pattern for extension related s
wjmaclean
2014/06/24 18:04:07
I'm new to extension work, so I can't properly ans
sky
2014/06/25 16:02:39
Please get a reviewer of the extension side of thi
wjmaclean
2014/06/25 17:21:30
Ok, moved observer registration to TabsEventRouter
| |
| 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 void ZoomController::AddObserver(ZoomObserver* observer) { | |
| 69 observers_.AddObserver(observer); | |
|
sky
2014/06/24 16:23:41
nit: spacing (same on 73).
wjmaclean
2014/06/24 18:04:07
Done.
| |
| 70 } | |
| 71 | |
| 72 void ZoomController::RemoveObserver(ZoomObserver* observer) { | |
| 73 observers_.RemoveObserver(observer); | |
| 74 } | |
| 75 | |
| 76 double ZoomController::GetZoomLevel() const { | |
| 77 return zoom_mode_ == kZoomModeManual | |
| 78 ? zoom_level_ | |
| 79 : content::HostZoomMap::GetZoomLevel(web_contents()); | |
| 80 } | |
| 81 | |
| 82 int ZoomController::GetZoomPercent() const { | |
| 83 double zoom_factor = content::ZoomLevelToZoomFactor(GetZoomLevel()); | |
| 84 return static_cast<int>(zoom_factor * 100 + 0.5); | |
| 85 } | |
| 86 | |
| 87 bool ZoomController::SetZoomLevel(double zoom_level) { | |
| 88 // An extension did not initiate this zoom change. | |
| 89 return SetZoomLevelByExtension(zoom_level, | |
| 90 scoped_refptr<const extensions::Extension>()); | |
| 91 } | |
| 92 | |
| 93 bool ZoomController::SetZoomLevelByExtension( | |
| 94 double zoom_level, | |
| 95 scoped_refptr<const extensions::Extension> extension) { | |
| 96 // Cannot zoom in disabled mode. | |
| 97 if (zoom_mode_ == kZoomModeDisabled) | |
| 98 return false; | |
| 99 | |
| 100 // Store extension data so that |extension| can be attributed when the zoom | |
| 101 // change completes. We expect that by the time this function returns that | |
| 102 // any observers that require this information will have requested it. | |
| 103 last_extension_ = extension; | |
| 104 | |
| 105 if (zoom_mode_ == kZoomModeManual) { | |
| 106 double old_zoom_level = zoom_level_; | |
| 107 zoom_level_ = zoom_level; | |
| 108 | |
| 109 // TODO(wjmaclean) Do we care about filling in host/scheme here? | |
| 110 content::HostZoomMap::ZoomLevelChange change; | |
| 111 change.mode = content::HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM; | |
| 112 change.zoom_level = zoom_level; | |
| 113 ZoomEventManager::GetForBrowserContext(browser_context_) | |
| 114 ->OnZoomLevelChanged(change); | |
| 115 | |
| 116 FOR_EACH_OBSERVER( | |
| 117 ZoomObserver, | |
| 118 observers_, | |
| 119 OnZoomChanged(web_contents(), true /* can_show_bubble */)); | |
| 120 FOR_EACH_OBSERVER( | |
| 121 ZoomObserver, | |
| 122 observers_, | |
| 123 OnZoomChangeInitiated( | |
| 124 web_contents(), old_zoom_level, zoom_level, zoom_mode_)); | |
| 125 | |
| 126 last_extension_ = NULL; | |
| 127 return true; | |
| 128 } | |
| 129 | |
| 130 // Do not actually rescale the page in manual mode. | |
| 131 content::HostZoomMap* zoom_map = | |
| 132 content::HostZoomMap::GetForBrowserContext(browser_context_); | |
| 133 DCHECK(zoom_map); | |
| 134 double old_zoom_level = GetZoomLevel(); | |
| 135 int render_process_id = web_contents()->GetRenderProcessHost()->GetID(); | |
| 136 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID(); | |
| 137 if (zoom_mode_ == kZoomModeIsolated || | |
| 138 zoom_map->UsesTemporaryZoomLevel(render_process_id, render_view_id)) { | |
| 139 zoom_map->SetTemporaryZoomLevel( | |
| 140 render_process_id, render_view_id, zoom_level); | |
| 141 } else { | |
| 142 content::NavigationEntry* entry = | |
| 143 web_contents()->GetController().GetLastCommittedEntry(); | |
| 144 // We allow empty |host| values for data urls. | |
| 145 std::string host = | |
| 146 net::GetHostOrSpecFromURL(entry ? entry->GetURL() : GURL::EmptyGURL()); | |
| 147 zoom_map->SetZoomLevelForHost(host, zoom_level); | |
| 148 } | |
| 149 FOR_EACH_OBSERVER( | |
| 150 ZoomObserver, | |
| 151 observers_, | |
| 152 OnZoomChangeInitiated( | |
| 153 web_contents(), old_zoom_level, zoom_level, zoom_mode_)); | |
| 154 | |
| 155 last_extension_ = NULL; | |
| 156 return true; | |
| 157 } | |
| 158 | |
| 159 void ZoomController::SetZoomMode(ZoomMode new_mode) { | |
| 160 if (new_mode == zoom_mode_) | |
| 161 return; | |
| 162 | |
| 163 content::HostZoomMap* zoom_map = | |
| 164 content::HostZoomMap::GetForBrowserContext(browser_context_); | |
| 165 DCHECK(zoom_map); | |
| 166 int render_process_id = web_contents()->GetRenderProcessHost()->GetID(); | |
| 167 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID(); | |
| 168 double original_zoom_level = GetZoomLevel(); | |
| 169 | |
| 170 switch (new_mode) { | |
| 171 case kZoomModeDefault: { | |
| 172 content::NavigationEntry* entry = | |
| 173 web_contents()->GetController().GetLastCommittedEntry(); | |
| 174 GURL url; | |
| 175 if (entry) | |
| 176 url = entry->GetURL(); | |
| 177 std::string host = net::GetHostOrSpecFromURL(url); | |
| 178 | |
| 179 if (zoom_map->HasZoomLevel(url.scheme(), host)) { | |
| 180 // If there are other tabs with the same origin, then set this tab's | |
| 181 // zoom level to match theirs. The temporary zoom level will be cleared | |
| 182 // below, but this call will make sure this tab re-draws at the correct | |
| 183 // zoom level. | |
| 184 double origin_zoom_level = | |
| 185 zoom_map->GetZoomLevelForHostAndScheme(url.scheme(), host); | |
| 186 zoom_map->SetTemporaryZoomLevel( | |
| 187 render_process_id, render_view_id, origin_zoom_level); | |
| 188 } else { | |
| 189 // The host will need a level prior to removing the temporary level. | |
| 190 // We don't want the zoom level to change just because we entered | |
| 191 // default mode. | |
| 192 zoom_map->SetZoomLevelForHost(host, original_zoom_level); | |
| 193 } | |
| 194 // Remove per-tab zoom data for this tab. | |
| 195 zoom_map->ClearTemporaryZoomLevel(render_process_id, render_view_id); | |
| 196 break; | |
| 197 } | |
| 198 case kZoomModeIsolated: { | |
| 199 // Unless the zoom mode was |kZoomModeDisabled| before this call, the page | |
| 200 // needs an initial isolated zoom back to the same level it was at in the | |
| 201 // other mode. | |
| 202 if (zoom_mode_ != kZoomModeDisabled) { | |
| 203 zoom_map->SetTemporaryZoomLevel( | |
| 204 render_process_id, render_view_id, original_zoom_level); | |
| 205 } | |
| 206 break; | |
| 207 } | |
| 208 case kZoomModeManual: { | |
| 209 // Unless the zoom mode was |kZoomModeDisabled| before this call, the page | |
| 210 // needs to be resized to the default zoom before calling SetZoomLevel() | |
| 211 // so that the page can be resized manually to the same zoom as before. | |
| 212 if (zoom_mode_ != kZoomModeDisabled) { | |
| 213 zoom_map->SetTemporaryZoomLevel(render_process_id, render_view_id, 0); | |
| 214 zoom_level_ = original_zoom_level; | |
| 215 } | |
| 216 break; | |
| 217 } | |
| 218 case kZoomModeDisabled: { | |
| 219 // The page needs to be zoomed back to default before disabling the zoom | |
| 220 zoom_map->SetTemporaryZoomLevel(render_process_id, render_view_id, 0); | |
| 221 break; | |
| 222 } | |
| 223 } | |
| 224 | |
| 225 zoom_mode_ = new_mode; | |
| 57 } | 226 } |
| 58 | 227 |
| 59 void ZoomController::DidNavigateMainFrame( | 228 void ZoomController::DidNavigateMainFrame( |
| 60 const content::LoadCommittedDetails& details, | 229 const content::LoadCommittedDetails& details, |
| 61 const content::FrameNavigateParams& params) { | 230 const content::FrameNavigateParams& params) { |
| 62 // If the main frame's content has changed, the new page may have a different | 231 // If the main frame's content has changed, the new page may have a different |
| 63 // zoom level from the old one. | 232 // zoom level from the old one. |
| 64 UpdateState(std::string()); | 233 UpdateState(std::string()); |
| 65 } | 234 } |
| 66 | 235 |
| 67 void ZoomController::OnZoomLevelChanged( | 236 void ZoomController::OnZoomLevelChanged( |
| 68 const content::HostZoomMap::ZoomLevelChange& change) { | 237 const content::HostZoomMap::ZoomLevelChange& change) { |
| 69 UpdateState(change.host); | 238 UpdateStateIncludingTemporary( |
| 239 change.host, | |
| 240 change.mode == content::HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM); | |
| 70 } | 241 } |
| 71 | 242 |
| 72 void ZoomController::UpdateState(const std::string& host) { | 243 void ZoomController::UpdateState(const std::string& host) { |
| 244 UpdateStateIncludingTemporary(host, false); | |
| 245 } | |
| 246 | |
| 247 void ZoomController::UpdateStateIncludingTemporary(const std::string& host, | |
| 248 bool is_temporary_zoom) { | |
| 73 // If |host| is empty, all observers should be updated. | 249 // If |host| is empty, all observers should be updated. |
| 74 if (!host.empty()) { | 250 if (!host.empty()) { |
| 75 // Use the navigation entry's URL instead of the WebContents' so virtual | 251 // Use the navigation entry's URL instead of the WebContents' so virtual |
| 76 // URLs work (e.g. chrome://settings). http://crbug.com/153950 | 252 // URLs work (e.g. chrome://settings). http://crbug.com/153950 |
| 77 content::NavigationEntry* entry = | 253 content::NavigationEntry* entry = |
| 78 web_contents()->GetController().GetLastCommittedEntry(); | 254 web_contents()->GetController().GetLastCommittedEntry(); |
| 79 if (!entry || | 255 if (!entry || |
| 80 host != net::GetHostOrSpecFromURL(entry->GetURL())) { | 256 host != net::GetHostOrSpecFromURL(entry->GetURL())) { |
| 81 return; | 257 return; |
| 82 } | 258 } |
| 83 } | 259 } |
| 84 | 260 |
| 85 bool dummy; | 261 // The zoom bubble can be shown for all normal, per-origin zoom changes |
| 86 zoom_percent_ = web_contents()->GetZoomPercent(&dummy, &dummy); | 262 // (where the host will not be empty and the zoom is not temporary), or any |
| 87 | 263 // special zoom changes (where the zoom mode will not be "default"). |
| 88 if (observer_) | 264 bool can_show_bubble = |
| 89 observer_->OnZoomChanged(web_contents(), !host.empty()); | 265 zoom_mode_ != kZoomModeDefault || (!host.empty() && !is_temporary_zoom); |
| 266 FOR_EACH_OBSERVER(ZoomObserver, | |
| 267 observers_, | |
| 268 OnZoomChanged(web_contents(), can_show_bubble)); | |
| 90 } | 269 } |
| OLD | NEW |