event_matcher.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #include "extensions/common/event_matcher.h"
  5. #include <utility>
  6. #include "base/callback.h"
  7. #include "extensions/common/mojom/event_dispatcher.mojom.h"
  8. namespace {
  9. const char kUrlFiltersKey[] = "url";
  10. const char kWindowTypesKey[] = "windowTypes";
  11. }
  12. namespace extensions {
  13. const char kEventFilterServiceTypeKey[] = "serviceType";
  14. EventMatcher::EventMatcher(std::unique_ptr<base::DictionaryValue> filter,
  15. int routing_id)
  16. : filter_(std::move(filter)), routing_id_(routing_id) {}
  17. EventMatcher::~EventMatcher() {
  18. }
  19. bool EventMatcher::MatchNonURLCriteria(
  20. const mojom::EventFilteringInfo& event_info) const {
  21. if (event_info.has_instance_id) {
  22. return event_info.instance_id == GetInstanceID();
  23. }
  24. if (event_info.window_type) {
  25. int window_type_count = GetWindowTypeCount();
  26. for (int i = 0; i < window_type_count; i++) {
  27. std::string window_type;
  28. if (GetWindowType(i, &window_type) &&
  29. window_type == *event_info.window_type) {
  30. return true;
  31. }
  32. }
  33. return false;
  34. }
  35. if (event_info.has_window_exposed_by_default) {
  36. // An event with a |window_exposed_by_default| set is only
  37. // relevant to the listener if no window type filter is set.
  38. if (HasWindowTypes())
  39. return false;
  40. return event_info.window_exposed_by_default;
  41. }
  42. const std::string& service_type_filter = GetServiceTypeFilter();
  43. return service_type_filter.empty() ||
  44. (event_info.service_type &&
  45. service_type_filter == *event_info.service_type);
  46. }
  47. int EventMatcher::GetURLFilterCount() const {
  48. base::ListValue* url_filters = nullptr;
  49. if (filter_->GetList(kUrlFiltersKey, &url_filters))
  50. return url_filters->GetList().size();
  51. return 0;
  52. }
  53. const base::Value::Dict* EventMatcher::GetURLFilter(int i) {
  54. base::ListValue* url_filters = nullptr;
  55. if (filter_->GetList(kUrlFiltersKey, &url_filters)) {
  56. base::Value& dict = url_filters->GetList()[i];
  57. return dict.GetIfDict();
  58. }
  59. return nullptr;
  60. }
  61. bool EventMatcher::HasURLFilters() const {
  62. return GetURLFilterCount() != 0;
  63. }
  64. std::string EventMatcher::GetServiceTypeFilter() const {
  65. std::string service_type_filter;
  66. if (const std::string* ptr =
  67. filter_->FindStringKey(kEventFilterServiceTypeKey)) {
  68. if (base::IsStringASCII(*ptr))
  69. service_type_filter = *ptr;
  70. }
  71. return service_type_filter;
  72. }
  73. int EventMatcher::GetInstanceID() const {
  74. return filter_->FindIntKey("instanceId").value_or(0);
  75. }
  76. int EventMatcher::GetWindowTypeCount() const {
  77. base::ListValue* window_type_filters = nullptr;
  78. if (filter_->GetList(kWindowTypesKey, &window_type_filters))
  79. return window_type_filters->GetList().size();
  80. return 0;
  81. }
  82. bool EventMatcher::GetWindowType(int i, std::string* window_type_out) const {
  83. base::ListValue* window_types = nullptr;
  84. if (filter_->GetList(kWindowTypesKey, &window_types)) {
  85. const base::Value::List& types_list = window_types->GetList();
  86. if (i >= 0 && static_cast<size_t>(i) < types_list.size() &&
  87. types_list[i].is_string()) {
  88. *window_type_out = types_list[i].GetString();
  89. return true;
  90. }
  91. }
  92. return false;
  93. }
  94. bool EventMatcher::HasWindowTypes() const {
  95. return GetWindowTypeCount() != 0;
  96. }
  97. int EventMatcher::GetRoutingID() const {
  98. return routing_id_;
  99. }
  100. } // namespace extensions