blob: 5223c960742d1b922208e31da7795f432b46505c [file] [log] [blame]
[email protected]7f070d42011-03-09 20:25:321// Copyright (c) 2011 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]a0421732011-02-23 03:55:4013#include "content/browser/renderer_host/render_view_host.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]0f083402011-11-22 02:59:0119#include "content/public/common/page_zoom.h"
[email protected]9d797f32010-04-23 07:17:5420#include "googleurl/src/gurl.h"
21#include "net/base/net_util.h"
[email protected]8bd0fe62011-01-17 06:44:3722#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]b75b8292010-10-01 07:28:2523
24using WebKit::WebView;
[email protected]4e2a25a2012-01-27 00:42:0825using content::BrowserThread;
26using content::RenderProcessHost;
[email protected]40bd6582009-12-04 23:49:5127
[email protected]5c9250872012-01-30 17:24:0528namespace content {
29
30HostZoomMap* HostZoomMap::Create() {
31 return new HostZoomMapImpl();
32}
33
34} // namespace content
35
36HostZoomMapImpl::HostZoomMapImpl()
[email protected]4e2a25a2012-01-27 00:42:0837 : default_zoom_level_(0.0) {
[email protected]b75b8292010-10-01 07:28:2538 registrar_.Add(
[email protected]432115822011-07-10 15:52:2739 this, content::NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW,
[email protected]ad50def52011-10-19 23:17:0740 content::NotificationService::AllSources());
[email protected]40bd6582009-12-04 23:49:5141}
42
[email protected]5c9250872012-01-30 17:24:0543void HostZoomMapImpl::CopyFrom(HostZoomMap* copy_interface) {
[email protected]4e2a25a2012-01-27 00:42:0844 // This can only be called on the UI thread to avoid deadlocks, otherwise
45 // UI: a.CopyFrom(b);
46 // IO: b.CopyFrom(a);
47 // can deadlock.
48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]5c9250872012-01-30 17:24:0549 HostZoomMapImpl* copy = static_cast<HostZoomMapImpl*>(copy_interface);
[email protected]4e2a25a2012-01-27 00:42:0850 base::AutoLock auto_lock(lock_);
51 base::AutoLock copy_auto_lock(copy->lock_);
52 for (HostZoomLevels::const_iterator i(copy->host_zoom_levels_.begin());
53 i != copy->host_zoom_levels_.end(); ++i) {
54 host_zoom_levels_[i->first] = i->second;
55 }
56}
57
[email protected]5c9250872012-01-30 17:24:0558double HostZoomMapImpl::GetZoomLevel(const std::string& host) const {
[email protected]20305ec2011-01-21 04:55:5259 base::AutoLock auto_lock(lock_);
[email protected]40bd6582009-12-04 23:49:5160 HostZoomLevels::const_iterator i(host_zoom_levels_.find(host));
[email protected]d0b8d092010-10-25 04:05:1761 return (i == host_zoom_levels_.end()) ? default_zoom_level_ : i->second;
[email protected]40bd6582009-12-04 23:49:5162}
63
[email protected]5c9250872012-01-30 17:24:0564void HostZoomMapImpl::SetZoomLevel(std::string host, double level) {
[email protected]f8b3ef82010-10-11 02:45:5265 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3ef4bc632010-04-09 04:25:3166
[email protected]40bd6582009-12-04 23:49:5167 {
[email protected]20305ec2011-01-21 04:55:5268 base::AutoLock auto_lock(lock_);
[email protected]0f083402011-11-22 02:59:0169
70 if (content::ZoomValuesEqual(level, default_zoom_level_))
[email protected]40bd6582009-12-04 23:49:5171 host_zoom_levels_.erase(host);
72 else
73 host_zoom_levels_[host] = level;
74 }
75
[email protected]4e2a25a2012-01-27 00:42:0876 // Notify renderers from this browser context.
77 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
78 !i.IsAtEnd(); i.Advance()) {
79 RenderProcessHost* render_process_host = i.GetCurrentValue();
80 if (render_process_host->GetBrowserContext()->GetHostZoomMap() == this) {
81 render_process_host->Send(
82 new ViewMsg_SetZoomLevelForCurrentURL(host, level));
83 }
84 }
85
[email protected]ad50def52011-10-19 23:17:0786 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:2787 content::NOTIFICATION_ZOOM_LEVEL_CHANGED,
[email protected]6c2381d2011-10-19 02:52:5388 content::Source<HostZoomMap>(this),
89 content::Details<const std::string>(&host));
[email protected]40bd6582009-12-04 23:49:5190}
91
[email protected]5c9250872012-01-30 17:24:0592double HostZoomMapImpl::GetDefaultZoomLevel() const {
93 return default_zoom_level_;
94}
95
96void HostZoomMapImpl::SetDefaultZoomLevel(double level) {
97 default_zoom_level_ = level;
98}
99
100double HostZoomMapImpl::GetTemporaryZoomLevel(int render_process_id,
101 int render_view_id) const {
[email protected]20305ec2011-01-21 04:55:52102 base::AutoLock auto_lock(lock_);
[email protected]b75b8292010-10-01 07:28:25103 for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) {
104 if (temporary_zoom_levels_[i].render_process_id == render_process_id &&
105 temporary_zoom_levels_[i].render_view_id == render_view_id) {
106 return temporary_zoom_levels_[i].zoom_level;
107 }
108 }
109 return 0;
110}
111
[email protected]5c9250872012-01-30 17:24:05112void HostZoomMapImpl::SetTemporaryZoomLevel(int render_process_id,
113 int render_view_id,
114 double level) {
[email protected]f8b3ef82010-10-11 02:45:52115 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b75b8292010-10-01 07:28:25116
117 {
[email protected]20305ec2011-01-21 04:55:52118 base::AutoLock auto_lock(lock_);
[email protected]b75b8292010-10-01 07:28:25119 size_t i;
120 for (i = 0; i < temporary_zoom_levels_.size(); ++i) {
121 if (temporary_zoom_levels_[i].render_process_id == render_process_id &&
122 temporary_zoom_levels_[i].render_view_id == render_view_id) {
123 if (level) {
124 temporary_zoom_levels_[i].zoom_level = level;
125 } else {
126 temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i);
127 }
128 break;
129 }
130 }
131
132 if (level && i == temporary_zoom_levels_.size()) {
133 TemporaryZoomLevel temp;
134 temp.render_process_id = render_process_id;
135 temp.render_view_id = render_view_id;
136 temp.zoom_level = level;
137 temporary_zoom_levels_.push_back(temp);
138 }
139 }
140
[email protected]a5f2695d2011-05-24 07:45:16141 std::string host;
[email protected]ad50def52011-10-19 23:17:07142 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27143 content::NOTIFICATION_ZOOM_LEVEL_CHANGED,
[email protected]6c2381d2011-10-19 02:52:53144 content::Source<HostZoomMap>(this),
145 content::Details<const std::string>(&host));
[email protected]b75b8292010-10-01 07:28:25146}
147
[email protected]5c9250872012-01-30 17:24:05148void HostZoomMapImpl::Observe(
[email protected]432115822011-07-10 15:52:27149 int type,
[email protected]6c2381d2011-10-19 02:52:53150 const content::NotificationSource& source,
151 const content::NotificationDetails& details) {
[email protected]f8b3ef82010-10-11 02:45:52152 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3ef4bc632010-04-09 04:25:31153
[email protected]432115822011-07-10 15:52:27154 switch (type) {
155 case content::NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW: {
[email protected]20305ec2011-01-21 04:55:52156 base::AutoLock auto_lock(lock_);
[email protected]6c2381d2011-10-19 02:52:53157 int render_view_id =
158 content::Source<RenderViewHost>(source)->routing_id();
159 int render_process_id =
[email protected]f3b1a082011-11-18 00:34:30160 content::Source<RenderViewHost>(source)->process()->GetID();
[email protected]3ef4bc632010-04-09 04:25:31161
[email protected]b75b8292010-10-01 07:28:25162 for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) {
163 if (temporary_zoom_levels_[i].render_process_id == render_process_id &&
164 temporary_zoom_levels_[i].render_view_id == render_view_id) {
165 temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i);
166 break;
167 }
168 }
169 break;
[email protected]3ef4bc632010-04-09 04:25:31170 }
[email protected]b75b8292010-10-01 07:28:25171 default:
172 NOTREACHED() << "Unexpected preference observed.";
[email protected]3ef4bc632010-04-09 04:25:31173 }
[email protected]779c3362010-01-30 01:09:33174}
175
[email protected]5c9250872012-01-30 17:24:05176HostZoomMapImpl::~HostZoomMapImpl() {
[email protected]40bd6582009-12-04 23:49:51177}