OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "chrome/browser/memory_purger.h" | 5 #include "chrome/browser/memory_purger.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/thread.h" | 9 #include "base/thread.h" |
10 #include "chrome/browser/browser_list.h" | 10 #include "chrome/browser/browser_list.h" |
(...skipping 19 matching lines...) Expand all Loading... |
30 // This is a small helper class used to ensure that the objects we want to use | 30 // This is a small helper class used to ensure that the objects we want to use |
31 // on multiple threads are properly refed, so they don't get deleted out from | 31 // on multiple threads are properly refed, so they don't get deleted out from |
32 // under us. | 32 // under us. |
33 class PurgeMemoryIOHelper | 33 class PurgeMemoryIOHelper |
34 : public base::RefCountedThreadSafe<PurgeMemoryIOHelper> { | 34 : public base::RefCountedThreadSafe<PurgeMemoryIOHelper> { |
35 public: | 35 public: |
36 explicit PurgeMemoryIOHelper(SafeBrowsingService* safe_browsing_service) | 36 explicit PurgeMemoryIOHelper(SafeBrowsingService* safe_browsing_service) |
37 : safe_browsing_service_(safe_browsing_service) { | 37 : safe_browsing_service_(safe_browsing_service) { |
38 } | 38 } |
39 | 39 |
40 void AddRequestContextGetter(URLRequestContextGetter* request_context_getter); | 40 void AddRequestContextGetter( |
| 41 scoped_refptr<URLRequestContextGetter> request_context_getter); |
41 | 42 |
42 void PurgeMemoryOnIOThread(); | 43 void PurgeMemoryOnIOThread(); |
43 | 44 |
44 private: | 45 private: |
45 typedef scoped_refptr<URLRequestContextGetter> RequestContextGetter; | 46 typedef scoped_refptr<URLRequestContextGetter> RequestContextGetter; |
46 typedef std::set<RequestContextGetter> RequestContextGetters; | 47 typedef std::set<RequestContextGetter> RequestContextGetters; |
47 | 48 |
48 RequestContextGetters request_context_getters_; | 49 RequestContextGetters request_context_getters_; |
49 scoped_refptr<SafeBrowsingService> safe_browsing_service_; | 50 scoped_refptr<SafeBrowsingService> safe_browsing_service_; |
50 | 51 |
51 DISALLOW_COPY_AND_ASSIGN(PurgeMemoryIOHelper); | 52 DISALLOW_COPY_AND_ASSIGN(PurgeMemoryIOHelper); |
52 }; | 53 }; |
53 | 54 |
54 void PurgeMemoryIOHelper::AddRequestContextGetter( | 55 void PurgeMemoryIOHelper::AddRequestContextGetter( |
55 URLRequestContextGetter* request_context_getter) { | 56 scoped_refptr<URLRequestContextGetter> request_context_getter) { |
56 if (!request_context_getters_.count(request_context_getter)) { | 57 request_context_getters_.insert(request_context_getter); |
57 request_context_getters_.insert( | |
58 RequestContextGetter(request_context_getter)); | |
59 } | |
60 } | 58 } |
61 | 59 |
62 void PurgeMemoryIOHelper::PurgeMemoryOnIOThread() { | 60 void PurgeMemoryIOHelper::PurgeMemoryOnIOThread() { |
63 // Ask ProxyServices to purge any memory they can (generally garbage in the | 61 // Ask ProxyServices to purge any memory they can (generally garbage in the |
64 // wrapped ProxyResolver's JS engine). | 62 // wrapped ProxyResolver's JS engine). |
65 for (RequestContextGetters::const_iterator i( | 63 for (RequestContextGetters::const_iterator i( |
66 request_context_getters_.begin()); | 64 request_context_getters_.begin()); |
67 i != request_context_getters_.end(); ++i) | 65 i != request_context_getters_.end(); ++i) |
68 (*i)->GetURLRequestContext()->proxy_service()->PurgeMemory(); | 66 (*i)->GetURLRequestContext()->proxy_service()->PurgeMemory(); |
69 | 67 |
(...skipping 27 matching lines...) Expand all Loading... |
97 | 95 |
98 // Per-profile cleanup. | 96 // Per-profile cleanup. |
99 scoped_refptr<PurgeMemoryIOHelper> purge_memory_io_helper( | 97 scoped_refptr<PurgeMemoryIOHelper> purge_memory_io_helper( |
100 new PurgeMemoryIOHelper(g_browser_process->resource_dispatcher_host()-> | 98 new PurgeMemoryIOHelper(g_browser_process->resource_dispatcher_host()-> |
101 safe_browsing_service())); | 99 safe_browsing_service())); |
102 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 100 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
103 for (ProfileManager::iterator i(profile_manager->begin()); | 101 for (ProfileManager::iterator i(profile_manager->begin()); |
104 i != profile_manager->end(); ++i) { | 102 i != profile_manager->end(); ++i) { |
105 Profile* profile = *i; | 103 Profile* profile = *i; |
106 purge_memory_io_helper->AddRequestContextGetter( | 104 purge_memory_io_helper->AddRequestContextGetter( |
107 profile->GetRequestContext()); | 105 make_scoped_refptr(profile->GetRequestContext())); |
108 | 106 |
109 // NOTE: Some objects below may be duplicates across profiles. We could | 107 // NOTE: Some objects below may be duplicates across profiles. We could |
110 // conceivably put all these in sets and then iterate over the sets. | 108 // conceivably put all these in sets and then iterate over the sets. |
111 | 109 |
112 // Unload all history backends (freeing memory used to cache sqlite). | 110 // Unload all history backends (freeing memory used to cache sqlite). |
113 // Spinning up the history service is expensive, so we avoid doing it if it | 111 // Spinning up the history service is expensive, so we avoid doing it if it |
114 // hasn't been done already. | 112 // hasn't been done already. |
115 HistoryService* history_service = | 113 HistoryService* history_service = |
116 profile->GetHistoryServiceWithoutCreating(); | 114 profile->GetHistoryServiceWithoutCreating(); |
117 if (history_service) | 115 if (history_service) |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); | 155 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); |
158 !i.IsAtEnd(); i.Advance()) | 156 !i.IsAtEnd(); i.Advance()) |
159 PurgeRendererForHost(i.GetCurrentValue()); | 157 PurgeRendererForHost(i.GetCurrentValue()); |
160 } | 158 } |
161 | 159 |
162 // static | 160 // static |
163 void MemoryPurger::PurgeRendererForHost(RenderProcessHost* host) { | 161 void MemoryPurger::PurgeRendererForHost(RenderProcessHost* host) { |
164 // Direct the renderer to free everything it can. | 162 // Direct the renderer to free everything it can. |
165 host->Send(new ViewMsg_PurgeMemory()); | 163 host->Send(new ViewMsg_PurgeMemory()); |
166 } | 164 } |
OLD | NEW |