Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5159)

Unified Diff: chrome/browser/extensions/event_listener_map.h

Issue 10514013: Filtered events. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rename and move EventListenerMap's delegate Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/event_listener_map.h
diff --git a/chrome/browser/extensions/event_listener_map.h b/chrome/browser/extensions/event_listener_map.h
new file mode 100644
index 0000000000000000000000000000000000000000..74419f9169ed273c4d8fae84991219a0d3a7cf24
--- /dev/null
+++ b/chrome/browser/extensions/event_listener_map.h
@@ -0,0 +1,148 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_EXTENSIONS_EVENT_LISTENER_MAP_H_
+#define CHROME_BROWSER_EXTENSIONS_EVENT_LISTENER_MAP_H_
+#pragma once
+
+#include "base/memory/scoped_ptr.h"
+#include "chrome/common/extensions/event_filter.h"
+
+#include <map>
+#include <set>
+#include <string>
+#include <vector>
+
+namespace base {
+class DictionaryValue;
+}
+
+namespace content {
+class RenderProcessHost;
+}
+
+struct ExtensionEvent;
+class ListenerRemovalListener;
+
+using base::DictionaryValue;
+
+namespace extensions {
+
+// 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.
+//
+// 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 {
+ // |filter| represents a generic filter structure that EventFilter knows how to
battre 2012/06/14 20:05:11 nit: 80 characters
koz (OOO until 15th September) 2012/06/15 00:07:15 Done.
+ // filter events with. A typical filter instance will look like
+ //
+ // {
+ // url: [{hostSuffix: 'google.com'}],
+ // tabId: 5
+ // }
+ EventListener(const std::string& event_name,
+ const std::string& extension_id,
+ content::RenderProcessHost* process,
+ scoped_ptr<DictionaryValue> filter);
+ ~EventListener();
+
+ bool Equals(const EventListener* other) const;
+
+ scoped_ptr<EventListener> Copy() const;
+
+ const std::string event_name;
+ const std::string extension_id;
+ content::RenderProcessHost* process;
+ scoped_ptr<DictionaryValue> filter;
+ int matcher_id;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(EventListener);
+};
+
+// Holds listeners for extension events and can answer questions about which
+// listeners are interested in what events.
+class EventListenerMap {
+ public:
+ typedef std::vector<linked_ptr<EventListener> > ListenerList;
+
+ class Delegate {
+ public:
+ virtual void OnListenerRemoved(const EventListener* listener) = 0;
+ };
+
+ explicit EventListenerMap(Delegate* delegate);
+ ~EventListenerMap();
+
+ // Add a listener for a particular event. GetEventTargets() will include a
+ // weak pointer to |listener| in its results if passed a relevant
+ // ExtensionEvent.
+ bool AddListener(scoped_ptr<EventListener> listener);
+
+ // Remove a listener that .Equals() |listener|.
+ bool RemoveListener(const EventListener& listener);
battre 2012/06/14 20:05:11 nit: const EventListener* for symmetry to HasListe
koz (OOO until 15th September) 2012/06/15 00:07:15 Done.
+
+ // Returns the set of listeners that want to be notified of |event|.
+ std::set<const EventListener*> GetEventTargets(
+ const ExtensionEvent& event);
+
+ // Removes all listeners with process equal to |process|.
+ void RemoveListenersForProcess(const content::RenderProcessHost* process);
+
+ // Returns true if there are any listeners on the event named |event_name|.
+ bool HasListenerForEvent(const std::string& event_name);
+
+ // Returns true if there are any listeners on |event_name| from
+ // |extension_id|.
+ bool HasListenerForExtension(const std::string& extension_id,
+ const std::string& event_name);
+
+ // Returns true if this map contains an EventListener that .Equals()
+ // |listener|.
+ bool HasListener(const EventListener* listener);
+
+ // Returns true if there is a listener for |extension_id| in |process|.
+ bool HasProcessListener(content::RenderProcessHost* process,
+ const std::string& extension_id);
+
+ // Removes any lazy listeners that |extension_id| has added.
+ void RemoveLazyListenersFor(const std::string& extension_id);
+
+ // Adds lazy listeners as described by |unfiltered| and |filtered|, which
+ // contain serialised descriptions of listeners. We can only load lazy
+ // listeners in this fashion, because there is no way to serialise a
+ // RenderProcessHost*.
+ void AddLazyListenersFromPreferences(
+ const std::string& extension_id,
+ const std::set<std::string>& unfiltered,
+ const DictionaryValue& filtered);
+
+ private:
+ typedef std::map<std::string, ListenerList> ListenerMap;
+
+ void CleanupListener(EventListener* listener);
+ bool IsFilteredEvent(const ExtensionEvent& event) const;
+ scoped_ptr<EventMatcher> ParseEventMatcher(DictionaryValue* filter_dict);
+
+ // Listens for removals from this map.
+ Delegate* delegate_;
+
+ std::set<std::string> filtered_events_;
+ ListenerMap listeners_;
+
+ std::map<int, EventListener*> listeners_by_matcher_id_;
+
+ EventFilter event_filter_;
+
+ DISALLOW_COPY_AND_ASSIGN(EventListenerMap);
+};
+
+} // namespace extensions
+
+#endif // CHROME_BROWSER_EXTENSIONS_EVENT_LISTENER_MAP_H_

Powered by Google App Engine
This is Rietveld 408576698