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.
4
5#ifndef EXTENSIONS_COMMON_EVENT_FILTER_H_
6#define EXTENSIONS_COMMON_EVENT_FILTER_H_
7
8#include <map>
9#include <memory>
10#include <set>
11#include <string>
12#include <vector>
13
14#include "base/macros.h"
15#include "components/url_matcher/url_matcher.h"
16#include "extensions/common/event_filtering_info.h"
17#include "extensions/common/event_matcher.h"
18
19namespace extensions {
20
21// Matches incoming events against a collection of EventMatchers. Each added
22// EventMatcher is given an id which is returned by MatchEvent() when it is
23// passed a matching event.
24class EventFilter {
25 public:
26 typedef int MatcherID;
27 EventFilter();
28 ~EventFilter();
29
30 // Adds an event matcher that will be used in calls to MatchEvent(). Returns
31 // the id of the matcher, or -1 if there was an error.
32 MatcherID AddEventMatcher(const std::string& event_name,
33 std::unique_ptr<EventMatcher> matcher);
34
35 // Retrieve the EventMatcher with the given id.
36 EventMatcher* GetEventMatcher(MatcherID id);
37
38 // Retrieve the name of the event that the EventMatcher specified by |id| is
39 // referring to.
40 const std::string& GetEventName(MatcherID id) const;
41
42 // Removes an event matcher, returning the name of the event that it was for.
43 std::string RemoveEventMatcher(MatcherID id);
44
45 // Match an event named |event_name| with filtering info |event_info| against
46 // our set of event matchers. Returns a set of ids that correspond to the
47 // event matchers that matched the event.
48 // TODO(koz): Add a std::string* parameter for retrieving error messages.
49 std::set<MatcherID> MatchEvent(const std::string& event_name,
50 const EventFilteringInfo& event_info,
51 int routing_id) const;
52
53 int GetMatcherCountForEventForTesting(const std::string& event_name) const;
54
55 bool IsURLMatcherEmptyForTesting() const { return url_matcher_.IsEmpty(); }
56
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 std::unique_ptr<EventMatcher> event_matcher,
66 url_matcher::URLMatcher* url_matcher,
67 const url_matcher::URLMatcherConditionSet::Vector& condition_sets);
68 ~EventMatcherEntry();
69
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();
75
76 EventMatcher* event_matcher() {
77 return event_matcher_.get();
78 }
79
80 private:
81 std::unique_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_;
85
86 DISALLOW_COPY_AND_ASSIGN(EventMatcherEntry);
87 };
88
89 // Maps from a matcher id to an event matcher entry.
90 using EventMatcherMap =
91 std::map<MatcherID, std::unique_ptr<EventMatcherEntry>>;
92
93 // Maps from event name to the map of matchers that are registered for it.
94 using EventMatcherMultiMap = std::map<std::string, EventMatcherMap>;
95
96 // Adds the list of URL filters in |matcher| to the URL matcher.
97 bool CreateConditionSets(
98 EventMatcher* matcher,
99 url_matcher::URLMatcherConditionSet::Vector* condition_sets);
100
101 bool AddDictionaryAsConditionSet(
102 base::DictionaryValue* url_filter,
103 url_matcher::URLMatcherConditionSet::Vector* condition_sets);
104
105 url_matcher::URLMatcher url_matcher_;
106 EventMatcherMultiMap event_matchers_;
107
108 // The next id to assign to an EventMatcher.
109 MatcherID next_id_;
110
111 // The next id to assign to a condition set passed to URLMatcher.
112 url_matcher::URLMatcherConditionSet::ID next_condition_set_id_;
113
114 // Maps condition set ids, which URLMatcher operates in, to event matcher
115 // ids, which the interface to this class operates in. As each EventFilter
116 // can specify many condition sets this is a many to one relationship.
117 std::map<url_matcher::URLMatcherConditionSet::ID, MatcherID>
118 condition_set_id_to_event_matcher_id_;
119
120 // Maps from event matcher ids to the name of the event they match on.
121 std::map<MatcherID, std::string> id_to_event_name_;
122
123 DISALLOW_COPY_AND_ASSIGN(EventFilter);
124};
125
126} // namespace extensions
127
128#endif // EXTENSIONS_COMMON_EVENT_FILTER_H_
129