blob: 19b64f1fc3057d420394ee85166acb21254e26d8 [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]1625ffd2011-03-01 17:51:5012#include "content/browser/browser_thread.h"
[email protected]a0421732011-02-23 03:55:4013#include "content/browser/renderer_host/render_process_host.h"
14#include "content/browser/renderer_host/render_view_host.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]9d797f32010-04-23 07:17:5417#include "googleurl/src/gurl.h"
18#include "net/base/net_util.h"
[email protected]8bd0fe62011-01-17 06:44:3719#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]b75b8292010-10-01 07:28:2520
21using WebKit::WebView;
[email protected]40bd6582009-12-04 23:49:5122
[email protected]d407cc02011-09-13 16:01:4623HostZoomMap::HostZoomMap()
24 : default_zoom_level_(0.0),
25 original_(this) {
26 Init();
27}
28
29HostZoomMap::HostZoomMap(HostZoomMap* original)
30 : default_zoom_level_(0.0),
31 original_(original) {
32 DCHECK(original);
33 Init();
34 base::AutoLock auto_lock(original->lock_);
35 for (HostZoomLevels::const_iterator i(original->host_zoom_levels_.begin());
36 i != original->host_zoom_levels_.end(); ++i) {
37 host_zoom_levels_[i->first] = i->second;
38 }
39}
40
41void HostZoomMap::Init() {
[email protected]b75b8292010-10-01 07:28:2542 registrar_.Add(
[email protected]432115822011-07-10 15:52:2743 this, content::NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW,
[email protected]ad50def52011-10-19 23:17:0744 content::NotificationService::AllSources());
[email protected]40bd6582009-12-04 23:49:5145}
46
[email protected]0008e3f2011-06-22 21:02:3647double HostZoomMap::GetZoomLevel(const std::string& host) const {
[email protected]20305ec2011-01-21 04:55:5248 base::AutoLock auto_lock(lock_);
[email protected]40bd6582009-12-04 23:49:5149 HostZoomLevels::const_iterator i(host_zoom_levels_.find(host));
[email protected]d0b8d092010-10-25 04:05:1750 return (i == host_zoom_levels_.end()) ? default_zoom_level_ : i->second;
[email protected]40bd6582009-12-04 23:49:5151}
52
[email protected]13ffa32e2011-05-27 16:37:0153void HostZoomMap::SetZoomLevel(std::string host, double level) {
[email protected]f8b3ef82010-10-11 02:45:5254 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3ef4bc632010-04-09 04:25:3155
[email protected]40bd6582009-12-04 23:49:5156 {
[email protected]20305ec2011-01-21 04:55:5257 base::AutoLock auto_lock(lock_);
[email protected]d0b8d092010-10-25 04:05:1758 if (level == default_zoom_level_)
[email protected]40bd6582009-12-04 23:49:5159 host_zoom_levels_.erase(host);
60 else
61 host_zoom_levels_[host] = level;
62 }
63
[email protected]ad50def52011-10-19 23:17:0764 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:2765 content::NOTIFICATION_ZOOM_LEVEL_CHANGED,
[email protected]6c2381d2011-10-19 02:52:5366 content::Source<HostZoomMap>(this),
67 content::Details<const std::string>(&host));
[email protected]40bd6582009-12-04 23:49:5168}
69
[email protected]b75b8292010-10-01 07:28:2570double HostZoomMap::GetTemporaryZoomLevel(int render_process_id,
71 int render_view_id) const {
[email protected]20305ec2011-01-21 04:55:5272 base::AutoLock auto_lock(lock_);
[email protected]b75b8292010-10-01 07:28:2573 for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) {
74 if (temporary_zoom_levels_[i].render_process_id == render_process_id &&
75 temporary_zoom_levels_[i].render_view_id == render_view_id) {
76 return temporary_zoom_levels_[i].zoom_level;
77 }
78 }
79 return 0;
80}
81
82void HostZoomMap::SetTemporaryZoomLevel(int render_process_id,
83 int render_view_id,
84 double level) {
[email protected]f8b3ef82010-10-11 02:45:5285 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b75b8292010-10-01 07:28:2586
87 {
[email protected]20305ec2011-01-21 04:55:5288 base::AutoLock auto_lock(lock_);
[email protected]b75b8292010-10-01 07:28:2589 size_t i;
90 for (i = 0; i < temporary_zoom_levels_.size(); ++i) {
91 if (temporary_zoom_levels_[i].render_process_id == render_process_id &&
92 temporary_zoom_levels_[i].render_view_id == render_view_id) {
93 if (level) {
94 temporary_zoom_levels_[i].zoom_level = level;
95 } else {
96 temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i);
97 }
98 break;
99 }
100 }
101
102 if (level && i == temporary_zoom_levels_.size()) {
103 TemporaryZoomLevel temp;
104 temp.render_process_id = render_process_id;
105 temp.render_view_id = render_view_id;
106 temp.zoom_level = level;
107 temporary_zoom_levels_.push_back(temp);
108 }
109 }
110
[email protected]a5f2695d2011-05-24 07:45:16111 std::string host;
[email protected]ad50def52011-10-19 23:17:07112 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27113 content::NOTIFICATION_ZOOM_LEVEL_CHANGED,
[email protected]6c2381d2011-10-19 02:52:53114 content::Source<HostZoomMap>(this),
115 content::Details<const std::string>(&host));
[email protected]b75b8292010-10-01 07:28:25116}
117
[email protected]3ef4bc632010-04-09 04:25:31118void HostZoomMap::Observe(
[email protected]432115822011-07-10 15:52:27119 int type,
[email protected]6c2381d2011-10-19 02:52:53120 const content::NotificationSource& source,
121 const content::NotificationDetails& details) {
[email protected]f8b3ef82010-10-11 02:45:52122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3ef4bc632010-04-09 04:25:31123
[email protected]432115822011-07-10 15:52:27124 switch (type) {
125 case content::NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW: {
[email protected]20305ec2011-01-21 04:55:52126 base::AutoLock auto_lock(lock_);
[email protected]6c2381d2011-10-19 02:52:53127 int render_view_id =
128 content::Source<RenderViewHost>(source)->routing_id();
129 int render_process_id =
130 content::Source<RenderViewHost>(source)->process()->id();
[email protected]3ef4bc632010-04-09 04:25:31131
[email protected]b75b8292010-10-01 07:28:25132 for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) {
133 if (temporary_zoom_levels_[i].render_process_id == render_process_id &&
134 temporary_zoom_levels_[i].render_view_id == render_view_id) {
135 temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i);
136 break;
137 }
138 }
139 break;
[email protected]3ef4bc632010-04-09 04:25:31140 }
[email protected]b75b8292010-10-01 07:28:25141 default:
142 NOTREACHED() << "Unexpected preference observed.";
[email protected]3ef4bc632010-04-09 04:25:31143 }
[email protected]779c3362010-01-30 01:09:33144}
145
[email protected]40bd6582009-12-04 23:49:51146HostZoomMap::~HostZoomMap() {
[email protected]40bd6582009-12-04 23:49:51147}