Revert 99759 - Add two new valgrind suppressions.
[chromium-blink-merge.git] / content / browser / host_zoom_map.cc
blob64e96f48a148f923c960de27e6cdd0317c46001a
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <cmath>
7 #include "content/browser/host_zoom_map.h"
9 #include "base/string_piece.h"
10 #include "base/utf_string_conversions.h"
11 #include "content/browser/browser_thread.h"
12 #include "content/browser/renderer_host/render_process_host.h"
13 #include "content/browser/renderer_host/render_view_host.h"
14 #include "content/common/notification_service.h"
15 #include "googleurl/src/gurl.h"
16 #include "net/base/net_util.h"
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
19 using WebKit::WebView;
21 HostZoomMap::HostZoomMap() : default_zoom_level_(0.0) {
22 registrar_.Add(
23 this, content::NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW,
24 NotificationService::AllSources());
27 double HostZoomMap::GetZoomLevel(const std::string& host) const {
28 base::AutoLock auto_lock(lock_);
29 HostZoomLevels::const_iterator i(host_zoom_levels_.find(host));
30 return (i == host_zoom_levels_.end()) ? default_zoom_level_ : i->second;
33 void HostZoomMap::SetZoomLevel(std::string host, double level) {
34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
37 base::AutoLock auto_lock(lock_);
38 if (level == default_zoom_level_)
39 host_zoom_levels_.erase(host);
40 else
41 host_zoom_levels_[host] = level;
44 NotificationService::current()->Notify(
45 content::NOTIFICATION_ZOOM_LEVEL_CHANGED,
46 Source<HostZoomMap>(this),
47 Details<const std::string>(&host));
50 double HostZoomMap::GetTemporaryZoomLevel(int render_process_id,
51 int render_view_id) const {
52 base::AutoLock auto_lock(lock_);
53 for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) {
54 if (temporary_zoom_levels_[i].render_process_id == render_process_id &&
55 temporary_zoom_levels_[i].render_view_id == render_view_id) {
56 return temporary_zoom_levels_[i].zoom_level;
59 return 0;
62 void HostZoomMap::SetTemporaryZoomLevel(int render_process_id,
63 int render_view_id,
64 double level) {
65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
68 base::AutoLock auto_lock(lock_);
69 size_t i;
70 for (i = 0; i < temporary_zoom_levels_.size(); ++i) {
71 if (temporary_zoom_levels_[i].render_process_id == render_process_id &&
72 temporary_zoom_levels_[i].render_view_id == render_view_id) {
73 if (level) {
74 temporary_zoom_levels_[i].zoom_level = level;
75 } else {
76 temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i);
78 break;
82 if (level && i == temporary_zoom_levels_.size()) {
83 TemporaryZoomLevel temp;
84 temp.render_process_id = render_process_id;
85 temp.render_view_id = render_view_id;
86 temp.zoom_level = level;
87 temporary_zoom_levels_.push_back(temp);
91 std::string host;
92 NotificationService::current()->Notify(
93 content::NOTIFICATION_ZOOM_LEVEL_CHANGED,
94 Source<HostZoomMap>(this),
95 Details<const std::string>(&host));
99 void HostZoomMap::Observe(
100 int type,
101 const NotificationSource& source,
102 const NotificationDetails& details) {
103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
105 switch (type) {
106 case content::NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW: {
107 base::AutoLock auto_lock(lock_);
108 int render_view_id = Source<RenderViewHost>(source)->routing_id();
109 int render_process_id = Source<RenderViewHost>(source)->process()->id();
111 for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) {
112 if (temporary_zoom_levels_[i].render_process_id == render_process_id &&
113 temporary_zoom_levels_[i].render_view_id == render_view_id) {
114 temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i);
115 break;
118 break;
120 default:
121 NOTREACHED() << "Unexpected preference observed.";
125 HostZoomMap::~HostZoomMap() {