Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / components / ui / zoom / zoom_controller.cc
blob1bd965bc9fef21a6b9544c49d43363a26f03b883
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
3 // found in the LICENSE file.
5 #include "components/ui/zoom/zoom_controller.h"
7 #include "components/ui/zoom/zoom_event_manager.h"
8 #include "components/ui/zoom/zoom_observer.h"
9 #include "content/public/browser/host_zoom_map.h"
10 #include "content/public/browser/navigation_details.h"
11 #include "content/public/browser/navigation_entry.h"
12 #include "content/public/browser/render_process_host.h"
13 #include "content/public/browser/render_view_host.h"
14 #include "content/public/browser/web_contents.h"
15 #include "content/public/common/page_type.h"
16 #include "content/public/common/page_zoom.h"
17 #include "net/base/net_util.h"
19 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ui_zoom::ZoomController);
21 namespace ui_zoom {
23 double ZoomController::GetZoomLevelForWebContents(
24 const content::WebContents* web_contents) {
25 if (!web_contents)
26 return 0.0;
28 auto zoom_controller = FromWebContents(web_contents);
29 if (zoom_controller)
30 return zoom_controller->GetZoomLevel();
32 return content::HostZoomMap::GetZoomLevel(web_contents);
35 ZoomController::ZoomController(content::WebContents* web_contents)
36 : content::WebContentsObserver(web_contents),
37 can_show_bubble_(true),
38 zoom_mode_(ZOOM_MODE_DEFAULT),
39 zoom_level_(1.0),
40 browser_context_(web_contents->GetBrowserContext()) {
41 host_zoom_map_ = content::HostZoomMap::GetForWebContents(web_contents);
42 zoom_level_ = host_zoom_map_->GetDefaultZoomLevel();
44 zoom_subscription_ = host_zoom_map_->AddZoomLevelChangedCallback(
45 base::Bind(&ZoomController::OnZoomLevelChanged, base::Unretained(this)));
47 UpdateState(std::string());
50 ZoomController::~ZoomController() {
53 bool ZoomController::IsAtDefaultZoom() const {
54 return content::ZoomValuesEqual(GetZoomLevel(), GetDefaultZoomLevel());
57 ZoomController::RelativeZoom ZoomController::GetZoomRelativeToDefault() const {
58 double current_level = GetZoomLevel();
59 double default_level = GetDefaultZoomLevel();
60 if (content::ZoomValuesEqual(current_level, default_level))
61 return ZOOM_AT_DEFAULT_ZOOM;
62 else if (current_level > default_level)
63 return ZOOM_ABOVE_DEFAULT_ZOOM;
64 return ZOOM_BELOW_DEFAULT_ZOOM;
67 void ZoomController::AddObserver(ZoomObserver* observer) {
68 observers_.AddObserver(observer);
71 void ZoomController::RemoveObserver(ZoomObserver* observer) {
72 observers_.RemoveObserver(observer);
75 double ZoomController::GetZoomLevel() const {
76 return zoom_mode_ == ZOOM_MODE_MANUAL
77 ? zoom_level_
78 : content::HostZoomMap::GetZoomLevel(web_contents());
81 int ZoomController::GetZoomPercent() const {
82 double zoom_factor = content::ZoomLevelToZoomFactor(GetZoomLevel());
83 // Round double for return.
84 return static_cast<int>(zoom_factor * 100 + 0.5);
87 bool ZoomController::SetZoomLevel(double zoom_level) {
88 // A client did not initiate this zoom change.
89 return SetZoomLevelByClient(zoom_level, NULL);
92 bool ZoomController::SetZoomLevelByClient(
93 double zoom_level,
94 const scoped_refptr<const ZoomRequestClient>& client) {
95 content::NavigationEntry* entry =
96 web_contents()->GetController().GetLastCommittedEntry();
97 // Cannot zoom in disabled mode. Also, don't allow changing zoom level on
98 // a crashed tab, an error page or an interstitial page.
99 if (zoom_mode_ == ZOOM_MODE_DISABLED ||
100 !web_contents()->GetRenderViewHost()->IsRenderViewLive())
101 return false;
103 // Store client data so the |client| can be attributed when the zoom
104 // change completes. We expect that by the time this function returns that
105 // any observers that require this information will have requested it.
106 last_client_ = client;
108 // Do not actually rescale the page in manual mode.
109 if (zoom_mode_ == ZOOM_MODE_MANUAL) {
110 // If the zoom level hasn't changed, early out to avoid sending an event.
111 if (content::ZoomValuesEqual(zoom_level_, zoom_level))
112 return true;
114 double old_zoom_level = zoom_level_;
115 zoom_level_ = zoom_level;
117 // TODO(wjmaclean) Do we care about filling in host/scheme here?
118 content::HostZoomMap::ZoomLevelChange change;
119 change.mode = content::HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM;
120 change.zoom_level = zoom_level;
121 ZoomEventManager::GetForBrowserContext(browser_context_)
122 ->OnZoomLevelChanged(change);
124 ZoomChangedEventData zoom_change_data(web_contents(), old_zoom_level,
125 zoom_level_, zoom_mode_,
126 can_show_bubble_);
127 FOR_EACH_OBSERVER(ZoomObserver, observers_,
128 OnZoomChanged(zoom_change_data));
130 last_client_ = NULL;
131 return true;
134 content::HostZoomMap* zoom_map =
135 content::HostZoomMap::GetForWebContents(web_contents());
136 DCHECK(zoom_map);
137 DCHECK(!event_data_);
138 event_data_.reset(new ZoomChangedEventData(web_contents(), GetZoomLevel(),
139 zoom_level, zoom_mode_,
140 false /* can_show_bubble */));
141 int render_process_id = web_contents()->GetRenderProcessHost()->GetID();
142 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID();
143 if (zoom_mode_ == ZOOM_MODE_ISOLATED ||
144 zoom_map->UsesTemporaryZoomLevel(render_process_id, render_view_id)) {
145 zoom_map->SetTemporaryZoomLevel(render_process_id, render_view_id,
146 zoom_level);
147 } else {
148 if (!entry) {
149 last_client_ = NULL;
150 // If we exit without triggering an update, we should clear event_data_,
151 // else we may later trigger a DCHECK(event_data_).
152 event_data_.reset();
153 return false;
155 std::string host =
156 net::GetHostOrSpecFromURL(content::HostZoomMap::GetURLFromEntry(entry));
157 zoom_map->SetZoomLevelForHost(host, zoom_level);
160 DCHECK(!event_data_);
161 last_client_ = NULL;
162 return true;
165 void ZoomController::SetZoomMode(ZoomMode new_mode) {
166 if (new_mode == zoom_mode_)
167 return;
169 content::HostZoomMap* zoom_map =
170 content::HostZoomMap::GetForWebContents(web_contents());
171 DCHECK(zoom_map);
172 int render_process_id = web_contents()->GetRenderProcessHost()->GetID();
173 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID();
174 double original_zoom_level = GetZoomLevel();
176 DCHECK(!event_data_);
177 event_data_.reset(new ZoomChangedEventData(
178 web_contents(), original_zoom_level, original_zoom_level, new_mode,
179 new_mode != ZOOM_MODE_DEFAULT));
181 switch (new_mode) {
182 case ZOOM_MODE_DEFAULT: {
183 content::NavigationEntry* entry =
184 web_contents()->GetController().GetLastCommittedEntry();
186 if (entry) {
187 GURL url = content::HostZoomMap::GetURLFromEntry(entry);
188 std::string host = net::GetHostOrSpecFromURL(url);
190 if (zoom_map->HasZoomLevel(url.scheme(), host)) {
191 // If there are other tabs with the same origin, then set this tab's
192 // zoom level to match theirs. The temporary zoom level will be
193 // cleared below, but this call will make sure this tab re-draws at
194 // the correct zoom level.
195 double origin_zoom_level =
196 zoom_map->GetZoomLevelForHostAndScheme(url.scheme(), host);
197 event_data_->new_zoom_level = origin_zoom_level;
198 zoom_map->SetTemporaryZoomLevel(render_process_id, render_view_id,
199 origin_zoom_level);
200 } else {
201 // The host will need a level prior to removing the temporary level.
202 // We don't want the zoom level to change just because we entered
203 // default mode.
204 zoom_map->SetZoomLevelForHost(host, original_zoom_level);
207 // Remove per-tab zoom data for this tab. No event callback expected.
208 zoom_map->ClearTemporaryZoomLevel(render_process_id, render_view_id);
209 break;
211 case ZOOM_MODE_ISOLATED: {
212 // Unless the zoom mode was |ZOOM_MODE_DISABLED| before this call, the
213 // page needs an initial isolated zoom back to the same level it was at
214 // in the other mode.
215 if (zoom_mode_ != ZOOM_MODE_DISABLED) {
216 zoom_map->SetTemporaryZoomLevel(render_process_id, render_view_id,
217 original_zoom_level);
218 } else {
219 // When we don't call any HostZoomMap set functions, we send the event
220 // manually.
221 FOR_EACH_OBSERVER(ZoomObserver, observers_,
222 OnZoomChanged(*event_data_));
223 event_data_.reset();
225 break;
227 case ZOOM_MODE_MANUAL: {
228 // Unless the zoom mode was |ZOOM_MODE_DISABLED| before this call, the
229 // page needs to be resized to the default zoom. While in manual mode,
230 // the zoom level is handled independently.
231 if (zoom_mode_ != ZOOM_MODE_DISABLED) {
232 zoom_map->SetTemporaryZoomLevel(render_process_id, render_view_id,
233 GetDefaultZoomLevel());
234 zoom_level_ = original_zoom_level;
235 } else {
236 // When we don't call any HostZoomMap set functions, we send the event
237 // manually.
238 FOR_EACH_OBSERVER(ZoomObserver, observers_,
239 OnZoomChanged(*event_data_));
240 event_data_.reset();
242 break;
244 case ZOOM_MODE_DISABLED: {
245 // The page needs to be zoomed back to default before disabling the zoom
246 double new_zoom_level = GetDefaultZoomLevel();
247 event_data_->new_zoom_level = new_zoom_level;
248 zoom_map->SetTemporaryZoomLevel(render_process_id, render_view_id,
249 new_zoom_level);
250 break;
253 // Any event data we've stored should have been consumed by this point.
254 DCHECK(!event_data_);
256 zoom_mode_ = new_mode;
259 void ZoomController::ResetZoomModeOnNavigationIfNeeded(const GURL& url) {
260 if (zoom_mode_ != ZOOM_MODE_ISOLATED && zoom_mode_ != ZOOM_MODE_MANUAL)
261 return;
263 int render_process_id = web_contents()->GetRenderProcessHost()->GetID();
264 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID();
265 content::HostZoomMap* zoom_map =
266 content::HostZoomMap::GetForWebContents(web_contents());
267 zoom_level_ = zoom_map->GetDefaultZoomLevel();
268 double old_zoom_level = zoom_map->GetZoomLevel(web_contents());
269 double new_zoom_level = zoom_map->GetZoomLevelForHostAndScheme(
270 url.scheme(), net::GetHostOrSpecFromURL(url));
271 event_data_.reset(new ZoomChangedEventData(
272 web_contents(), old_zoom_level, new_zoom_level, ZOOM_MODE_DEFAULT,
273 false /* can_show_bubble */));
274 // The call to ClearTemporaryZoomLevel() doesn't generate any events from
275 // HostZoomMap, but the call to UpdateState() at the end of this function
276 // will notify our observers.
277 // Note: it's possible the render_process/view ids have disappeared (e.g.
278 // if we navigated to a new origin), but this won't cause a problem in the
279 // call below.
280 zoom_map->ClearTemporaryZoomLevel(render_process_id, render_view_id);
281 zoom_mode_ = ZOOM_MODE_DEFAULT;
284 void ZoomController::DidNavigateMainFrame(
285 const content::LoadCommittedDetails& details,
286 const content::FrameNavigateParams& params) {
287 if (details.entry && details.entry->GetPageType() == content::PAGE_TYPE_ERROR)
288 content::HostZoomMap::SendErrorPageZoomLevelRefresh(web_contents());
290 if (!details.is_in_page)
291 ResetZoomModeOnNavigationIfNeeded(params.url);
293 // If the main frame's content has changed, the new page may have a different
294 // zoom level from the old one.
295 UpdateState(std::string());
296 DCHECK(!event_data_);
299 void ZoomController::WebContentsDestroyed() {
300 // At this point we should no longer be sending any zoom events with this
301 // WebContents.
302 observers_.Clear();
305 void ZoomController::RenderFrameHostChanged(
306 content::RenderFrameHost* old_host,
307 content::RenderFrameHost* new_host) {
308 // If our associated HostZoomMap changes, update our event subscription.
309 content::HostZoomMap* new_host_zoom_map =
310 content::HostZoomMap::GetForWebContents(web_contents());
311 if (new_host_zoom_map == host_zoom_map_)
312 return;
314 host_zoom_map_ = new_host_zoom_map;
315 zoom_subscription_ = host_zoom_map_->AddZoomLevelChangedCallback(
316 base::Bind(&ZoomController::OnZoomLevelChanged, base::Unretained(this)));
319 void ZoomController::OnZoomLevelChanged(
320 const content::HostZoomMap::ZoomLevelChange& change) {
321 UpdateState(change.host);
324 void ZoomController::UpdateState(const std::string& host) {
325 // If |host| is empty, all observers should be updated.
326 if (!host.empty()) {
327 // Use the navigation entry's URL instead of the WebContents' so virtual
328 // URLs work (e.g. chrome://settings). http://crbug.com/153950
329 content::NavigationEntry* entry =
330 web_contents()->GetController().GetLastCommittedEntry();
331 if (!entry ||
332 host != net::GetHostOrSpecFromURL(
333 content::HostZoomMap::GetURLFromEntry(entry))) {
334 return;
338 if (event_data_) {
339 // For state changes initiated within the ZoomController, information about
340 // the change should be sent.
341 ZoomChangedEventData zoom_change_data = *event_data_;
342 event_data_.reset();
343 // The zoom bubble should not be shown for zoom changes where the host
344 // is empty.
345 zoom_change_data.can_show_bubble = can_show_bubble_ && !host.empty();
346 FOR_EACH_OBSERVER(ZoomObserver, observers_,
347 OnZoomChanged(zoom_change_data));
348 } else {
349 // TODO(wjmaclean) Should we consider having HostZoomMap send both old and
350 // new zoom levels here?
351 double zoom_level = GetZoomLevel();
352 // We never show a zoom bubble for an event we didn't generate.
353 ZoomChangedEventData zoom_change_data(web_contents(), zoom_level,
354 zoom_level, zoom_mode_,
355 false /* can_show_bubble */);
356 FOR_EACH_OBSERVER(ZoomObserver, observers_,
357 OnZoomChanged(zoom_change_data));
361 void ZoomController::SetPageScaleFactorIsOneForTesting(bool is_one) {
362 int render_process_id = web_contents()->GetRenderProcessHost()->GetID();
363 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID();
364 host_zoom_map_->SetPageScaleFactorIsOneForView(
365 render_process_id, render_view_id, is_one);
368 bool ZoomController::PageScaleFactorIsOne() const {
369 return content::HostZoomMap::PageScaleFactorIsOne(web_contents());
372 } // namespace ui_zoom