Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(241)

Side by Side Diff: content/browser/host_zoom_map_impl.cc

Issue 224733018: Changes to content/ to facilitate new zoom extension API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed Comments. Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "content/browser/host_zoom_map_impl.h" 5 #include "content/browser/host_zoom_map_impl.h"
6 6
7 #include <cmath> 7 #include <cmath>
8 #include <set>
8 9
9 #include "base/strings/string_piece.h" 10 #include "base/strings/string_piece.h"
10 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
11 #include "base/values.h" 12 #include "base/values.h"
12 #include "content/browser/renderer_host/render_process_host_impl.h" 13 #include "content/browser/renderer_host/render_process_host_impl.h"
13 #include "content/browser/renderer_host/render_view_host_impl.h" 14 #include "content/browser/renderer_host/render_view_host_impl.h"
14 #include "content/common/view_messages.h" 15 #include "content/common/view_messages.h"
15 #include "content/public/browser/browser_context.h" 16 #include "content/public/browser/browser_context.h"
16 #include "content/public/browser/browser_thread.h" 17 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/notification_service.h" 18 #include "content/public/browser/notification_service.h"
18 #include "content/public/browser/notification_types.h" 19 #include "content/public/browser/notification_types.h"
19 #include "content/public/browser/resource_context.h" 20 #include "content/public/browser/resource_context.h"
21 #include "content/public/browser/web_contents.h"
20 #include "content/public/common/page_zoom.h" 22 #include "content/public/common/page_zoom.h"
21 #include "net/base/net_util.h" 23 #include "net/base/net_util.h"
22 24
23 static const char* kHostZoomMapKeyName = "content_host_zoom_map"; 25 static const char* kHostZoomMapKeyName = "content_host_zoom_map";
24 26
25 namespace content { 27 namespace content {
26 28
27 HostZoomMap* HostZoomMap::GetForBrowserContext(BrowserContext* context) { 29 HostZoomMap* HostZoomMap::GetForBrowserContext(BrowserContext* context) {
28 HostZoomMapImpl* rv = static_cast<HostZoomMapImpl*>( 30 HostZoomMapImpl* rv = static_cast<HostZoomMapImpl*>(
29 context->GetUserData(kHostZoomMapKeyName)); 31 context->GetUserData(kHostZoomMapKeyName));
30 if (!rv) { 32 if (!rv) {
31 rv = new HostZoomMapImpl(); 33 rv = new HostZoomMapImpl();
32 context->SetUserData(kHostZoomMapKeyName, rv); 34 context->SetUserData(kHostZoomMapKeyName, rv);
33 } 35 }
34 return rv; 36 return rv;
35 } 37 }
36 38
37 HostZoomMapImpl::HostZoomMapImpl() 39 HostZoomMapImpl::HostZoomMapImpl()
38 : default_zoom_level_(0.0) { 40 : default_zoom_level_(0.0) {
39 registrar_.Add( 41 registrar_.Add(
40 this, NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW, 42 this, NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW,
41 NotificationService::AllSources()); 43 NotificationService::AllSources());
44 registrar_.Add(
45 this, NOTIFICATION_WEB_CONTENTS_NO_TEMPORARY_ZOOM_LEVEL,
46 NotificationService::AllSources());
42 } 47 }
43 48
44 void HostZoomMapImpl::CopyFrom(HostZoomMap* copy_interface) { 49 void HostZoomMapImpl::CopyFrom(HostZoomMap* copy_interface) {
45 // This can only be called on the UI thread to avoid deadlocks, otherwise 50 // This can only be called on the UI thread to avoid deadlocks, otherwise
46 // UI: a.CopyFrom(b); 51 // UI: a.CopyFrom(b);
47 // IO: b.CopyFrom(a); 52 // IO: b.CopyFrom(a);
48 // can deadlock. 53 // can deadlock.
49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
50 HostZoomMapImpl* copy = static_cast<HostZoomMapImpl*>(copy_interface); 55 HostZoomMapImpl* copy = static_cast<HostZoomMapImpl*>(copy_interface);
51 base::AutoLock auto_lock(lock_); 56 base::AutoLock auto_lock(lock_);
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 }; 120 };
116 result.push_back(change); 121 result.push_back(change);
117 } 122 }
118 } 123 }
119 } 124 }
120 return result; 125 return result;
121 } 126 }
122 127
123 void HostZoomMapImpl::SetZoomLevelForHost(const std::string& host, 128 void HostZoomMapImpl::SetZoomLevelForHost(const std::string& host,
124 double level) { 129 double level) {
130 SetZoomLevelForHost(host, 0, level);
131 }
132
133 void HostZoomMapImpl::SetZoomLevelForHost(const std::string& host,
134 int zoom_id,
135 double level) {
125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 136 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
126 137
127 { 138 {
128 base::AutoLock auto_lock(lock_); 139 base::AutoLock auto_lock(lock_);
129 140
130 if (ZoomValuesEqual(level, default_zoom_level_)) 141 if (ZoomValuesEqual(level, default_zoom_level_))
131 host_zoom_levels_.erase(host); 142 host_zoom_levels_.erase(host);
132 else 143 else
133 host_zoom_levels_[host] = level; 144 host_zoom_levels_[host] = level;
134 } 145 }
135 146
136 // Notify renderers from this browser context. 147 SendZoomLevelChange(std::string(), host, level);
137 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); 148
138 !i.IsAtEnd(); i.Advance()) {
139 RenderProcessHost* render_process_host = i.GetCurrentValue();
140 if (HostZoomMap::GetForBrowserContext(
141 render_process_host->GetBrowserContext()) == this) {
142 render_process_host->Send(
143 new ViewMsg_SetZoomLevelForCurrentURL(std::string(), host, level));
144 }
145 }
146 HostZoomMap::ZoomLevelChange change; 149 HostZoomMap::ZoomLevelChange change;
147 change.mode = HostZoomMap::ZOOM_CHANGED_FOR_HOST; 150 change.mode = HostZoomMap::ZOOM_CHANGED_FOR_HOST;
148 change.host = host; 151 change.host = host;
149 change.zoom_level = level; 152 change.zoom_level = level;
150 153
154 CallZoomCallback(zoom_id);
151 zoom_level_changed_callbacks_.Notify(change); 155 zoom_level_changed_callbacks_.Notify(change);
152 } 156 }
153 157
154 void HostZoomMapImpl::SetZoomLevelForHostAndScheme(const std::string& scheme, 158 void HostZoomMapImpl::SetZoomLevelForHostAndScheme(const std::string& scheme,
155 const std::string& host, 159 const std::string& host,
156 double level) { 160 double level) {
157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 161 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
158 { 162 {
159 base::AutoLock auto_lock(lock_); 163 base::AutoLock auto_lock(lock_);
160 scheme_host_zoom_levels_[scheme][host] = level; 164 scheme_host_zoom_levels_[scheme][host] = level;
161 } 165 }
162 166
163 // Notify renderers from this browser context. 167 SendZoomLevelChange(scheme, host, level);
164 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
165 !i.IsAtEnd(); i.Advance()) {
166 RenderProcessHost* render_process_host = i.GetCurrentValue();
167 if (HostZoomMap::GetForBrowserContext(
168 render_process_host->GetBrowserContext()) == this) {
169 render_process_host->Send(
170 new ViewMsg_SetZoomLevelForCurrentURL(scheme, host, level));
171 }
172 }
173 168
174 HostZoomMap::ZoomLevelChange change; 169 HostZoomMap::ZoomLevelChange change;
175 change.mode = HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST; 170 change.mode = HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST;
176 change.host = host; 171 change.host = host;
177 change.scheme = scheme; 172 change.scheme = scheme;
178 change.zoom_level = level; 173 change.zoom_level = level;
179 174
180 zoom_level_changed_callbacks_.Notify(change); 175 zoom_level_changed_callbacks_.Notify(change);
181 } 176 }
182 177
183 double HostZoomMapImpl::GetDefaultZoomLevel() const { 178 double HostZoomMapImpl::GetDefaultZoomLevel() const {
184 return default_zoom_level_; 179 return default_zoom_level_;
185 } 180 }
186 181
187 void HostZoomMapImpl::SetDefaultZoomLevel(double level) { 182 void HostZoomMapImpl::SetDefaultZoomLevel(double level) {
188 default_zoom_level_ = level; 183 default_zoom_level_ = level;
189 } 184 }
190 185
191 scoped_ptr<HostZoomMap::Subscription> 186 scoped_ptr<HostZoomMap::Subscription>
192 HostZoomMapImpl::AddZoomLevelChangedCallback( 187 HostZoomMapImpl::AddZoomLevelChangedCallback(
193 const ZoomLevelChangedCallback& callback) { 188 const ZoomLevelChangedCallback& callback) {
194 return zoom_level_changed_callbacks_.Add(callback); 189 return zoom_level_changed_callbacks_.Add(callback);
195 } 190 }
196 191
192 void HostZoomMapImpl::AddZoomCallback(
193 int zoom_id,
194 const base::Callback<void(void)>& callback) {
195 zoom_callback_map_[zoom_id] = callback;
196 }
197
197 double HostZoomMapImpl::GetTemporaryZoomLevel(int render_process_id, 198 double HostZoomMapImpl::GetTemporaryZoomLevel(int render_process_id,
198 int render_view_id) const { 199 int render_view_id) const {
199 base::AutoLock auto_lock(lock_); 200 base::AutoLock auto_lock(lock_);
200 for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) { 201 for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) {
201 if (temporary_zoom_levels_[i].render_process_id == render_process_id && 202 if (temporary_zoom_levels_[i].render_process_id == render_process_id &&
202 temporary_zoom_levels_[i].render_view_id == render_view_id) { 203 temporary_zoom_levels_[i].render_view_id == render_view_id) {
203 return temporary_zoom_levels_[i].zoom_level; 204 return temporary_zoom_levels_[i].zoom_level;
204 } 205 }
205 } 206 }
206 return 0; 207 return 0;
207 } 208 }
208 209
209 void HostZoomMapImpl::SetTemporaryZoomLevel(int render_process_id, 210 void HostZoomMapImpl::SetTemporaryZoomLevel(int render_process_id,
210 int render_view_id, 211 int render_view_id,
212 const std::string& host,
213 int zoom_id,
211 double level) { 214 double level) {
212 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 215 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
213 216
214 { 217 {
215 base::AutoLock auto_lock(lock_); 218 base::AutoLock auto_lock(lock_);
216 size_t i; 219 size_t i;
217 for (i = 0; i < temporary_zoom_levels_.size(); ++i) { 220 for (i = 0; i < temporary_zoom_levels_.size(); ++i) {
218 if (temporary_zoom_levels_[i].render_process_id == render_process_id && 221 if (temporary_zoom_levels_[i].render_process_id == render_process_id &&
219 temporary_zoom_levels_[i].render_view_id == render_view_id) { 222 temporary_zoom_levels_[i].render_view_id == render_view_id) {
220 if (level) { 223 temporary_zoom_levels_[i].zoom_level = level;
221 temporary_zoom_levels_[i].zoom_level = level;
222 } else {
223 temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i);
224 }
225 break; 224 break;
226 } 225 }
227 } 226 }
228 227
229 if (level && i == temporary_zoom_levels_.size()) { 228 if (level && i == temporary_zoom_levels_.size()) {
230 TemporaryZoomLevel temp; 229 TemporaryZoomLevel temp;
231 temp.render_process_id = render_process_id; 230 temp.render_process_id = render_process_id;
232 temp.render_view_id = render_view_id; 231 temp.render_view_id = render_view_id;
233 temp.zoom_level = level; 232 temp.zoom_level = level;
234 temporary_zoom_levels_.push_back(temp); 233 temporary_zoom_levels_.push_back(temp);
235 } 234 }
236 } 235 }
237 236
238 HostZoomMap::ZoomLevelChange change; 237 HostZoomMap::ZoomLevelChange change;
239 change.mode = HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM; 238 change.mode = HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM;
239 change.host = host;
240 change.zoom_level = level; 240 change.zoom_level = level;
241 241
242 CallZoomCallback(zoom_id);
242 zoom_level_changed_callbacks_.Notify(change); 243 zoom_level_changed_callbacks_.Notify(change);
243 } 244 }
244 245
245 void HostZoomMapImpl::Observe(int type, 246 void HostZoomMapImpl::Observe(int type,
246 const NotificationSource& source, 247 const NotificationSource& source,
247 const NotificationDetails& details) { 248 const NotificationDetails& details) {
248 switch (type) { 249 switch (type) {
249 case NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW: { 250 case NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW: {
250 base::AutoLock auto_lock(lock_); 251 base::AutoLock auto_lock(lock_);
251 int render_view_id = Source<RenderViewHost>(source)->GetRoutingID(); 252 int render_view_id = Source<RenderViewHost>(source)->GetRoutingID();
252 int render_process_id = 253 int render_process_id =
253 Source<RenderViewHost>(source)->GetProcess()->GetID(); 254 Source<RenderViewHost>(source)->GetProcess()->GetID();
254 255 EraseTemporaryZoomLevel(render_process_id, render_view_id);
255 for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) { 256 break;
256 if (temporary_zoom_levels_[i].render_process_id == render_process_id && 257 }
257 temporary_zoom_levels_[i].render_view_id == render_view_id) { 258 case NOTIFICATION_WEB_CONTENTS_NO_TEMPORARY_ZOOM_LEVEL: {
258 temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i); 259 base::AutoLock auto_lock(lock_);
259 break; 260 int render_view_id =
260 } 261 Source<WebContents>(source)->GetRenderViewHost()->GetRoutingID();
261 } 262 int render_process_id =
263 Source<WebContents>(source)->GetRenderProcessHost()->GetID();
264 EraseTemporaryZoomLevel(render_process_id, render_view_id);
262 break; 265 break;
263 } 266 }
264 default: 267 default:
265 NOTREACHED() << "Unexpected preference observed."; 268 NOTREACHED() << "Unexpected preference observed.";
266 } 269 }
267 } 270 }
268 271
272 void HostZoomMapImpl::EraseTemporaryZoomLevel(int render_process_id,
273 int render_view_id) {
274 for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) {
275 if (temporary_zoom_levels_[i].render_process_id == render_process_id &&
276 temporary_zoom_levels_[i].render_view_id == render_view_id) {
277 temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i);
278 break;
279 }
280 }
281 }
282
283 void HostZoomMapImpl::CallZoomCallback(int zoom_id) {
284 std::map<int, base::Callback<void(void)> >::iterator it =
285 zoom_callback_map_.find(zoom_id);
286 if (it != zoom_callback_map_.end()) {
Fady Samuel 2014/04/10 14:27:25 Preferred early exit. if (it == zoom_callback_map
paulmeyer 2014/04/10 15:11:49 Done.
287 it->second.Run();
288 zoom_callback_map_.erase(it);
289 }
290 }
291
292 void HostZoomMapImpl::SendZoomLevelChange(const std::string& scheme,
293 const std::string& host,
294 double level) {
295 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
296 !i.IsAtEnd(); i.Advance()) {
297 RenderProcessHost* render_process_host = i.GetCurrentValue();
298 if (HostZoomMap::GetForBrowserContext(
299 render_process_host->GetBrowserContext()) == this) {
300 int render_process_id = render_process_host->GetID();
301 std::set<int> exceptions;
302 for (size_t i = 0; i != temporary_zoom_levels_.size(); ++i) {
303 if (temporary_zoom_levels_[i].render_process_id == render_process_id) {
Fady Samuel 2014/04/10 14:27:25 Braces unnecessary.
paulmeyer 2014/04/10 15:11:49 Done.
304 exceptions.insert(temporary_zoom_levels_[i].render_view_id);
305 }
306 }
307 render_process_host->Send(new ViewMsg_SetZoomLevelForCurrentURL(
308 scheme, host, level, exceptions));
309 }
310 }
311 }
312
269 HostZoomMapImpl::~HostZoomMapImpl() { 313 HostZoomMapImpl::~HostZoomMapImpl() {
270 } 314 }
271 315
272 } // namespace content 316 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698