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 @@
#include "base/bind.h"
#include "base/compiler_specific.h"
#include "base/location.h"
#include "base/memory/singleton.h"
#include "base/metrics/histogram_macros.h"
#include "base/single_thread_task_runner.h"
#include "base/system/sys_info.h"
......@@ -19,8 +18,6 @@
#include "base/time/time.h"
#include "components/prefs/pref_registry_simple.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 "services/service_manager/public/cpp/interface_provider.h"
......@@ -57,17 +54,12 @@ int GetDefaultCacheSize() {
// static
WebCacheManager* WebCacheManager::GetInstance() {
return base::Singleton<WebCacheManager>::get();
static base::NoDestructor<WebCacheManager> s_instance;
return s_instance.get();
}
WebCacheManager::WebCacheManager()
: 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() {
......@@ -164,27 +156,22 @@ void WebCacheManager::ClearCacheOnNavigation() {
ClearRendererCache(inactive_renderers_, ON_NAVIGATION);
}
void WebCacheManager::Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
switch (type) {
case content::NOTIFICATION_RENDERER_PROCESS_CREATED: {
content::RenderProcessHost* process =
content::Source<content::RenderProcessHost>(source).ptr();
Add(process->GetID());
break;
}
case content::NOTIFICATION_RENDERER_PROCESS_CLOSED:
case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
content::RenderProcessHost* process =
content::Source<content::RenderProcessHost>(source).ptr();
Remove(process->GetID());
break;
}
default:
NOTREACHED();
break;
}
void WebCacheManager::OnRenderProcessHostCreated(
content::RenderProcessHost* process_host) {
Add(process_host->GetID());
rph_observers_.Add(process_host);
}
void WebCacheManager::RenderProcessExited(
content::RenderProcessHost* process_host,
const content::ChildProcessTerminationInfo& info) {
RenderProcessHostDestroyed(process_host);
}
void WebCacheManager::RenderProcessHostDestroyed(
content::RenderProcessHost* process_host) {
rph_observers_.Remove(process_host);
Remove(process_host->GetID());
}
// static
......
......@@ -18,22 +18,20 @@
#include "base/gtest_prod_util.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/no_destructor.h"
#include "base/scoped_observer.h"
#include "base/time/time.h"
#include "components/web_cache/public/mojom/web_cache.mojom.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "content/public/browser/render_process_host_creation_observer.h"
#include "content/public/browser/render_process_host_observer.h"
#include "mojo/public/cpp/bindings/remote.h"
namespace base {
template<typename Type>
struct DefaultSingletonTraits;
} // namespace base
namespace web_cache {
// Note: memory usage uses uint64_t because potentially the browser could be
// 32 bit and the renderers 64 bits.
class WebCacheManager : public content::NotificationObserver {
class WebCacheManager : public content::RenderProcessHostCreationObserver,
public content::RenderProcessHostObserver {
friend class WebCacheManagerTest;
FRIEND_TEST_ALL_PREFIXES(
WebCacheManagerTest,
......@@ -102,10 +100,15 @@ class WebCacheManager : public content::NotificationObserver {
// to a different website.
void ClearCacheOnNavigation();
// content::NotificationObserver implementation:
void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) override;
// content::RenderProcessHostCreationObserver:
void OnRenderProcessHostCreated(
content::RenderProcessHost* process_host) 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
// tune the default size to the current system.
......@@ -136,9 +139,10 @@ class WebCacheManager : public content::NotificationObserver {
// The key is the unique id of every render process host.
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();
friend struct base::DefaultSingletonTraits<WebCacheManager>;
friend class base::NoDestructor<WebCacheManager>;
~WebCacheManager() override;
......@@ -238,12 +242,13 @@ class WebCacheManager : public content::NotificationObserver {
// recently than they have been active.
std::set<int> inactive_renderers_;
content::NotificationRegistrar registrar_;
// Maps every renderer_id with its corresponding
// mojo::Remote<mojom::WebCache>.
WebCacheServicesMap web_cache_services_;
ScopedObserver<content::RenderProcessHost, content::RenderProcessHostObserver>
rph_observers_{this};
base::WeakPtrFactory<WebCacheManager> weak_factory_{this};
DISALLOW_COPY_AND_ASSIGN(WebCacheManager);
......
......@@ -93,8 +93,10 @@ class WebCacheManagerTest : public testing::Test {
WebCacheManager* manager() { return &manager_; }
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_;
WebCacheManager manager_;
};
// 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