| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "extensions/browser/api/declarative_webrequest/webrequest_rules_registr
y.h" | 5 #include "extensions/browser/api/declarative_webrequest/webrequest_rules_registr
y.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <utility> |
| 11 #include <vector> | 12 #include <vector> |
| 12 | 13 |
| 13 #include "base/json/json_reader.h" | 14 #include "base/json/json_reader.h" |
| 14 #include "base/macros.h" | 15 #include "base/macros.h" |
| 15 #include "base/memory/linked_ptr.h" | 16 #include "base/memory/linked_ptr.h" |
| 17 #include "base/memory/ptr_util.h" |
| 16 #include "base/message_loop/message_loop.h" | 18 #include "base/message_loop/message_loop.h" |
| 17 #include "base/run_loop.h" | 19 #include "base/run_loop.h" |
| 18 #include "base/stl_util.h" | 20 #include "base/stl_util.h" |
| 19 #include "base/test/values_test_util.h" | 21 #include "base/test/values_test_util.h" |
| 20 #include "base/values.h" | 22 #include "base/values.h" |
| 21 #include "chrome/common/extensions/extension_test_util.h" | 23 #include "chrome/common/extensions/extension_test_util.h" |
| 22 #include "components/url_matcher/url_matcher_constants.h" | 24 #include "components/url_matcher/url_matcher_constants.h" |
| 23 #include "content/public/test/test_browser_thread.h" | 25 #include "content/public/test/test_browser_thread.h" |
| 24 #include "extensions/browser/api/declarative/rules_registry_service.h" | 26 #include "extensions/browser/api/declarative/rules_registry_service.h" |
| 25 #include "extensions/browser/api/declarative_webrequest/webrequest_constants.h" | 27 #include "extensions/browser/api/declarative_webrequest/webrequest_constants.h" |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 void SetUp() override; | 96 void SetUp() override; |
| 95 | 97 |
| 96 void TearDown() override { | 98 void TearDown() override { |
| 97 // Make sure that deletion traits of all registries are executed. | 99 // Make sure that deletion traits of all registries are executed. |
| 98 base::RunLoop().RunUntilIdle(); | 100 base::RunLoop().RunUntilIdle(); |
| 99 } | 101 } |
| 100 | 102 |
| 101 // Returns a rule that roughly matches http://*.example.com and | 103 // Returns a rule that roughly matches http://*.example.com and |
| 102 // https://www.example.com and cancels it | 104 // https://www.example.com and cancels it |
| 103 linked_ptr<api::events::Rule> CreateRule1() { | 105 linked_ptr<api::events::Rule> CreateRule1() { |
| 104 base::ListValue* scheme_http = new base::ListValue(); | 106 auto scheme_http = base::MakeUnique<base::ListValue>(); |
| 105 scheme_http->AppendString("http"); | 107 scheme_http->AppendString("http"); |
| 106 base::DictionaryValue* http_condition_dict = new base::DictionaryValue(); | 108 auto http_condition_dict = base::MakeUnique<base::DictionaryValue>(); |
| 107 http_condition_dict->Set(keys2::kSchemesKey, scheme_http); | |
| 108 http_condition_dict->SetString(keys2::kHostSuffixKey, "example.com"); | 109 http_condition_dict->SetString(keys2::kHostSuffixKey, "example.com"); |
| 109 base::DictionaryValue http_condition_url_filter; | 110 base::DictionaryValue http_condition_url_filter; |
| 110 http_condition_url_filter.Set(keys::kUrlKey, http_condition_dict); | |
| 111 http_condition_url_filter.SetString(keys::kInstanceTypeKey, | 111 http_condition_url_filter.SetString(keys::kInstanceTypeKey, |
| 112 keys::kRequestMatcherType); | 112 keys::kRequestMatcherType); |
| 113 | 113 |
| 114 base::ListValue* scheme_https = new base::ListValue(); | |
| 115 scheme_http->AppendString("https"); | 114 scheme_http->AppendString("https"); |
| 116 base::DictionaryValue* https_condition_dict = new base::DictionaryValue(); | 115 auto https_condition_dict = base::MakeUnique<base::DictionaryValue>(); |
| 117 https_condition_dict->Set(keys2::kSchemesKey, scheme_https); | 116 https_condition_dict->Set(keys2::kSchemesKey, |
| 117 base::MakeUnique<base::ListValue>()); |
| 118 https_condition_dict->SetString(keys2::kHostSuffixKey, "example.com"); | 118 https_condition_dict->SetString(keys2::kHostSuffixKey, "example.com"); |
| 119 https_condition_dict->SetString(keys2::kHostPrefixKey, "www"); | 119 https_condition_dict->SetString(keys2::kHostPrefixKey, "www"); |
| 120 base::DictionaryValue https_condition_url_filter; | 120 base::DictionaryValue https_condition_url_filter; |
| 121 https_condition_url_filter.Set(keys::kUrlKey, https_condition_dict); | 121 https_condition_url_filter.Set(keys::kUrlKey, |
| 122 std::move(https_condition_dict)); |
| 122 https_condition_url_filter.SetString(keys::kInstanceTypeKey, | 123 https_condition_url_filter.SetString(keys::kInstanceTypeKey, |
| 123 keys::kRequestMatcherType); | 124 keys::kRequestMatcherType); |
| 124 | 125 |
| 125 base::DictionaryValue action_dict; | 126 base::DictionaryValue action_dict; |
| 126 action_dict.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType); | 127 action_dict.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType); |
| 127 | 128 |
| 128 linked_ptr<api::events::Rule> rule(new api::events::Rule); | 129 linked_ptr<api::events::Rule> rule(new api::events::Rule); |
| 129 rule->id.reset(new std::string(kRuleId1)); | 130 rule->id.reset(new std::string(kRuleId1)); |
| 130 rule->priority.reset(new int(100)); | 131 rule->priority.reset(new int(100)); |
| 131 rule->actions.push_back(action_dict.CreateDeepCopy()); | 132 rule->actions.push_back(action_dict.CreateDeepCopy()); |
| 133 http_condition_dict->Set(keys2::kSchemesKey, std::move(scheme_http)); |
| 134 http_condition_url_filter.Set(keys::kUrlKey, |
| 135 std::move(http_condition_dict)); |
| 132 rule->conditions.push_back(http_condition_url_filter.CreateDeepCopy()); | 136 rule->conditions.push_back(http_condition_url_filter.CreateDeepCopy()); |
| 133 rule->conditions.push_back(https_condition_url_filter.CreateDeepCopy()); | 137 rule->conditions.push_back(https_condition_url_filter.CreateDeepCopy()); |
| 134 return rule; | 138 return rule; |
| 135 } | 139 } |
| 136 | 140 |
| 137 // Returns a rule that matches anything and cancels it. | 141 // Returns a rule that matches anything and cancels it. |
| 138 linked_ptr<api::events::Rule> CreateRule2() { | 142 linked_ptr<api::events::Rule> CreateRule2() { |
| 139 base::DictionaryValue condition_dict; | 143 base::DictionaryValue condition_dict; |
| 140 condition_dict.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); | 144 condition_dict.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); |
| 141 | 145 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 164 rule->priority.reset(new int(100)); | 168 rule->priority.reset(new int(100)); |
| 165 rule->actions.push_back(action_dict.CreateDeepCopy()); | 169 rule->actions.push_back(action_dict.CreateDeepCopy()); |
| 166 rule->conditions.push_back(condition_dict.CreateDeepCopy()); | 170 rule->conditions.push_back(condition_dict.CreateDeepCopy()); |
| 167 return rule; | 171 return rule; |
| 168 } | 172 } |
| 169 | 173 |
| 170 // Create a rule to ignore all other rules for a destination that | 174 // Create a rule to ignore all other rules for a destination that |
| 171 // contains index.html. | 175 // contains index.html. |
| 172 linked_ptr<api::events::Rule> CreateIgnoreRule() { | 176 linked_ptr<api::events::Rule> CreateIgnoreRule() { |
| 173 base::DictionaryValue condition_dict; | 177 base::DictionaryValue condition_dict; |
| 174 base::DictionaryValue* http_condition_dict = new base::DictionaryValue(); | 178 auto http_condition_dict = base::MakeUnique<base::DictionaryValue>(); |
| 175 http_condition_dict->SetString(keys2::kPathContainsKey, "index.html"); | 179 http_condition_dict->SetString(keys2::kPathContainsKey, "index.html"); |
| 176 condition_dict.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); | 180 condition_dict.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); |
| 177 condition_dict.Set(keys::kUrlKey, http_condition_dict); | 181 condition_dict.Set(keys::kUrlKey, std::move(http_condition_dict)); |
| 178 | 182 |
| 179 base::DictionaryValue action_dict; | 183 base::DictionaryValue action_dict; |
| 180 action_dict.SetString(keys::kInstanceTypeKey, keys::kIgnoreRulesType); | 184 action_dict.SetString(keys::kInstanceTypeKey, keys::kIgnoreRulesType); |
| 181 action_dict.SetInteger(keys::kLowerPriorityThanKey, 150); | 185 action_dict.SetInteger(keys::kLowerPriorityThanKey, 150); |
| 182 | 186 |
| 183 linked_ptr<api::events::Rule> rule(new api::events::Rule); | 187 linked_ptr<api::events::Rule> rule(new api::events::Rule); |
| 184 rule->id.reset(new std::string(kRuleId4)); | 188 rule->id.reset(new std::string(kRuleId4)); |
| 185 rule->priority.reset(new int(200)); | 189 rule->priority.reset(new int(200)); |
| 186 rule->actions.push_back(action_dict.CreateDeepCopy()); | 190 rule->actions.push_back(action_dict.CreateDeepCopy()); |
| 187 rule->conditions.push_back(condition_dict.CreateDeepCopy()); | 191 rule->conditions.push_back(condition_dict.CreateDeepCopy()); |
| (...skipping 625 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 813 // This is a correct match. | 817 // This is a correct match. |
| 814 GURL url2("http://foo.com/index.html"); | 818 GURL url2("http://foo.com/index.html"); |
| 815 std::unique_ptr<net::URLRequest> request2(context.CreateRequest( | 819 std::unique_ptr<net::URLRequest> request2(context.CreateRequest( |
| 816 url2, net::DEFAULT_PRIORITY, NULL, TRAFFIC_ANNOTATION_FOR_TESTS)); | 820 url2, net::DEFAULT_PRIORITY, NULL, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 817 WebRequestData request_data2(request2.get(), ON_BEFORE_REQUEST); | 821 WebRequestData request_data2(request2.get(), ON_BEFORE_REQUEST); |
| 818 deltas = registry->CreateDeltas(NULL, request_data2, false); | 822 deltas = registry->CreateDeltas(NULL, request_data2, false); |
| 819 EXPECT_EQ(1u, deltas.size()); | 823 EXPECT_EQ(1u, deltas.size()); |
| 820 } | 824 } |
| 821 | 825 |
| 822 } // namespace extensions | 826 } // namespace extensions |
| OLD | NEW |