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
);
23 ZoomController::ZoomController(content::WebContents
* web_contents
)
24 : content::WebContentsObserver(web_contents
),
25 can_show_bubble_(true),
26 zoom_mode_(ZOOM_MODE_DEFAULT
),
28 browser_context_(web_contents
->GetBrowserContext()) {
29 host_zoom_map_
= content::HostZoomMap::GetForWebContents(web_contents
);
30 zoom_level_
= host_zoom_map_
->GetDefaultZoomLevel();
32 zoom_subscription_
= host_zoom_map_
->AddZoomLevelChangedCallback(
33 base::Bind(&ZoomController::OnZoomLevelChanged
, base::Unretained(this)));
35 UpdateState(std::string());
38 ZoomController::~ZoomController() {
41 bool ZoomController::IsAtDefaultZoom() const {
42 return content::ZoomValuesEqual(GetZoomLevel(), GetDefaultZoomLevel());
45 ZoomController::RelativeZoom
ZoomController::GetZoomRelativeToDefault() const {
46 double current_level
= GetZoomLevel();
47 double default_level
= GetDefaultZoomLevel();
48 if (content::ZoomValuesEqual(current_level
, default_level
))
49 return ZOOM_AT_DEFAULT_ZOOM
;
50 else if (current_level
> default_level
)
51 return ZOOM_ABOVE_DEFAULT_ZOOM
;
52 return ZOOM_BELOW_DEFAULT_ZOOM
;
55 void ZoomController::AddObserver(ZoomObserver
* observer
) {
56 observers_
.AddObserver(observer
);
59 void ZoomController::RemoveObserver(ZoomObserver
* observer
) {
60 observers_
.RemoveObserver(observer
);
63 double ZoomController::GetZoomLevel() const {
64 return zoom_mode_
== ZOOM_MODE_MANUAL
66 : content::HostZoomMap::GetZoomLevel(web_contents());
69 int ZoomController::GetZoomPercent() const {
70 double zoom_factor
= content::ZoomLevelToZoomFactor(GetZoomLevel());
71 // Round double for return.
72 return static_cast<int>(zoom_factor
* 100 + 0.5);
75 bool ZoomController::SetZoomLevel(double zoom_level
) {
76 // A client did not initiate this zoom change.
77 return SetZoomLevelByClient(zoom_level
, NULL
);
80 bool ZoomController::SetZoomLevelByClient(
82 const scoped_refptr
<const ZoomRequestClient
>& client
) {
83 content::NavigationEntry
* entry
=
84 web_contents()->GetController().GetLastCommittedEntry();
85 // Cannot zoom in disabled mode. Also, don't allow changing zoom level on
86 // a crashed tab, an error page or an interstitial page.
87 if (zoom_mode_
== ZOOM_MODE_DISABLED
||
88 !web_contents()->GetRenderViewHost()->IsRenderViewLive())
91 // Store client data so the |client| can be attributed when the zoom
92 // change completes. We expect that by the time this function returns that
93 // any observers that require this information will have requested it.
94 last_client_
= client
;
96 // Do not actually rescale the page in manual mode.
97 if (zoom_mode_
== ZOOM_MODE_MANUAL
) {
98 // If the zoom level hasn't changed, early out to avoid sending an event.
99 if (zoom_level_
== zoom_level
)
102 double old_zoom_level
= zoom_level_
;
103 zoom_level_
= zoom_level
;
105 // TODO(wjmaclean) Do we care about filling in host/scheme here?
106 content::HostZoomMap::ZoomLevelChange change
;
107 change
.mode
= content::HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM
;
108 change
.zoom_level
= zoom_level
;
109 ZoomEventManager::GetForBrowserContext(browser_context_
)
110 ->OnZoomLevelChanged(change
);
112 ZoomChangedEventData
zoom_change_data(web_contents(), old_zoom_level
,
113 zoom_level_
, zoom_mode_
,
115 FOR_EACH_OBSERVER(ZoomObserver
, observers_
,
116 OnZoomChanged(zoom_change_data
));
122 content::HostZoomMap
* zoom_map
=
123 content::HostZoomMap::GetForWebContents(web_contents());
125 DCHECK(!event_data_
);
126 event_data_
.reset(new ZoomChangedEventData(web_contents(), GetZoomLevel(),
127 zoom_level
, zoom_mode_
,
128 false /* can_show_bubble */));
129 int render_process_id
= web_contents()->GetRenderProcessHost()->GetID();
130 int render_view_id
= web_contents()->GetRenderViewHost()->GetRoutingID();
131 if (zoom_mode_
== ZOOM_MODE_ISOLATED
||
132 zoom_map
->UsesTemporaryZoomLevel(render_process_id
, render_view_id
)) {
133 zoom_map
->SetTemporaryZoomLevel(render_process_id
, render_view_id
,
141 net::GetHostOrSpecFromURL(content::HostZoomMap::GetURLFromEntry(entry
));
142 zoom_map
->SetZoomLevelForHost(host
, zoom_level
);
145 DCHECK(!event_data_
);
150 void ZoomController::SetZoomMode(ZoomMode new_mode
) {
151 if (new_mode
== zoom_mode_
)
154 content::HostZoomMap
* zoom_map
=
155 content::HostZoomMap::GetForWebContents(web_contents());
157 int render_process_id
= web_contents()->GetRenderProcessHost()->GetID();
158 int render_view_id
= web_contents()->GetRenderViewHost()->GetRoutingID();
159 double original_zoom_level
= GetZoomLevel();
161 DCHECK(!event_data_
);
162 event_data_
.reset(new ZoomChangedEventData(
163 web_contents(), original_zoom_level
, original_zoom_level
, new_mode
,
164 new_mode
!= ZOOM_MODE_DEFAULT
));
167 case ZOOM_MODE_DEFAULT
: {
168 content::NavigationEntry
* entry
=
169 web_contents()->GetController().GetLastCommittedEntry();
172 GURL url
= content::HostZoomMap::GetURLFromEntry(entry
);
173 std::string host
= net::GetHostOrSpecFromURL(url
);
175 if (zoom_map
->HasZoomLevel(url
.scheme(), host
)) {
176 // If there are other tabs with the same origin, then set this tab's
177 // zoom level to match theirs. The temporary zoom level will be
178 // cleared below, but this call will make sure this tab re-draws at
179 // the correct zoom level.
180 double origin_zoom_level
=
181 zoom_map
->GetZoomLevelForHostAndScheme(url
.scheme(), host
);
182 event_data_
->new_zoom_level
= origin_zoom_level
;
183 zoom_map
->SetTemporaryZoomLevel(render_process_id
, render_view_id
,
186 // The host will need a level prior to removing the temporary level.
187 // We don't want the zoom level to change just because we entered
189 zoom_map
->SetZoomLevelForHost(host
, original_zoom_level
);
192 // Remove per-tab zoom data for this tab. No event callback expected.
193 zoom_map
->ClearTemporaryZoomLevel(render_process_id
, render_view_id
);
196 case ZOOM_MODE_ISOLATED
: {
197 // Unless the zoom mode was |ZOOM_MODE_DISABLED| before this call, the
198 // page needs an initial isolated zoom back to the same level it was at
199 // in the other mode.
200 if (zoom_mode_
!= ZOOM_MODE_DISABLED
) {
201 zoom_map
->SetTemporaryZoomLevel(render_process_id
, render_view_id
,
202 original_zoom_level
);
204 // When we don't call any HostZoomMap set functions, we send the event
206 FOR_EACH_OBSERVER(ZoomObserver
, observers_
,
207 OnZoomChanged(*event_data_
));
212 case ZOOM_MODE_MANUAL
: {
213 // Unless the zoom mode was |ZOOM_MODE_DISABLED| before this call, the
214 // page needs to be resized to the default zoom. While in manual mode,
215 // the zoom level is handled independently.
216 if (zoom_mode_
!= ZOOM_MODE_DISABLED
) {
217 zoom_map
->SetTemporaryZoomLevel(render_process_id
, render_view_id
,
218 GetDefaultZoomLevel());
219 zoom_level_
= original_zoom_level
;
221 // When we don't call any HostZoomMap set functions, we send the event
223 FOR_EACH_OBSERVER(ZoomObserver
, observers_
,
224 OnZoomChanged(*event_data_
));
229 case ZOOM_MODE_DISABLED
: {
230 // The page needs to be zoomed back to default before disabling the zoom
231 zoom_map
->SetTemporaryZoomLevel(render_process_id
, render_view_id
,
232 GetDefaultZoomLevel());
236 // Any event data we've stored should have been consumed by this point.
237 DCHECK(!event_data_
);
239 zoom_mode_
= new_mode
;
242 void ZoomController::ResetZoomModeOnNavigationIfNeeded(const GURL
& url
) {
243 if (zoom_mode_
!= ZOOM_MODE_ISOLATED
&& zoom_mode_
!= ZOOM_MODE_MANUAL
)
246 int render_process_id
= web_contents()->GetRenderProcessHost()->GetID();
247 int render_view_id
= web_contents()->GetRenderViewHost()->GetRoutingID();
248 content::HostZoomMap
* zoom_map
=
249 content::HostZoomMap::GetForWebContents(web_contents());
250 zoom_level_
= zoom_map
->GetDefaultZoomLevel();
251 double old_zoom_level
= zoom_map
->GetZoomLevel(web_contents());
252 double new_zoom_level
= zoom_map
->GetZoomLevelForHostAndScheme(
253 url
.scheme(), net::GetHostOrSpecFromURL(url
));
254 event_data_
.reset(new ZoomChangedEventData(
255 web_contents(), old_zoom_level
, new_zoom_level
, ZOOM_MODE_DEFAULT
,
256 false /* can_show_bubble */));
257 // The call to ClearTemporaryZoomLevel() doesn't generate any events from
258 // HostZoomMap, but the call to UpdateState() at the end of this function
259 // will notify our observers.
260 // Note: it's possible the render_process/view ids have disappeared (e.g.
261 // if we navigated to a new origin), but this won't cause a problem in the
263 zoom_map
->ClearTemporaryZoomLevel(render_process_id
, render_view_id
);
264 zoom_mode_
= ZOOM_MODE_DEFAULT
;
267 void ZoomController::DidNavigateMainFrame(
268 const content::LoadCommittedDetails
& details
,
269 const content::FrameNavigateParams
& params
) {
270 if (details
.entry
&& details
.entry
->GetPageType() == content::PAGE_TYPE_ERROR
)
271 content::HostZoomMap::SendErrorPageZoomLevelRefresh(web_contents());
273 if (!details
.is_in_page
)
274 ResetZoomModeOnNavigationIfNeeded(params
.url
);
276 // If the main frame's content has changed, the new page may have a different
277 // zoom level from the old one.
278 UpdateState(std::string());
279 DCHECK(!event_data_
);
282 void ZoomController::WebContentsDestroyed() {
283 // At this point we should no longer be sending any zoom events with this
288 void ZoomController::RenderFrameHostChanged(
289 content::RenderFrameHost
* old_host
,
290 content::RenderFrameHost
* new_host
) {
291 // If our associated HostZoomMap changes, update our event subscription.
292 content::HostZoomMap
* new_host_zoom_map
=
293 content::HostZoomMap::GetForWebContents(web_contents());
294 if (new_host_zoom_map
== host_zoom_map_
)
297 host_zoom_map_
= new_host_zoom_map
;
298 zoom_subscription_
= host_zoom_map_
->AddZoomLevelChangedCallback(
299 base::Bind(&ZoomController::OnZoomLevelChanged
, base::Unretained(this)));
302 void ZoomController::OnZoomLevelChanged(
303 const content::HostZoomMap::ZoomLevelChange
& change
) {
304 UpdateState(change
.host
);
307 void ZoomController::UpdateState(const std::string
& host
) {
308 // If |host| is empty, all observers should be updated.
310 // Use the navigation entry's URL instead of the WebContents' so virtual
311 // URLs work (e.g. chrome://settings). http://crbug.com/153950
312 content::NavigationEntry
* entry
=
313 web_contents()->GetController().GetLastCommittedEntry();
315 host
!= net::GetHostOrSpecFromURL(
316 content::HostZoomMap::GetURLFromEntry(entry
))) {
322 // For state changes initiated within the ZoomController, information about
323 // the change should be sent.
324 ZoomChangedEventData zoom_change_data
= *event_data_
;
326 // The zoom bubble should not be shown for zoom changes where the host
328 zoom_change_data
.can_show_bubble
= can_show_bubble_
&& !host
.empty();
329 FOR_EACH_OBSERVER(ZoomObserver
, observers_
,
330 OnZoomChanged(zoom_change_data
));
332 // TODO(wjmaclean) Should we consider having HostZoomMap send both old and
333 // new zoom levels here?
334 double zoom_level
= GetZoomLevel();
335 // We never show a zoom bubble for an event we didn't generate.
336 ZoomChangedEventData
zoom_change_data(web_contents(), zoom_level
,
337 zoom_level
, zoom_mode_
,
338 false /* can_show_bubble */);
339 FOR_EACH_OBSERVER(ZoomObserver
, observers_
,
340 OnZoomChanged(zoom_change_data
));
344 } // namespace ui_zoom