blob: 81f8182924d58d4d7700b3e072a914cd3c572d4d [file] [log] [blame]
[email protected]9f76c1e2012-03-05 15:15:581// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]40bd6582009-12-04 23:49:512// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]5c9250872012-01-30 17:24:055#include "content/browser/host_zoom_map_impl.h"
[email protected]40bd6582009-12-04 23:49:516
[email protected]fce823222014-05-30 16:24:307#include <algorithm>
[email protected]b9e7c479f2013-04-12 04:33:248#include <cmath>
9
10#include "base/strings/string_piece.h"
[email protected]74ebfb12013-06-07 20:48:0011#include "base/strings/utf_string_conversions.h"
[email protected]d407cc02011-09-13 16:01:4612#include "base/values.h"
[email protected]fce823222014-05-30 16:24:3013#include "content/browser/frame_host/navigation_entry_impl.h"
[email protected]f3b1a082011-11-18 00:34:3014#include "content/browser/renderer_host/render_process_host_impl.h"
[email protected]b3c41c0b2012-03-06 15:48:3215#include "content/browser/renderer_host/render_view_host_impl.h"
[email protected]fce823222014-05-30 16:24:3016#include "content/browser/web_contents/web_contents_impl.h"
[email protected]4e2a25a2012-01-27 00:42:0817#include "content/common/view_messages.h"
18#include "content/public/browser/browser_context.h"
[email protected]c38831a12011-10-28 12:44:4919#include "content/public/browser/browser_thread.h"
[email protected]ad50def52011-10-19 23:17:0720#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1621#include "content/public/browser/notification_types.h"
[email protected]5fe3713a2012-02-22 08:31:5622#include "content/public/browser/resource_context.h"
[email protected]0f083402011-11-22 02:59:0123#include "content/public/common/page_zoom.h"
wjmacleande29ed52014-10-28 21:09:0624#include "content/public/common/url_constants.h"
[email protected]9d797f32010-04-23 07:17:5425#include "net/base/net_util.h"
[email protected]40bd6582009-12-04 23:49:5126
[email protected]5c9250872012-01-30 17:24:0527namespace content {
28
[email protected]d42bf472014-06-14 01:49:3829namespace {
30
[email protected]e2a8b0e2014-08-21 08:49:5331const char kHostZoomMapKeyName[] = "content_host_zoom_map";
32
[email protected]d42bf472014-06-14 01:49:3833std::string GetHostFromProcessView(int render_process_id, int render_view_id) {
34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
35 RenderViewHost* render_view_host =
36 RenderViewHost::FromID(render_process_id, render_view_id);
37 if (!render_view_host)
38 return std::string();
39
40 WebContents* web_contents = WebContents::FromRenderViewHost(render_view_host);
41
42 NavigationEntry* entry =
43 web_contents->GetController().GetLastCommittedEntry();
44 if (!entry)
45 return std::string();
46
wjmacleande29ed52014-10-28 21:09:0647 return net::GetHostOrSpecFromURL(HostZoomMap::GetURLFromEntry(entry));
[email protected]d42bf472014-06-14 01:49:3848}
49
50} // namespace
51
wjmacleande29ed52014-10-28 21:09:0652GURL HostZoomMap::GetURLFromEntry(const NavigationEntry* entry) {
53 switch (entry->GetPageType()) {
54 case PAGE_TYPE_ERROR:
55 return GURL(kUnreachableWebDataURL);
56 // TODO(wjmaclean): In future, give interstitial pages special treatment as
57 // well.
58 default:
59 return entry->GetURL();
60 }
61}
62
wjmacleanf9b6ec82014-08-27 14:33:2463HostZoomMap* HostZoomMap::GetDefaultForBrowserContext(BrowserContext* context) {
[email protected]5fe3713a2012-02-22 08:31:5664 HostZoomMapImpl* rv = static_cast<HostZoomMapImpl*>(
65 context->GetUserData(kHostZoomMapKeyName));
66 if (!rv) {
67 rv = new HostZoomMapImpl();
68 context->SetUserData(kHostZoomMapKeyName, rv);
69 }
70 return rv;
[email protected]5c9250872012-01-30 17:24:0571}
72
[email protected]fce823222014-05-30 16:24:3073// Helper function for setting/getting zoom levels for WebContents without
74// having to import HostZoomMapImpl everywhere.
75double HostZoomMap::GetZoomLevel(const WebContents* web_contents) {
wjmacleanf9b6ec82014-08-27 14:33:2476 HostZoomMapImpl* host_zoom_map =
77 static_cast<HostZoomMapImpl*>(HostZoomMap::GetDefaultForBrowserContext(
78 web_contents->GetBrowserContext()));
[email protected]fce823222014-05-30 16:24:3079 return host_zoom_map->GetZoomLevelForWebContents(
80 *static_cast<const WebContentsImpl*>(web_contents));
81}
82
83void HostZoomMap::SetZoomLevel(const WebContents* web_contents, double level) {
wjmacleanf9b6ec82014-08-27 14:33:2484 HostZoomMapImpl* host_zoom_map =
85 static_cast<HostZoomMapImpl*>(HostZoomMap::GetDefaultForBrowserContext(
86 web_contents->GetBrowserContext()));
[email protected]fce823222014-05-30 16:24:3087 host_zoom_map->SetZoomLevelForWebContents(
88 *static_cast<const WebContentsImpl*>(web_contents), level);
89}
90
wjmacleande29ed52014-10-28 21:09:0691void HostZoomMap::SendErrorPageZoomLevelRefresh(
92 const WebContents* web_contents) {
93 HostZoomMapImpl* host_zoom_map =
94 static_cast<HostZoomMapImpl*>(HostZoomMap::GetDefaultForBrowserContext(
95 web_contents->GetBrowserContext()));
96 host_zoom_map->SendErrorPageZoomLevelRefresh();
97}
98
[email protected]5c9250872012-01-30 17:24:0599HostZoomMapImpl::HostZoomMapImpl()
[email protected]4e2a25a2012-01-27 00:42:08100 : default_zoom_level_(0.0) {
[email protected]b75b8292010-10-01 07:28:25101 registrar_.Add(
[email protected]46488322012-10-30 03:22:20102 this, NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW,
103 NotificationService::AllSources());
[email protected]40bd6582009-12-04 23:49:51104}
105
[email protected]5c9250872012-01-30 17:24:05106void HostZoomMapImpl::CopyFrom(HostZoomMap* copy_interface) {
[email protected]4e2a25a2012-01-27 00:42:08107 // This can only be called on the UI thread to avoid deadlocks, otherwise
108 // UI: a.CopyFrom(b);
109 // IO: b.CopyFrom(a);
110 // can deadlock.
111 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]5c9250872012-01-30 17:24:05112 HostZoomMapImpl* copy = static_cast<HostZoomMapImpl*>(copy_interface);
[email protected]4e2a25a2012-01-27 00:42:08113 base::AutoLock auto_lock(lock_);
114 base::AutoLock copy_auto_lock(copy->lock_);
[email protected]367c5c1d2013-03-11 18:59:02115 host_zoom_levels_.
116 insert(copy->host_zoom_levels_.begin(), copy->host_zoom_levels_.end());
117 for (SchemeHostZoomLevels::const_iterator i(copy->
118 scheme_host_zoom_levels_.begin());
119 i != copy->scheme_host_zoom_levels_.end(); ++i) {
120 scheme_host_zoom_levels_[i->first] = HostZoomLevels();
121 scheme_host_zoom_levels_[i->first].
122 insert(i->second.begin(), i->second.end());
[email protected]4e2a25a2012-01-27 00:42:08123 }
[email protected]19d26692013-01-12 19:56:33124 default_zoom_level_ = copy->default_zoom_level_;
[email protected]4e2a25a2012-01-27 00:42:08125}
126
[email protected]367c5c1d2013-03-11 18:59:02127double HostZoomMapImpl::GetZoomLevelForHost(const std::string& host) const {
[email protected]20305ec2011-01-21 04:55:52128 base::AutoLock auto_lock(lock_);
[email protected]40bd6582009-12-04 23:49:51129 HostZoomLevels::const_iterator i(host_zoom_levels_.find(host));
[email protected]d0b8d092010-10-25 04:05:17130 return (i == host_zoom_levels_.end()) ? default_zoom_level_ : i->second;
[email protected]40bd6582009-12-04 23:49:51131}
132
[email protected]d42bf472014-06-14 01:49:38133bool HostZoomMapImpl::HasZoomLevel(const std::string& scheme,
134 const std::string& host) const {
135 base::AutoLock auto_lock(lock_);
136
137 SchemeHostZoomLevels::const_iterator scheme_iterator(
138 scheme_host_zoom_levels_.find(scheme));
139
140 const HostZoomLevels& zoom_levels =
141 (scheme_iterator != scheme_host_zoom_levels_.end())
142 ? scheme_iterator->second
143 : host_zoom_levels_;
144
145 HostZoomLevels::const_iterator i(zoom_levels.find(host));
146 return i != zoom_levels.end();
147}
148
[email protected]367c5c1d2013-03-11 18:59:02149double HostZoomMapImpl::GetZoomLevelForHostAndScheme(
150 const std::string& scheme,
151 const std::string& host) const {
152 {
153 base::AutoLock auto_lock(lock_);
154 SchemeHostZoomLevels::const_iterator scheme_iterator(
155 scheme_host_zoom_levels_.find(scheme));
156 if (scheme_iterator != scheme_host_zoom_levels_.end()) {
157 HostZoomLevels::const_iterator i(scheme_iterator->second.find(host));
158 if (i != scheme_iterator->second.end())
159 return i->second;
160 }
161 }
162 return GetZoomLevelForHost(host);
163}
164
[email protected]0f374052014-03-18 20:37:22165HostZoomMap::ZoomLevelVector HostZoomMapImpl::GetAllZoomLevels() const {
166 HostZoomMap::ZoomLevelVector result;
167 {
168 base::AutoLock auto_lock(lock_);
169 result.reserve(host_zoom_levels_.size() + scheme_host_zoom_levels_.size());
170 for (HostZoomLevels::const_iterator i = host_zoom_levels_.begin();
171 i != host_zoom_levels_.end();
172 ++i) {
173 ZoomLevelChange change = {HostZoomMap::ZOOM_CHANGED_FOR_HOST,
174 i->first, // host
175 std::string(), // scheme
176 i->second // zoom level
177 };
178 result.push_back(change);
179 }
180 for (SchemeHostZoomLevels::const_iterator i =
181 scheme_host_zoom_levels_.begin();
182 i != scheme_host_zoom_levels_.end();
183 ++i) {
184 const std::string& scheme = i->first;
185 const HostZoomLevels& host_zoom_levels = i->second;
186 for (HostZoomLevels::const_iterator j = host_zoom_levels.begin();
187 j != host_zoom_levels.end();
188 ++j) {
189 ZoomLevelChange change = {HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST,
190 j->first, // host
191 scheme, // scheme
192 j->second // zoom level
193 };
194 result.push_back(change);
195 }
196 }
197 }
198 return result;
199}
200
[email protected]367c5c1d2013-03-11 18:59:02201void HostZoomMapImpl::SetZoomLevelForHost(const std::string& host,
202 double level) {
[email protected]f8b3ef82010-10-11 02:45:52203 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3ef4bc632010-04-09 04:25:31204
[email protected]40bd6582009-12-04 23:49:51205 {
[email protected]20305ec2011-01-21 04:55:52206 base::AutoLock auto_lock(lock_);
[email protected]0f083402011-11-22 02:59:01207
[email protected]46488322012-10-30 03:22:20208 if (ZoomValuesEqual(level, default_zoom_level_))
[email protected]40bd6582009-12-04 23:49:51209 host_zoom_levels_.erase(host);
210 else
211 host_zoom_levels_[host] = level;
212 }
213
[email protected]d42bf472014-06-14 01:49:38214 // TODO(wjmaclean) Should we use a GURL here? crbug.com/384486
215 SendZoomLevelChange(std::string(), host, level);
216
[email protected]367c5c1d2013-03-11 18:59:02217 HostZoomMap::ZoomLevelChange change;
218 change.mode = HostZoomMap::ZOOM_CHANGED_FOR_HOST;
219 change.host = host;
220 change.zoom_level = level;
221
[email protected]117832812013-10-02 07:06:02222 zoom_level_changed_callbacks_.Notify(change);
[email protected]367c5c1d2013-03-11 18:59:02223}
224
225void HostZoomMapImpl::SetZoomLevelForHostAndScheme(const std::string& scheme,
226 const std::string& host,
227 double level) {
228 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
229 {
230 base::AutoLock auto_lock(lock_);
231 scheme_host_zoom_levels_[scheme][host] = level;
232 }
233
[email protected]d42bf472014-06-14 01:49:38234 SendZoomLevelChange(scheme, host, level);
[email protected]4e2a25a2012-01-27 00:42:08235
[email protected]367c5c1d2013-03-11 18:59:02236 HostZoomMap::ZoomLevelChange change;
237 change.mode = HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST;
238 change.host = host;
239 change.scheme = scheme;
240 change.zoom_level = level;
241
[email protected]117832812013-10-02 07:06:02242 zoom_level_changed_callbacks_.Notify(change);
[email protected]40bd6582009-12-04 23:49:51243}
244
[email protected]5c9250872012-01-30 17:24:05245double HostZoomMapImpl::GetDefaultZoomLevel() const {
246 return default_zoom_level_;
247}
248
249void HostZoomMapImpl::SetDefaultZoomLevel(double level) {
250 default_zoom_level_ = level;
251}
252
[email protected]117832812013-10-02 07:06:02253scoped_ptr<HostZoomMap::Subscription>
254HostZoomMapImpl::AddZoomLevelChangedCallback(
[email protected]89c9aca2013-02-07 15:08:42255 const ZoomLevelChangedCallback& callback) {
[email protected]117832812013-10-02 07:06:02256 return zoom_level_changed_callbacks_.Add(callback);
[email protected]89c9aca2013-02-07 15:08:42257}
258
[email protected]fce823222014-05-30 16:24:30259double HostZoomMapImpl::GetZoomLevelForWebContents(
260 const WebContentsImpl& web_contents_impl) const {
261 int render_process_id = web_contents_impl.GetRenderProcessHost()->GetID();
262 int routing_id = web_contents_impl.GetRenderViewHost()->GetRoutingID();
263
264 if (UsesTemporaryZoomLevel(render_process_id, routing_id))
265 return GetTemporaryZoomLevel(render_process_id, routing_id);
266
[email protected]acda0ef32014-06-05 23:13:30267 // Get the url from the navigation controller directly, as calling
268 // WebContentsImpl::GetLastCommittedURL() may give us a virtual url that
269 // is different than is stored in the map.
[email protected]fce823222014-05-30 16:24:30270 GURL url;
271 NavigationEntry* entry =
272 web_contents_impl.GetController().GetLastCommittedEntry();
[email protected]acda0ef32014-06-05 23:13:30273 // It is possible for a WebContent's zoom level to be queried before
274 // a navigation has occurred.
[email protected]fce823222014-05-30 16:24:30275 if (entry)
wjmacleande29ed52014-10-28 21:09:06276 url = GetURLFromEntry(entry);
[email protected]fce823222014-05-30 16:24:30277 return GetZoomLevelForHostAndScheme(url.scheme(),
278 net::GetHostOrSpecFromURL(url));
279}
280
281void HostZoomMapImpl::SetZoomLevelForWebContents(
282 const WebContentsImpl& web_contents_impl,
283 double level) {
284 int render_process_id = web_contents_impl.GetRenderProcessHost()->GetID();
285 int render_view_id = web_contents_impl.GetRenderViewHost()->GetRoutingID();
286 if (UsesTemporaryZoomLevel(render_process_id, render_view_id)) {
[email protected]fce823222014-05-30 16:24:30287 SetTemporaryZoomLevel(render_process_id, render_view_id, level);
288 } else {
[email protected]acda0ef32014-06-05 23:13:30289 // Get the url from the navigation controller directly, as calling
290 // WebContentsImpl::GetLastCommittedURL() may give us a virtual url that
291 // is different than what the render view is using. If the two don't match,
292 // the attempt to set the zoom will fail.
[email protected]acda0ef32014-06-05 23:13:30293 NavigationEntry* entry =
294 web_contents_impl.GetController().GetLastCommittedEntry();
[email protected]f3b3e5e2014-06-10 15:47:41295 // Tests may invoke this function with a null entry, but we don't
296 // want to save zoom levels in this case.
297 if (!entry)
298 return;
299
wjmacleande29ed52014-10-28 21:09:06300 GURL url = GetURLFromEntry(entry);
[email protected]acda0ef32014-06-05 23:13:30301 SetZoomLevelForHost(net::GetHostOrSpecFromURL(url), level);
[email protected]fce823222014-05-30 16:24:30302 }
303}
304
305void HostZoomMapImpl::SetZoomLevelForView(int render_process_id,
306 int render_view_id,
307 double level,
308 const std::string& host) {
309 if (UsesTemporaryZoomLevel(render_process_id, render_view_id))
310 SetTemporaryZoomLevel(render_process_id, render_view_id, level);
311 else
312 SetZoomLevelForHost(host, level);
313}
314
315bool HostZoomMapImpl::UsesTemporaryZoomLevel(int render_process_id,
316 int render_view_id) const {
[email protected]d42bf472014-06-14 01:49:38317 RenderViewKey key(render_process_id, render_view_id);
[email protected]fce823222014-05-30 16:24:30318
319 base::AutoLock auto_lock(lock_);
[email protected]d42bf472014-06-14 01:49:38320 return ContainsKey(temporary_zoom_levels_, key);
[email protected]fce823222014-05-30 16:24:30321}
322
[email protected]5c9250872012-01-30 17:24:05323double HostZoomMapImpl::GetTemporaryZoomLevel(int render_process_id,
324 int render_view_id) const {
[email protected]20305ec2011-01-21 04:55:52325 base::AutoLock auto_lock(lock_);
[email protected]d42bf472014-06-14 01:49:38326 RenderViewKey key(render_process_id, render_view_id);
327 if (!ContainsKey(temporary_zoom_levels_, key))
328 return 0;
[email protected]fce823222014-05-30 16:24:30329
[email protected]d42bf472014-06-14 01:49:38330 return temporary_zoom_levels_.find(key)->second;
[email protected]b75b8292010-10-01 07:28:25331}
332
[email protected]5c9250872012-01-30 17:24:05333void HostZoomMapImpl::SetTemporaryZoomLevel(int render_process_id,
334 int render_view_id,
335 double level) {
[email protected]f8b3ef82010-10-11 02:45:52336 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b75b8292010-10-01 07:28:25337
338 {
[email protected]d42bf472014-06-14 01:49:38339 RenderViewKey key(render_process_id, render_view_id);
[email protected]20305ec2011-01-21 04:55:52340 base::AutoLock auto_lock(lock_);
[email protected]d42bf472014-06-14 01:49:38341 temporary_zoom_levels_[key] = level;
[email protected]b75b8292010-10-01 07:28:25342 }
343
[email protected]d42bf472014-06-14 01:49:38344 RenderViewHost* host =
345 RenderViewHost::FromID(render_process_id, render_view_id);
346 host->Send(new ViewMsg_SetZoomLevelForView(render_view_id, true, level));
347
[email protected]367c5c1d2013-03-11 18:59:02348 HostZoomMap::ZoomLevelChange change;
349 change.mode = HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM;
[email protected]d42bf472014-06-14 01:49:38350 change.host = GetHostFromProcessView(render_process_id, render_view_id);
[email protected]367c5c1d2013-03-11 18:59:02351 change.zoom_level = level;
352
[email protected]117832812013-10-02 07:06:02353 zoom_level_changed_callbacks_.Notify(change);
[email protected]b75b8292010-10-01 07:28:25354}
355
[email protected]46488322012-10-30 03:22:20356void HostZoomMapImpl::Observe(int type,
357 const NotificationSource& source,
358 const NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:27359 switch (type) {
[email protected]46488322012-10-30 03:22:20360 case NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW: {
[email protected]46488322012-10-30 03:22:20361 int render_view_id = Source<RenderViewHost>(source)->GetRoutingID();
[email protected]6c2381d2011-10-19 02:52:53362 int render_process_id =
[email protected]46488322012-10-30 03:22:20363 Source<RenderViewHost>(source)->GetProcess()->GetID();
[email protected]d42bf472014-06-14 01:49:38364 ClearTemporaryZoomLevel(render_process_id, render_view_id);
[email protected]b75b8292010-10-01 07:28:25365 break;
[email protected]3ef4bc632010-04-09 04:25:31366 }
[email protected]b75b8292010-10-01 07:28:25367 default:
368 NOTREACHED() << "Unexpected preference observed.";
[email protected]3ef4bc632010-04-09 04:25:31369 }
[email protected]779c3362010-01-30 01:09:33370}
371
[email protected]d42bf472014-06-14 01:49:38372void HostZoomMapImpl::ClearTemporaryZoomLevel(int render_process_id,
373 int render_view_id) {
374 {
375 base::AutoLock auto_lock(lock_);
376 RenderViewKey key(render_process_id, render_view_id);
377 TemporaryZoomLevels::iterator it = temporary_zoom_levels_.find(key);
378 if (it == temporary_zoom_levels_.end())
379 return;
380 temporary_zoom_levels_.erase(it);
381 }
382 RenderViewHost* host =
383 RenderViewHost::FromID(render_process_id, render_view_id);
384 DCHECK(host);
385 // Send a new zoom level, host-specific if one exists.
386 host->Send(new ViewMsg_SetZoomLevelForView(
387 render_view_id,
388 false,
389 GetZoomLevelForHost(
390 GetHostFromProcessView(render_process_id, render_view_id))));
391}
392
393void HostZoomMapImpl::SendZoomLevelChange(const std::string& scheme,
394 const std::string& host,
395 double level) {
396 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
397 !i.IsAtEnd(); i.Advance()) {
398 RenderProcessHost* render_process_host = i.GetCurrentValue();
wjmacleanf9b6ec82014-08-27 14:33:24399 if (HostZoomMap::GetDefaultForBrowserContext(
[email protected]d42bf472014-06-14 01:49:38400 render_process_host->GetBrowserContext()) == this) {
401 render_process_host->Send(
402 new ViewMsg_SetZoomLevelForCurrentURL(scheme, host, level));
403 }
404 }
405}
406
wjmacleande29ed52014-10-28 21:09:06407void HostZoomMapImpl::SendErrorPageZoomLevelRefresh() {
408 GURL error_url(kUnreachableWebDataURL);
409 std::string host = net::GetHostOrSpecFromURL(error_url);
410 double error_page_zoom_level = GetZoomLevelForHost(host);
411
412 SendZoomLevelChange(std::string(), host, error_page_zoom_level);
413}
414
[email protected]5c9250872012-01-30 17:24:05415HostZoomMapImpl::~HostZoomMapImpl() {
[email protected]40bd6582009-12-04 23:49:51416}
[email protected]46488322012-10-30 03:22:20417
418} // namespace content