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 CHROME_COMMON_EXTENSIONS_EVENT_FILTER_H_
6 #define CHROME_COMMON_EXTENSIONS_EVENT_FILTER_H_
8 #include "base/memory/linked_ptr.h"
9 #include "chrome/common/extensions/event_matcher.h"
10 #include "chrome/common/extensions/event_filtering_info.h"
11 #include "chrome/common/extensions/matcher/url_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.
23 typedef int MatcherID
;
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
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
);
49 int GetMatcherCountForEvent(const std::string
& event_name
);
52 bool IsURLMatcherEmpty() const {
53 return url_matcher_
.IsEmpty();
57 class EventMatcherEntry
{
59 // Adds |condition_sets| to |url_matcher| on construction and removes them
60 // again on destruction. |condition_sets| should be the
61 // URLMatcherConditionSets that match the URL constraints specified by
63 EventMatcherEntry(scoped_ptr
<EventMatcher
> event_matcher
,
64 URLMatcher
* url_matcher
,
65 const URLMatcherConditionSet::Vector
& condition_sets
);
68 // Prevents the removal of condition sets when this class is destroyed. We
69 // call this in EventFilter's destructor so that we don't do the costly
70 // removal of condition sets when the URLMatcher is going to be destroyed
71 // and clean them up anyway.
72 void DontRemoveConditionSetsInDestructor();
74 EventMatcher
* event_matcher() {
75 return event_matcher_
.get();
79 scoped_ptr
<EventMatcher
> event_matcher_
;
80 // The id sets in url_matcher_ that this EventMatcher owns.
81 std::vector
<URLMatcherConditionSet::ID
> condition_set_ids_
;
82 URLMatcher
* url_matcher_
;
84 DISALLOW_COPY_AND_ASSIGN(EventMatcherEntry
);
87 // Maps from a matcher id to an event matcher entry.
88 typedef std::map
<MatcherID
, linked_ptr
<EventMatcherEntry
> > EventMatcherMap
;
90 // Maps from event name to the map of matchers that are registered for it.
91 typedef std::map
<std::string
, EventMatcherMap
> EventMatcherMultiMap
;
93 // Adds the list of URL filters in |matcher| to the URL matcher, having
94 // matches for those URLs map to |id|.
95 bool CreateConditionSets(MatcherID id
,
96 EventMatcher
* matcher
,
97 URLMatcherConditionSet::Vector
* condition_sets
);
99 bool AddDictionaryAsConditionSet(
100 base::DictionaryValue
* url_filter
,
101 URLMatcherConditionSet::Vector
* condition_sets
);
103 URLMatcher url_matcher_
;
104 EventMatcherMultiMap event_matchers_
;
106 // The next id to assign to an EventMatcher.
109 // The next id to assign to a condition set passed to URLMatcher.
110 URLMatcherConditionSet::ID next_condition_set_id_
;
112 // Maps condition set ids, which URLMatcher operates in, to event matcher
113 // ids, which the interface to this class operates in. As each EventFilter
114 // can specify many condition sets this is a many to one relationship.
115 std::map
<URLMatcherConditionSet::ID
, MatcherID
>
116 condition_set_id_to_event_matcher_id_
;
118 // Maps from event matcher ids to the name of the event they match on.
119 std::map
<MatcherID
, std::string
> id_to_event_name_
;
121 DISALLOW_COPY_AND_ASSIGN(EventFilter
);
124 } // namespace extensions
126 #endif // CHROME_COMMON_EXTENSIONS_EVENT_FILTER_H_