OLD | NEW |
---|---|
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 <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 | 9 |
10 #include "base/strings/string_piece.h" | 10 #include "base/strings/string_piece.h" |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "content/browser/frame_host/navigation_entry_impl.h" | 13 #include "content/browser/frame_host/navigation_entry_impl.h" |
14 #include "content/browser/renderer_host/render_process_host_impl.h" | 14 #include "content/browser/renderer_host/render_process_host_impl.h" |
15 #include "content/browser/renderer_host/render_view_host_impl.h" | 15 #include "content/browser/renderer_host/render_view_host_impl.h" |
16 #include "content/browser/web_contents/web_contents_impl.h" | 16 #include "content/browser/web_contents/web_contents_impl.h" |
17 #include "content/common/view_messages.h" | 17 #include "content/common/view_messages.h" |
18 #include "content/public/browser/browser_context.h" | 18 #include "content/public/browser/browser_context.h" |
19 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
20 #include "content/public/browser/notification_service.h" | 20 #include "content/public/browser/notification_service.h" |
21 #include "content/public/browser/notification_types.h" | 21 #include "content/public/browser/notification_types.h" |
22 #include "content/public/browser/resource_context.h" | 22 #include "content/public/browser/resource_context.h" |
23 #include "content/public/common/page_zoom.h" | 23 #include "content/public/common/page_zoom.h" |
24 #include "net/base/net_util.h" | 24 #include "net/base/net_util.h" |
25 | 25 |
26 static const char* kHostZoomMapKeyName = "content_host_zoom_map"; | 26 static const char* kHostZoomMapKeyName = "content_host_zoom_map"; |
27 | 27 |
28 namespace content { | 28 namespace content { |
29 | 29 |
30 namespace { | |
31 | |
32 std::string GetHostFromProcessView(int render_process_id, int render_view_id) { | |
33 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
34 RenderViewHost* render_view_host = | |
35 RenderViewHost::FromID(render_process_id, render_view_id); | |
36 if (!render_view_host) | |
37 return std::string(); | |
38 | |
39 WebContents* web_contents = WebContents::FromRenderViewHost(render_view_host); | |
40 | |
41 NavigationEntry* entry = | |
42 web_contents->GetController().GetLastCommittedEntry(); | |
43 if (!entry) | |
44 return std::string(); | |
45 | |
46 return net::GetHostOrSpecFromURL(entry->GetURL()); | |
47 } | |
48 | |
49 } // namespace | |
50 | |
30 HostZoomMap* HostZoomMap::GetForBrowserContext(BrowserContext* context) { | 51 HostZoomMap* HostZoomMap::GetForBrowserContext(BrowserContext* context) { |
31 HostZoomMapImpl* rv = static_cast<HostZoomMapImpl*>( | 52 HostZoomMapImpl* rv = static_cast<HostZoomMapImpl*>( |
32 context->GetUserData(kHostZoomMapKeyName)); | 53 context->GetUserData(kHostZoomMapKeyName)); |
33 if (!rv) { | 54 if (!rv) { |
34 rv = new HostZoomMapImpl(); | 55 rv = new HostZoomMapImpl(); |
35 context->SetUserData(kHostZoomMapKeyName, rv); | 56 context->SetUserData(kHostZoomMapKeyName, rv); |
36 } | 57 } |
37 return rv; | 58 return rv; |
38 } | 59 } |
39 | 60 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
80 } | 101 } |
81 default_zoom_level_ = copy->default_zoom_level_; | 102 default_zoom_level_ = copy->default_zoom_level_; |
82 } | 103 } |
83 | 104 |
84 double HostZoomMapImpl::GetZoomLevelForHost(const std::string& host) const { | 105 double HostZoomMapImpl::GetZoomLevelForHost(const std::string& host) const { |
85 base::AutoLock auto_lock(lock_); | 106 base::AutoLock auto_lock(lock_); |
86 HostZoomLevels::const_iterator i(host_zoom_levels_.find(host)); | 107 HostZoomLevels::const_iterator i(host_zoom_levels_.find(host)); |
87 return (i == host_zoom_levels_.end()) ? default_zoom_level_ : i->second; | 108 return (i == host_zoom_levels_.end()) ? default_zoom_level_ : i->second; |
88 } | 109 } |
89 | 110 |
111 bool HostZoomMapImpl::HasZoomLevel(const std::string& scheme, | |
112 const std::string& host) const { | |
113 base::AutoLock auto_lock(lock_); | |
114 | |
115 SchemeHostZoomLevels::const_iterator scheme_iterator( | |
116 scheme_host_zoom_levels_.find(scheme)); | |
117 | |
118 const HostZoomLevels& zoom_levels = | |
119 (scheme_iterator != scheme_host_zoom_levels_.end()) | |
120 ? scheme_iterator->second | |
121 : host_zoom_levels_; | |
122 | |
123 HostZoomLevels::const_iterator i(zoom_levels.find(host)); | |
124 return i != zoom_levels.end(); | |
125 } | |
126 | |
90 double HostZoomMapImpl::GetZoomLevelForHostAndScheme( | 127 double HostZoomMapImpl::GetZoomLevelForHostAndScheme( |
91 const std::string& scheme, | 128 const std::string& scheme, |
92 const std::string& host) const { | 129 const std::string& host) const { |
93 { | 130 { |
94 base::AutoLock auto_lock(lock_); | 131 base::AutoLock auto_lock(lock_); |
95 SchemeHostZoomLevels::const_iterator scheme_iterator( | 132 SchemeHostZoomLevels::const_iterator scheme_iterator( |
96 scheme_host_zoom_levels_.find(scheme)); | 133 scheme_host_zoom_levels_.find(scheme)); |
97 if (scheme_iterator != scheme_host_zoom_levels_.end()) { | 134 if (scheme_iterator != scheme_host_zoom_levels_.end()) { |
98 HostZoomLevels::const_iterator i(scheme_iterator->second.find(host)); | 135 HostZoomLevels::const_iterator i(scheme_iterator->second.find(host)); |
99 if (i != scheme_iterator->second.end()) | 136 if (i != scheme_iterator->second.end()) |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
145 | 182 |
146 { | 183 { |
147 base::AutoLock auto_lock(lock_); | 184 base::AutoLock auto_lock(lock_); |
148 | 185 |
149 if (ZoomValuesEqual(level, default_zoom_level_)) | 186 if (ZoomValuesEqual(level, default_zoom_level_)) |
150 host_zoom_levels_.erase(host); | 187 host_zoom_levels_.erase(host); |
151 else | 188 else |
152 host_zoom_levels_[host] = level; | 189 host_zoom_levels_[host] = level; |
153 } | 190 } |
154 | 191 |
155 // Notify renderers from this browser context. | 192 SendZoomLevelChange(std::string(), host, level); |
palmer
2014/06/13 17:47:01
Empty scheme? Why? It seems like this should alway
wjmaclean
2014/06/13 20:05:22
Filed bug to investigate: crbug.com/384486.
| |
156 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); | 193 |
157 !i.IsAtEnd(); i.Advance()) { | |
158 RenderProcessHost* render_process_host = i.GetCurrentValue(); | |
159 if (HostZoomMap::GetForBrowserContext( | |
160 render_process_host->GetBrowserContext()) == this) { | |
161 render_process_host->Send( | |
162 new ViewMsg_SetZoomLevelForCurrentURL(std::string(), host, level)); | |
163 } | |
164 } | |
165 HostZoomMap::ZoomLevelChange change; | 194 HostZoomMap::ZoomLevelChange change; |
166 change.mode = HostZoomMap::ZOOM_CHANGED_FOR_HOST; | 195 change.mode = HostZoomMap::ZOOM_CHANGED_FOR_HOST; |
167 change.host = host; | 196 change.host = host; |
168 change.zoom_level = level; | 197 change.zoom_level = level; |
169 | 198 |
170 zoom_level_changed_callbacks_.Notify(change); | 199 zoom_level_changed_callbacks_.Notify(change); |
171 } | 200 } |
172 | 201 |
173 void HostZoomMapImpl::SetZoomLevelForHostAndScheme(const std::string& scheme, | 202 void HostZoomMapImpl::SetZoomLevelForHostAndScheme(const std::string& scheme, |
174 const std::string& host, | 203 const std::string& host, |
175 double level) { | 204 double level) { |
176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 205 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
177 { | 206 { |
178 base::AutoLock auto_lock(lock_); | 207 base::AutoLock auto_lock(lock_); |
179 scheme_host_zoom_levels_[scheme][host] = level; | 208 scheme_host_zoom_levels_[scheme][host] = level; |
180 } | 209 } |
181 | 210 |
182 // Notify renderers from this browser context. | 211 SendZoomLevelChange(scheme, host, level); |
183 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); | |
184 !i.IsAtEnd(); i.Advance()) { | |
185 RenderProcessHost* render_process_host = i.GetCurrentValue(); | |
186 if (HostZoomMap::GetForBrowserContext( | |
187 render_process_host->GetBrowserContext()) == this) { | |
188 render_process_host->Send( | |
189 new ViewMsg_SetZoomLevelForCurrentURL(scheme, host, level)); | |
190 } | |
191 } | |
192 | 212 |
193 HostZoomMap::ZoomLevelChange change; | 213 HostZoomMap::ZoomLevelChange change; |
194 change.mode = HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST; | 214 change.mode = HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST; |
195 change.host = host; | 215 change.host = host; |
196 change.scheme = scheme; | 216 change.scheme = scheme; |
197 change.zoom_level = level; | 217 change.zoom_level = level; |
198 | 218 |
199 zoom_level_changed_callbacks_.Notify(change); | 219 zoom_level_changed_callbacks_.Notify(change); |
200 } | 220 } |
201 | 221 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
264 double level, | 284 double level, |
265 const std::string& host) { | 285 const std::string& host) { |
266 if (UsesTemporaryZoomLevel(render_process_id, render_view_id)) | 286 if (UsesTemporaryZoomLevel(render_process_id, render_view_id)) |
267 SetTemporaryZoomLevel(render_process_id, render_view_id, level); | 287 SetTemporaryZoomLevel(render_process_id, render_view_id, level); |
268 else | 288 else |
269 SetZoomLevelForHost(host, level); | 289 SetZoomLevelForHost(host, level); |
270 } | 290 } |
271 | 291 |
272 bool HostZoomMapImpl::UsesTemporaryZoomLevel(int render_process_id, | 292 bool HostZoomMapImpl::UsesTemporaryZoomLevel(int render_process_id, |
273 int render_view_id) const { | 293 int render_view_id) const { |
274 TemporaryZoomLevel zoom_level(render_process_id, render_view_id); | 294 RenderViewKey key(render_process_id, render_view_id); |
275 | 295 |
276 base::AutoLock auto_lock(lock_); | 296 base::AutoLock auto_lock(lock_); |
277 TemporaryZoomLevels::const_iterator it = std::find( | 297 return ContainsKey(temporary_zoom_levels_, key); |
278 temporary_zoom_levels_.begin(), temporary_zoom_levels_.end(), zoom_level); | |
279 return it != temporary_zoom_levels_.end(); | |
280 } | |
281 | |
282 void HostZoomMapImpl::SetUsesTemporaryZoomLevel( | |
283 int render_process_id, | |
284 int render_view_id, | |
285 bool uses_temporary_zoom_level) { | |
286 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
287 | |
288 TemporaryZoomLevel zoom_level( | |
289 render_process_id, render_view_id, default_zoom_level_); | |
290 | |
291 base::AutoLock auto_lock(lock_); | |
292 TemporaryZoomLevels::iterator it = std::find( | |
293 temporary_zoom_levels_.begin(), temporary_zoom_levels_.end(), zoom_level); | |
294 if (uses_temporary_zoom_level) { | |
295 if (it == temporary_zoom_levels_.end()) | |
296 temporary_zoom_levels_.push_back(zoom_level); | |
297 } else if (it != temporary_zoom_levels_.end()) { | |
298 temporary_zoom_levels_.erase(it); | |
299 } | |
300 } | 298 } |
301 | 299 |
302 double HostZoomMapImpl::GetTemporaryZoomLevel(int render_process_id, | 300 double HostZoomMapImpl::GetTemporaryZoomLevel(int render_process_id, |
303 int render_view_id) const { | 301 int render_view_id) const { |
304 base::AutoLock auto_lock(lock_); | 302 base::AutoLock auto_lock(lock_); |
305 for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) { | 303 RenderViewKey key(render_process_id, render_view_id); |
306 if (temporary_zoom_levels_[i].render_process_id == render_process_id && | 304 if (!ContainsKey(temporary_zoom_levels_, key)) |
307 temporary_zoom_levels_[i].render_view_id == render_view_id) { | 305 return 0; |
308 return temporary_zoom_levels_[i].zoom_level; | |
309 } | |
310 } | |
311 | 306 |
312 return 0; | 307 return temporary_zoom_levels_.find(key)->second; |
313 } | 308 } |
314 | 309 |
315 void HostZoomMapImpl::SetTemporaryZoomLevel(int render_process_id, | 310 void HostZoomMapImpl::SetTemporaryZoomLevel(int render_process_id, |
316 int render_view_id, | 311 int render_view_id, |
317 double level) { | 312 double level) { |
318 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 313 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
319 | 314 |
320 { | 315 { |
316 RenderViewKey key(render_process_id, render_view_id); | |
321 base::AutoLock auto_lock(lock_); | 317 base::AutoLock auto_lock(lock_); |
322 size_t i; | 318 temporary_zoom_levels_[key] = level; |
323 for (i = 0; i < temporary_zoom_levels_.size(); ++i) { | 319 } |
324 if (temporary_zoom_levels_[i].render_process_id == render_process_id && | |
325 temporary_zoom_levels_[i].render_view_id == render_view_id) { | |
326 if (level) { | |
327 temporary_zoom_levels_[i].zoom_level = level; | |
328 } else { | |
329 temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i); | |
330 } | |
331 break; | |
332 } | |
333 } | |
334 | 320 |
335 if (level && i == temporary_zoom_levels_.size()) { | 321 RenderViewHost* host = |
336 TemporaryZoomLevel temp(render_process_id, render_view_id, level); | 322 RenderViewHost::FromID(render_process_id, render_view_id); |
337 temporary_zoom_levels_.push_back(temp); | 323 host->Send(new ViewMsg_SetZoomLevelForView(render_view_id, true, level)); |
338 } | |
339 } | |
340 | 324 |
341 HostZoomMap::ZoomLevelChange change; | 325 HostZoomMap::ZoomLevelChange change; |
342 change.mode = HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM; | 326 change.mode = HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM; |
327 change.host = GetHostFromProcessView(render_process_id, render_view_id); | |
343 change.zoom_level = level; | 328 change.zoom_level = level; |
344 | 329 |
345 zoom_level_changed_callbacks_.Notify(change); | 330 zoom_level_changed_callbacks_.Notify(change); |
346 } | 331 } |
347 | 332 |
348 void HostZoomMapImpl::Observe(int type, | 333 void HostZoomMapImpl::Observe(int type, |
349 const NotificationSource& source, | 334 const NotificationSource& source, |
350 const NotificationDetails& details) { | 335 const NotificationDetails& details) { |
351 switch (type) { | 336 switch (type) { |
352 case NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW: { | 337 case NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW: { |
353 base::AutoLock auto_lock(lock_); | |
354 int render_view_id = Source<RenderViewHost>(source)->GetRoutingID(); | 338 int render_view_id = Source<RenderViewHost>(source)->GetRoutingID(); |
355 int render_process_id = | 339 int render_process_id = |
356 Source<RenderViewHost>(source)->GetProcess()->GetID(); | 340 Source<RenderViewHost>(source)->GetProcess()->GetID(); |
357 | 341 ClearTemporaryZoomLevel(render_process_id, render_view_id); |
358 for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) { | |
359 if (temporary_zoom_levels_[i].render_process_id == render_process_id && | |
360 temporary_zoom_levels_[i].render_view_id == render_view_id) { | |
361 temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i); | |
362 break; | |
363 } | |
364 } | |
365 break; | 342 break; |
366 } | 343 } |
367 default: | 344 default: |
368 NOTREACHED() << "Unexpected preference observed."; | 345 NOTREACHED() << "Unexpected preference observed."; |
369 } | 346 } |
370 } | 347 } |
371 | 348 |
349 void HostZoomMapImpl::ClearTemporaryZoomLevel(int render_process_id, | |
350 int render_view_id) { | |
351 { | |
352 base::AutoLock auto_lock(lock_); | |
353 RenderViewKey key(render_process_id, render_view_id); | |
354 TemporaryZoomLevels::iterator it = temporary_zoom_levels_.find(key); | |
355 if (it == temporary_zoom_levels_.end()) | |
356 return; | |
357 temporary_zoom_levels_.erase(it); | |
358 } | |
359 RenderViewHost* host = | |
360 RenderViewHost::FromID(render_process_id, render_view_id); | |
361 DCHECK(host); | |
362 // Send a new zoom level, host-specific if one exists. | |
363 host->Send(new ViewMsg_SetZoomLevelForView( | |
364 render_view_id, | |
365 false, | |
366 GetZoomLevelForHost( | |
367 GetHostFromProcessView(render_process_id, render_view_id)))); | |
368 } | |
369 | |
370 void HostZoomMapImpl::SendZoomLevelChange(const std::string& scheme, | |
371 const std::string& host, | |
372 double level) { | |
373 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); | |
374 !i.IsAtEnd(); i.Advance()) { | |
375 RenderProcessHost* render_process_host = i.GetCurrentValue(); | |
376 if (HostZoomMap::GetForBrowserContext( | |
377 render_process_host->GetBrowserContext()) == this) { | |
378 render_process_host->Send( | |
379 new ViewMsg_SetZoomLevelForCurrentURL(scheme, host, level)); | |
380 } | |
381 } | |
382 } | |
383 | |
372 HostZoomMapImpl::~HostZoomMapImpl() { | 384 HostZoomMapImpl::~HostZoomMapImpl() { |
373 } | 385 } |
374 | 386 |
375 HostZoomMapImpl::TemporaryZoomLevel::TemporaryZoomLevel(int process_id, | |
376 int view_id, | |
377 double level) | |
378 : render_process_id(process_id), | |
379 render_view_id(view_id), | |
380 zoom_level(level) { | |
381 } | |
382 | |
383 HostZoomMapImpl::TemporaryZoomLevel::TemporaryZoomLevel(int process_id, | |
384 int view_id) | |
385 : render_process_id(process_id), | |
386 render_view_id(view_id), | |
387 zoom_level(0.0) { | |
388 } | |
389 | |
390 bool HostZoomMapImpl::TemporaryZoomLevel::operator==( | |
391 const TemporaryZoomLevel& other) const { | |
392 return other.render_process_id == render_process_id && | |
393 other.render_view_id == render_view_id; | |
394 } | |
395 | |
396 } // namespace content | 387 } // namespace content |
OLD | NEW |