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 "components/web_cache/browser/web_cache_manager.h" | 5 #include "components/web_cache/browser/web_cache_manager.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
13 #include "base/location.h" | 13 #include "base/location.h" |
14 #include "base/memory/singleton.h" | 14 #include "base/memory/singleton.h" |
15 #include "base/metrics/histogram_macros.h" | 15 #include "base/metrics/histogram_macros.h" |
16 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
17 #include "base/sys_info.h" | 17 #include "base/sys_info.h" |
18 #include "base/thread_task_runner_handle.h" | 18 #include "base/thread_task_runner_handle.h" |
19 #include "base/time/time.h" | 19 #include "base/time/time.h" |
20 #include "components/prefs/pref_registry_simple.h" | 20 #include "components/prefs/pref_registry_simple.h" |
21 #include "components/prefs/pref_service.h" | 21 #include "components/prefs/pref_service.h" |
22 #include "components/web_cache/common/web_cache_messages.h" | |
23 #include "content/public/browser/notification_service.h" | 22 #include "content/public/browser/notification_service.h" |
24 #include "content/public/browser/notification_types.h" | 23 #include "content/public/browser/notification_types.h" |
25 #include "content/public/browser/render_process_host.h" | 24 #include "content/public/browser/render_process_host.h" |
| 25 #include "content/public/common/service_registry.h" |
26 | 26 |
27 using base::Time; | 27 using base::Time; |
28 using base::TimeDelta; | 28 using base::TimeDelta; |
29 | 29 |
30 namespace web_cache { | 30 namespace web_cache { |
31 | 31 |
32 static const int kReviseAllocationDelayMS = 200; | 32 static const int kReviseAllocationDelayMS = 200; |
33 | 33 |
34 // The default size limit of the in-memory cache is 8 MB | 34 // The default size limit of the in-memory cache is 8 MB |
35 static const int kDefaultMemoryCacheSize = 8 * 1024 * 1024; | 35 static const int kDefaultMemoryCacheSize = 8 * 1024 * 1024; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 // | 83 // |
84 // However, there doesn't seem to be much harm in receiving the calls in this | 84 // However, there doesn't seem to be much harm in receiving the calls in this |
85 // order. | 85 // order. |
86 | 86 |
87 active_renderers_.insert(renderer_id); | 87 active_renderers_.insert(renderer_id); |
88 | 88 |
89 RendererInfo* stats = &(stats_[renderer_id]); | 89 RendererInfo* stats = &(stats_[renderer_id]); |
90 memset(stats, 0, sizeof(*stats)); | 90 memset(stats, 0, sizeof(*stats)); |
91 stats->access = Time::Now(); | 91 stats->access = Time::Now(); |
92 | 92 |
| 93 WebCacheServicePtr* service_ptr = web_cache_services_[renderer_id]; |
| 94 if (service_ptr) |
| 95 delete service_ptr; |
| 96 service_ptr = new WebCacheServicePtr(); |
| 97 web_cache_services_[renderer_id] = service_ptr; |
| 98 |
| 99 content::RenderProcessHost* host = |
| 100 content::RenderProcessHost::FromID(renderer_id); |
| 101 DCHECK(host); |
| 102 host->GetServiceRegistry()->ConnectToRemoteService( |
| 103 mojo::GetProxy(service_ptr)); |
| 104 |
93 // Revise our allocation strategy to account for this new renderer. | 105 // Revise our allocation strategy to account for this new renderer. |
94 ReviseAllocationStrategyLater(); | 106 ReviseAllocationStrategyLater(); |
95 } | 107 } |
96 | 108 |
97 void WebCacheManager::Remove(int renderer_id) { | 109 void WebCacheManager::Remove(int renderer_id) { |
98 // Erase all knowledge of this renderer | 110 // Erase all knowledge of this renderer |
99 active_renderers_.erase(renderer_id); | 111 active_renderers_.erase(renderer_id); |
100 inactive_renderers_.erase(renderer_id); | 112 inactive_renderers_.erase(renderer_id); |
101 stats_.erase(renderer_id); | 113 stats_.erase(renderer_id); |
102 | 114 |
| 115 WebCacheServicePtr* service_ptr = web_cache_services_[renderer_id]; |
| 116 if (service_ptr) |
| 117 delete service_ptr; |
| 118 web_cache_services_.erase(renderer_id); |
| 119 |
103 // Reallocate the resources used by this renderer | 120 // Reallocate the resources used by this renderer |
104 ReviseAllocationStrategyLater(); | 121 ReviseAllocationStrategyLater(); |
105 } | 122 } |
106 | 123 |
107 void WebCacheManager::ObserveActivity(int renderer_id) { | 124 void WebCacheManager::ObserveActivity(int renderer_id) { |
108 StatsMap::iterator item = stats_.find(renderer_id); | 125 StatsMap::iterator item = stats_.find(renderer_id); |
109 if (item == stats_.end()) | 126 if (item == stats_.end()) |
110 return; // We might see stats for a renderer that has been destroyed. | 127 return; // We might see stats for a renderer that has been destroyed. |
111 | 128 |
112 // Record activity. | 129 // Record activity. |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
322 // tuning to be done here. | 339 // tuning to be done here. |
323 uint64_t min_dead_capacity = 0; | 340 uint64_t min_dead_capacity = 0; |
324 | 341 |
325 // We allow the dead objects to consume up to half of the cache capacity. | 342 // We allow the dead objects to consume up to half of the cache capacity. |
326 uint64_t max_dead_capacity = capacity / 2; | 343 uint64_t max_dead_capacity = capacity / 2; |
327 if (base::SysInfo::IsLowEndDevice()) { | 344 if (base::SysInfo::IsLowEndDevice()) { |
328 max_dead_capacity = std::min(static_cast<uint64_t>(512 * 1024u), | 345 max_dead_capacity = std::min(static_cast<uint64_t>(512 * 1024u), |
329 max_dead_capacity); | 346 max_dead_capacity); |
330 } | 347 } |
331 | 348 |
332 host->Send(new WebCacheMsg_SetCacheCapacities(min_dead_capacity, | 349 WebCacheServicePtr* service_ptr = web_cache_services_[allocation->first]; |
333 max_dead_capacity, | 350 DCHECK(service_ptr->get()); |
334 capacity)); | 351 (*service_ptr) |
| 352 ->SetCacheCapacities(min_dead_capacity, max_dead_capacity, capacity); |
335 } | 353 } |
336 ++allocation; | 354 ++allocation; |
337 } | 355 } |
338 } | 356 } |
339 | 357 |
340 void WebCacheManager::ClearCacheForProcess(int render_process_id) { | 358 void WebCacheManager::ClearCacheForProcess(int render_process_id) { |
341 std::set<int> renderers; | 359 std::set<int> renderers; |
342 renderers.insert(render_process_id); | 360 renderers.insert(render_process_id); |
343 ClearRendererCache(renderers, INSTANTLY); | 361 ClearRendererCache(renderers, INSTANTLY); |
344 } | 362 } |
345 | 363 |
346 void WebCacheManager::ClearRendererCache( | 364 void WebCacheManager::ClearRendererCache( |
347 const std::set<int>& renderers, | 365 const std::set<int>& renderers, |
348 WebCacheManager::ClearCacheOccasion occasion) { | 366 WebCacheManager::ClearCacheOccasion occasion) { |
349 std::set<int>::const_iterator iter = renderers.begin(); | 367 std::set<int>::const_iterator iter = renderers.begin(); |
350 for (; iter != renderers.end(); ++iter) { | 368 for (; iter != renderers.end(); ++iter) { |
351 content::RenderProcessHost* host = | 369 content::RenderProcessHost* host = |
352 content::RenderProcessHost::FromID(*iter); | 370 content::RenderProcessHost::FromID(*iter); |
353 if (host) | 371 if (host) { |
354 host->Send(new WebCacheMsg_ClearCache(occasion == ON_NAVIGATION)); | 372 WebCacheServicePtr* service_ptr = web_cache_services_[*iter]; |
| 373 DCHECK(service_ptr->get()); |
| 374 (*service_ptr)->ClearCache(occasion == ON_NAVIGATION); |
| 375 } |
355 } | 376 } |
356 } | 377 } |
357 | 378 |
358 void WebCacheManager::ReviseAllocationStrategy() { | 379 void WebCacheManager::ReviseAllocationStrategy() { |
359 DCHECK(stats_.size() <= | 380 DCHECK(stats_.size() <= |
360 active_renderers_.size() + inactive_renderers_.size()); | 381 active_renderers_.size() + inactive_renderers_.size()); |
361 | 382 |
362 // Check if renderers have gone inactive. | 383 // Check if renderers have gone inactive. |
363 FindInactiveRenderers(); | 384 FindInactiveRenderers(); |
364 | 385 |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
453 inactive_renderers_.insert(*iter); | 474 inactive_renderers_.insert(*iter); |
454 active_renderers_.erase(*iter); | 475 active_renderers_.erase(*iter); |
455 iter = active_renderers_.begin(); | 476 iter = active_renderers_.begin(); |
456 continue; | 477 continue; |
457 } | 478 } |
458 ++iter; | 479 ++iter; |
459 } | 480 } |
460 } | 481 } |
461 | 482 |
462 } // namespace web_cache | 483 } // namespace web_cache |
OLD | NEW |