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 "extensions/common/event_matcher.h"
6
7 #include "extensions/common/event_filtering_info.h"
8
9 namespace {
10 const char kUrlFiltersKey[] = "url";
11 }
12
13 namespace extensions {
14
15 const char kEventFilterServiceTypeKey[] = "serviceType";
16
EventMatcher(scoped_ptr<base::DictionaryValue> filter,int routing_id)17 EventMatcher::EventMatcher(scoped_ptr<base::DictionaryValue> filter,
18 int routing_id)
19 : filter_(filter.Pass()),
20 routing_id_(routing_id) {
21 }
22
~EventMatcher()23 EventMatcher::~EventMatcher() {
24 }
25
MatchNonURLCriteria(const EventFilteringInfo & event_info) const26 bool EventMatcher::MatchNonURLCriteria(
27 const EventFilteringInfo& event_info) const {
28 if (event_info.has_instance_id()) {
29 return event_info.instance_id() == GetInstanceID();
30 }
31
32 const std::string& service_type_filter = GetServiceTypeFilter();
33 return service_type_filter.empty() ||
34 service_type_filter == event_info.service_type();
35 }
36
GetURLFilterCount() const37 int EventMatcher::GetURLFilterCount() const {
38 base::ListValue* url_filters = NULL;
39 if (filter_->GetList(kUrlFiltersKey, &url_filters))
40 return url_filters->GetSize();
41 return 0;
42 }
43
GetURLFilter(int i,base::DictionaryValue ** url_filter_out)44 bool EventMatcher::GetURLFilter(int i, base::DictionaryValue** url_filter_out) {
45 base::ListValue* url_filters = NULL;
46 if (filter_->GetList(kUrlFiltersKey, &url_filters)) {
47 return url_filters->GetDictionary(i, url_filter_out);
48 }
49 return false;
50 }
51
HasURLFilters() const52 int EventMatcher::HasURLFilters() const {
53 return GetURLFilterCount() != 0;
54 }
55
GetServiceTypeFilter() const56 std::string EventMatcher::GetServiceTypeFilter() const {
57 std::string service_type_filter;
58 filter_->GetStringASCII(kEventFilterServiceTypeKey, &service_type_filter);
59 return service_type_filter;
60 }
61
GetInstanceID() const62 int EventMatcher::GetInstanceID() const {
63 int instance_id = 0;
64 filter_->GetInteger("instanceId", &instance_id);
65 return instance_id;
66 }
67
GetRoutingID() const68 int EventMatcher::GetRoutingID() const {
69 return routing_id_;
70 }
71
72 } // namespace extensions
73