blob: 72bd65bcdbc9dfc5e249ec93cf27cd4cbec69ffa [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]b75b8292010-10-01 07:28:255#include <cmath>
6
[email protected]5c9250872012-01-30 17:24:057#include "content/browser/host_zoom_map_impl.h"
[email protected]40bd6582009-12-04 23:49:518
[email protected]864b1362010-08-19 03:49:389#include "base/string_piece.h"
[email protected]40bd6582009-12-04 23:49:5110#include "base/utf_string_conversions.h"
[email protected]d407cc02011-09-13 16:01:4611#include "base/values.h"
[email protected]f3b1a082011-11-18 00:34:3012#include "content/browser/renderer_host/render_process_host_impl.h"
[email protected]b3c41c0b2012-03-06 15:48:3213#include "content/browser/renderer_host/render_view_host_impl.h"
[email protected]4e2a25a2012-01-27 00:42:0814#include "content/common/view_messages.h"
15#include "content/public/browser/browser_context.h"
[email protected]c38831a12011-10-28 12:44:4916#include "content/public/browser/browser_thread.h"
[email protected]ad50def52011-10-19 23:17:0717#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1618#include "content/public/browser/notification_types.h"
[email protected]5fe3713a2012-02-22 08:31:5619#include "content/public/browser/resource_context.h"
[email protected]0f083402011-11-22 02:59:0120#include "content/public/common/page_zoom.h"
[email protected]9d797f32010-04-23 07:17:5421#include "googleurl/src/gurl.h"
22#include "net/base/net_util.h"
[email protected]8bd0fe62011-01-17 06:44:3723#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]b75b8292010-10-01 07:28:2524
25using WebKit::WebView;
[email protected]4e2a25a2012-01-27 00:42:0826using content::BrowserThread;
27using content::RenderProcessHost;
[email protected]eaabba22012-03-07 15:02:1128using content::RenderViewHost;
[email protected]40bd6582009-12-04 23:49:5129
[email protected]5fe3713a2012-02-22 08:31:5630static const char* kHostZoomMapKeyName = "content_host_zoom_map";
31
[email protected]5c9250872012-01-30 17:24:0532namespace content {
33
[email protected]5fe3713a2012-02-22 08:31:5634HostZoomMap* HostZoomMap::GetForBrowserContext(BrowserContext* context) {
35 HostZoomMapImpl* rv = static_cast<HostZoomMapImpl*>(
36 context->GetUserData(kHostZoomMapKeyName));
37 if (!rv) {
38 rv = new HostZoomMapImpl();
39 context->SetUserData(kHostZoomMapKeyName, rv);
40 }
41 return rv;
[email protected]5c9250872012-01-30 17:24:0542}
43
44} // namespace content
45
46HostZoomMapImpl::HostZoomMapImpl()
[email protected]4e2a25a2012-01-27 00:42:0847 : default_zoom_level_(0.0) {
[email protected]b75b8292010-10-01 07:28:2548 registrar_.Add(
[email protected]432115822011-07-10 15:52:2749 this, content::NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW,
[email protected]ad50def52011-10-19 23:17:0750 content::NotificationService::AllSources());
[email protected]40bd6582009-12-04 23:49:5151}
52
[email protected]5c9250872012-01-30 17:24:0553void HostZoomMapImpl::CopyFrom(HostZoomMap* copy_interface) {
[email protected]4e2a25a2012-01-27 00:42:0854 // This can only be called on the UI thread to avoid deadlocks, otherwise
55 // UI: a.CopyFrom(b);
56 // IO: b.CopyFrom(a);
57 // can deadlock.
58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]5c9250872012-01-30 17:24:0559 HostZoomMapImpl* copy = static_cast<HostZoomMapImpl*>(copy_interface);
[email protected]4e2a25a2012-01-27 00:42:0860 base::AutoLock auto_lock(lock_);
61 base::AutoLock copy_auto_lock(copy->lock_);
62 for (HostZoomLevels::const_iterator i(copy->host_zoom_levels_.begin());
63 i != copy->host_zoom_levels_.end(); ++i) {
64 host_zoom_levels_[i->first] = i->second;
65 }
66}
67
[email protected]5c9250872012-01-30 17:24:0568double HostZoomMapImpl::GetZoomLevel(const std::string& host) const {
[email protected]20305ec2011-01-21 04:55:5269 base::AutoLock auto_lock(lock_);
[email protected]40bd6582009-12-04 23:49:5170 HostZoomLevels::const_iterator i(host_zoom_levels_.find(host));
[email protected]d0b8d092010-10-25 04:05:1771 return (i == host_zoom_levels_.end()) ? default_zoom_level_ : i->second;
[email protected]40bd6582009-12-04 23:49:5172}
73
[email protected]4bceb6a2012-05-15 20:28:5074void HostZoomMapImpl::SetZoomLevel(const std::string& host, double level) {
[email protected]f8b3ef82010-10-11 02:45:5275 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3ef4bc632010-04-09 04:25:3176
[email protected]40bd6582009-12-04 23:49:5177 {
[email protected]20305ec2011-01-21 04:55:5278 base::AutoLock auto_lock(lock_);
[email protected]0f083402011-11-22 02:59:0179
80 if (content::ZoomValuesEqual(level, default_zoom_level_))
[email protected]40bd6582009-12-04 23:49:5181 host_zoom_levels_.erase(host);
82 else
83 host_zoom_levels_[host] = level;
84 }
85
[email protected]4e2a25a2012-01-27 00:42:0886 // Notify renderers from this browser context.
87 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
88 !i.IsAtEnd(); i.Advance()) {
89 RenderProcessHost* render_process_host = i.GetCurrentValue();
[email protected]5fe3713a2012-02-22 08:31:5690 if (HostZoomMap::GetForBrowserContext(
91 render_process_host->GetBrowserContext()) == this) {
[email protected]4e2a25a2012-01-27 00:42:0892 render_process_host->Send(
93 new ViewMsg_SetZoomLevelForCurrentURL(host, level));
94 }
95 }
96
[email protected]ad50def52011-10-19 23:17:0797 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:2798 content::NOTIFICATION_ZOOM_LEVEL_CHANGED,
[email protected]6c2381d2011-10-19 02:52:5399 content::Source<HostZoomMap>(this),
100 content::Details<const std::string>(&host));
[email protected]40bd6582009-12-04 23:49:51101}
102
[email protected]5c9250872012-01-30 17:24:05103double HostZoomMapImpl::GetDefaultZoomLevel() const {
104 return default_zoom_level_;
105}
106
107void HostZoomMapImpl::SetDefaultZoomLevel(double level) {
108 default_zoom_level_ = level;
109}
110
111double HostZoomMapImpl::GetTemporaryZoomLevel(int render_process_id,
112 int render_view_id) const {
[email protected]20305ec2011-01-21 04:55:52113 base::AutoLock auto_lock(lock_);
[email protected]b75b8292010-10-01 07:28:25114 for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) {
115 if (temporary_zoom_levels_[i].render_process_id == render_process_id &&
116 temporary_zoom_levels_[i].render_view_id == render_view_id) {
117 return temporary_zoom_levels_[i].zoom_level;
118 }
119 }
120 return 0;
121}
122
[email protected]5c9250872012-01-30 17:24:05123void HostZoomMapImpl::SetTemporaryZoomLevel(int render_process_id,
124 int render_view_id,
125 double level) {
[email protected]f8b3ef82010-10-11 02:45:52126 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b75b8292010-10-01 07:28:25127
128 {
[email protected]20305ec2011-01-21 04:55:52129 base::AutoLock auto_lock(lock_);
[email protected]b75b8292010-10-01 07:28:25130 size_t i;
131 for (i = 0; i < temporary_zoom_levels_.size(); ++i) {
132 if (temporary_zoom_levels_[i].render_process_id == render_process_id &&
133 temporary_zoom_levels_[i].render_view_id == render_view_id) {
134 if (level) {
135 temporary_zoom_levels_[i].zoom_level = level;
136 } else {
137 temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i);
138 }
139 break;
140 }
141 }
142
143 if (level && i == temporary_zoom_levels_.size()) {
144 TemporaryZoomLevel temp;
145 temp.render_process_id = render_process_id;
146 temp.render_view_id = render_view_id;
147 temp.zoom_level = level;
148 temporary_zoom_levels_.push_back(temp);
149 }
150 }
151
[email protected]a5f2695d2011-05-24 07:45:16152 std::string host;
[email protected]ad50def52011-10-19 23:17:07153 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27154 content::NOTIFICATION_ZOOM_LEVEL_CHANGED,
[email protected]6c2381d2011-10-19 02:52:53155 content::Source<HostZoomMap>(this),
156 content::Details<const std::string>(&host));
[email protected]b75b8292010-10-01 07:28:25157}
158
[email protected]5c9250872012-01-30 17:24:05159void HostZoomMapImpl::Observe(
[email protected]432115822011-07-10 15:52:27160 int type,
[email protected]6c2381d2011-10-19 02:52:53161 const content::NotificationSource& source,
162 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:27163 switch (type) {
164 case content::NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW: {
[email protected]20305ec2011-01-21 04:55:52165 base::AutoLock auto_lock(lock_);
[email protected]6c2381d2011-10-19 02:52:53166 int render_view_id =
[email protected]9f76c1e2012-03-05 15:15:58167 content::Source<RenderViewHost>(source)->GetRoutingID();
[email protected]6c2381d2011-10-19 02:52:53168 int render_process_id =
[email protected]9f76c1e2012-03-05 15:15:58169 content::Source<RenderViewHost>(source)->GetProcess()->GetID();
[email protected]3ef4bc632010-04-09 04:25:31170
[email protected]b75b8292010-10-01 07:28:25171 for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) {
172 if (temporary_zoom_levels_[i].render_process_id == render_process_id &&
173 temporary_zoom_levels_[i].render_view_id == render_view_id) {
174 temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i);
175 break;
176 }
177 }
178 break;
[email protected]3ef4bc632010-04-09 04:25:31179 }
[email protected]b75b8292010-10-01 07:28:25180 default:
181 NOTREACHED() << "Unexpected preference observed.";
[email protected]3ef4bc632010-04-09 04:25:31182 }
[email protected]779c3362010-01-30 01:09:33183}
184
[email protected]5c9250872012-01-30 17:24:05185HostZoomMapImpl::~HostZoomMapImpl() {
[email protected]40bd6582009-12-04 23:49:51186}