blob: 09d3887c1c061a9341c8750b73027366e6e930be [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]40bd6582009-12-04 23:49:5128
[email protected]5fe3713a2012-02-22 08:31:5629static const char* kHostZoomMapKeyName = "content_host_zoom_map";
30
[email protected]5c9250872012-01-30 17:24:0531namespace content {
32
[email protected]5fe3713a2012-02-22 08:31:5633HostZoomMap* HostZoomMap::GetForBrowserContext(BrowserContext* context) {
34 HostZoomMapImpl* rv = static_cast<HostZoomMapImpl*>(
35 context->GetUserData(kHostZoomMapKeyName));
36 if (!rv) {
37 rv = new HostZoomMapImpl();
38 context->SetUserData(kHostZoomMapKeyName, rv);
39 }
40 return rv;
[email protected]5c9250872012-01-30 17:24:0541}
42
43} // namespace content
44
45HostZoomMapImpl::HostZoomMapImpl()
[email protected]4e2a25a2012-01-27 00:42:0846 : default_zoom_level_(0.0) {
[email protected]b75b8292010-10-01 07:28:2547 registrar_.Add(
[email protected]432115822011-07-10 15:52:2748 this, content::NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW,
[email protected]ad50def52011-10-19 23:17:0749 content::NotificationService::AllSources());
[email protected]40bd6582009-12-04 23:49:5150}
51
[email protected]5c9250872012-01-30 17:24:0552void HostZoomMapImpl::CopyFrom(HostZoomMap* copy_interface) {
[email protected]4e2a25a2012-01-27 00:42:0853 // This can only be called on the UI thread to avoid deadlocks, otherwise
54 // UI: a.CopyFrom(b);
55 // IO: b.CopyFrom(a);
56 // can deadlock.
57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]5c9250872012-01-30 17:24:0558 HostZoomMapImpl* copy = static_cast<HostZoomMapImpl*>(copy_interface);
[email protected]4e2a25a2012-01-27 00:42:0859 base::AutoLock auto_lock(lock_);
60 base::AutoLock copy_auto_lock(copy->lock_);
61 for (HostZoomLevels::const_iterator i(copy->host_zoom_levels_.begin());
62 i != copy->host_zoom_levels_.end(); ++i) {
63 host_zoom_levels_[i->first] = i->second;
64 }
65}
66
[email protected]5c9250872012-01-30 17:24:0567double HostZoomMapImpl::GetZoomLevel(const std::string& host) const {
[email protected]20305ec2011-01-21 04:55:5268 base::AutoLock auto_lock(lock_);
[email protected]40bd6582009-12-04 23:49:5169 HostZoomLevels::const_iterator i(host_zoom_levels_.find(host));
[email protected]d0b8d092010-10-25 04:05:1770 return (i == host_zoom_levels_.end()) ? default_zoom_level_ : i->second;
[email protected]40bd6582009-12-04 23:49:5171}
72
[email protected]5c9250872012-01-30 17:24:0573void HostZoomMapImpl::SetZoomLevel(std::string host, double level) {
[email protected]f8b3ef82010-10-11 02:45:5274 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3ef4bc632010-04-09 04:25:3175
[email protected]40bd6582009-12-04 23:49:5176 {
[email protected]20305ec2011-01-21 04:55:5277 base::AutoLock auto_lock(lock_);
[email protected]0f083402011-11-22 02:59:0178
79 if (content::ZoomValuesEqual(level, default_zoom_level_))
[email protected]40bd6582009-12-04 23:49:5180 host_zoom_levels_.erase(host);
81 else
82 host_zoom_levels_[host] = level;
83 }
84
[email protected]4e2a25a2012-01-27 00:42:0885 // Notify renderers from this browser context.
86 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
87 !i.IsAtEnd(); i.Advance()) {
88 RenderProcessHost* render_process_host = i.GetCurrentValue();
[email protected]5fe3713a2012-02-22 08:31:5689 if (HostZoomMap::GetForBrowserContext(
90 render_process_host->GetBrowserContext()) == this) {
[email protected]4e2a25a2012-01-27 00:42:0891 render_process_host->Send(
92 new ViewMsg_SetZoomLevelForCurrentURL(host, level));
93 }
94 }
95
[email protected]ad50def52011-10-19 23:17:0796 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:2797 content::NOTIFICATION_ZOOM_LEVEL_CHANGED,
[email protected]6c2381d2011-10-19 02:52:5398 content::Source<HostZoomMap>(this),
99 content::Details<const std::string>(&host));
[email protected]40bd6582009-12-04 23:49:51100}
101
[email protected]5c9250872012-01-30 17:24:05102double HostZoomMapImpl::GetDefaultZoomLevel() const {
103 return default_zoom_level_;
104}
105
106void HostZoomMapImpl::SetDefaultZoomLevel(double level) {
107 default_zoom_level_ = level;
108}
109
110double HostZoomMapImpl::GetTemporaryZoomLevel(int render_process_id,
111 int render_view_id) const {
[email protected]20305ec2011-01-21 04:55:52112 base::AutoLock auto_lock(lock_);
[email protected]b75b8292010-10-01 07:28:25113 for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) {
114 if (temporary_zoom_levels_[i].render_process_id == render_process_id &&
115 temporary_zoom_levels_[i].render_view_id == render_view_id) {
116 return temporary_zoom_levels_[i].zoom_level;
117 }
118 }
119 return 0;
120}
121
[email protected]5c9250872012-01-30 17:24:05122void HostZoomMapImpl::SetTemporaryZoomLevel(int render_process_id,
123 int render_view_id,
124 double level) {
[email protected]f8b3ef82010-10-11 02:45:52125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b75b8292010-10-01 07:28:25126
127 {
[email protected]20305ec2011-01-21 04:55:52128 base::AutoLock auto_lock(lock_);
[email protected]b75b8292010-10-01 07:28:25129 size_t i;
130 for (i = 0; i < temporary_zoom_levels_.size(); ++i) {
131 if (temporary_zoom_levels_[i].render_process_id == render_process_id &&
132 temporary_zoom_levels_[i].render_view_id == render_view_id) {
133 if (level) {
134 temporary_zoom_levels_[i].zoom_level = level;
135 } else {
136 temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i);
137 }
138 break;
139 }
140 }
141
142 if (level && i == temporary_zoom_levels_.size()) {
143 TemporaryZoomLevel temp;
144 temp.render_process_id = render_process_id;
145 temp.render_view_id = render_view_id;
146 temp.zoom_level = level;
147 temporary_zoom_levels_.push_back(temp);
148 }
149 }
150
[email protected]a5f2695d2011-05-24 07:45:16151 std::string host;
[email protected]ad50def52011-10-19 23:17:07152 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27153 content::NOTIFICATION_ZOOM_LEVEL_CHANGED,
[email protected]6c2381d2011-10-19 02:52:53154 content::Source<HostZoomMap>(this),
155 content::Details<const std::string>(&host));
[email protected]b75b8292010-10-01 07:28:25156}
157
[email protected]5c9250872012-01-30 17:24:05158void HostZoomMapImpl::Observe(
[email protected]432115822011-07-10 15:52:27159 int type,
[email protected]6c2381d2011-10-19 02:52:53160 const content::NotificationSource& source,
161 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:27162 switch (type) {
163 case content::NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW: {
[email protected]20305ec2011-01-21 04:55:52164 base::AutoLock auto_lock(lock_);
[email protected]6c2381d2011-10-19 02:52:53165 int render_view_id =
[email protected]9f76c1e2012-03-05 15:15:58166 content::Source<RenderViewHost>(source)->GetRoutingID();
[email protected]6c2381d2011-10-19 02:52:53167 int render_process_id =
[email protected]9f76c1e2012-03-05 15:15:58168 content::Source<RenderViewHost>(source)->GetProcess()->GetID();
[email protected]3ef4bc632010-04-09 04:25:31169
[email protected]b75b8292010-10-01 07:28:25170 for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) {
171 if (temporary_zoom_levels_[i].render_process_id == render_process_id &&
172 temporary_zoom_levels_[i].render_view_id == render_view_id) {
173 temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i);
174 break;
175 }
176 }
177 break;
[email protected]3ef4bc632010-04-09 04:25:31178 }
[email protected]b75b8292010-10-01 07:28:25179 default:
180 NOTREACHED() << "Unexpected preference observed.";
[email protected]3ef4bc632010-04-09 04:25:31181 }
[email protected]779c3362010-01-30 01:09:33182}
183
[email protected]5c9250872012-01-30 17:24:05184HostZoomMapImpl::~HostZoomMapImpl() {
[email protected]40bd6582009-12-04 23:49:51185}