Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / extensions / common / event_matcher.cc
blob007111186aefe10b8ab6189a88100aea3b1388d2
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 #include "base/callback.h"
7 #include "extensions/common/event_matcher.h"
9 #include "extensions/common/event_filtering_info.h"
11 namespace {
12 const char kUrlFiltersKey[] = "url";
13 const char kWindowTypesKey[] = "windowTypes";
15 const char* const kDefaultWindowTypes[] = {"normal", "panel", "popup"};
18 namespace extensions {
20 const char kEventFilterServiceTypeKey[] = "serviceType";
22 EventMatcher::EventMatcher(scoped_ptr<base::DictionaryValue> filter,
23 int routing_id)
24 : filter_(filter.Pass()),
25 routing_id_(routing_id) {
28 EventMatcher::~EventMatcher() {
31 bool EventMatcher::MatchNonURLCriteria(
32 const EventFilteringInfo& event_info) const {
33 if (event_info.has_instance_id()) {
34 return event_info.instance_id() == GetInstanceID();
37 if (event_info.has_window_type()) {
38 for (int i = 0; i < GetWindowTypeCount(); i++) {
39 std::string window_type;
40 if (GetWindowType(i, &window_type) &&
41 window_type == event_info.window_type())
42 return true;
44 return false;
47 const std::string& service_type_filter = GetServiceTypeFilter();
48 return service_type_filter.empty() ||
49 service_type_filter == event_info.service_type();
52 int EventMatcher::GetURLFilterCount() const {
53 base::ListValue* url_filters = nullptr;
54 if (filter_->GetList(kUrlFiltersKey, &url_filters))
55 return url_filters->GetSize();
56 return 0;
59 bool EventMatcher::GetURLFilter(int i, base::DictionaryValue** url_filter_out) {
60 base::ListValue* url_filters = nullptr;
61 if (filter_->GetList(kUrlFiltersKey, &url_filters)) {
62 return url_filters->GetDictionary(i, url_filter_out);
64 return false;
67 int EventMatcher::HasURLFilters() const {
68 return GetURLFilterCount() != 0;
71 std::string EventMatcher::GetServiceTypeFilter() const {
72 std::string service_type_filter;
73 filter_->GetStringASCII(kEventFilterServiceTypeKey, &service_type_filter);
74 return service_type_filter;
77 int EventMatcher::GetInstanceID() const {
78 int instance_id = 0;
79 filter_->GetInteger("instanceId", &instance_id);
80 return instance_id;
83 int EventMatcher::GetWindowTypeCount() const {
84 base::ListValue* window_type_filters = nullptr;
85 if (filter_->GetList(kWindowTypesKey, &window_type_filters))
86 return window_type_filters->GetSize();
87 return arraysize(kDefaultWindowTypes);
90 bool EventMatcher::GetWindowType(int i, std::string* window_type_out) const {
91 base::ListValue* window_types = nullptr;
92 if (filter_->GetList(kWindowTypesKey, &window_types)) {
93 return window_types->GetString(i, window_type_out);
95 if (i >= 0 && i < static_cast<int>(arraysize(kDefaultWindowTypes))) {
96 *window_type_out = kDefaultWindowTypes[i];
97 return true;
99 return false;
102 int EventMatcher::GetRoutingID() const {
103 return routing_id_;
106 } // namespace extensions