OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <cmath> | 5 #include <cmath> |
6 | 6 |
7 #include "content/browser/host_zoom_map.h" | 7 #include "content/browser/host_zoom_map_impl.h" |
8 | 8 |
9 #include "base/string_piece.h" | 9 #include "base/string_piece.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "content/browser/renderer_host/render_process_host_impl.h" | 12 #include "content/browser/renderer_host/render_process_host_impl.h" |
13 #include "content/browser/renderer_host/render_view_host.h" | 13 #include "content/browser/renderer_host/render_view_host.h" |
14 #include "content/common/view_messages.h" | 14 #include "content/common/view_messages.h" |
15 #include "content/public/browser/browser_context.h" | 15 #include "content/public/browser/browser_context.h" |
16 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
17 #include "content/public/browser/notification_service.h" | 17 #include "content/public/browser/notification_service.h" |
18 #include "content/public/browser/notification_types.h" | 18 #include "content/public/browser/notification_types.h" |
19 #include "content/public/common/page_zoom.h" | 19 #include "content/public/common/page_zoom.h" |
20 #include "googleurl/src/gurl.h" | 20 #include "googleurl/src/gurl.h" |
21 #include "net/base/net_util.h" | 21 #include "net/base/net_util.h" |
22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
23 | 23 |
24 using WebKit::WebView; | 24 using WebKit::WebView; |
25 using content::BrowserThread; | 25 using content::BrowserThread; |
26 using content::RenderProcessHost; | 26 using content::RenderProcessHost; |
27 | 27 |
28 HostZoomMap::HostZoomMap() | 28 namespace content { |
| 29 |
| 30 HostZoomMap* HostZoomMap::Create() { |
| 31 return new HostZoomMapImpl(); |
| 32 } |
| 33 |
| 34 } // namespace content |
| 35 |
| 36 HostZoomMapImpl::HostZoomMapImpl() |
29 : default_zoom_level_(0.0) { | 37 : default_zoom_level_(0.0) { |
30 registrar_.Add( | 38 registrar_.Add( |
31 this, content::NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW, | 39 this, content::NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW, |
32 content::NotificationService::AllSources()); | 40 content::NotificationService::AllSources()); |
33 } | 41 } |
34 | 42 |
35 void HostZoomMap::CopyFrom(HostZoomMap* copy) { | 43 void HostZoomMapImpl::CopyFrom(HostZoomMap* copy_interface) { |
36 // This can only be called on the UI thread to avoid deadlocks, otherwise | 44 // This can only be called on the UI thread to avoid deadlocks, otherwise |
37 // UI: a.CopyFrom(b); | 45 // UI: a.CopyFrom(b); |
38 // IO: b.CopyFrom(a); | 46 // IO: b.CopyFrom(a); |
39 // can deadlock. | 47 // can deadlock. |
40 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 49 HostZoomMapImpl* copy = static_cast<HostZoomMapImpl*>(copy_interface); |
41 base::AutoLock auto_lock(lock_); | 50 base::AutoLock auto_lock(lock_); |
42 base::AutoLock copy_auto_lock(copy->lock_); | 51 base::AutoLock copy_auto_lock(copy->lock_); |
43 for (HostZoomLevels::const_iterator i(copy->host_zoom_levels_.begin()); | 52 for (HostZoomLevels::const_iterator i(copy->host_zoom_levels_.begin()); |
44 i != copy->host_zoom_levels_.end(); ++i) { | 53 i != copy->host_zoom_levels_.end(); ++i) { |
45 host_zoom_levels_[i->first] = i->second; | 54 host_zoom_levels_[i->first] = i->second; |
46 } | 55 } |
47 } | 56 } |
48 | 57 |
49 double HostZoomMap::GetZoomLevel(const std::string& host) const { | 58 double HostZoomMapImpl::GetZoomLevel(const std::string& host) const { |
50 base::AutoLock auto_lock(lock_); | 59 base::AutoLock auto_lock(lock_); |
51 HostZoomLevels::const_iterator i(host_zoom_levels_.find(host)); | 60 HostZoomLevels::const_iterator i(host_zoom_levels_.find(host)); |
52 return (i == host_zoom_levels_.end()) ? default_zoom_level_ : i->second; | 61 return (i == host_zoom_levels_.end()) ? default_zoom_level_ : i->second; |
53 } | 62 } |
54 | 63 |
55 void HostZoomMap::SetZoomLevel(std::string host, double level) { | 64 void HostZoomMapImpl::SetZoomLevel(std::string host, double level) { |
56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
57 | 66 |
58 { | 67 { |
59 base::AutoLock auto_lock(lock_); | 68 base::AutoLock auto_lock(lock_); |
60 | 69 |
61 if (content::ZoomValuesEqual(level, default_zoom_level_)) | 70 if (content::ZoomValuesEqual(level, default_zoom_level_)) |
62 host_zoom_levels_.erase(host); | 71 host_zoom_levels_.erase(host); |
63 else | 72 else |
64 host_zoom_levels_[host] = level; | 73 host_zoom_levels_[host] = level; |
65 } | 74 } |
66 | 75 |
67 // Notify renderers from this browser context. | 76 // Notify renderers from this browser context. |
68 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); | 77 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); |
69 !i.IsAtEnd(); i.Advance()) { | 78 !i.IsAtEnd(); i.Advance()) { |
70 RenderProcessHost* render_process_host = i.GetCurrentValue(); | 79 RenderProcessHost* render_process_host = i.GetCurrentValue(); |
71 if (render_process_host->GetBrowserContext()->GetHostZoomMap() == this) { | 80 if (render_process_host->GetBrowserContext()->GetHostZoomMap() == this) { |
72 render_process_host->Send( | 81 render_process_host->Send( |
73 new ViewMsg_SetZoomLevelForCurrentURL(host, level)); | 82 new ViewMsg_SetZoomLevelForCurrentURL(host, level)); |
74 } | 83 } |
75 } | 84 } |
76 | 85 |
77 content::NotificationService::current()->Notify( | 86 content::NotificationService::current()->Notify( |
78 content::NOTIFICATION_ZOOM_LEVEL_CHANGED, | 87 content::NOTIFICATION_ZOOM_LEVEL_CHANGED, |
79 content::Source<HostZoomMap>(this), | 88 content::Source<HostZoomMap>(this), |
80 content::Details<const std::string>(&host)); | 89 content::Details<const std::string>(&host)); |
81 } | 90 } |
82 | 91 |
83 double HostZoomMap::GetTemporaryZoomLevel(int render_process_id, | 92 double HostZoomMapImpl::GetDefaultZoomLevel() const { |
84 int render_view_id) const { | 93 return default_zoom_level_; |
| 94 } |
| 95 |
| 96 void HostZoomMapImpl::SetDefaultZoomLevel(double level) { |
| 97 default_zoom_level_ = level; |
| 98 } |
| 99 |
| 100 double HostZoomMapImpl::GetTemporaryZoomLevel(int render_process_id, |
| 101 int render_view_id) const { |
85 base::AutoLock auto_lock(lock_); | 102 base::AutoLock auto_lock(lock_); |
86 for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) { | 103 for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) { |
87 if (temporary_zoom_levels_[i].render_process_id == render_process_id && | 104 if (temporary_zoom_levels_[i].render_process_id == render_process_id && |
88 temporary_zoom_levels_[i].render_view_id == render_view_id) { | 105 temporary_zoom_levels_[i].render_view_id == render_view_id) { |
89 return temporary_zoom_levels_[i].zoom_level; | 106 return temporary_zoom_levels_[i].zoom_level; |
90 } | 107 } |
91 } | 108 } |
92 return 0; | 109 return 0; |
93 } | 110 } |
94 | 111 |
95 void HostZoomMap::SetTemporaryZoomLevel(int render_process_id, | 112 void HostZoomMapImpl::SetTemporaryZoomLevel(int render_process_id, |
96 int render_view_id, | 113 int render_view_id, |
97 double level) { | 114 double level) { |
98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 115 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
99 | 116 |
100 { | 117 { |
101 base::AutoLock auto_lock(lock_); | 118 base::AutoLock auto_lock(lock_); |
102 size_t i; | 119 size_t i; |
103 for (i = 0; i < temporary_zoom_levels_.size(); ++i) { | 120 for (i = 0; i < temporary_zoom_levels_.size(); ++i) { |
104 if (temporary_zoom_levels_[i].render_process_id == render_process_id && | 121 if (temporary_zoom_levels_[i].render_process_id == render_process_id && |
105 temporary_zoom_levels_[i].render_view_id == render_view_id) { | 122 temporary_zoom_levels_[i].render_view_id == render_view_id) { |
106 if (level) { | 123 if (level) { |
107 temporary_zoom_levels_[i].zoom_level = level; | 124 temporary_zoom_levels_[i].zoom_level = level; |
(...skipping 13 matching lines...) Expand all Loading... |
121 } | 138 } |
122 } | 139 } |
123 | 140 |
124 std::string host; | 141 std::string host; |
125 content::NotificationService::current()->Notify( | 142 content::NotificationService::current()->Notify( |
126 content::NOTIFICATION_ZOOM_LEVEL_CHANGED, | 143 content::NOTIFICATION_ZOOM_LEVEL_CHANGED, |
127 content::Source<HostZoomMap>(this), | 144 content::Source<HostZoomMap>(this), |
128 content::Details<const std::string>(&host)); | 145 content::Details<const std::string>(&host)); |
129 } | 146 } |
130 | 147 |
131 void HostZoomMap::Observe( | 148 void HostZoomMapImpl::Observe( |
132 int type, | 149 int type, |
133 const content::NotificationSource& source, | 150 const content::NotificationSource& source, |
134 const content::NotificationDetails& details) { | 151 const content::NotificationDetails& details) { |
135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 152 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
136 | 153 |
137 switch (type) { | 154 switch (type) { |
138 case content::NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW: { | 155 case content::NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW: { |
139 base::AutoLock auto_lock(lock_); | 156 base::AutoLock auto_lock(lock_); |
140 int render_view_id = | 157 int render_view_id = |
141 content::Source<RenderViewHost>(source)->routing_id(); | 158 content::Source<RenderViewHost>(source)->routing_id(); |
142 int render_process_id = | 159 int render_process_id = |
143 content::Source<RenderViewHost>(source)->process()->GetID(); | 160 content::Source<RenderViewHost>(source)->process()->GetID(); |
144 | 161 |
145 for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) { | 162 for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) { |
146 if (temporary_zoom_levels_[i].render_process_id == render_process_id && | 163 if (temporary_zoom_levels_[i].render_process_id == render_process_id && |
147 temporary_zoom_levels_[i].render_view_id == render_view_id) { | 164 temporary_zoom_levels_[i].render_view_id == render_view_id) { |
148 temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i); | 165 temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i); |
149 break; | 166 break; |
150 } | 167 } |
151 } | 168 } |
152 break; | 169 break; |
153 } | 170 } |
154 default: | 171 default: |
155 NOTREACHED() << "Unexpected preference observed."; | 172 NOTREACHED() << "Unexpected preference observed."; |
156 } | 173 } |
157 } | 174 } |
158 | 175 |
159 HostZoomMap::~HostZoomMap() { | 176 HostZoomMapImpl::~HostZoomMapImpl() { |
160 } | 177 } |
OLD | NEW |