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
9namespace {
10const char kUrlFiltersKey[] = "url";
11}
12
13namespace extensions {
14
15const char kEventFilterServiceTypeKey[] = "serviceType";
16
17EventMatcher::EventMatcher(scoped_ptr<base::DictionaryValue> filter,
18                           int routing_id)
19    : filter_(filter.Pass()),
20      routing_id_(routing_id) {
21}
22
23EventMatcher::~EventMatcher() {
24}
25
26bool 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
37int 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
44bool 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
52int EventMatcher::HasURLFilters() const {
53  return GetURLFilterCount() != 0;
54}
55
56std::string EventMatcher::GetServiceTypeFilter() const {
57  std::string service_type_filter;
58  filter_->GetStringASCII(kEventFilterServiceTypeKey, &service_type_filter);
59  return service_type_filter;
60}
61
62int EventMatcher::GetInstanceID() const {
63  int instance_id = 0;
64  filter_->GetInteger("instanceId", &instance_id);
65  return instance_id;
66}
67
68int EventMatcher::GetRoutingID() const {
69  return routing_id_;
70}
71
72}  // namespace extensions
73