blob: 66a03d66f8d1ea556644710f4b11c87ccc606878 [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]40bd6582009-12-04 23:49:5126
[email protected]5fe3713a2012-02-22 08:31:5627static const char* kHostZoomMapKeyName = "content_host_zoom_map";
28
[email protected]5c9250872012-01-30 17:24:0529namespace content {
30
[email protected]5fe3713a2012-02-22 08:31:5631HostZoomMap* HostZoomMap::GetForBrowserContext(BrowserContext* context) {
32 HostZoomMapImpl* rv = static_cast<HostZoomMapImpl*>(
33 context->GetUserData(kHostZoomMapKeyName));
34 if (!rv) {
35 rv = new HostZoomMapImpl();
36 context->SetUserData(kHostZoomMapKeyName, rv);
37 }
38 return rv;
[email protected]5c9250872012-01-30 17:24:0539}
40
[email protected]5c9250872012-01-30 17:24:0541HostZoomMapImpl::HostZoomMapImpl()
[email protected]4e2a25a2012-01-27 00:42:0842 : default_zoom_level_(0.0) {
[email protected]b75b8292010-10-01 07:28:2543 registrar_.Add(
[email protected]46488322012-10-30 03:22:2044 this, NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW,
45 NotificationService::AllSources());
[email protected]40bd6582009-12-04 23:49:5146}
47
[email protected]5c9250872012-01-30 17:24:0548void HostZoomMapImpl::CopyFrom(HostZoomMap* copy_interface) {
[email protected]4e2a25a2012-01-27 00:42:0849 // This can only be called on the UI thread to avoid deadlocks, otherwise
50 // UI: a.CopyFrom(b);
51 // IO: b.CopyFrom(a);
52 // can deadlock.
53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]5c9250872012-01-30 17:24:0554 HostZoomMapImpl* copy = static_cast<HostZoomMapImpl*>(copy_interface);
[email protected]4e2a25a2012-01-27 00:42:0855 base::AutoLock auto_lock(lock_);
56 base::AutoLock copy_auto_lock(copy->lock_);
57 for (HostZoomLevels::const_iterator i(copy->host_zoom_levels_.begin());
58 i != copy->host_zoom_levels_.end(); ++i) {
59 host_zoom_levels_[i->first] = i->second;
60 }
61}
62
[email protected]5c9250872012-01-30 17:24:0563double HostZoomMapImpl::GetZoomLevel(const std::string& host) const {
[email protected]20305ec2011-01-21 04:55:5264 base::AutoLock auto_lock(lock_);
[email protected]40bd6582009-12-04 23:49:5165 HostZoomLevels::const_iterator i(host_zoom_levels_.find(host));
[email protected]d0b8d092010-10-25 04:05:1766 return (i == host_zoom_levels_.end()) ? default_zoom_level_ : i->second;
[email protected]40bd6582009-12-04 23:49:5167}
68
[email protected]4bceb6a2012-05-15 20:28:5069void HostZoomMapImpl::SetZoomLevel(const std::string& host, double level) {
[email protected]f8b3ef82010-10-11 02:45:5270 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3ef4bc632010-04-09 04:25:3171
[email protected]40bd6582009-12-04 23:49:5172 {
[email protected]20305ec2011-01-21 04:55:5273 base::AutoLock auto_lock(lock_);
[email protected]0f083402011-11-22 02:59:0174
[email protected]46488322012-10-30 03:22:2075 if (ZoomValuesEqual(level, default_zoom_level_))
[email protected]40bd6582009-12-04 23:49:5176 host_zoom_levels_.erase(host);
77 else
78 host_zoom_levels_[host] = level;
79 }
80
[email protected]4e2a25a2012-01-27 00:42:0881 // Notify renderers from this browser context.
82 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
83 !i.IsAtEnd(); i.Advance()) {
84 RenderProcessHost* render_process_host = i.GetCurrentValue();
[email protected]5fe3713a2012-02-22 08:31:5685 if (HostZoomMap::GetForBrowserContext(
86 render_process_host->GetBrowserContext()) == this) {
[email protected]4e2a25a2012-01-27 00:42:0887 render_process_host->Send(
88 new ViewMsg_SetZoomLevelForCurrentURL(host, level));
89 }
90 }
91
[email protected]46488322012-10-30 03:22:2092 NotificationService::current()->Notify(
93 NOTIFICATION_ZOOM_LEVEL_CHANGED,
94 Source<HostZoomMap>(this),
95 Details<const std::string>(&host));
[email protected]40bd6582009-12-04 23:49:5196}
97
[email protected]5c9250872012-01-30 17:24:0598double HostZoomMapImpl::GetDefaultZoomLevel() const {
99 return default_zoom_level_;
100}
101
102void HostZoomMapImpl::SetDefaultZoomLevel(double level) {
103 default_zoom_level_ = level;
104}
105
106double HostZoomMapImpl::GetTemporaryZoomLevel(int render_process_id,
107 int render_view_id) const {
[email protected]20305ec2011-01-21 04:55:52108 base::AutoLock auto_lock(lock_);
[email protected]b75b8292010-10-01 07:28:25109 for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) {
110 if (temporary_zoom_levels_[i].render_process_id == render_process_id &&
111 temporary_zoom_levels_[i].render_view_id == render_view_id) {
112 return temporary_zoom_levels_[i].zoom_level;
113 }
114 }
115 return 0;
116}
117
[email protected]5c9250872012-01-30 17:24:05118void HostZoomMapImpl::SetTemporaryZoomLevel(int render_process_id,
119 int render_view_id,
120 double level) {
[email protected]f8b3ef82010-10-11 02:45:52121 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b75b8292010-10-01 07:28:25122
123 {
[email protected]20305ec2011-01-21 04:55:52124 base::AutoLock auto_lock(lock_);
[email protected]b75b8292010-10-01 07:28:25125 size_t i;
126 for (i = 0; i < temporary_zoom_levels_.size(); ++i) {
127 if (temporary_zoom_levels_[i].render_process_id == render_process_id &&
128 temporary_zoom_levels_[i].render_view_id == render_view_id) {
129 if (level) {
130 temporary_zoom_levels_[i].zoom_level = level;
131 } else {
132 temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i);
133 }
134 break;
135 }
136 }
137
138 if (level && i == temporary_zoom_levels_.size()) {
139 TemporaryZoomLevel temp;
140 temp.render_process_id = render_process_id;
141 temp.render_view_id = render_view_id;
142 temp.zoom_level = level;
143 temporary_zoom_levels_.push_back(temp);
144 }
145 }
146
[email protected]a5f2695d2011-05-24 07:45:16147 std::string host;
[email protected]46488322012-10-30 03:22:20148 NotificationService::current()->Notify(
149 NOTIFICATION_ZOOM_LEVEL_CHANGED,
150 Source<HostZoomMap>(this),
151 Details<const std::string>(&host));
[email protected]b75b8292010-10-01 07:28:25152}
153
[email protected]46488322012-10-30 03:22:20154void HostZoomMapImpl::Observe(int type,
155 const NotificationSource& source,
156 const NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:27157 switch (type) {
[email protected]46488322012-10-30 03:22:20158 case NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW: {
[email protected]20305ec2011-01-21 04:55:52159 base::AutoLock auto_lock(lock_);
[email protected]46488322012-10-30 03:22:20160 int render_view_id = Source<RenderViewHost>(source)->GetRoutingID();
[email protected]6c2381d2011-10-19 02:52:53161 int render_process_id =
[email protected]46488322012-10-30 03:22:20162 Source<RenderViewHost>(source)->GetProcess()->GetID();
[email protected]3ef4bc632010-04-09 04:25:31163
[email protected]b75b8292010-10-01 07:28:25164 for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) {
165 if (temporary_zoom_levels_[i].render_process_id == render_process_id &&
166 temporary_zoom_levels_[i].render_view_id == render_view_id) {
167 temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i);
168 break;
169 }
170 }
171 break;
[email protected]3ef4bc632010-04-09 04:25:31172 }
[email protected]b75b8292010-10-01 07:28:25173 default:
174 NOTREACHED() << "Unexpected preference observed.";
[email protected]3ef4bc632010-04-09 04:25:31175 }
[email protected]779c3362010-01-30 01:09:33176}
177
[email protected]5c9250872012-01-30 17:24:05178HostZoomMapImpl::~HostZoomMapImpl() {
[email protected]40bd6582009-12-04 23:49:51179}
[email protected]46488322012-10-30 03:22:20180
181} // namespace content