ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / components / ui / zoom / zoom_controller.cc
blob6df05d9e78c6abe316c6203a419f7a4e2b6acdc7
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 ZoomController::ZoomController(content::WebContents* web_contents)
24 : content::WebContentsObserver(web_contents),
25 can_show_bubble_(true),
26 zoom_mode_(ZOOM_MODE_DEFAULT),
27 zoom_level_(1.0),
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
65 ? zoom_level_
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(
81 double zoom_level,
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())
89 return false;
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)
100 return true;
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_,
114 can_show_bubble_);
115 FOR_EACH_OBSERVER(ZoomObserver, observers_,
116 OnZoomChanged(zoom_change_data));
118 last_client_ = NULL;
119 return true;
122 content::HostZoomMap* zoom_map =
123 content::HostZoomMap::GetForWebContents(web_contents());
124 DCHECK(zoom_map);
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,
134 zoom_level);
135 } else {
136 if (!entry) {
137 last_client_ = NULL;
138 return false;
140 std::string host =
141 net::GetHostOrSpecFromURL(content::HostZoomMap::GetURLFromEntry(entry));
142 zoom_map->SetZoomLevelForHost(host, zoom_level);
145 DCHECK(!event_data_);
146 last_client_ = NULL;
147 return true;
150 void ZoomController::SetZoomMode(ZoomMode new_mode) {
151 if (new_mode == zoom_mode_)
152 return;
154 content::HostZoomMap* zoom_map =
155 content::HostZoomMap::GetForWebContents(web_contents());
156 DCHECK(zoom_map);
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));
166 switch (new_mode) {
167 case ZOOM_MODE_DEFAULT: {
168 content::NavigationEntry* entry =
169 web_contents()->GetController().GetLastCommittedEntry();
171 if (entry) {
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,
184 origin_zoom_level);
185 } else {
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
188 // default mode.
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);
194 break;
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);
203 } else {
204 // When we don't call any HostZoomMap set functions, we send the event
205 // manually.
206 FOR_EACH_OBSERVER(ZoomObserver, observers_,
207 OnZoomChanged(*event_data_));
208 event_data_.reset();
210 break;
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;
220 } else {
221 // When we don't call any HostZoomMap set functions, we send the event
222 // manually.
223 FOR_EACH_OBSERVER(ZoomObserver, observers_,
224 OnZoomChanged(*event_data_));
225 event_data_.reset();
227 break;
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());
233 break;
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)
244 return;
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
262 // call below.
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
284 // WebContents.
285 observers_.Clear();
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_)
295 return;
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.
309 if (!host.empty()) {
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();
314 if (!entry ||
315 host != net::GetHostOrSpecFromURL(
316 content::HostZoomMap::GetURLFromEntry(entry))) {
317 return;
321 if (event_data_) {
322 // For state changes initiated within the ZoomController, information about
323 // the change should be sent.
324 ZoomChangedEventData zoom_change_data = *event_data_;
325 event_data_.reset();
326 // The zoom bubble should not be shown for zoom changes where the host
327 // is empty.
328 zoom_change_data.can_show_bubble = can_show_bubble_ && !host.empty();
329 FOR_EACH_OBSERVER(ZoomObserver, observers_,
330 OnZoomChanged(zoom_change_data));
331 } else {
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