Commit 3124de3b authored by Sadrul Habib Chowdhury's avatar Sadrul Habib Chowdhury Committed by Commit Bot

[code health] extensions: Remove some NotificationService usage.

Use RenderProcessHostObserver and RenderProcessHostCreationObserver
APIs, to replace the usage of NotificationService. More details in
the bug.

Bug: 357627
Change-Id: I57d75492ac13b7da014833c24be52f8c37fe531b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1842611Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Reviewed-by: default avatarFinnur Thorarinsson <finnur@chromium.org>
Commit-Queue: Sadrul Chowdhury <sadrul@chromium.org>
Cr-Commit-Position: refs/heads/master@{#703357}
parent 188c0a27
...@@ -39,10 +39,6 @@ AutomationEventRouter::AutomationEventRouter() ...@@ -39,10 +39,6 @@ AutomationEventRouter::AutomationEventRouter()
: active_context_(ExtensionsAPIClient::Get() : active_context_(ExtensionsAPIClient::Get()
->GetAutomationInternalApiDelegate() ->GetAutomationInternalApiDelegate()
->GetActiveUserContext()) { ->GetActiveUserContext()) {
registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
content::NotificationService::AllBrowserContextsAndSources());
registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED,
content::NotificationService::AllBrowserContextsAndSources());
#if defined(USE_AURA) #if defined(USE_AURA)
// Not reset because |this| is leaked. // Not reset because |this| is leaked.
ExtensionsAPIClient::Get() ExtensionsAPIClient::Get()
...@@ -201,6 +197,7 @@ void AutomationEventRouter::Register(const ExtensionId& extension_id, ...@@ -201,6 +197,7 @@ void AutomationEventRouter::Register(const ExtensionId& extension_id,
if (!desktop) if (!desktop)
listener.tree_ids.insert(ax_tree_id); listener.tree_ids.insert(ax_tree_id);
listeners_.push_back(listener); listeners_.push_back(listener);
rph_observers_.Add(content::RenderProcessHost::FromID(listener_process_id));
UpdateActiveProfile(); UpdateActiveProfile();
return; return;
} }
...@@ -227,22 +224,19 @@ void AutomationEventRouter::DispatchAccessibilityEvents( ...@@ -227,22 +224,19 @@ void AutomationEventRouter::DispatchAccessibilityEvents(
DispatchAccessibilityEvents(event_bundle); DispatchAccessibilityEvents(event_bundle);
} }
void AutomationEventRouter::Observe( void AutomationEventRouter::RenderProcessExited(
int type, content::RenderProcessHost* host,
const content::NotificationSource& source, const content::ChildProcessTerminationInfo& info) {
const content::NotificationDetails& details) { RenderProcessHostDestroyed(host);
if (type != content::NOTIFICATION_RENDERER_PROCESS_TERMINATED && }
type != content::NOTIFICATION_RENDERER_PROCESS_CLOSED) {
NOTREACHED();
return;
}
content::RenderProcessHost* rph = void AutomationEventRouter::RenderProcessHostDestroyed(
content::Source<content::RenderProcessHost>(source).ptr(); content::RenderProcessHost* host) {
int process_id = rph->GetID(); int process_id = host->GetID();
base::EraseIf(listeners_, [process_id](const AutomationListener& item) { base::EraseIf(listeners_, [process_id](const AutomationListener& item) {
return item.process_id == process_id; return item.process_id == process_id;
}); });
rph_observers_.Remove(host);
UpdateActiveProfile(); UpdateActiveProfile();
} }
......
...@@ -10,9 +10,9 @@ ...@@ -10,9 +10,9 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/singleton.h" #include "base/memory/singleton.h"
#include "base/scoped_observer.h"
#include "content/public/browser/ax_event_notification_details.h" #include "content/public/browser/ax_event_notification_details.h"
#include "content/public/browser/notification_observer.h" #include "content/public/browser/render_process_host_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "extensions/browser/api/automation_internal/automation_event_router_interface.h" #include "extensions/browser/api/automation_internal/automation_event_router_interface.h"
#include "extensions/common/api/automation_internal.h" #include "extensions/common/api/automation_internal.h"
#include "extensions/common/extension_id.h" #include "extensions/common/extension_id.h"
...@@ -35,7 +35,7 @@ namespace extensions { ...@@ -35,7 +35,7 @@ namespace extensions {
struct AutomationListener; struct AutomationListener;
class AutomationEventRouter : public ui::AXEventBundleSink, class AutomationEventRouter : public ui::AXEventBundleSink,
public content::NotificationObserver, public content::RenderProcessHostObserver,
public AutomationEventRouterInterface { public AutomationEventRouterInterface {
public: public:
static AutomationEventRouter* GetInstance(); static AutomationEventRouter* GetInstance();
...@@ -104,10 +104,11 @@ class AutomationEventRouter : public ui::AXEventBundleSink, ...@@ -104,10 +104,11 @@ class AutomationEventRouter : public ui::AXEventBundleSink,
const gfx::Point& mouse_location, const gfx::Point& mouse_location,
std::vector<ui::AXEvent> events) override; std::vector<ui::AXEvent> events) override;
// content::NotificationObserver interface. // RenderProcessHostObserver:
void Observe(int type, void RenderProcessExited(
const content::NotificationSource& source, content::RenderProcessHost* host,
const content::NotificationDetails& details) override; const content::ChildProcessTerminationInfo& info) override;
void RenderProcessHostDestroyed(content::RenderProcessHost* host) override;
// Called when the user switches profiles or when a listener is added // Called when the user switches profiles or when a listener is added
// or removed. The purpose is to ensure that multiple instances of the // or removed. The purpose is to ensure that multiple instances of the
...@@ -126,6 +127,9 @@ class AutomationEventRouter : public ui::AXEventBundleSink, ...@@ -126,6 +127,9 @@ class AutomationEventRouter : public ui::AXEventBundleSink,
content::BrowserContext* active_context_; content::BrowserContext* active_context_;
ScopedObserver<content::RenderProcessHost, content::RenderProcessHostObserver>
rph_observers_{this};
friend struct base::DefaultSingletonTraits<AutomationEventRouter>; friend struct base::DefaultSingletonTraits<AutomationEventRouter>;
DISALLOW_COPY_AND_ASSIGN(AutomationEventRouter); DISALLOW_COPY_AND_ASSIGN(AutomationEventRouter);
......
...@@ -61,35 +61,27 @@ bool IsExtensionVisibleToContext(const Extension& extension, ...@@ -61,35 +61,27 @@ bool IsExtensionVisibleToContext(const Extension& extension,
RendererStartupHelper::RendererStartupHelper(BrowserContext* browser_context) RendererStartupHelper::RendererStartupHelper(BrowserContext* browser_context)
: browser_context_(browser_context) { : browser_context_(browser_context) {
DCHECK(browser_context); DCHECK(browser_context);
registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED,
content::NotificationService::AllBrowserContextsAndSources());
registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
content::NotificationService::AllBrowserContextsAndSources());
registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED,
content::NotificationService::AllBrowserContextsAndSources());
} }
RendererStartupHelper::~RendererStartupHelper() {} RendererStartupHelper::~RendererStartupHelper() {
for (auto* process : initialized_processes_)
void RendererStartupHelper::Observe( process->RemoveObserver(this);
int type, }
const content::NotificationSource& source,
const content::NotificationDetails& details) { void RendererStartupHelper::OnRenderProcessHostCreated(
switch (type) { content::RenderProcessHost* host) {
case content::NOTIFICATION_RENDERER_PROCESS_CREATED: InitializeProcess(host);
InitializeProcess( }
content::Source<content::RenderProcessHost>(source).ptr());
break; void RendererStartupHelper::RenderProcessExited(
case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: content::RenderProcessHost* host,
// Fall through. const content::ChildProcessTerminationInfo& info) {
case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: UntrackProcess(host);
// This is needed to take care of the case when a RenderProcessHost is }
// reused for a different renderer process.
UntrackProcess(content::Source<content::RenderProcessHost>(source).ptr()); void RendererStartupHelper::RenderProcessHostDestroyed(
break; content::RenderProcessHost* host) {
default: UntrackProcess(host);
NOTREACHED() << "Unexpected notification: " << type;
}
} }
void RendererStartupHelper::InitializeProcess( void RendererStartupHelper::InitializeProcess(
...@@ -181,6 +173,7 @@ void RendererStartupHelper::InitializeProcess( ...@@ -181,6 +173,7 @@ void RendererStartupHelper::InitializeProcess(
initialized_processes_.insert(process); initialized_processes_.insert(process);
pending_active_extensions_.erase(process); pending_active_extensions_.erase(process);
process->AddObserver(this);
} }
void RendererStartupHelper::UntrackProcess( void RendererStartupHelper::UntrackProcess(
...@@ -190,6 +183,7 @@ void RendererStartupHelper::UntrackProcess( ...@@ -190,6 +183,7 @@ void RendererStartupHelper::UntrackProcess(
return; return;
} }
process->RemoveObserver(this);
initialized_processes_.erase(process); initialized_processes_.erase(process);
pending_active_extensions_.erase(process); pending_active_extensions_.erase(process);
for (auto& extension_process_pair : extension_process_map_) for (auto& extension_process_pair : extension_process_map_)
......
...@@ -13,8 +13,8 @@ ...@@ -13,8 +13,8 @@
#include "base/memory/singleton.h" #include "base/memory/singleton.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h" #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
#include "components/keyed_service/core/keyed_service.h" #include "components/keyed_service/core/keyed_service.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 "extensions/common/extension_id.h" #include "extensions/common/extension_id.h"
namespace content { namespace content {
...@@ -35,16 +35,22 @@ class Extension; ...@@ -35,16 +35,22 @@ class Extension;
// TODO(devlin): "StartupHelper" is no longer sufficient to describe the entire // TODO(devlin): "StartupHelper" is no longer sufficient to describe the entire
// behavior of this class. // behavior of this class.
class RendererStartupHelper : public KeyedService, class RendererStartupHelper : public KeyedService,
public content::NotificationObserver { public content::RenderProcessHostCreationObserver,
public content::RenderProcessHostObserver {
public: public:
// This class sends messages to all renderers started for |browser_context|. // This class sends messages to all renderers started for |browser_context|.
explicit RendererStartupHelper(content::BrowserContext* browser_context); explicit RendererStartupHelper(content::BrowserContext* browser_context);
~RendererStartupHelper() override; ~RendererStartupHelper() override;
// content::NotificationObserver overrides: // 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;
// Sends a message to the specified |process| activating the given extension // Sends a message to the specified |process| activating the given extension
// once the process is initialized. OnExtensionLoaded should have already been // once the process is initialized. OnExtensionLoaded should have already been
...@@ -87,8 +93,6 @@ class RendererStartupHelper : public KeyedService, ...@@ -87,8 +93,6 @@ class RendererStartupHelper : public KeyedService,
std::map<content::RenderProcessHost*, std::set<ExtensionId>> std::map<content::RenderProcessHost*, std::set<ExtensionId>>
pending_active_extensions_; pending_active_extensions_;
content::NotificationRegistrar registrar_;
DISALLOW_COPY_AND_ASSIGN(RendererStartupHelper); DISALLOW_COPY_AND_ASSIGN(RendererStartupHelper);
}; };
......
...@@ -6,8 +6,6 @@ ...@@ -6,8 +6,6 @@
#include "base/stl_util.h" #include "base/stl_util.h"
#include "components/crx_file/id_util.h" #include "components/crx_file/id_util.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_types.h"
#include "content/public/test/mock_render_process_host.h" #include "content/public/test/mock_render_process_host.h"
#include "extensions/browser/extension_prefs.h" #include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registry.h" #include "extensions/browser/extension_registry.h"
...@@ -46,17 +44,11 @@ class RendererStartupHelperTest : public ExtensionsTest { ...@@ -46,17 +44,11 @@ class RendererStartupHelperTest : public ExtensionsTest {
protected: protected:
void SimulateRenderProcessCreated(content::RenderProcessHost* rph) { void SimulateRenderProcessCreated(content::RenderProcessHost* rph) {
content::NotificationService::current()->Notify( helper_->OnRenderProcessHostCreated(rph);
content::NOTIFICATION_RENDERER_PROCESS_CREATED,
content::Source<content::RenderProcessHost>(rph),
content::NotificationService::NoDetails());
} }
void SimulateRenderProcessTerminated(content::RenderProcessHost* rph) { void SimulateRenderProcessTerminated(content::RenderProcessHost* rph) {
content::NotificationService::current()->Notify( helper_->RenderProcessHostDestroyed(rph);
content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
content::Source<content::RenderProcessHost>(rph),
content::NotificationService::NoDetails());
} }
scoped_refptr<const Extension> CreateExtension(const std::string& id_input) { scoped_refptr<const Extension> CreateExtension(const std::string& id_input) {
......
...@@ -165,9 +165,6 @@ UserScriptLoader::UserScriptLoader(BrowserContext* browser_context, ...@@ -165,9 +165,6 @@ UserScriptLoader::UserScriptLoader(BrowserContext* browser_context,
queued_load_(false), queued_load_(false),
browser_context_(browser_context), browser_context_(browser_context),
host_id_(host_id) { host_id_(host_id) {
registrar_.Add(this,
content::NOTIFICATION_RENDERER_PROCESS_CREATED,
content::NotificationService::AllBrowserContextsAndSources());
} }
UserScriptLoader::~UserScriptLoader() { UserScriptLoader::~UserScriptLoader() {
...@@ -216,17 +213,13 @@ void UserScriptLoader::ClearScripts() { ...@@ -216,17 +213,13 @@ void UserScriptLoader::ClearScripts() {
AttemptLoad(); AttemptLoad();
} }
void UserScriptLoader::Observe(int type, void UserScriptLoader::OnRenderProcessHostCreated(
const content::NotificationSource& source, content::RenderProcessHost* process_host) {
const content::NotificationDetails& details) {
DCHECK_EQ(type, content::NOTIFICATION_RENDERER_PROCESS_CREATED);
content::RenderProcessHost* process =
content::Source<content::RenderProcessHost>(source).ptr();
if (!ExtensionsBrowserClient::Get()->IsSameContext( if (!ExtensionsBrowserClient::Get()->IsSameContext(
browser_context_, process->GetBrowserContext())) browser_context_, process_host->GetBrowserContext()))
return; return;
if (initial_load_complete()) { if (initial_load_complete()) {
SendUpdate(process, shared_memory_, SendUpdate(process_host, shared_memory_,
std::set<HostID>()); // Include all hosts. std::set<HostID>()); // Include all hosts.
} }
} }
......
...@@ -16,8 +16,7 @@ ...@@ -16,8 +16,7 @@
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/observer_list.h" #include "base/observer_list.h"
#include "base/scoped_observer.h" #include "base/scoped_observer.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 "extensions/common/host_id.h" #include "extensions/common/host_id.h"
#include "extensions/common/user_script.h" #include "extensions/common/user_script.h"
...@@ -39,7 +38,7 @@ namespace extensions { ...@@ -39,7 +38,7 @@ namespace extensions {
// of this class are embedded within classes with names ending in // of this class are embedded within classes with names ending in
// UserScriptMaster. These "master" classes implement the strategy for which // UserScriptMaster. These "master" classes implement the strategy for which
// scripts to load/unload on this logical unit of scripts. // scripts to load/unload on this logical unit of scripts.
class UserScriptLoader : public content::NotificationObserver { class UserScriptLoader : public content::RenderProcessHostCreationObserver {
public: public:
using LoadScriptsCallback = using LoadScriptsCallback =
base::OnceCallback<void(std::unique_ptr<UserScriptList>, base::OnceCallback<void(std::unique_ptr<UserScriptList>,
...@@ -113,10 +112,9 @@ class UserScriptLoader : public content::NotificationObserver { ...@@ -113,10 +112,9 @@ class UserScriptLoader : public content::NotificationObserver {
const HostID& host_id() const { return host_id_; } const HostID& host_id() const { return host_id_; }
private: private:
// 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;
// Returns whether or not it is possible that calls to AddScripts(), // Returns whether or not it is possible that calls to AddScripts(),
// RemoveScripts(), and/or ClearScripts() have caused any real change in the // RemoveScripts(), and/or ClearScripts() have caused any real change in the
...@@ -143,9 +141,6 @@ class UserScriptLoader : public content::NotificationObserver { ...@@ -143,9 +141,6 @@ class UserScriptLoader : public content::NotificationObserver {
return loaded_scripts_.get() == nullptr; return loaded_scripts_.get() == nullptr;
} }
// Manages our notification registrations.
content::NotificationRegistrar registrar_;
// Contains the scripts that were found the last time scripts were updated. // Contains the scripts that were found the last time scripts were updated.
base::ReadOnlySharedMemoryRegion shared_memory_; base::ReadOnlySharedMemoryRegion shared_memory_;
......
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