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#include <string>
6
7#include "extensions/common/event_filter.h"
8
9#include "components/url_matcher/url_matcher_factory.h"
10#include "ipc/ipc_message.h"
11
12using url_matcher::URLMatcher;
13using url_matcher::URLMatcherConditionSet;
14using url_matcher::URLMatcherFactory;
15
16namespace extensions {
17
18EventFilter::EventMatcherEntry::EventMatcherEntry(
19    scoped_ptr<EventMatcher> event_matcher,
20    URLMatcher* url_matcher,
21    const URLMatcherConditionSet::Vector& condition_sets)
22    : event_matcher_(event_matcher.Pass()),
23      url_matcher_(url_matcher) {
24  for (URLMatcherConditionSet::Vector::const_iterator it =
25       condition_sets.begin(); it != condition_sets.end(); it++)
26    condition_set_ids_.push_back((*it)->id());
27  url_matcher_->AddConditionSets(condition_sets);
28}
29
30EventFilter::EventMatcherEntry::~EventMatcherEntry() {
31  url_matcher_->RemoveConditionSets(condition_set_ids_);
32}
33
34void EventFilter::EventMatcherEntry::DontRemoveConditionSetsInDestructor() {
35  condition_set_ids_.clear();
36}
37
38EventFilter::EventFilter()
39    : next_id_(0),
40      next_condition_set_id_(0) {
41}
42
43EventFilter::~EventFilter() {
44  // Normally when an event matcher entry is removed from event_matchers_ it
45  // will remove its condition sets from url_matcher_, but as url_matcher_ is
46  // being destroyed anyway there is no need to do that step here.
47  for (EventMatcherMultiMap::iterator it = event_matchers_.begin();
48       it != event_matchers_.end(); it++) {
49    for (EventMatcherMap::iterator it2 = it->second.begin();
50         it2 != it->second.end(); it2++) {
51      it2->second->DontRemoveConditionSetsInDestructor();
52    }
53  }
54}
55
56EventFilter::MatcherID
57EventFilter::AddEventMatcher(const std::string& event_name,
58                             scoped_ptr<EventMatcher> matcher) {
59  MatcherID id = next_id_++;
60  URLMatcherConditionSet::Vector condition_sets;
61  if (!CreateConditionSets(id, matcher.get(), &condition_sets))
62    return -1;
63
64  for (URLMatcherConditionSet::Vector::iterator it = condition_sets.begin();
65       it != condition_sets.end(); it++) {
66    condition_set_id_to_event_matcher_id_.insert(
67        std::make_pair((*it)->id(), id));
68  }
69  id_to_event_name_[id] = event_name;
70  event_matchers_[event_name][id] = linked_ptr<EventMatcherEntry>(
71      new EventMatcherEntry(matcher.Pass(), &url_matcher_, condition_sets));
72  return id;
73}
74
75EventMatcher* EventFilter::GetEventMatcher(MatcherID id) {
76  DCHECK(id_to_event_name_.find(id) != id_to_event_name_.end());
77  const std::string& event_name = id_to_event_name_[id];
78  return event_matchers_[event_name][id]->event_matcher();
79}
80
81const std::string& EventFilter::GetEventName(MatcherID id) {
82  DCHECK(id_to_event_name_.find(id) != id_to_event_name_.end());
83  return id_to_event_name_[id];
84}
85
86bool EventFilter::CreateConditionSets(
87    MatcherID id,
88    EventMatcher* matcher,
89    URLMatcherConditionSet::Vector* condition_sets) {
90  if (matcher->GetURLFilterCount() == 0) {
91    // If there are no URL filters then we want to match all events, so create a
92    // URLFilter from an empty dictionary.
93    base::DictionaryValue empty_dict;
94    return AddDictionaryAsConditionSet(&empty_dict, condition_sets);
95  }
96  for (int i = 0; i < matcher->GetURLFilterCount(); i++) {
97    base::DictionaryValue* url_filter;
98    if (!matcher->GetURLFilter(i, &url_filter))
99      return false;
100    if (!AddDictionaryAsConditionSet(url_filter, condition_sets))
101      return false;
102  }
103  return true;
104}
105
106bool EventFilter::AddDictionaryAsConditionSet(
107    base::DictionaryValue* url_filter,
108    URLMatcherConditionSet::Vector* condition_sets) {
109  std::string error;
110  URLMatcherConditionSet::ID condition_set_id = next_condition_set_id_++;
111  condition_sets->push_back(URLMatcherFactory::CreateFromURLFilterDictionary(
112      url_matcher_.condition_factory(),
113      url_filter,
114      condition_set_id,
115      &error));
116  if (!error.empty()) {
117    LOG(ERROR) << "CreateFromURLFilterDictionary failed: " << error;
118    url_matcher_.ClearUnusedConditionSets();
119    condition_sets->clear();
120    return false;
121  }
122  return true;
123}
124
125std::string EventFilter::RemoveEventMatcher(MatcherID id) {
126  std::map<MatcherID, std::string>::iterator it = id_to_event_name_.find(id);
127  std::string event_name = it->second;
128  // EventMatcherEntry's destructor causes the condition set ids to be removed
129  // from url_matcher_.
130  event_matchers_[event_name].erase(id);
131  id_to_event_name_.erase(it);
132  return event_name;
133}
134
135std::set<EventFilter::MatcherID> EventFilter::MatchEvent(
136    const std::string& event_name, const EventFilteringInfo& event_info,
137    int routing_id) {
138  std::set<MatcherID> matchers;
139
140  EventMatcherMultiMap::iterator it = event_matchers_.find(event_name);
141  if (it == event_matchers_.end())
142    return matchers;
143
144  EventMatcherMap& matcher_map = it->second;
145  GURL url_to_match_against = event_info.has_url() ? event_info.url() : GURL();
146  std::set<URLMatcherConditionSet::ID> matching_condition_set_ids =
147      url_matcher_.MatchURL(url_to_match_against);
148  for (std::set<URLMatcherConditionSet::ID>::iterator it =
149       matching_condition_set_ids.begin();
150       it != matching_condition_set_ids.end(); it++) {
151    std::map<URLMatcherConditionSet::ID, MatcherID>::iterator matcher_id =
152        condition_set_id_to_event_matcher_id_.find(*it);
153    if (matcher_id == condition_set_id_to_event_matcher_id_.end()) {
154      NOTREACHED() << "id not found in condition set map (" << (*it) << ")";
155      continue;
156    }
157    MatcherID id = matcher_id->second;
158    EventMatcherMap::iterator matcher_entry = matcher_map.find(id);
159    if (matcher_entry == matcher_map.end()) {
160      // Matcher must be for a different event.
161      continue;
162    }
163    const EventMatcher* event_matcher = matcher_entry->second->event_matcher();
164    // The context that installed the event listener should be the same context
165    // as the one where the event listener is called.
166    if ((routing_id != MSG_ROUTING_NONE) &&
167        (event_matcher->GetRoutingID() != routing_id)) {
168      continue;
169    }
170    if (event_matcher->MatchNonURLCriteria(event_info)) {
171      CHECK(!event_matcher->HasURLFilters() || event_info.has_url());
172      matchers.insert(id);
173    }
174  }
175
176  return matchers;
177}
178
179int EventFilter::GetMatcherCountForEvent(const std::string& name) {
180  EventMatcherMultiMap::const_iterator it = event_matchers_.find(name);
181  if (it == event_matchers_.end())
182    return 0;
183
184  return it->second.size();
185}
186
187}  // namespace extensions
188