diff options
author | scheib@chromium.org <scheib@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-05 19:41:54 +0000 |
---|---|---|
committer | scheib@chromium.org <scheib@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-05 19:41:54 +0000 |
commit | 9f38459653b3da44309545f673006a3e5faff5d1 (patch) | |
tree | cf94138ff8f55e8951f1c576ba5d80934b8be792 /extensions/browser | |
parent | d40d39bb6f66d7a67026d8a3f7c7041212dc513c (diff) | |
download | chromium_src-9f38459653b3da44309545f673006a3e5faff5d1.zip chromium_src-9f38459653b3da44309545f673006a3e5faff5d1.tar.gz chromium_src-9f38459653b3da44309545f673006a3e5faff5d1.tar.bz2 |
Upgrade EventListener to a class in preparation of adding ServiceWorkerHost.
Refactors the EventListener struct with implied semantics based on the
value of member variable |process| to a class with an explicit accessor for
IsLazy. Also simplifies future abstraction for when ServiceWorkerHost
may be an alternative to a RenderProcessHost.
BUG=369841
Review URL: https://codereview.chromium.org/261223002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@268251 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'extensions/browser')
-rw-r--r-- | extensions/browser/event_listener_map.cc | 75 | ||||
-rw-r--r-- | extensions/browser/event_listener_map.h | 37 | ||||
-rw-r--r-- | extensions/browser/event_router.cc | 38 |
3 files changed, 91 insertions, 59 deletions
diff --git a/extensions/browser/event_listener_map.cc b/extensions/browser/event_listener_map.cc index 1c9fba4..5d90e98 100644 --- a/extensions/browser/event_listener_map.cc +++ b/extensions/browser/event_listener_map.cc @@ -5,6 +5,7 @@ #include "extensions/browser/event_listener_map.h" #include "base/values.h" +#include "content/public/browser/render_process_host.h" #include "extensions/browser/event_router.h" #include "ipc/ipc_message.h" @@ -18,11 +19,12 @@ EventListener::EventListener(const std::string& event_name, const std::string& extension_id, content::RenderProcessHost* process, scoped_ptr<DictionaryValue> filter) - : event_name(event_name), - extension_id(extension_id), - process(process), - filter(filter.Pass()), - matcher_id(-1) {} + : event_name_(event_name), + extension_id_(extension_id), + process_(process), + filter_(filter.Pass()), + matcher_id_(-1) { +} EventListener::~EventListener() {} @@ -30,20 +32,30 @@ bool EventListener::Equals(const EventListener* other) const { // We don't check matcher_id equality because we want a listener with a // filter that hasn't been added to EventFilter to match one that is // equivalent but has. - return event_name == other->event_name && - extension_id == other->extension_id && - process == other->process && - ((!!filter.get()) == (!!other->filter.get())) && - (!filter.get() || filter->Equals(other->filter.get())); + return event_name_ == other->event_name_ && + extension_id_ == other->extension_id_ && process_ == other->process_ && + ((!!filter_.get()) == (!!other->filter_.get())) && + (!filter_.get() || filter_->Equals(other->filter_.get())); } scoped_ptr<EventListener> EventListener::Copy() const { scoped_ptr<DictionaryValue> filter_copy; - if (filter) - filter_copy.reset(filter->DeepCopy()); - return scoped_ptr<EventListener>(new EventListener(event_name, extension_id, - process, - filter_copy.Pass())); + if (filter_) + filter_copy.reset(filter_->DeepCopy()); + return scoped_ptr<EventListener>(new EventListener( + event_name_, extension_id_, process_, filter_copy.Pass())); +} + +bool EventListener::IsLazy() const { + return !process_; +} + +void EventListener::MakeLazy() { + process_ = NULL; +} + +content::BrowserContext* EventListener::GetBrowserContext() const { + return process_ ? process_->GetBrowserContext() : NULL; } EventListenerMap::EventListenerMap(Delegate* delegate) @@ -55,16 +67,16 @@ EventListenerMap::~EventListenerMap() {} bool EventListenerMap::AddListener(scoped_ptr<EventListener> listener) { if (HasListener(listener.get())) return false; - if (listener->filter) { - scoped_ptr<EventMatcher> matcher(ParseEventMatcher(listener->filter.get())); - MatcherID id = event_filter_.AddEventMatcher(listener->event_name, - matcher.Pass()); - listener->matcher_id = id; + if (listener->filter()) { + scoped_ptr<EventMatcher> matcher(ParseEventMatcher(listener->filter())); + MatcherID id = + event_filter_.AddEventMatcher(listener->event_name(), matcher.Pass()); + listener->set_matcher_id(id); listeners_by_matcher_id_[id] = listener.get(); - filtered_events_.insert(listener->event_name); + filtered_events_.insert(listener->event_name()); } linked_ptr<EventListener> listener_ptr(listener.release()); - listeners_[listener_ptr->event_name].push_back(listener_ptr); + listeners_[listener_ptr->event_name()].push_back(listener_ptr); delegate_->OnListenerAdded(listener_ptr.get()); @@ -78,7 +90,7 @@ scoped_ptr<EventMatcher> EventListenerMap::ParseEventMatcher( } bool EventListenerMap::RemoveListener(const EventListener* listener) { - ListenerList& listeners = listeners_[listener->event_name]; + ListenerList& listeners = listeners_[listener->event_name()]; for (ListenerList::iterator it = listeners.begin(); it != listeners.end(); it++) { if ((*it)->Equals(listener)) { @@ -107,14 +119,14 @@ bool EventListenerMap::HasListenerForExtension( for (ListenerList::iterator it2 = it->second.begin(); it2 != it->second.end(); it2++) { - if ((*it2)->extension_id == extension_id) + if ((*it2)->extension_id() == extension_id) return true; } return false; } bool EventListenerMap::HasListener(const EventListener* listener) { - ListenerMap::iterator it = listeners_.find(listener->event_name); + ListenerMap::iterator it = listeners_.find(listener->event_name()); if (it == listeners_.end()) return false; for (ListenerList::iterator it2 = it->second.begin(); @@ -132,7 +144,8 @@ bool EventListenerMap::HasProcessListener(content::RenderProcessHost* process, it++) { for (ListenerList::iterator it2 = it->second.begin(); it2 != it->second.end(); it2++) { - if ((*it2)->process == process && (*it2)->extension_id == extension_id) + if ((*it2)->process() == process && + (*it2)->extension_id() == extension_id) return true; } } @@ -145,7 +158,7 @@ void EventListenerMap::RemoveLazyListenersForExtension( it++) { for (ListenerList::iterator it2 = it->second.begin(); it2 != it->second.end();) { - if (!(*it2)->process && (*it2)->extension_id == extension_id) { + if ((*it2)->IsLazy() && (*it2)->extension_id() == extension_id) { CleanupListener(it2->get()); it2 = it->second.erase(it2); } else { @@ -216,7 +229,7 @@ void EventListenerMap::RemoveListenersForProcess( it++) { for (ListenerList::iterator it2 = it->second.begin(); it2 != it->second.end();) { - if ((*it2)->process == process) { + if ((*it2)->process() == process) { linked_ptr<EventListener> listener(*it2); CleanupListener(it2->get()); it2 = it->second.erase(it2); @@ -230,10 +243,10 @@ void EventListenerMap::RemoveListenersForProcess( void EventListenerMap::CleanupListener(EventListener* listener) { // If the listener doesn't have a filter then we have nothing to clean up. - if (listener->matcher_id == -1) + if (listener->matcher_id() == -1) return; - event_filter_.RemoveEventMatcher(listener->matcher_id); - CHECK_EQ(1u, listeners_by_matcher_id_.erase(listener->matcher_id)); + event_filter_.RemoveEventMatcher(listener->matcher_id()); + CHECK_EQ(1u, listeners_by_matcher_id_.erase(listener->matcher_id())); } bool EventListenerMap::IsFilteredEvent(const Event& event) const { diff --git a/extensions/browser/event_listener_map.h b/extensions/browser/event_listener_map.h index 71aa192..163fc8f 100644 --- a/extensions/browser/event_listener_map.h +++ b/extensions/browser/event_listener_map.h @@ -18,6 +18,7 @@ class DictionaryValue; } namespace content { +class BrowserContext; class RenderProcessHost; } @@ -27,15 +28,18 @@ namespace extensions { struct Event; // A listener for an extension event. A listener is essentially an endpoint -// that an event can be dispatched to. This is a lazy listener if |process| is -// NULL and a filtered listener if |filter| is defined. +// that an event can be dispatched to. +// +// This is a lazy listener if |IsLazy| is returns true, and a filtered listener +// if |filter| is defined. // // A lazy listener is added to an event to indicate that a lazy background page // is listening to the event. It is associated with no process, so to dispatch // an event to a lazy listener one must start a process running the associated // extension and dispatch the event to that. // -struct EventListener { +class EventListener { + public: // |filter| represents a generic filter structure that EventFilter knows how // to filter events with. A typical filter instance will look like // @@ -53,13 +57,30 @@ struct EventListener { scoped_ptr<EventListener> Copy() const; - const std::string event_name; - const std::string extension_id; - content::RenderProcessHost* process; - scoped_ptr<base::DictionaryValue> filter; - EventFilter::MatcherID matcher_id; + // Returns true in the case of a lazy background page, and thus no process. + bool IsLazy() const; + + // Modifies this listener to be a lazy listener, clearing process references. + void MakeLazy(); + + // Returns the browser context associated with the listener, or NULL if + // IsLazy. + content::BrowserContext* GetBrowserContext() const; + + const std::string event_name() const { return event_name_; } + const std::string extension_id() const { return extension_id_; } + content::RenderProcessHost* process() const { return process_; } + base::DictionaryValue* filter() const { return filter_.get(); } + EventFilter::MatcherID matcher_id() const { return matcher_id_; } + void set_matcher_id(EventFilter::MatcherID id) { matcher_id_ = id; } private: + const std::string event_name_; + const std::string extension_id_; + content::RenderProcessHost* process_; + scoped_ptr<base::DictionaryValue> filter_; + EventFilter::MatcherID matcher_id_; // -1 if unset. + DISALLOW_COPY_AND_ASSIGN(EventListener); }; diff --git a/extensions/browser/event_router.cc b/extensions/browser/event_router.cc index 8a6a0a3..0ff308d 100644 --- a/extensions/browser/event_router.cc +++ b/extensions/browser/event_router.cc @@ -216,22 +216,20 @@ void EventRouter::UnregisterObserver(Observer* observer) { } void EventRouter::OnListenerAdded(const EventListener* listener) { - const EventListenerInfo details( - listener->event_name, - listener->extension_id, - listener->process ? listener->process->GetBrowserContext() : NULL); - std::string base_event_name = GetBaseEventName(listener->event_name); + const EventListenerInfo details(listener->event_name(), + listener->extension_id(), + listener->GetBrowserContext()); + std::string base_event_name = GetBaseEventName(listener->event_name()); ObserverMap::iterator observer = observers_.find(base_event_name); if (observer != observers_.end()) observer->second->OnListenerAdded(details); } void EventRouter::OnListenerRemoved(const EventListener* listener) { - const EventListenerInfo details( - listener->event_name, - listener->extension_id, - listener->process ? listener->process->GetBrowserContext() : NULL); - std::string base_event_name = GetBaseEventName(listener->event_name); + const EventListenerInfo details(listener->event_name(), + listener->extension_id(), + listener->GetBrowserContext()); + std::string base_event_name = GetBaseEventName(listener->event_name()); ObserverMap::iterator observer = observers_.find(base_event_name); if (observer != observers_.end()) observer->second->OnListenerRemoved(details); @@ -296,7 +294,7 @@ void EventRouter::RemoveFilteredEventListener( listeners_.RemoveListener(&listener); if (remove_lazy_listener) { - listener.process = NULL; + listener.MakeLazy(); bool removed = listeners_.RemoveListener(&listener); if (removed) @@ -454,9 +452,9 @@ void EventRouter::DispatchEventImpl(const std::string& restrict_to_extension_id, it != listeners.end(); it++) { const EventListener* listener = *it; if (restrict_to_extension_id.empty() || - restrict_to_extension_id == listener->extension_id) { - if (!listener->process) { - DispatchLazyEvent(listener->extension_id, event, &already_dispatched); + restrict_to_extension_id == listener->extension_id()) { + if (listener->IsLazy()) { + DispatchLazyEvent(listener->extension_id(), event, &already_dispatched); } } } @@ -465,13 +463,13 @@ void EventRouter::DispatchEventImpl(const std::string& restrict_to_extension_id, it != listeners.end(); it++) { const EventListener* listener = *it; if (restrict_to_extension_id.empty() || - restrict_to_extension_id == listener->extension_id) { - if (listener->process) { - EventDispatchIdentifier dispatch_id( - listener->process->GetBrowserContext(), listener->extension_id); + restrict_to_extension_id == listener->extension_id()) { + if (listener->process()) { + EventDispatchIdentifier dispatch_id(listener->GetBrowserContext(), + listener->extension_id()); if (!ContainsKey(already_dispatched, dispatch_id)) { - DispatchEventToProcess(listener->extension_id, listener->process, - event); + DispatchEventToProcess( + listener->extension_id(), listener->process(), event); } } } |