Fix search results being clipped in app list.
[chromium-blink-merge.git] / extensions / common / event_filter.h
blobddd0681fc8112165ee425143ab771f7f1b7839b8
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef EXTENSIONS_COMMON_EVENT_FILTER_H_
6 #define EXTENSIONS_COMMON_EVENT_FILTER_H_
8 #include <map>
9 #include <set>
11 #include "base/memory/linked_ptr.h"
12 #include "components/url_matcher/url_matcher.h"
13 #include "extensions/common/event_filtering_info.h"
14 #include "extensions/common/event_matcher.h"
16 namespace extensions {
18 // Matches incoming events against a collection of EventMatchers. Each added
19 // EventMatcher is given an id which is returned by MatchEvent() when it is
20 // passed a matching event.
21 class EventFilter {
22 public:
23 typedef int MatcherID;
24 EventFilter();
25 ~EventFilter();
27 // Adds an event matcher that will be used in calls to MatchEvent(). Returns
28 // the id of the matcher, or -1 if there was an error.
29 MatcherID AddEventMatcher(const std::string& event_name,
30 scoped_ptr<EventMatcher> matcher);
32 // Retrieve the EventMatcher with the given id.
33 EventMatcher* GetEventMatcher(MatcherID id);
35 // Retrieve the name of the event that the EventMatcher specified by |id| is
36 // referring to.
37 const std::string& GetEventName(MatcherID id);
39 // Removes an event matcher, returning the name of the event that it was for.
40 std::string RemoveEventMatcher(MatcherID id);
42 // Match an event named |event_name| with filtering info |event_info| against
43 // our set of event matchers. Returns a set of ids that correspond to the
44 // event matchers that matched the event.
45 // TODO(koz): Add a std::string* parameter for retrieving error messages.
46 std::set<MatcherID> MatchEvent(const std::string& event_name,
47 const EventFilteringInfo& event_info,
48 int routing_id);
50 int GetMatcherCountForEvent(const std::string& event_name);
52 // For testing.
53 bool IsURLMatcherEmpty() const {
54 return url_matcher_.IsEmpty();
57 private:
58 class EventMatcherEntry {
59 public:
60 // Adds |condition_sets| to |url_matcher| on construction and removes them
61 // again on destruction. |condition_sets| should be the
62 // URLMatcherConditionSets that match the URL constraints specified by
63 // |event_matcher|.
64 EventMatcherEntry(
65 scoped_ptr<EventMatcher> event_matcher,
66 url_matcher::URLMatcher* url_matcher,
67 const url_matcher::URLMatcherConditionSet::Vector& condition_sets);
68 ~EventMatcherEntry();
70 // Prevents the removal of condition sets when this class is destroyed. We
71 // call this in EventFilter's destructor so that we don't do the costly
72 // removal of condition sets when the URLMatcher is going to be destroyed
73 // and clean them up anyway.
74 void DontRemoveConditionSetsInDestructor();
76 EventMatcher* event_matcher() {
77 return event_matcher_.get();
80 private:
81 scoped_ptr<EventMatcher> event_matcher_;
82 // The id sets in url_matcher_ that this EventMatcher owns.
83 std::vector<url_matcher::URLMatcherConditionSet::ID> condition_set_ids_;
84 url_matcher::URLMatcher* url_matcher_;
86 DISALLOW_COPY_AND_ASSIGN(EventMatcherEntry);
89 // Maps from a matcher id to an event matcher entry.
90 typedef std::map<MatcherID, linked_ptr<EventMatcherEntry> > EventMatcherMap;
92 // Maps from event name to the map of matchers that are registered for it.
93 typedef std::map<std::string, EventMatcherMap> EventMatcherMultiMap;
95 // Adds the list of URL filters in |matcher| to the URL matcher, having
96 // matches for those URLs map to |id|.
97 bool CreateConditionSets(
98 MatcherID id,
99 EventMatcher* matcher,
100 url_matcher::URLMatcherConditionSet::Vector* condition_sets);
102 bool AddDictionaryAsConditionSet(
103 base::DictionaryValue* url_filter,
104 url_matcher::URLMatcherConditionSet::Vector* condition_sets);
106 url_matcher::URLMatcher url_matcher_;
107 EventMatcherMultiMap event_matchers_;
109 // The next id to assign to an EventMatcher.
110 MatcherID next_id_;
112 // The next id to assign to a condition set passed to URLMatcher.
113 url_matcher::URLMatcherConditionSet::ID next_condition_set_id_;
115 // Maps condition set ids, which URLMatcher operates in, to event matcher
116 // ids, which the interface to this class operates in. As each EventFilter
117 // can specify many condition sets this is a many to one relationship.
118 std::map<url_matcher::URLMatcherConditionSet::ID, MatcherID>
119 condition_set_id_to_event_matcher_id_;
121 // Maps from event matcher ids to the name of the event they match on.
122 std::map<MatcherID, std::string> id_to_event_name_;
124 DISALLOW_COPY_AND_ASSIGN(EventFilter);
127 } // namespace extensions
129 #endif // EXTENSIONS_COMMON_EVENT_FILTER_H_