Commit 4243f9a4 authored by limasdf@gmail.com's avatar limasdf@gmail.com

Use ExtensionRegistry instead of deprecated extension's notification in EventRouter.

And Make EventRouterTest inherit ExtensionsTest to setup for test.

R=kalman@chromium.org
BUG=354046
TEST=unittest

Review URL: https://codereview.chromium.org/430243002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@287383 0039d316-1c4b-4281-b951-d872f2087c98
parent 61ff6ae4
...@@ -169,6 +169,7 @@ EventRouter::EventRouter(BrowserContext* browser_context, ...@@ -169,6 +169,7 @@ EventRouter::EventRouter(BrowserContext* browser_context,
ExtensionPrefs* extension_prefs) ExtensionPrefs* extension_prefs)
: browser_context_(browser_context), : browser_context_(browser_context),
extension_prefs_(extension_prefs), extension_prefs_(extension_prefs),
extension_registry_observer_(this),
listeners_(this) { listeners_(this) {
registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
content::NotificationService::AllSources()); content::NotificationService::AllSources());
...@@ -177,12 +178,7 @@ EventRouter::EventRouter(BrowserContext* browser_context, ...@@ -177,12 +178,7 @@ EventRouter::EventRouter(BrowserContext* browser_context,
registrar_.Add(this, registrar_.Add(this,
extensions::NOTIFICATION_EXTENSION_ENABLED, extensions::NOTIFICATION_EXTENSION_ENABLED,
content::Source<BrowserContext>(browser_context_)); content::Source<BrowserContext>(browser_context_));
registrar_.Add(this, extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_));
extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
content::Source<BrowserContext>(browser_context_));
registrar_.Add(this,
extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
content::Source<BrowserContext>(browser_context_));
} }
EventRouter::~EventRouter() {} EventRouter::~EventRouter() {}
...@@ -750,33 +746,29 @@ void EventRouter::Observe(int type, ...@@ -750,33 +746,29 @@ void EventRouter::Observe(int type,
} }
break; break;
} }
case extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED: {
// Add all registered lazy listeners to our cache.
const Extension* extension =
content::Details<const Extension>(details).ptr();
std::set<std::string> registered_events =
GetRegisteredEvents(extension->id());
listeners_.LoadUnfilteredLazyListeners(extension->id(),
registered_events);
const DictionaryValue* filtered_events =
GetFilteredEvents(extension->id());
if (filtered_events)
listeners_.LoadFilteredLazyListeners(extension->id(), *filtered_events);
break;
}
case extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: {
// Remove all registered lazy listeners from our cache.
UnloadedExtensionInfo* unloaded =
content::Details<UnloadedExtensionInfo>(details).ptr();
listeners_.RemoveLazyListenersForExtension(unloaded->extension->id());
break;
}
default: default:
NOTREACHED(); NOTREACHED();
return;
} }
} }
void EventRouter::OnExtensionLoaded(content::BrowserContext* browser_context,
const Extension* extension) {
// Add all registered lazy listeners to our cache.
std::set<std::string> registered_events =
GetRegisteredEvents(extension->id());
listeners_.LoadUnfilteredLazyListeners(extension->id(), registered_events);
const DictionaryValue* filtered_events = GetFilteredEvents(extension->id());
if (filtered_events)
listeners_.LoadFilteredLazyListeners(extension->id(), *filtered_events);
}
void EventRouter::OnExtensionUnloaded(content::BrowserContext* browser_context,
const Extension* extension,
UnloadedExtensionInfo::Reason reason) {
// Remove all registered lazy listeners from our cache.
listeners_.RemoveLazyListenersForExtension(extension->id());
}
Event::Event(const std::string& event_name, Event::Event(const std::string& event_name,
scoped_ptr<base::ListValue> event_args) scoped_ptr<base::ListValue> event_args)
: event_name(event_name), : event_name(event_name),
......
...@@ -15,10 +15,12 @@ ...@@ -15,10 +15,12 @@
#include "base/containers/hash_tables.h" #include "base/containers/hash_tables.h"
#include "base/memory/linked_ptr.h" #include "base/memory/linked_ptr.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/scoped_observer.h"
#include "base/values.h" #include "base/values.h"
#include "content/public/browser/notification_observer.h" #include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h" #include "content/public/browser/notification_registrar.h"
#include "extensions/browser/event_listener_map.h" #include "extensions/browser/event_listener_map.h"
#include "extensions/browser/extension_registry_observer.h"
#include "extensions/common/event_filtering_info.h" #include "extensions/common/event_filtering_info.h"
#include "ipc/ipc_sender.h" #include "ipc/ipc_sender.h"
...@@ -35,12 +37,14 @@ class ActivityLog; ...@@ -35,12 +37,14 @@ class ActivityLog;
class Extension; class Extension;
class ExtensionHost; class ExtensionHost;
class ExtensionPrefs; class ExtensionPrefs;
class ExtensionRegistry;
struct Event; struct Event;
struct EventDispatchInfo; struct EventDispatchInfo;
struct EventListenerInfo; struct EventListenerInfo;
class EventRouter : public content::NotificationObserver, class EventRouter : public content::NotificationObserver,
public ExtensionRegistryObserver,
public EventListenerMap::Delegate { public EventListenerMap::Delegate {
public: public:
// These constants convey the state of our knowledge of whether we're in // These constants convey the state of our knowledge of whether we're in
...@@ -207,6 +211,13 @@ class EventRouter : public content::NotificationObserver, ...@@ -207,6 +211,13 @@ class EventRouter : public content::NotificationObserver,
virtual void Observe(int type, virtual void Observe(int type,
const content::NotificationSource& source, const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE; const content::NotificationDetails& details) OVERRIDE;
// ExtensionRegistryObserver implementation.
virtual void OnExtensionLoaded(content::BrowserContext* browser_context,
const Extension* extension) OVERRIDE;
virtual void OnExtensionUnloaded(
content::BrowserContext* browser_context,
const Extension* extension,
UnloadedExtensionInfo::Reason reason) OVERRIDE;
// Returns true if the given listener map contains a event listeners for // Returns true if the given listener map contains a event listeners for
// the given event. If |extension_id| is non-empty, we also check that that // the given event. If |extension_id| is non-empty, we also check that that
...@@ -292,6 +303,9 @@ class EventRouter : public content::NotificationObserver, ...@@ -292,6 +303,9 @@ class EventRouter : public content::NotificationObserver,
content::NotificationRegistrar registrar_; content::NotificationRegistrar registrar_;
ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
extension_registry_observer_;
EventListenerMap listeners_; EventListenerMap listeners_;
// Map from base event name to observer. // Map from base event name to observer.
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "base/values.h" #include "base/values.h"
#include "content/public/browser/notification_service.h" #include "content/public/browser/notification_service.h"
#include "extensions/browser/event_listener_map.h" #include "extensions/browser/event_listener_map.h"
#include "extensions/browser/extensions_test.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
namespace extensions { namespace extensions {
...@@ -81,7 +82,7 @@ scoped_ptr<EventListener> CreateEventListenerForURL( ...@@ -81,7 +82,7 @@ scoped_ptr<EventListener> CreateEventListenerForURL(
} // namespace } // namespace
class EventRouterTest : public testing::Test { class EventRouterTest : public ExtensionsTest {
public: public:
EventRouterTest() EventRouterTest()
: notification_service_(content::NotificationService::Create()) {} : notification_service_(content::NotificationService::Create()) {}
......
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