| 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 "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/allocator/allocator_extension.h" | 9 #include "base/allocator/allocator_extension.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/lazy_instance.h" |
| 12 #include "base/memory/memory_pressure_notifier.h" |
| 11 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
| 12 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 13 #include "chrome/browser/history/history_service.h" | 15 #include "chrome/browser/history/history_service.h" |
| 14 #include "chrome/browser/history/history_service_factory.h" | 16 #include "chrome/browser/history/history_service_factory.h" |
| 15 #include "chrome/browser/profiles/profile_manager.h" | 17 #include "chrome/browser/profiles/profile_manager.h" |
| 16 #include "chrome/browser/safe_browsing/database_manager.h" | 18 #include "chrome/browser/safe_browsing/database_manager.h" |
| 17 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 19 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 18 #include "chrome/browser/ui/browser_list.h" | 20 #include "chrome/browser/ui/browser_list.h" |
| 19 #include "chrome/browser/webdata/web_data_service.h" | 21 #include "chrome/browser/webdata/web_data_service.h" |
| 20 #include "chrome/browser/webdata/web_data_service_factory.h" | 22 #include "chrome/browser/webdata/web_data_service_factory.h" |
| 21 #include "chrome/common/render_messages.h" | 23 #include "chrome/common/render_messages.h" |
| 22 #include "content/public/browser/render_process_host.h" | 24 #include "content/public/browser/render_process_host.h" |
| 23 #include "content/public/browser/render_widget_host.h" | 25 #include "content/public/browser/render_widget_host.h" |
| 24 #include "content/public/browser/resource_context.h" | 26 #include "content/public/browser/resource_context.h" |
| 25 #include "net/proxy/proxy_resolver.h" | 27 #include "net/proxy/proxy_resolver.h" |
| 26 #include "net/proxy/proxy_service.h" | 28 #include "net/proxy/proxy_service.h" |
| 27 #include "net/url_request/url_request_context.h" | 29 #include "net/url_request/url_request_context.h" |
| 28 #include "net/url_request/url_request_context_getter.h" | 30 #include "net/url_request/url_request_context_getter.h" |
| 29 | 31 |
| 30 using content::BrowserContext; | 32 using content::BrowserContext; |
| 31 using content::BrowserThread; | 33 using content::BrowserThread; |
| 32 using content::ResourceContext; | 34 using content::ResourceContext; |
| 33 | 35 |
| 36 namespace { |
| 37 |
| 34 // PurgeMemoryHelper ----------------------------------------------------------- | 38 // PurgeMemoryHelper ----------------------------------------------------------- |
| 35 | 39 |
| 36 // This is a small helper class used to ensure that the objects we want to use | 40 // This is a small helper class used to ensure that the objects we want to use |
| 37 // on multiple threads are properly refed, so they don't get deleted out from | 41 // on multiple threads are properly refed, so they don't get deleted out from |
| 38 // under us. | 42 // under us. |
| 39 class PurgeMemoryIOHelper | 43 class PurgeMemoryIOHelper |
| 40 : public base::RefCountedThreadSafe<PurgeMemoryIOHelper> { | 44 : public base::RefCountedThreadSafe<PurgeMemoryIOHelper> { |
| 41 public: | 45 public: |
| 42 PurgeMemoryIOHelper() { | 46 PurgeMemoryIOHelper() { |
| 43 safe_browsing_service_ = g_browser_process->safe_browsing_service(); | 47 safe_browsing_service_ = g_browser_process->safe_browsing_service(); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 72 for (size_t i = 0; i < request_context_getters_.size(); ++i) { | 76 for (size_t i = 0; i < request_context_getters_.size(); ++i) { |
| 73 request_context_getters_[i]->GetURLRequestContext()->proxy_service()-> | 77 request_context_getters_[i]->GetURLRequestContext()->proxy_service()-> |
| 74 PurgeMemory(); | 78 PurgeMemory(); |
| 75 } | 79 } |
| 76 | 80 |
| 77 #if defined(FULL_SAFE_BROWSING) | 81 #if defined(FULL_SAFE_BROWSING) |
| 78 safe_browsing_service_->database_manager()->PurgeMemory(); | 82 safe_browsing_service_->database_manager()->PurgeMemory(); |
| 79 #endif | 83 #endif |
| 80 } | 84 } |
| 81 | 85 |
| 86 void PurgeAllOnMemoryPressure( |
| 87 base::MemoryPressureNotifier::MemoryPressureType memory_pressure_type) { |
| 88 if (memory_pressure_type == |
| 89 base::MemoryPressureNotifier::MEMORY_PRESSURE_CRITICAL) |
| 90 MemoryPurger::PurgeAll(); |
| 91 } |
| 92 |
| 93 struct LeakyLazyMemoryPressureListenerTraits { |
| 94 static const bool kRegisterOnExit = false; |
| 95 static const bool kAllowedToAccessOnNonjoinableThread = true; |
| 96 |
| 97 static base::MemoryPressureNotifier::Listener* New(void* instance) { |
| 98 return new (instance) base::MemoryPressureNotifier::Listener( |
| 99 base::Bind(&PurgeAllOnMemoryPressure)); |
| 100 } |
| 101 |
| 102 static void Delete(base::MemoryPressureNotifier::Listener* instance) { |
| 103 } |
| 104 }; |
| 105 |
| 106 base::LazyInstance<base::MemoryPressureNotifier::Listener, |
| 107 LeakyLazyMemoryPressureListenerTraits> |
| 108 g_memory_pressure_listener = LAZY_INSTANCE_INITIALIZER; |
| 109 |
| 110 } // namespace |
| 111 |
| 82 // ----------------------------------------------------------------------------- | 112 // ----------------------------------------------------------------------------- |
| 83 | 113 |
| 84 // static | 114 // static |
| 85 void MemoryPurger::PurgeAll() { | 115 void MemoryPurger::PurgeAll() { |
| 86 PurgeBrowser(); | 116 PurgeBrowser(); |
| 87 PurgeRenderers(); | 117 PurgeRenderers(); |
| 88 | 118 |
| 89 // TODO(pkasting): | 119 // TODO(pkasting): |
| 90 // * Tell the plugin processes to release their free memory? Other stuff? | 120 // * Tell the plugin processes to release their free memory? Other stuff? |
| 91 // * Enumerate what other processes exist and what to do for them. | 121 // * Enumerate what other processes exist and what to do for them. |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 content::RenderProcessHost::AllHostsIterator()); | 184 content::RenderProcessHost::AllHostsIterator()); |
| 155 !i.IsAtEnd(); i.Advance()) | 185 !i.IsAtEnd(); i.Advance()) |
| 156 PurgeRendererForHost(i.GetCurrentValue()); | 186 PurgeRendererForHost(i.GetCurrentValue()); |
| 157 } | 187 } |
| 158 | 188 |
| 159 // static | 189 // static |
| 160 void MemoryPurger::PurgeRendererForHost(content::RenderProcessHost* host) { | 190 void MemoryPurger::PurgeRendererForHost(content::RenderProcessHost* host) { |
| 161 // Direct the renderer to free everything it can. | 191 // Direct the renderer to free everything it can. |
| 162 host->Send(new ChromeViewMsg_PurgeMemory()); | 192 host->Send(new ChromeViewMsg_PurgeMemory()); |
| 163 } | 193 } |
| 194 |
| 195 // static |
| 196 void MemoryPurger::RegisterMemoryPressureListener() { |
| 197 CHECK(g_memory_pressure_listener.Pointer()); |
| 198 } |
| OLD | NEW |