blob: c36aa4e4d647934a5d5036b82fc364ed422ce4ec [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]a0421732011-02-23 03:55:407#include "content/browser/host_zoom_map.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]c38831a12011-10-28 12:44:4914#include "content/public/browser/browser_thread.h"
[email protected]ad50def52011-10-19 23:17:0715#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1616#include "content/public/browser/notification_types.h"
[email protected]0f083402011-11-22 02:59:0117#include "content/public/common/page_zoom.h"
[email protected]9d797f32010-04-23 07:17:5418#include "googleurl/src/gurl.h"
19#include "net/base/net_util.h"
[email protected]8bd0fe62011-01-17 06:44:3720#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]b75b8292010-10-01 07:28:2521
[email protected]631bb742011-11-02 11:29:3922using content::BrowserThread;
[email protected]b75b8292010-10-01 07:28:2523using WebKit::WebView;
[email protected]40bd6582009-12-04 23:49:5124
[email protected]d407cc02011-09-13 16:01:4625HostZoomMap::HostZoomMap()
26 : default_zoom_level_(0.0),
27 original_(this) {
28 Init();
29}
30
31HostZoomMap::HostZoomMap(HostZoomMap* original)
32 : default_zoom_level_(0.0),
33 original_(original) {
34 DCHECK(original);
35 Init();
36 base::AutoLock auto_lock(original->lock_);
37 for (HostZoomLevels::const_iterator i(original->host_zoom_levels_.begin());
38 i != original->host_zoom_levels_.end(); ++i) {
39 host_zoom_levels_[i->first] = i->second;
40 }
41}
42
43void HostZoomMap::Init() {
[email protected]b75b8292010-10-01 07:28:2544 registrar_.Add(
[email protected]432115822011-07-10 15:52:2745 this, content::NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW,
[email protected]ad50def52011-10-19 23:17:0746 content::NotificationService::AllSources());
[email protected]40bd6582009-12-04 23:49:5147}
48
[email protected]0008e3f2011-06-22 21:02:3649double HostZoomMap::GetZoomLevel(const std::string& host) const {
[email protected]20305ec2011-01-21 04:55:5250 base::AutoLock auto_lock(lock_);
[email protected]40bd6582009-12-04 23:49:5151 HostZoomLevels::const_iterator i(host_zoom_levels_.find(host));
[email protected]d0b8d092010-10-25 04:05:1752 return (i == host_zoom_levels_.end()) ? default_zoom_level_ : i->second;
[email protected]40bd6582009-12-04 23:49:5153}
54
[email protected]13ffa32e2011-05-27 16:37:0155void HostZoomMap::SetZoomLevel(std::string host, double level) {
[email protected]f8b3ef82010-10-11 02:45:5256 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3ef4bc632010-04-09 04:25:3157
[email protected]40bd6582009-12-04 23:49:5158 {
[email protected]20305ec2011-01-21 04:55:5259 base::AutoLock auto_lock(lock_);
[email protected]0f083402011-11-22 02:59:0160
61 if (content::ZoomValuesEqual(level, default_zoom_level_))
[email protected]40bd6582009-12-04 23:49:5162 host_zoom_levels_.erase(host);
63 else
64 host_zoom_levels_[host] = level;
65 }
66
[email protected]ad50def52011-10-19 23:17:0767 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:2768 content::NOTIFICATION_ZOOM_LEVEL_CHANGED,
[email protected]6c2381d2011-10-19 02:52:5369 content::Source<HostZoomMap>(this),
70 content::Details<const std::string>(&host));
[email protected]40bd6582009-12-04 23:49:5171}
72
[email protected]b75b8292010-10-01 07:28:2573double HostZoomMap::GetTemporaryZoomLevel(int render_process_id,
74 int render_view_id) const {
[email protected]20305ec2011-01-21 04:55:5275 base::AutoLock auto_lock(lock_);
[email protected]b75b8292010-10-01 07:28:2576 for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) {
77 if (temporary_zoom_levels_[i].render_process_id == render_process_id &&
78 temporary_zoom_levels_[i].render_view_id == render_view_id) {
79 return temporary_zoom_levels_[i].zoom_level;
80 }
81 }
82 return 0;
83}
84
85void HostZoomMap::SetTemporaryZoomLevel(int render_process_id,
86 int render_view_id,
87 double level) {
[email protected]f8b3ef82010-10-11 02:45:5288 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b75b8292010-10-01 07:28:2589
90 {
[email protected]20305ec2011-01-21 04:55:5291 base::AutoLock auto_lock(lock_);
[email protected]b75b8292010-10-01 07:28:2592 size_t i;
93 for (i = 0; i < temporary_zoom_levels_.size(); ++i) {
94 if (temporary_zoom_levels_[i].render_process_id == render_process_id &&
95 temporary_zoom_levels_[i].render_view_id == render_view_id) {
96 if (level) {
97 temporary_zoom_levels_[i].zoom_level = level;
98 } else {
99 temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i);
100 }
101 break;
102 }
103 }
104
105 if (level && i == temporary_zoom_levels_.size()) {
106 TemporaryZoomLevel temp;
107 temp.render_process_id = render_process_id;
108 temp.render_view_id = render_view_id;
109 temp.zoom_level = level;
110 temporary_zoom_levels_.push_back(temp);
111 }
112 }
113
[email protected]a5f2695d2011-05-24 07:45:16114 std::string host;
[email protected]ad50def52011-10-19 23:17:07115 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27116 content::NOTIFICATION_ZOOM_LEVEL_CHANGED,
[email protected]6c2381d2011-10-19 02:52:53117 content::Source<HostZoomMap>(this),
118 content::Details<const std::string>(&host));
[email protected]b75b8292010-10-01 07:28:25119}
120
[email protected]3ef4bc632010-04-09 04:25:31121void HostZoomMap::Observe(
[email protected]432115822011-07-10 15:52:27122 int type,
[email protected]6c2381d2011-10-19 02:52:53123 const content::NotificationSource& source,
124 const content::NotificationDetails& details) {
[email protected]f8b3ef82010-10-11 02:45:52125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3ef4bc632010-04-09 04:25:31126
[email protected]432115822011-07-10 15:52:27127 switch (type) {
128 case content::NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW: {
[email protected]20305ec2011-01-21 04:55:52129 base::AutoLock auto_lock(lock_);
[email protected]6c2381d2011-10-19 02:52:53130 int render_view_id =
131 content::Source<RenderViewHost>(source)->routing_id();
132 int render_process_id =
[email protected]f3b1a082011-11-18 00:34:30133 content::Source<RenderViewHost>(source)->process()->GetID();
[email protected]3ef4bc632010-04-09 04:25:31134
[email protected]b75b8292010-10-01 07:28:25135 for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) {
136 if (temporary_zoom_levels_[i].render_process_id == render_process_id &&
137 temporary_zoom_levels_[i].render_view_id == render_view_id) {
138 temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i);
139 break;
140 }
141 }
142 break;
[email protected]3ef4bc632010-04-09 04:25:31143 }
[email protected]b75b8292010-10-01 07:28:25144 default:
145 NOTREACHED() << "Unexpected preference observed.";
[email protected]3ef4bc632010-04-09 04:25:31146 }
[email protected]779c3362010-01-30 01:09:33147}
148
[email protected]40bd6582009-12-04 23:49:51149HostZoomMap::~HostZoomMap() {
[email protected]40bd6582009-12-04 23:49:51150}