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 <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 | 89 |
90 void SetUp() override; | 90 void SetUp() override; |
91 | 91 |
92 void TearDown() override { | 92 void TearDown() override { |
93 // Make sure that deletion traits of all registries are executed. | 93 // Make sure that deletion traits of all registries are executed. |
94 message_loop_.RunUntilIdle(); | 94 message_loop_.RunUntilIdle(); |
95 } | 95 } |
96 | 96 |
97 // Returns a rule that roughly matches http://*.example.com and | 97 // Returns a rule that roughly matches http://*.example.com and |
98 // https://www.example.com and cancels it | 98 // https://www.example.com and cancels it |
99 linked_ptr<core_api::events::Rule> CreateRule1() { | 99 linked_ptr<api::events::Rule> CreateRule1() { |
100 base::ListValue* scheme_http = new base::ListValue(); | 100 base::ListValue* scheme_http = new base::ListValue(); |
101 scheme_http->Append(new base::StringValue("http")); | 101 scheme_http->Append(new base::StringValue("http")); |
102 base::DictionaryValue* http_condition_dict = new base::DictionaryValue(); | 102 base::DictionaryValue* http_condition_dict = new base::DictionaryValue(); |
103 http_condition_dict->Set(keys2::kSchemesKey, scheme_http); | 103 http_condition_dict->Set(keys2::kSchemesKey, scheme_http); |
104 http_condition_dict->SetString(keys2::kHostSuffixKey, "example.com"); | 104 http_condition_dict->SetString(keys2::kHostSuffixKey, "example.com"); |
105 base::DictionaryValue http_condition_url_filter; | 105 base::DictionaryValue http_condition_url_filter; |
106 http_condition_url_filter.Set(keys::kUrlKey, http_condition_dict); | 106 http_condition_url_filter.Set(keys::kUrlKey, http_condition_dict); |
107 http_condition_url_filter.SetString(keys::kInstanceTypeKey, | 107 http_condition_url_filter.SetString(keys::kInstanceTypeKey, |
108 keys::kRequestMatcherType); | 108 keys::kRequestMatcherType); |
109 | 109 |
110 base::ListValue* scheme_https = new base::ListValue(); | 110 base::ListValue* scheme_https = new base::ListValue(); |
111 scheme_http->Append(new base::StringValue("https")); | 111 scheme_http->Append(new base::StringValue("https")); |
112 base::DictionaryValue* https_condition_dict = new base::DictionaryValue(); | 112 base::DictionaryValue* https_condition_dict = new base::DictionaryValue(); |
113 https_condition_dict->Set(keys2::kSchemesKey, scheme_https); | 113 https_condition_dict->Set(keys2::kSchemesKey, scheme_https); |
114 https_condition_dict->SetString(keys2::kHostSuffixKey, "example.com"); | 114 https_condition_dict->SetString(keys2::kHostSuffixKey, "example.com"); |
115 https_condition_dict->SetString(keys2::kHostPrefixKey, "www"); | 115 https_condition_dict->SetString(keys2::kHostPrefixKey, "www"); |
116 base::DictionaryValue https_condition_url_filter; | 116 base::DictionaryValue https_condition_url_filter; |
117 https_condition_url_filter.Set(keys::kUrlKey, https_condition_dict); | 117 https_condition_url_filter.Set(keys::kUrlKey, https_condition_dict); |
118 https_condition_url_filter.SetString(keys::kInstanceTypeKey, | 118 https_condition_url_filter.SetString(keys::kInstanceTypeKey, |
119 keys::kRequestMatcherType); | 119 keys::kRequestMatcherType); |
120 | 120 |
121 base::DictionaryValue action_dict; | 121 base::DictionaryValue action_dict; |
122 action_dict.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType); | 122 action_dict.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType); |
123 | 123 |
124 linked_ptr<core_api::events::Rule> rule(new core_api::events::Rule); | 124 linked_ptr<api::events::Rule> rule(new api::events::Rule); |
125 rule->id.reset(new std::string(kRuleId1)); | 125 rule->id.reset(new std::string(kRuleId1)); |
126 rule->priority.reset(new int(100)); | 126 rule->priority.reset(new int(100)); |
127 rule->actions.push_back(linked_ptr<base::Value>(action_dict.DeepCopy())); | 127 rule->actions.push_back(linked_ptr<base::Value>(action_dict.DeepCopy())); |
128 rule->conditions.push_back( | 128 rule->conditions.push_back( |
129 linked_ptr<base::Value>(http_condition_url_filter.DeepCopy())); | 129 linked_ptr<base::Value>(http_condition_url_filter.DeepCopy())); |
130 rule->conditions.push_back( | 130 rule->conditions.push_back( |
131 linked_ptr<base::Value>(https_condition_url_filter.DeepCopy())); | 131 linked_ptr<base::Value>(https_condition_url_filter.DeepCopy())); |
132 return rule; | 132 return rule; |
133 } | 133 } |
134 | 134 |
135 // Returns a rule that matches anything and cancels it. | 135 // Returns a rule that matches anything and cancels it. |
136 linked_ptr<core_api::events::Rule> CreateRule2() { | 136 linked_ptr<api::events::Rule> CreateRule2() { |
137 base::DictionaryValue condition_dict; | 137 base::DictionaryValue condition_dict; |
138 condition_dict.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); | 138 condition_dict.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); |
139 | 139 |
140 base::DictionaryValue action_dict; | 140 base::DictionaryValue action_dict; |
141 action_dict.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType); | 141 action_dict.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType); |
142 | 142 |
143 linked_ptr<core_api::events::Rule> rule(new core_api::events::Rule); | 143 linked_ptr<api::events::Rule> rule(new api::events::Rule); |
144 rule->id.reset(new std::string(kRuleId2)); | 144 rule->id.reset(new std::string(kRuleId2)); |
145 rule->priority.reset(new int(100)); | 145 rule->priority.reset(new int(100)); |
146 rule->actions.push_back(linked_ptr<base::Value>(action_dict.DeepCopy())); | 146 rule->actions.push_back(linked_ptr<base::Value>(action_dict.DeepCopy())); |
147 rule->conditions.push_back( | 147 rule->conditions.push_back( |
148 linked_ptr<base::Value>(condition_dict.DeepCopy())); | 148 linked_ptr<base::Value>(condition_dict.DeepCopy())); |
149 return rule; | 149 return rule; |
150 } | 150 } |
151 | 151 |
152 linked_ptr<core_api::events::Rule> CreateRedirectRule( | 152 linked_ptr<api::events::Rule> CreateRedirectRule( |
153 const std::string& destination) { | 153 const std::string& destination) { |
154 base::DictionaryValue condition_dict; | 154 base::DictionaryValue condition_dict; |
155 condition_dict.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); | 155 condition_dict.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); |
156 | 156 |
157 base::DictionaryValue action_dict; | 157 base::DictionaryValue action_dict; |
158 action_dict.SetString(keys::kInstanceTypeKey, keys::kRedirectRequestType); | 158 action_dict.SetString(keys::kInstanceTypeKey, keys::kRedirectRequestType); |
159 action_dict.SetString(keys::kRedirectUrlKey, destination); | 159 action_dict.SetString(keys::kRedirectUrlKey, destination); |
160 | 160 |
161 linked_ptr<core_api::events::Rule> rule(new core_api::events::Rule); | 161 linked_ptr<api::events::Rule> rule(new api::events::Rule); |
162 rule->id.reset(new std::string(kRuleId3)); | 162 rule->id.reset(new std::string(kRuleId3)); |
163 rule->priority.reset(new int(100)); | 163 rule->priority.reset(new int(100)); |
164 rule->actions.push_back(linked_ptr<base::Value>(action_dict.DeepCopy())); | 164 rule->actions.push_back(linked_ptr<base::Value>(action_dict.DeepCopy())); |
165 rule->conditions.push_back( | 165 rule->conditions.push_back( |
166 linked_ptr<base::Value>(condition_dict.DeepCopy())); | 166 linked_ptr<base::Value>(condition_dict.DeepCopy())); |
167 return rule; | 167 return rule; |
168 } | 168 } |
169 | 169 |
170 // Create a rule to ignore all other rules for a destination that | 170 // Create a rule to ignore all other rules for a destination that |
171 // contains index.html. | 171 // contains index.html. |
172 linked_ptr<core_api::events::Rule> CreateIgnoreRule() { | 172 linked_ptr<api::events::Rule> CreateIgnoreRule() { |
173 base::DictionaryValue condition_dict; | 173 base::DictionaryValue condition_dict; |
174 base::DictionaryValue* http_condition_dict = new base::DictionaryValue(); | 174 base::DictionaryValue* http_condition_dict = new base::DictionaryValue(); |
175 http_condition_dict->SetString(keys2::kPathContainsKey, "index.html"); | 175 http_condition_dict->SetString(keys2::kPathContainsKey, "index.html"); |
176 condition_dict.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); | 176 condition_dict.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); |
177 condition_dict.Set(keys::kUrlKey, http_condition_dict); | 177 condition_dict.Set(keys::kUrlKey, http_condition_dict); |
178 | 178 |
179 base::DictionaryValue action_dict; | 179 base::DictionaryValue action_dict; |
180 action_dict.SetString(keys::kInstanceTypeKey, keys::kIgnoreRulesType); | 180 action_dict.SetString(keys::kInstanceTypeKey, keys::kIgnoreRulesType); |
181 action_dict.SetInteger(keys::kLowerPriorityThanKey, 150); | 181 action_dict.SetInteger(keys::kLowerPriorityThanKey, 150); |
182 | 182 |
183 linked_ptr<core_api::events::Rule> rule(new core_api::events::Rule); | 183 linked_ptr<api::events::Rule> rule(new api::events::Rule); |
184 rule->id.reset(new std::string(kRuleId4)); | 184 rule->id.reset(new std::string(kRuleId4)); |
185 rule->priority.reset(new int(200)); | 185 rule->priority.reset(new int(200)); |
186 rule->actions.push_back(linked_ptr<base::Value>(action_dict.DeepCopy())); | 186 rule->actions.push_back(linked_ptr<base::Value>(action_dict.DeepCopy())); |
187 rule->conditions.push_back( | 187 rule->conditions.push_back( |
188 linked_ptr<base::Value>(condition_dict.DeepCopy())); | 188 linked_ptr<base::Value>(condition_dict.DeepCopy())); |
189 return rule; | 189 return rule; |
190 } | 190 } |
191 | 191 |
192 // Create a condition with the attributes specified. An example value of | 192 // Create a condition with the attributes specified. An example value of |
193 // |attributes| is: "\"resourceType\": [\"stylesheet\"], \n". | 193 // |attributes| is: "\"resourceType\": [\"stylesheet\"], \n". |
194 linked_ptr<base::Value> CreateCondition(const std::string& attributes) { | 194 linked_ptr<base::Value> CreateCondition(const std::string& attributes) { |
195 std::string json_description = | 195 std::string json_description = |
196 "{ \n" | 196 "{ \n" |
197 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n"; | 197 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n"; |
198 json_description += attributes; | 198 json_description += attributes; |
199 json_description += "}"; | 199 json_description += "}"; |
200 | 200 |
201 return linked_ptr<base::Value>( | 201 return linked_ptr<base::Value>( |
202 base::test::ParseJson(json_description).release()); | 202 base::test::ParseJson(json_description).release()); |
203 } | 203 } |
204 | 204 |
205 // Create a rule with the ID |rule_id| and with conditions created from the | 205 // Create a rule with the ID |rule_id| and with conditions created from the |
206 // |attributes| specified (one entry one condition). An example value of a | 206 // |attributes| specified (one entry one condition). An example value of a |
207 // string from |attributes| is: "\"resourceType\": [\"stylesheet\"], \n". | 207 // string from |attributes| is: "\"resourceType\": [\"stylesheet\"], \n". |
208 linked_ptr<core_api::events::Rule> CreateCancellingRule( | 208 linked_ptr<api::events::Rule> CreateCancellingRule( |
209 const char* rule_id, | 209 const char* rule_id, |
210 const std::vector<const std::string*>& attributes) { | 210 const std::vector<const std::string*>& attributes) { |
211 base::DictionaryValue action_dict; | 211 base::DictionaryValue action_dict; |
212 action_dict.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType); | 212 action_dict.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType); |
213 | 213 |
214 linked_ptr<core_api::events::Rule> rule(new core_api::events::Rule); | 214 linked_ptr<api::events::Rule> rule(new api::events::Rule); |
215 rule->id.reset(new std::string(rule_id)); | 215 rule->id.reset(new std::string(rule_id)); |
216 rule->priority.reset(new int(1)); | 216 rule->priority.reset(new int(1)); |
217 rule->actions.push_back(linked_ptr<base::Value>(action_dict.DeepCopy())); | 217 rule->actions.push_back(linked_ptr<base::Value>(action_dict.DeepCopy())); |
218 for (std::vector<const std::string*>::const_iterator it = | 218 for (std::vector<const std::string*>::const_iterator it = |
219 attributes.begin(); | 219 attributes.begin(); |
220 it != attributes.end(); ++it) | 220 it != attributes.end(); ++it) |
221 rule->conditions.push_back(CreateCondition(**it)); | 221 rule->conditions.push_back(CreateCondition(**it)); |
222 return rule; | 222 return rule; |
223 } | 223 } |
224 | 224 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 false /*incognito_enabled*/, | 263 false /*incognito_enabled*/, |
264 false /*notifications_disabled*/); | 264 false /*notifications_disabled*/); |
265 } | 265 } |
266 | 266 |
267 | 267 |
268 TEST_F(WebRequestRulesRegistryTest, AddRulesImpl) { | 268 TEST_F(WebRequestRulesRegistryTest, AddRulesImpl) { |
269 scoped_refptr<TestWebRequestRulesRegistry> registry( | 269 scoped_refptr<TestWebRequestRulesRegistry> registry( |
270 new TestWebRequestRulesRegistry(extension_info_map_)); | 270 new TestWebRequestRulesRegistry(extension_info_map_)); |
271 std::string error; | 271 std::string error; |
272 | 272 |
273 std::vector<linked_ptr<core_api::events::Rule>> rules; | 273 std::vector<linked_ptr<api::events::Rule>> rules; |
274 rules.push_back(CreateRule1()); | 274 rules.push_back(CreateRule1()); |
275 rules.push_back(CreateRule2()); | 275 rules.push_back(CreateRule2()); |
276 | 276 |
277 error = registry->AddRules(kExtensionId, rules); | 277 error = registry->AddRules(kExtensionId, rules); |
278 EXPECT_EQ("", error); | 278 EXPECT_EQ("", error); |
279 EXPECT_EQ(1, registry->num_clear_cache_calls()); | 279 EXPECT_EQ(1, registry->num_clear_cache_calls()); |
280 | 280 |
281 std::set<const WebRequestRule*> matches; | 281 std::set<const WebRequestRule*> matches; |
282 | 282 |
283 GURL http_url("http://www.example.com"); | 283 GURL http_url("http://www.example.com"); |
(...skipping 21 matching lines...) Expand all Loading... |
305 std::make_pair(kExtensionId, kRuleId2); | 305 std::make_pair(kExtensionId, kRuleId2); |
306 EXPECT_EQ(expected_pair, (*matches.begin())->id()); | 306 EXPECT_EQ(expected_pair, (*matches.begin())->id()); |
307 } | 307 } |
308 | 308 |
309 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { | 309 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { |
310 scoped_refptr<TestWebRequestRulesRegistry> registry( | 310 scoped_refptr<TestWebRequestRulesRegistry> registry( |
311 new TestWebRequestRulesRegistry(extension_info_map_)); | 311 new TestWebRequestRulesRegistry(extension_info_map_)); |
312 std::string error; | 312 std::string error; |
313 | 313 |
314 // Setup RulesRegistry to contain two rules. | 314 // Setup RulesRegistry to contain two rules. |
315 std::vector<linked_ptr<core_api::events::Rule>> rules_to_add; | 315 std::vector<linked_ptr<api::events::Rule>> rules_to_add; |
316 rules_to_add.push_back(CreateRule1()); | 316 rules_to_add.push_back(CreateRule1()); |
317 rules_to_add.push_back(CreateRule2()); | 317 rules_to_add.push_back(CreateRule2()); |
318 error = registry->AddRules(kExtensionId, rules_to_add); | 318 error = registry->AddRules(kExtensionId, rules_to_add); |
319 EXPECT_EQ("", error); | 319 EXPECT_EQ("", error); |
320 EXPECT_EQ(1, registry->num_clear_cache_calls()); | 320 EXPECT_EQ(1, registry->num_clear_cache_calls()); |
321 | 321 |
322 // Verify initial state. | 322 // Verify initial state. |
323 std::vector<linked_ptr<core_api::events::Rule>> registered_rules; | 323 std::vector<linked_ptr<api::events::Rule>> registered_rules; |
324 registry->GetAllRules(kExtensionId, ®istered_rules); | 324 registry->GetAllRules(kExtensionId, ®istered_rules); |
325 EXPECT_EQ(2u, registered_rules.size()); | 325 EXPECT_EQ(2u, registered_rules.size()); |
326 EXPECT_EQ(1u, registry->RulesWithoutTriggers()); | 326 EXPECT_EQ(1u, registry->RulesWithoutTriggers()); |
327 | 327 |
328 // Remove first rule. | 328 // Remove first rule. |
329 std::vector<std::string> rules_to_remove; | 329 std::vector<std::string> rules_to_remove; |
330 rules_to_remove.push_back(kRuleId1); | 330 rules_to_remove.push_back(kRuleId1); |
331 error = registry->RemoveRules(kExtensionId, rules_to_remove); | 331 error = registry->RemoveRules(kExtensionId, rules_to_remove); |
332 EXPECT_EQ("", error); | 332 EXPECT_EQ("", error); |
333 EXPECT_EQ(2, registry->num_clear_cache_calls()); | 333 EXPECT_EQ(2, registry->num_clear_cache_calls()); |
(...skipping 19 matching lines...) Expand all Loading... |
353 | 353 |
354 EXPECT_TRUE(registry->IsEmpty()); | 354 EXPECT_TRUE(registry->IsEmpty()); |
355 } | 355 } |
356 | 356 |
357 TEST_F(WebRequestRulesRegistryTest, RemoveAllRulesImpl) { | 357 TEST_F(WebRequestRulesRegistryTest, RemoveAllRulesImpl) { |
358 scoped_refptr<TestWebRequestRulesRegistry> registry( | 358 scoped_refptr<TestWebRequestRulesRegistry> registry( |
359 new TestWebRequestRulesRegistry(extension_info_map_)); | 359 new TestWebRequestRulesRegistry(extension_info_map_)); |
360 std::string error; | 360 std::string error; |
361 | 361 |
362 // Setup RulesRegistry to contain two rules, one for each extension. | 362 // Setup RulesRegistry to contain two rules, one for each extension. |
363 std::vector<linked_ptr<core_api::events::Rule>> rules_to_add(1); | 363 std::vector<linked_ptr<api::events::Rule>> rules_to_add(1); |
364 rules_to_add[0] = CreateRule1(); | 364 rules_to_add[0] = CreateRule1(); |
365 error = registry->AddRules(kExtensionId, rules_to_add); | 365 error = registry->AddRules(kExtensionId, rules_to_add); |
366 EXPECT_EQ("", error); | 366 EXPECT_EQ("", error); |
367 EXPECT_EQ(1, registry->num_clear_cache_calls()); | 367 EXPECT_EQ(1, registry->num_clear_cache_calls()); |
368 | 368 |
369 rules_to_add[0] = CreateRule2(); | 369 rules_to_add[0] = CreateRule2(); |
370 error = registry->AddRules(kExtensionId2, rules_to_add); | 370 error = registry->AddRules(kExtensionId2, rules_to_add); |
371 EXPECT_EQ("", error); | 371 EXPECT_EQ("", error); |
372 EXPECT_EQ(2, registry->num_clear_cache_calls()); | 372 EXPECT_EQ(2, registry->num_clear_cache_calls()); |
373 | 373 |
374 // Verify initial state. | 374 // Verify initial state. |
375 std::vector<linked_ptr<core_api::events::Rule>> registered_rules; | 375 std::vector<linked_ptr<api::events::Rule>> registered_rules; |
376 registry->GetAllRules(kExtensionId, ®istered_rules); | 376 registry->GetAllRules(kExtensionId, ®istered_rules); |
377 EXPECT_EQ(1u, registered_rules.size()); | 377 EXPECT_EQ(1u, registered_rules.size()); |
378 registered_rules.clear(); | 378 registered_rules.clear(); |
379 registry->GetAllRules(kExtensionId2, ®istered_rules); | 379 registry->GetAllRules(kExtensionId2, ®istered_rules); |
380 EXPECT_EQ(1u, registered_rules.size()); | 380 EXPECT_EQ(1u, registered_rules.size()); |
381 | 381 |
382 // Remove rule of first extension. | 382 // Remove rule of first extension. |
383 error = registry->RemoveAllRules(kExtensionId); | 383 error = registry->RemoveAllRules(kExtensionId); |
384 EXPECT_EQ("", error); | 384 EXPECT_EQ("", error); |
385 EXPECT_EQ(3, registry->num_clear_cache_calls()); | 385 EXPECT_EQ(3, registry->num_clear_cache_calls()); |
(...skipping 18 matching lines...) Expand all Loading... |
404 | 404 |
405 EXPECT_TRUE(registry->IsEmpty()); | 405 EXPECT_TRUE(registry->IsEmpty()); |
406 } | 406 } |
407 | 407 |
408 // Test precedences between extensions. | 408 // Test precedences between extensions. |
409 TEST_F(WebRequestRulesRegistryTest, Precedences) { | 409 TEST_F(WebRequestRulesRegistryTest, Precedences) { |
410 scoped_refptr<WebRequestRulesRegistry> registry( | 410 scoped_refptr<WebRequestRulesRegistry> registry( |
411 new TestWebRequestRulesRegistry(extension_info_map_)); | 411 new TestWebRequestRulesRegistry(extension_info_map_)); |
412 std::string error; | 412 std::string error; |
413 | 413 |
414 std::vector<linked_ptr<core_api::events::Rule>> rules_to_add_1(1); | 414 std::vector<linked_ptr<api::events::Rule>> rules_to_add_1(1); |
415 rules_to_add_1[0] = CreateRedirectRule("http://www.foo.com"); | 415 rules_to_add_1[0] = CreateRedirectRule("http://www.foo.com"); |
416 error = registry->AddRules(kExtensionId, rules_to_add_1); | 416 error = registry->AddRules(kExtensionId, rules_to_add_1); |
417 EXPECT_EQ("", error); | 417 EXPECT_EQ("", error); |
418 | 418 |
419 std::vector<linked_ptr<core_api::events::Rule>> rules_to_add_2(1); | 419 std::vector<linked_ptr<api::events::Rule>> rules_to_add_2(1); |
420 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com"); | 420 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com"); |
421 error = registry->AddRules(kExtensionId2, rules_to_add_2); | 421 error = registry->AddRules(kExtensionId2, rules_to_add_2); |
422 EXPECT_EQ("", error); | 422 EXPECT_EQ("", error); |
423 | 423 |
424 GURL url("http://www.google.com"); | 424 GURL url("http://www.google.com"); |
425 net::TestURLRequestContext context; | 425 net::TestURLRequestContext context; |
426 scoped_ptr<net::URLRequest> request(context.CreateRequest( | 426 scoped_ptr<net::URLRequest> request(context.CreateRequest( |
427 url, net::DEFAULT_PRIORITY, NULL)); | 427 url, net::DEFAULT_PRIORITY, NULL)); |
428 WebRequestData request_data(request.get(), ON_BEFORE_REQUEST); | 428 WebRequestData request_data(request.get(), ON_BEFORE_REQUEST); |
429 std::list<LinkedPtrEventResponseDelta> deltas = | 429 std::list<LinkedPtrEventResponseDelta> deltas = |
(...skipping 18 matching lines...) Expand all Loading... |
448 loser->extension_install_time); | 448 loser->extension_install_time); |
449 EXPECT_EQ(GURL("http://www.foo.com"), loser->new_url); | 449 EXPECT_EQ(GURL("http://www.foo.com"), loser->new_url); |
450 } | 450 } |
451 | 451 |
452 // Test priorities of rules within one extension. | 452 // Test priorities of rules within one extension. |
453 TEST_F(WebRequestRulesRegistryTest, Priorities) { | 453 TEST_F(WebRequestRulesRegistryTest, Priorities) { |
454 scoped_refptr<WebRequestRulesRegistry> registry( | 454 scoped_refptr<WebRequestRulesRegistry> registry( |
455 new TestWebRequestRulesRegistry(extension_info_map_)); | 455 new TestWebRequestRulesRegistry(extension_info_map_)); |
456 std::string error; | 456 std::string error; |
457 | 457 |
458 std::vector<linked_ptr<core_api::events::Rule>> rules_to_add_1(1); | 458 std::vector<linked_ptr<api::events::Rule>> rules_to_add_1(1); |
459 rules_to_add_1[0] = CreateRedirectRule("http://www.foo.com"); | 459 rules_to_add_1[0] = CreateRedirectRule("http://www.foo.com"); |
460 error = registry->AddRules(kExtensionId, rules_to_add_1); | 460 error = registry->AddRules(kExtensionId, rules_to_add_1); |
461 EXPECT_EQ("", error); | 461 EXPECT_EQ("", error); |
462 | 462 |
463 std::vector<linked_ptr<core_api::events::Rule>> rules_to_add_2(1); | 463 std::vector<linked_ptr<api::events::Rule>> rules_to_add_2(1); |
464 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com"); | 464 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com"); |
465 error = registry->AddRules(kExtensionId2, rules_to_add_2); | 465 error = registry->AddRules(kExtensionId2, rules_to_add_2); |
466 EXPECT_EQ("", error); | 466 EXPECT_EQ("", error); |
467 | 467 |
468 std::vector<linked_ptr<core_api::events::Rule>> rules_to_add_3(1); | 468 std::vector<linked_ptr<api::events::Rule>> rules_to_add_3(1); |
469 rules_to_add_3[0] = CreateIgnoreRule(); | 469 rules_to_add_3[0] = CreateIgnoreRule(); |
470 error = registry->AddRules(kExtensionId, rules_to_add_3); | 470 error = registry->AddRules(kExtensionId, rules_to_add_3); |
471 EXPECT_EQ("", error); | 471 EXPECT_EQ("", error); |
472 | 472 |
473 GURL url("http://www.google.com/index.html"); | 473 GURL url("http://www.google.com/index.html"); |
474 net::TestURLRequestContext context; | 474 net::TestURLRequestContext context; |
475 scoped_ptr<net::URLRequest> request(context.CreateRequest( | 475 scoped_ptr<net::URLRequest> request(context.CreateRequest( |
476 url, net::DEFAULT_PRIORITY, NULL)); | 476 url, net::DEFAULT_PRIORITY, NULL)); |
477 WebRequestData request_data(request.get(), ON_BEFORE_REQUEST); | 477 WebRequestData request_data(request.get(), ON_BEFORE_REQUEST); |
478 std::list<LinkedPtrEventResponseDelta> deltas = | 478 std::list<LinkedPtrEventResponseDelta> deltas = |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
525 " } \n" | 525 " } \n" |
526 " ], \n" | 526 " ], \n" |
527 " \"priority\": 300 \n" | 527 " \"priority\": 300 \n" |
528 "} "; | 528 "} "; |
529 | 529 |
530 scoped_ptr<base::Value> value1 = base::JSONReader::Read(kRule1); | 530 scoped_ptr<base::Value> value1 = base::JSONReader::Read(kRule1); |
531 ASSERT_TRUE(value1.get()); | 531 ASSERT_TRUE(value1.get()); |
532 scoped_ptr<base::Value> value2 = base::JSONReader::Read(kRule2); | 532 scoped_ptr<base::Value> value2 = base::JSONReader::Read(kRule2); |
533 ASSERT_TRUE(value2.get()); | 533 ASSERT_TRUE(value2.get()); |
534 | 534 |
535 std::vector<linked_ptr<core_api::events::Rule>> rules; | 535 std::vector<linked_ptr<api::events::Rule>> rules; |
536 rules.push_back(make_linked_ptr(new core_api::events::Rule)); | 536 rules.push_back(make_linked_ptr(new api::events::Rule)); |
537 rules.push_back(make_linked_ptr(new core_api::events::Rule)); | 537 rules.push_back(make_linked_ptr(new api::events::Rule)); |
538 ASSERT_TRUE(core_api::events::Rule::Populate(*value1, rules[0].get())); | 538 ASSERT_TRUE(api::events::Rule::Populate(*value1, rules[0].get())); |
539 ASSERT_TRUE(core_api::events::Rule::Populate(*value2, rules[1].get())); | 539 ASSERT_TRUE(api::events::Rule::Populate(*value2, rules[1].get())); |
540 | 540 |
541 scoped_refptr<WebRequestRulesRegistry> registry( | 541 scoped_refptr<WebRequestRulesRegistry> registry( |
542 new TestWebRequestRulesRegistry(extension_info_map_)); | 542 new TestWebRequestRulesRegistry(extension_info_map_)); |
543 std::string error = registry->AddRulesImpl(kExtensionId, rules); | 543 std::string error = registry->AddRulesImpl(kExtensionId, rules); |
544 EXPECT_EQ("", error); | 544 EXPECT_EQ("", error); |
545 EXPECT_FALSE(registry->IsEmpty()); | 545 EXPECT_FALSE(registry->IsEmpty()); |
546 | 546 |
547 GURL url("http://www.foo.com/test"); | 547 GURL url("http://www.foo.com/test"); |
548 net::TestURLRequestContext context; | 548 net::TestURLRequestContext context; |
549 scoped_ptr<net::URLRequest> request(context.CreateRequest( | 549 scoped_ptr<net::URLRequest> request(context.CreateRequest( |
(...skipping 14 matching lines...) Expand all Loading... |
564 scoped_refptr<TestWebRequestRulesRegistry> registry( | 564 scoped_refptr<TestWebRequestRulesRegistry> registry( |
565 new TestWebRequestRulesRegistry(extension_info_map_)); | 565 new TestWebRequestRulesRegistry(extension_info_map_)); |
566 const std::string kMatchingUrlAttribute( | 566 const std::string kMatchingUrlAttribute( |
567 "\"url\": { \"pathContains\": \"\" }, \n"); | 567 "\"url\": { \"pathContains\": \"\" }, \n"); |
568 const std::string kNonMatchingNonUrlAttribute( | 568 const std::string kNonMatchingNonUrlAttribute( |
569 "\"resourceType\": [\"stylesheet\"], \n"); | 569 "\"resourceType\": [\"stylesheet\"], \n"); |
570 const std::string kBothAttributes(kMatchingUrlAttribute + | 570 const std::string kBothAttributes(kMatchingUrlAttribute + |
571 kNonMatchingNonUrlAttribute); | 571 kNonMatchingNonUrlAttribute); |
572 std::string error; | 572 std::string error; |
573 std::vector<const std::string*> attributes; | 573 std::vector<const std::string*> attributes; |
574 std::vector<linked_ptr<core_api::events::Rule>> rules; | 574 std::vector<linked_ptr<api::events::Rule>> rules; |
575 | 575 |
576 // Rules 1 and 2 have one condition, neither of them should fire. | 576 // Rules 1 and 2 have one condition, neither of them should fire. |
577 attributes.push_back(&kNonMatchingNonUrlAttribute); | 577 attributes.push_back(&kNonMatchingNonUrlAttribute); |
578 rules.push_back(CreateCancellingRule(kRuleId1, attributes)); | 578 rules.push_back(CreateCancellingRule(kRuleId1, attributes)); |
579 | 579 |
580 attributes.clear(); | 580 attributes.clear(); |
581 attributes.push_back(&kBothAttributes); | 581 attributes.push_back(&kBothAttributes); |
582 rules.push_back(CreateCancellingRule(kRuleId2, attributes)); | 582 rules.push_back(CreateCancellingRule(kRuleId2, attributes)); |
583 | 583 |
584 // Rule 3 has two conditions, one with a matching URL attribute, and one | 584 // Rule 3 has two conditions, one with a matching URL attribute, and one |
(...skipping 26 matching lines...) Expand all Loading... |
611 // differ. | 611 // differ. |
612 TEST_F(WebRequestRulesRegistryTest, GetMatchesDifferentUrls) { | 612 TEST_F(WebRequestRulesRegistryTest, GetMatchesDifferentUrls) { |
613 scoped_refptr<TestWebRequestRulesRegistry> registry( | 613 scoped_refptr<TestWebRequestRulesRegistry> registry( |
614 new TestWebRequestRulesRegistry(extension_info_map_)); | 614 new TestWebRequestRulesRegistry(extension_info_map_)); |
615 const std::string kUrlAttribute( | 615 const std::string kUrlAttribute( |
616 "\"url\": { \"hostContains\": \"url\" }, \n"); | 616 "\"url\": { \"hostContains\": \"url\" }, \n"); |
617 const std::string kFirstPartyUrlAttribute( | 617 const std::string kFirstPartyUrlAttribute( |
618 "\"firstPartyForCookiesUrl\": { \"hostContains\": \"fpfc\" }, \n"); | 618 "\"firstPartyForCookiesUrl\": { \"hostContains\": \"fpfc\" }, \n"); |
619 std::string error; | 619 std::string error; |
620 std::vector<const std::string*> attributes; | 620 std::vector<const std::string*> attributes; |
621 std::vector<linked_ptr<core_api::events::Rule>> rules; | 621 std::vector<linked_ptr<api::events::Rule>> rules; |
622 | 622 |
623 // Rule 1 has one condition, with a url attribute | 623 // Rule 1 has one condition, with a url attribute |
624 attributes.push_back(&kUrlAttribute); | 624 attributes.push_back(&kUrlAttribute); |
625 rules.push_back(CreateCancellingRule(kRuleId1, attributes)); | 625 rules.push_back(CreateCancellingRule(kRuleId1, attributes)); |
626 | 626 |
627 // Rule 2 has one condition, with a firstPartyForCookiesUrl attribute | 627 // Rule 2 has one condition, with a firstPartyForCookiesUrl attribute |
628 attributes.clear(); | 628 attributes.clear(); |
629 attributes.push_back(&kFirstPartyUrlAttribute); | 629 attributes.push_back(&kFirstPartyUrlAttribute); |
630 rules.push_back(CreateCancellingRule(kRuleId2, attributes)); | 630 rules.push_back(CreateCancellingRule(kRuleId2, attributes)); |
631 | 631 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
692 " \"name\": \"Content-Type\", \n" | 692 " \"name\": \"Content-Type\", \n" |
693 " \"value\": \"text/plain\" \n" | 693 " \"value\": \"text/plain\" \n" |
694 " } \n" | 694 " } \n" |
695 " ], \n" | 695 " ], \n" |
696 " \"priority\": 200 \n" | 696 " \"priority\": 200 \n" |
697 "} "; | 697 "} "; |
698 | 698 |
699 scoped_ptr<base::Value> value = base::JSONReader::Read(kRule); | 699 scoped_ptr<base::Value> value = base::JSONReader::Read(kRule); |
700 ASSERT_TRUE(value); | 700 ASSERT_TRUE(value); |
701 | 701 |
702 core_api::events::Rule rule; | 702 api::events::Rule rule; |
703 ASSERT_TRUE(core_api::events::Rule::Populate(*value, &rule)); | 703 ASSERT_TRUE(api::events::Rule::Populate(*value, &rule)); |
704 | 704 |
705 std::string error; | 705 std::string error; |
706 URLMatcher matcher; | 706 URLMatcher matcher; |
707 scoped_ptr<WebRequestConditionSet> conditions = | 707 scoped_ptr<WebRequestConditionSet> conditions = |
708 WebRequestConditionSet::Create( | 708 WebRequestConditionSet::Create( |
709 NULL, matcher.condition_factory(), rule.conditions, &error); | 709 NULL, matcher.condition_factory(), rule.conditions, &error); |
710 ASSERT_TRUE(error.empty()) << error; | 710 ASSERT_TRUE(error.empty()) << error; |
711 ASSERT_TRUE(conditions); | 711 ASSERT_TRUE(conditions); |
712 | 712 |
713 bool bad_message = false; | 713 bool bad_message = false; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
782 " \"instanceType\": \"declarativeWebRequest.RedirectRequest\",\n" | 782 " \"instanceType\": \"declarativeWebRequest.RedirectRequest\",\n" |
783 " \"redirectUrl\": \"http://bar.com\" \n" | 783 " \"redirectUrl\": \"http://bar.com\" \n" |
784 " } \n" | 784 " } \n" |
785 " ], \n" | 785 " ], \n" |
786 " \"priority\": 200 \n" | 786 " \"priority\": 200 \n" |
787 "} "; | 787 "} "; |
788 | 788 |
789 scoped_ptr<base::Value> value = base::JSONReader::Read(kRule); | 789 scoped_ptr<base::Value> value = base::JSONReader::Read(kRule); |
790 ASSERT_TRUE(value.get()); | 790 ASSERT_TRUE(value.get()); |
791 | 791 |
792 std::vector<linked_ptr<core_api::events::Rule>> rules; | 792 std::vector<linked_ptr<api::events::Rule>> rules; |
793 rules.push_back(make_linked_ptr(new core_api::events::Rule)); | 793 rules.push_back(make_linked_ptr(new api::events::Rule)); |
794 ASSERT_TRUE(core_api::events::Rule::Populate(*value, rules.back().get())); | 794 ASSERT_TRUE(api::events::Rule::Populate(*value, rules.back().get())); |
795 | 795 |
796 scoped_refptr<WebRequestRulesRegistry> registry( | 796 scoped_refptr<WebRequestRulesRegistry> registry( |
797 new TestWebRequestRulesRegistry(extension_info_map_)); | 797 new TestWebRequestRulesRegistry(extension_info_map_)); |
798 | 798 |
799 URLMatcher matcher; | 799 URLMatcher matcher; |
800 std::string error = registry->AddRulesImpl(kExtensionId, rules); | 800 std::string error = registry->AddRulesImpl(kExtensionId, rules); |
801 EXPECT_EQ("", error); | 801 EXPECT_EQ("", error); |
802 | 802 |
803 net::TestURLRequestContext context; | 803 net::TestURLRequestContext context; |
804 std::list<LinkedPtrEventResponseDelta> deltas; | 804 std::list<LinkedPtrEventResponseDelta> deltas; |
805 | 805 |
806 // No match because match is in the query parameter. | 806 // No match because match is in the query parameter. |
807 GURL url1("http://bar.com/index.html?foo.com"); | 807 GURL url1("http://bar.com/index.html?foo.com"); |
808 scoped_ptr<net::URLRequest> request1(context.CreateRequest( | 808 scoped_ptr<net::URLRequest> request1(context.CreateRequest( |
809 url1, net::DEFAULT_PRIORITY, NULL)); | 809 url1, net::DEFAULT_PRIORITY, NULL)); |
810 WebRequestData request_data1(request1.get(), ON_BEFORE_REQUEST); | 810 WebRequestData request_data1(request1.get(), ON_BEFORE_REQUEST); |
811 deltas = registry->CreateDeltas(NULL, request_data1, false); | 811 deltas = registry->CreateDeltas(NULL, request_data1, false); |
812 EXPECT_EQ(0u, deltas.size()); | 812 EXPECT_EQ(0u, deltas.size()); |
813 | 813 |
814 // This is a correct match. | 814 // This is a correct match. |
815 GURL url2("http://foo.com/index.html"); | 815 GURL url2("http://foo.com/index.html"); |
816 scoped_ptr<net::URLRequest> request2(context.CreateRequest( | 816 scoped_ptr<net::URLRequest> request2(context.CreateRequest( |
817 url2, net::DEFAULT_PRIORITY, NULL)); | 817 url2, net::DEFAULT_PRIORITY, NULL)); |
818 WebRequestData request_data2(request2.get(), ON_BEFORE_REQUEST); | 818 WebRequestData request_data2(request2.get(), ON_BEFORE_REQUEST); |
819 deltas = registry->CreateDeltas(NULL, request_data2, false); | 819 deltas = registry->CreateDeltas(NULL, request_data2, false); |
820 EXPECT_EQ(1u, deltas.size()); | 820 EXPECT_EQ(1u, deltas.size()); |
821 } | 821 } |
822 | 822 |
823 } // namespace extensions | 823 } // namespace extensions |
OLD | NEW |