Commit 2c5db2de authored by Sadrul Habib Chowdhury's avatar Sadrul Habib Chowdhury Committed by Commit Bot

[code health] components/web_cache: Remove NotificationService usage.

Use RenderProcessHostObserver and RenderProcessHostCreationObserver
APIs to replace the usage of NotificationService.

BUG=357627

Change-Id: Idae690c3cc8bb7fffdca0558301b938df3ea1441
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1844274Reviewed-by: default avatarScott Violet <sky@chromium.org>
Commit-Queue: Sadrul Chowdhury <sadrul@chromium.org>
Cr-Commit-Position: refs/heads/master@{#703726}
parent ebfa658e
...@@ -11,7 +11,6 @@ ...@@ -11,7 +11,6 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/compiler_specific.h" #include "base/compiler_specific.h"
#include "base/location.h" #include "base/location.h"
#include "base/memory/singleton.h"
#include "base/metrics/histogram_macros.h" #include "base/metrics/histogram_macros.h"
#include "base/single_thread_task_runner.h" #include "base/single_thread_task_runner.h"
#include "base/system/sys_info.h" #include "base/system/sys_info.h"
...@@ -19,8 +18,6 @@ ...@@ -19,8 +18,6 @@
#include "base/time/time.h" #include "base/time/time.h"
#include "components/prefs/pref_registry_simple.h" #include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h" #include "components/prefs/pref_service.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/render_process_host.h" #include "content/public/browser/render_process_host.h"
#include "services/service_manager/public/cpp/interface_provider.h" #include "services/service_manager/public/cpp/interface_provider.h"
...@@ -57,17 +54,12 @@ int GetDefaultCacheSize() { ...@@ -57,17 +54,12 @@ int GetDefaultCacheSize() {
// static // static
WebCacheManager* WebCacheManager::GetInstance() { WebCacheManager* WebCacheManager::GetInstance() {
return base::Singleton<WebCacheManager>::get(); static base::NoDestructor<WebCacheManager> s_instance;
return s_instance.get();
} }
WebCacheManager::WebCacheManager() WebCacheManager::WebCacheManager()
: global_size_limit_(GetDefaultGlobalSizeLimit()) { : global_size_limit_(GetDefaultGlobalSizeLimit()) {
registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED,
content::NotificationService::AllBrowserContextsAndSources());
registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED,
content::NotificationService::AllBrowserContextsAndSources());
registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
content::NotificationService::AllBrowserContextsAndSources());
} }
WebCacheManager::~WebCacheManager() { WebCacheManager::~WebCacheManager() {
...@@ -164,27 +156,22 @@ void WebCacheManager::ClearCacheOnNavigation() { ...@@ -164,27 +156,22 @@ void WebCacheManager::ClearCacheOnNavigation() {
ClearRendererCache(inactive_renderers_, ON_NAVIGATION); ClearRendererCache(inactive_renderers_, ON_NAVIGATION);
} }
void WebCacheManager::Observe(int type, void WebCacheManager::OnRenderProcessHostCreated(
const content::NotificationSource& source, content::RenderProcessHost* process_host) {
const content::NotificationDetails& details) { Add(process_host->GetID());
switch (type) { rph_observers_.Add(process_host);
case content::NOTIFICATION_RENDERER_PROCESS_CREATED: { }
content::RenderProcessHost* process =
content::Source<content::RenderProcessHost>(source).ptr(); void WebCacheManager::RenderProcessExited(
Add(process->GetID()); content::RenderProcessHost* process_host,
break; const content::ChildProcessTerminationInfo& info) {
} RenderProcessHostDestroyed(process_host);
case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: }
case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
content::RenderProcessHost* process = void WebCacheManager::RenderProcessHostDestroyed(
content::Source<content::RenderProcessHost>(source).ptr(); content::RenderProcessHost* process_host) {
Remove(process->GetID()); rph_observers_.Remove(process_host);
break; Remove(process_host->GetID());
}
default:
NOTREACHED();
break;
}
} }
// static // static
......
...@@ -18,22 +18,20 @@ ...@@ -18,22 +18,20 @@
#include "base/gtest_prod_util.h" #include "base/gtest_prod_util.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/no_destructor.h"
#include "base/scoped_observer.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "components/web_cache/public/mojom/web_cache.mojom.h" #include "components/web_cache/public/mojom/web_cache.mojom.h"
#include "content/public/browser/notification_observer.h" #include "content/public/browser/render_process_host_creation_observer.h"
#include "content/public/browser/notification_registrar.h" #include "content/public/browser/render_process_host_observer.h"
#include "mojo/public/cpp/bindings/remote.h" #include "mojo/public/cpp/bindings/remote.h"
namespace base {
template<typename Type>
struct DefaultSingletonTraits;
} // namespace base
namespace web_cache { namespace web_cache {
// Note: memory usage uses uint64_t because potentially the browser could be // Note: memory usage uses uint64_t because potentially the browser could be
// 32 bit and the renderers 64 bits. // 32 bit and the renderers 64 bits.
class WebCacheManager : public content::NotificationObserver { class WebCacheManager : public content::RenderProcessHostCreationObserver,
public content::RenderProcessHostObserver {
friend class WebCacheManagerTest; friend class WebCacheManagerTest;
FRIEND_TEST_ALL_PREFIXES( FRIEND_TEST_ALL_PREFIXES(
WebCacheManagerTest, WebCacheManagerTest,
...@@ -102,10 +100,15 @@ class WebCacheManager : public content::NotificationObserver { ...@@ -102,10 +100,15 @@ class WebCacheManager : public content::NotificationObserver {
// to a different website. // to a different website.
void ClearCacheOnNavigation(); void ClearCacheOnNavigation();
// content::NotificationObserver implementation: // content::RenderProcessHostCreationObserver:
void Observe(int type, void OnRenderProcessHostCreated(
const content::NotificationSource& source, content::RenderProcessHost* process_host) override;
const content::NotificationDetails& details) override;
// content::RenderProcessHostObserver:
void RenderProcessExited(
content::RenderProcessHost* host,
const content::ChildProcessTerminationInfo& info) override;
void RenderProcessHostDestroyed(content::RenderProcessHost* host) override;
// Gets the default global size limit. This interrogates system metrics to // Gets the default global size limit. This interrogates system metrics to
// tune the default size to the current system. // tune the default size to the current system.
...@@ -136,9 +139,10 @@ class WebCacheManager : public content::NotificationObserver { ...@@ -136,9 +139,10 @@ class WebCacheManager : public content::NotificationObserver {
// The key is the unique id of every render process host. // The key is the unique id of every render process host.
typedef std::map<int, mojo::Remote<mojom::WebCache>> WebCacheServicesMap; typedef std::map<int, mojo::Remote<mojom::WebCache>> WebCacheServicesMap;
// This class is a singleton. Do not instantiate directly. // This class is a singleton. Do not instantiate directly. Call GetInstance()
// instead.
WebCacheManager(); WebCacheManager();
friend struct base::DefaultSingletonTraits<WebCacheManager>; friend class base::NoDestructor<WebCacheManager>;
~WebCacheManager() override; ~WebCacheManager() override;
...@@ -238,12 +242,13 @@ class WebCacheManager : public content::NotificationObserver { ...@@ -238,12 +242,13 @@ class WebCacheManager : public content::NotificationObserver {
// recently than they have been active. // recently than they have been active.
std::set<int> inactive_renderers_; std::set<int> inactive_renderers_;
content::NotificationRegistrar registrar_;
// Maps every renderer_id with its corresponding // Maps every renderer_id with its corresponding
// mojo::Remote<mojom::WebCache>. // mojo::Remote<mojom::WebCache>.
WebCacheServicesMap web_cache_services_; WebCacheServicesMap web_cache_services_;
ScopedObserver<content::RenderProcessHost, content::RenderProcessHostObserver>
rph_observers_{this};
base::WeakPtrFactory<WebCacheManager> weak_factory_{this}; base::WeakPtrFactory<WebCacheManager> weak_factory_{this};
DISALLOW_COPY_AND_ASSIGN(WebCacheManager); DISALLOW_COPY_AND_ASSIGN(WebCacheManager);
......
...@@ -93,8 +93,10 @@ class WebCacheManagerTest : public testing::Test { ...@@ -93,8 +93,10 @@ class WebCacheManagerTest : public testing::Test {
WebCacheManager* manager() { return &manager_; } WebCacheManager* manager() { return &manager_; }
private: private:
WebCacheManager manager_; // Create the environment before creating the WebCacheManager, because the
// latter depends on the UI thread to have been set up correctly.
content::BrowserTaskEnvironment task_environment_; content::BrowserTaskEnvironment task_environment_;
WebCacheManager manager_;
}; };
// static // static
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment