| 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 <string> | 10 #include <string> |
| 10 #include <vector> | 11 #include <vector> |
| 11 | 12 |
| 12 #include "base/json/json_reader.h" | 13 #include "base/json/json_reader.h" |
| 13 #include "base/macros.h" | 14 #include "base/macros.h" |
| 14 #include "base/memory/linked_ptr.h" | 15 #include "base/memory/linked_ptr.h" |
| 15 #include "base/memory/scoped_ptr.h" | |
| 16 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
| 17 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
| 18 #include "base/test/values_test_util.h" | 18 #include "base/test/values_test_util.h" |
| 19 #include "base/values.h" | 19 #include "base/values.h" |
| 20 #include "chrome/common/extensions/extension_test_util.h" | 20 #include "chrome/common/extensions/extension_test_util.h" |
| 21 #include "components/url_matcher/url_matcher_constants.h" | 21 #include "components/url_matcher/url_matcher_constants.h" |
| 22 #include "content/public/test/test_browser_thread.h" | 22 #include "content/public/test/test_browser_thread.h" |
| 23 #include "extensions/browser/api/declarative/rules_registry_service.h" | 23 #include "extensions/browser/api/declarative/rules_registry_service.h" |
| 24 #include "extensions/browser/api/declarative_webrequest/webrequest_constants.h" | 24 #include "extensions/browser/api/declarative_webrequest/webrequest_constants.h" |
| 25 #include "extensions/browser/api/web_request/web_request_api_helpers.h" | 25 #include "extensions/browser/api/web_request/web_request_api_helpers.h" |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 linked_ptr<api::events::Rule> rule(new api::events::Rule); | 181 linked_ptr<api::events::Rule> rule(new api::events::Rule); |
| 182 rule->id.reset(new std::string(kRuleId4)); | 182 rule->id.reset(new std::string(kRuleId4)); |
| 183 rule->priority.reset(new int(200)); | 183 rule->priority.reset(new int(200)); |
| 184 rule->actions.push_back(action_dict.CreateDeepCopy()); | 184 rule->actions.push_back(action_dict.CreateDeepCopy()); |
| 185 rule->conditions.push_back(condition_dict.CreateDeepCopy()); | 185 rule->conditions.push_back(condition_dict.CreateDeepCopy()); |
| 186 return rule; | 186 return rule; |
| 187 } | 187 } |
| 188 | 188 |
| 189 // Create a condition with the attributes specified. An example value of | 189 // Create a condition with the attributes specified. An example value of |
| 190 // |attributes| is: "\"resourceType\": [\"stylesheet\"], \n". | 190 // |attributes| is: "\"resourceType\": [\"stylesheet\"], \n". |
| 191 scoped_ptr<base::Value> CreateCondition(const std::string& attributes) { | 191 std::unique_ptr<base::Value> CreateCondition(const std::string& attributes) { |
| 192 std::string json_description = | 192 std::string json_description = |
| 193 "{ \n" | 193 "{ \n" |
| 194 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n"; | 194 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n"; |
| 195 json_description += attributes; | 195 json_description += attributes; |
| 196 json_description += "}"; | 196 json_description += "}"; |
| 197 | 197 |
| 198 return base::test::ParseJson(json_description); | 198 return base::test::ParseJson(json_description); |
| 199 } | 199 } |
| 200 | 200 |
| 201 // Create a rule with the ID |rule_id| and with conditions created from the | 201 // Create a rule with the ID |rule_id| and with conditions created from the |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 rules.push_back(CreateRule2()); | 271 rules.push_back(CreateRule2()); |
| 272 | 272 |
| 273 error = registry->AddRules(kExtensionId, rules); | 273 error = registry->AddRules(kExtensionId, rules); |
| 274 EXPECT_EQ("", error); | 274 EXPECT_EQ("", error); |
| 275 EXPECT_EQ(1, registry->num_clear_cache_calls()); | 275 EXPECT_EQ(1, registry->num_clear_cache_calls()); |
| 276 | 276 |
| 277 std::set<const WebRequestRule*> matches; | 277 std::set<const WebRequestRule*> matches; |
| 278 | 278 |
| 279 GURL http_url("http://www.example.com"); | 279 GURL http_url("http://www.example.com"); |
| 280 net::TestURLRequestContext context; | 280 net::TestURLRequestContext context; |
| 281 scoped_ptr<net::URLRequest> http_request(context.CreateRequest( | 281 std::unique_ptr<net::URLRequest> http_request( |
| 282 http_url, net::DEFAULT_PRIORITY, NULL)); | 282 context.CreateRequest(http_url, net::DEFAULT_PRIORITY, NULL)); |
| 283 WebRequestData request_data(http_request.get(), ON_BEFORE_REQUEST); | 283 WebRequestData request_data(http_request.get(), ON_BEFORE_REQUEST); |
| 284 matches = registry->GetMatches(request_data); | 284 matches = registry->GetMatches(request_data); |
| 285 EXPECT_EQ(2u, matches.size()); | 285 EXPECT_EQ(2u, matches.size()); |
| 286 | 286 |
| 287 std::set<WebRequestRule::GlobalRuleId> matches_ids; | 287 std::set<WebRequestRule::GlobalRuleId> matches_ids; |
| 288 for (std::set<const WebRequestRule*>::const_iterator it = matches.begin(); | 288 for (std::set<const WebRequestRule*>::const_iterator it = matches.begin(); |
| 289 it != matches.end(); ++it) | 289 it != matches.end(); ++it) |
| 290 matches_ids.insert((*it)->id()); | 290 matches_ids.insert((*it)->id()); |
| 291 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId1))); | 291 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId1))); |
| 292 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId2))); | 292 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId2))); |
| 293 | 293 |
| 294 GURL foobar_url("http://www.foobar.com"); | 294 GURL foobar_url("http://www.foobar.com"); |
| 295 scoped_ptr<net::URLRequest> foobar_request(context.CreateRequest( | 295 std::unique_ptr<net::URLRequest> foobar_request( |
| 296 foobar_url, net::DEFAULT_PRIORITY, NULL)); | 296 context.CreateRequest(foobar_url, net::DEFAULT_PRIORITY, NULL)); |
| 297 request_data.request = foobar_request.get(); | 297 request_data.request = foobar_request.get(); |
| 298 matches = registry->GetMatches(request_data); | 298 matches = registry->GetMatches(request_data); |
| 299 EXPECT_EQ(1u, matches.size()); | 299 EXPECT_EQ(1u, matches.size()); |
| 300 WebRequestRule::GlobalRuleId expected_pair = | 300 WebRequestRule::GlobalRuleId expected_pair = |
| 301 std::make_pair(kExtensionId, kRuleId2); | 301 std::make_pair(kExtensionId, kRuleId2); |
| 302 EXPECT_EQ(expected_pair, (*matches.begin())->id()); | 302 EXPECT_EQ(expected_pair, (*matches.begin())->id()); |
| 303 } | 303 } |
| 304 | 304 |
| 305 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { | 305 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { |
| 306 scoped_refptr<TestWebRequestRulesRegistry> registry( | 306 scoped_refptr<TestWebRequestRulesRegistry> registry( |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 412 error = registry->AddRules(kExtensionId, rules_to_add_1); | 412 error = registry->AddRules(kExtensionId, rules_to_add_1); |
| 413 EXPECT_EQ("", error); | 413 EXPECT_EQ("", error); |
| 414 | 414 |
| 415 std::vector<linked_ptr<api::events::Rule>> rules_to_add_2(1); | 415 std::vector<linked_ptr<api::events::Rule>> rules_to_add_2(1); |
| 416 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com"); | 416 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com"); |
| 417 error = registry->AddRules(kExtensionId2, rules_to_add_2); | 417 error = registry->AddRules(kExtensionId2, rules_to_add_2); |
| 418 EXPECT_EQ("", error); | 418 EXPECT_EQ("", error); |
| 419 | 419 |
| 420 GURL url("http://www.google.com"); | 420 GURL url("http://www.google.com"); |
| 421 net::TestURLRequestContext context; | 421 net::TestURLRequestContext context; |
| 422 scoped_ptr<net::URLRequest> request(context.CreateRequest( | 422 std::unique_ptr<net::URLRequest> request( |
| 423 url, net::DEFAULT_PRIORITY, NULL)); | 423 context.CreateRequest(url, net::DEFAULT_PRIORITY, NULL)); |
| 424 WebRequestData request_data(request.get(), ON_BEFORE_REQUEST); | 424 WebRequestData request_data(request.get(), ON_BEFORE_REQUEST); |
| 425 std::list<LinkedPtrEventResponseDelta> deltas = | 425 std::list<LinkedPtrEventResponseDelta> deltas = |
| 426 registry->CreateDeltas(NULL, request_data, false); | 426 registry->CreateDeltas(NULL, request_data, false); |
| 427 | 427 |
| 428 // The second extension is installed later and will win for this reason | 428 // The second extension is installed later and will win for this reason |
| 429 // in conflict resolution. | 429 // in conflict resolution. |
| 430 ASSERT_EQ(2u, deltas.size()); | 430 ASSERT_EQ(2u, deltas.size()); |
| 431 deltas.sort(&helpers::InDecreasingExtensionInstallationTimeOrder); | 431 deltas.sort(&helpers::InDecreasingExtensionInstallationTimeOrder); |
| 432 | 432 |
| 433 std::list<LinkedPtrEventResponseDelta>::iterator i = deltas.begin(); | 433 std::list<LinkedPtrEventResponseDelta>::iterator i = deltas.begin(); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 461 error = registry->AddRules(kExtensionId2, rules_to_add_2); | 461 error = registry->AddRules(kExtensionId2, rules_to_add_2); |
| 462 EXPECT_EQ("", error); | 462 EXPECT_EQ("", error); |
| 463 | 463 |
| 464 std::vector<linked_ptr<api::events::Rule>> rules_to_add_3(1); | 464 std::vector<linked_ptr<api::events::Rule>> rules_to_add_3(1); |
| 465 rules_to_add_3[0] = CreateIgnoreRule(); | 465 rules_to_add_3[0] = CreateIgnoreRule(); |
| 466 error = registry->AddRules(kExtensionId, rules_to_add_3); | 466 error = registry->AddRules(kExtensionId, rules_to_add_3); |
| 467 EXPECT_EQ("", error); | 467 EXPECT_EQ("", error); |
| 468 | 468 |
| 469 GURL url("http://www.google.com/index.html"); | 469 GURL url("http://www.google.com/index.html"); |
| 470 net::TestURLRequestContext context; | 470 net::TestURLRequestContext context; |
| 471 scoped_ptr<net::URLRequest> request(context.CreateRequest( | 471 std::unique_ptr<net::URLRequest> request( |
| 472 url, net::DEFAULT_PRIORITY, NULL)); | 472 context.CreateRequest(url, net::DEFAULT_PRIORITY, NULL)); |
| 473 WebRequestData request_data(request.get(), ON_BEFORE_REQUEST); | 473 WebRequestData request_data(request.get(), ON_BEFORE_REQUEST); |
| 474 std::list<LinkedPtrEventResponseDelta> deltas = | 474 std::list<LinkedPtrEventResponseDelta> deltas = |
| 475 registry->CreateDeltas(NULL, request_data, false); | 475 registry->CreateDeltas(NULL, request_data, false); |
| 476 | 476 |
| 477 // The redirect by the first extension is ignored due to the ignore rule. | 477 // The redirect by the first extension is ignored due to the ignore rule. |
| 478 ASSERT_EQ(1u, deltas.size()); | 478 ASSERT_EQ(1u, deltas.size()); |
| 479 LinkedPtrEventResponseDelta effective_rule = *(deltas.begin()); | 479 LinkedPtrEventResponseDelta effective_rule = *(deltas.begin()); |
| 480 | 480 |
| 481 EXPECT_EQ(kExtensionId2, effective_rule->extension_id); | 481 EXPECT_EQ(kExtensionId2, effective_rule->extension_id); |
| 482 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2), | 482 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2), |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 516 " ], \n" | 516 " ], \n" |
| 517 " \"actions\": [ \n" | 517 " \"actions\": [ \n" |
| 518 " { \n" | 518 " { \n" |
| 519 " \"instanceType\": \"declarativeWebRequest.IgnoreRules\", \n" | 519 " \"instanceType\": \"declarativeWebRequest.IgnoreRules\", \n" |
| 520 " \"hasTag\": \"ignore_tag\" \n" | 520 " \"hasTag\": \"ignore_tag\" \n" |
| 521 " } \n" | 521 " } \n" |
| 522 " ], \n" | 522 " ], \n" |
| 523 " \"priority\": 300 \n" | 523 " \"priority\": 300 \n" |
| 524 "} "; | 524 "} "; |
| 525 | 525 |
| 526 scoped_ptr<base::Value> value1 = base::JSONReader::Read(kRule1); | 526 std::unique_ptr<base::Value> value1 = base::JSONReader::Read(kRule1); |
| 527 ASSERT_TRUE(value1.get()); | 527 ASSERT_TRUE(value1.get()); |
| 528 scoped_ptr<base::Value> value2 = base::JSONReader::Read(kRule2); | 528 std::unique_ptr<base::Value> value2 = base::JSONReader::Read(kRule2); |
| 529 ASSERT_TRUE(value2.get()); | 529 ASSERT_TRUE(value2.get()); |
| 530 | 530 |
| 531 std::vector<linked_ptr<api::events::Rule>> rules; | 531 std::vector<linked_ptr<api::events::Rule>> rules; |
| 532 rules.push_back(make_linked_ptr(new api::events::Rule)); | 532 rules.push_back(make_linked_ptr(new api::events::Rule)); |
| 533 rules.push_back(make_linked_ptr(new api::events::Rule)); | 533 rules.push_back(make_linked_ptr(new api::events::Rule)); |
| 534 ASSERT_TRUE(api::events::Rule::Populate(*value1, rules[0].get())); | 534 ASSERT_TRUE(api::events::Rule::Populate(*value1, rules[0].get())); |
| 535 ASSERT_TRUE(api::events::Rule::Populate(*value2, rules[1].get())); | 535 ASSERT_TRUE(api::events::Rule::Populate(*value2, rules[1].get())); |
| 536 | 536 |
| 537 scoped_refptr<WebRequestRulesRegistry> registry( | 537 scoped_refptr<WebRequestRulesRegistry> registry( |
| 538 new TestWebRequestRulesRegistry(extension_info_map_)); | 538 new TestWebRequestRulesRegistry(extension_info_map_)); |
| 539 std::string error = registry->AddRulesImpl(kExtensionId, rules); | 539 std::string error = registry->AddRulesImpl(kExtensionId, rules); |
| 540 EXPECT_EQ("", error); | 540 EXPECT_EQ("", error); |
| 541 EXPECT_FALSE(registry->IsEmpty()); | 541 EXPECT_FALSE(registry->IsEmpty()); |
| 542 | 542 |
| 543 GURL url("http://www.foo.com/test"); | 543 GURL url("http://www.foo.com/test"); |
| 544 net::TestURLRequestContext context; | 544 net::TestURLRequestContext context; |
| 545 scoped_ptr<net::URLRequest> request(context.CreateRequest( | 545 std::unique_ptr<net::URLRequest> request( |
| 546 url, net::DEFAULT_PRIORITY, NULL)); | 546 context.CreateRequest(url, net::DEFAULT_PRIORITY, NULL)); |
| 547 WebRequestData request_data(request.get(), ON_BEFORE_REQUEST); | 547 WebRequestData request_data(request.get(), ON_BEFORE_REQUEST); |
| 548 std::list<LinkedPtrEventResponseDelta> deltas = | 548 std::list<LinkedPtrEventResponseDelta> deltas = |
| 549 registry->CreateDeltas(NULL, request_data, false); | 549 registry->CreateDeltas(NULL, request_data, false); |
| 550 | 550 |
| 551 // The redirect by the redirect rule is ignored due to the ignore rule. | 551 // The redirect by the redirect rule is ignored due to the ignore rule. |
| 552 std::set<const WebRequestRule*> matches = registry->GetMatches(request_data); | 552 std::set<const WebRequestRule*> matches = registry->GetMatches(request_data); |
| 553 EXPECT_EQ(2u, matches.size()); | 553 EXPECT_EQ(2u, matches.size()); |
| 554 ASSERT_EQ(0u, deltas.size()); | 554 ASSERT_EQ(0u, deltas.size()); |
| 555 } | 555 } |
| 556 | 556 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 585 rules.push_back(CreateCancellingRule(kRuleId3, attributes)); | 585 rules.push_back(CreateCancellingRule(kRuleId3, attributes)); |
| 586 | 586 |
| 587 error = registry->AddRules(kExtensionId, rules); | 587 error = registry->AddRules(kExtensionId, rules); |
| 588 EXPECT_EQ("", error); | 588 EXPECT_EQ("", error); |
| 589 EXPECT_EQ(1, registry->num_clear_cache_calls()); | 589 EXPECT_EQ(1, registry->num_clear_cache_calls()); |
| 590 | 590 |
| 591 std::set<const WebRequestRule*> matches; | 591 std::set<const WebRequestRule*> matches; |
| 592 | 592 |
| 593 GURL http_url("http://www.example.com"); | 593 GURL http_url("http://www.example.com"); |
| 594 net::TestURLRequestContext context; | 594 net::TestURLRequestContext context; |
| 595 scoped_ptr<net::URLRequest> http_request(context.CreateRequest( | 595 std::unique_ptr<net::URLRequest> http_request( |
| 596 http_url, net::DEFAULT_PRIORITY, NULL)); | 596 context.CreateRequest(http_url, net::DEFAULT_PRIORITY, NULL)); |
| 597 WebRequestData request_data(http_request.get(), ON_BEFORE_REQUEST); | 597 WebRequestData request_data(http_request.get(), ON_BEFORE_REQUEST); |
| 598 matches = registry->GetMatches(request_data); | 598 matches = registry->GetMatches(request_data); |
| 599 EXPECT_EQ(1u, matches.size()); | 599 EXPECT_EQ(1u, matches.size()); |
| 600 WebRequestRule::GlobalRuleId expected_pair = std::make_pair(kExtensionId, | 600 WebRequestRule::GlobalRuleId expected_pair = std::make_pair(kExtensionId, |
| 601 kRuleId3); | 601 kRuleId3); |
| 602 EXPECT_EQ(expected_pair, (*matches.begin())->id()); | 602 EXPECT_EQ(expected_pair, (*matches.begin())->id()); |
| 603 } | 603 } |
| 604 | 604 |
| 605 // Test that the url and firstPartyForCookiesUrl attributes are evaluated | 605 // Test that the url and firstPartyForCookiesUrl attributes are evaluated |
| 606 // against corresponding URLs. Tested on requests where these URLs actually | 606 // against corresponding URLs. Tested on requests where these URLs actually |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 644 static_assert(arraysize(urls) == arraysize(firstPartyUrls), | 644 static_assert(arraysize(urls) == arraysize(firstPartyUrls), |
| 645 "urls and firstPartyUrls must have the same number " | 645 "urls and firstPartyUrls must have the same number " |
| 646 "of elements"); | 646 "of elements"); |
| 647 static_assert(arraysize(urls) == arraysize(matchingRuleIds), | 647 static_assert(arraysize(urls) == arraysize(matchingRuleIds), |
| 648 "urls and matchingRuleIds must have the same number " | 648 "urls and matchingRuleIds must have the same number " |
| 649 "of elements"); | 649 "of elements"); |
| 650 net::TestURLRequestContext context; | 650 net::TestURLRequestContext context; |
| 651 | 651 |
| 652 for (size_t i = 0; i < arraysize(matchingRuleIds); ++i) { | 652 for (size_t i = 0; i < arraysize(matchingRuleIds); ++i) { |
| 653 // Construct the inputs. | 653 // Construct the inputs. |
| 654 scoped_ptr<net::URLRequest> http_request(context.CreateRequest( | 654 std::unique_ptr<net::URLRequest> http_request( |
| 655 urls[i], net::DEFAULT_PRIORITY, NULL)); | 655 context.CreateRequest(urls[i], net::DEFAULT_PRIORITY, NULL)); |
| 656 WebRequestData request_data(http_request.get(), ON_BEFORE_REQUEST); | 656 WebRequestData request_data(http_request.get(), ON_BEFORE_REQUEST); |
| 657 http_request->set_first_party_for_cookies(firstPartyUrls[i]); | 657 http_request->set_first_party_for_cookies(firstPartyUrls[i]); |
| 658 // Now run both rules on the input. | 658 // Now run both rules on the input. |
| 659 matches = registry->GetMatches(request_data); | 659 matches = registry->GetMatches(request_data); |
| 660 SCOPED_TRACE(testing::Message("i = ") << i << ", rule id = " | 660 SCOPED_TRACE(testing::Message("i = ") << i << ", rule id = " |
| 661 << matchingRuleIds[i]); | 661 << matchingRuleIds[i]); |
| 662 // Make sure that the right rule succeeded. | 662 // Make sure that the right rule succeeded. |
| 663 EXPECT_EQ(1u, matches.size()); | 663 EXPECT_EQ(1u, matches.size()); |
| 664 EXPECT_EQ(WebRequestRule::GlobalRuleId(std::make_pair(kExtensionId, | 664 EXPECT_EQ(WebRequestRule::GlobalRuleId(std::make_pair(kExtensionId, |
| 665 matchingRuleIds[i])), | 665 matchingRuleIds[i])), |
| (...skipping 19 matching lines...) Expand all Loading... |
| 685 " \"actions\": [ \n" | 685 " \"actions\": [ \n" |
| 686 " { \n" | 686 " { \n" |
| 687 " \"instanceType\": \"declarativeWebRequest.SetRequestHeader\",\n" | 687 " \"instanceType\": \"declarativeWebRequest.SetRequestHeader\",\n" |
| 688 " \"name\": \"Content-Type\", \n" | 688 " \"name\": \"Content-Type\", \n" |
| 689 " \"value\": \"text/plain\" \n" | 689 " \"value\": \"text/plain\" \n" |
| 690 " } \n" | 690 " } \n" |
| 691 " ], \n" | 691 " ], \n" |
| 692 " \"priority\": 200 \n" | 692 " \"priority\": 200 \n" |
| 693 "} "; | 693 "} "; |
| 694 | 694 |
| 695 scoped_ptr<base::Value> value = base::JSONReader::Read(kRule); | 695 std::unique_ptr<base::Value> value = base::JSONReader::Read(kRule); |
| 696 ASSERT_TRUE(value); | 696 ASSERT_TRUE(value); |
| 697 | 697 |
| 698 api::events::Rule rule; | 698 api::events::Rule rule; |
| 699 ASSERT_TRUE(api::events::Rule::Populate(*value, &rule)); | 699 ASSERT_TRUE(api::events::Rule::Populate(*value, &rule)); |
| 700 | 700 |
| 701 std::string error; | 701 std::string error; |
| 702 URLMatcher matcher; | 702 URLMatcher matcher; |
| 703 scoped_ptr<WebRequestConditionSet> conditions = | 703 std::unique_ptr<WebRequestConditionSet> conditions = |
| 704 WebRequestConditionSet::Create( | 704 WebRequestConditionSet::Create(NULL, matcher.condition_factory(), |
| 705 NULL, matcher.condition_factory(), rule.conditions, &error); | 705 rule.conditions, &error); |
| 706 ASSERT_TRUE(error.empty()) << error; | 706 ASSERT_TRUE(error.empty()) << error; |
| 707 ASSERT_TRUE(conditions); | 707 ASSERT_TRUE(conditions); |
| 708 | 708 |
| 709 bool bad_message = false; | 709 bool bad_message = false; |
| 710 scoped_ptr<WebRequestActionSet> actions = | 710 std::unique_ptr<WebRequestActionSet> actions = WebRequestActionSet::Create( |
| 711 WebRequestActionSet::Create( | 711 NULL, NULL, rule.actions, &error, &bad_message); |
| 712 NULL, NULL, rule.actions, &error, &bad_message); | |
| 713 ASSERT_TRUE(error.empty()) << error; | 712 ASSERT_TRUE(error.empty()) << error; |
| 714 ASSERT_FALSE(bad_message); | 713 ASSERT_FALSE(bad_message); |
| 715 ASSERT_TRUE(actions); | 714 ASSERT_TRUE(actions); |
| 716 | 715 |
| 717 EXPECT_FALSE(WebRequestRulesRegistry::StageChecker( | 716 EXPECT_FALSE(WebRequestRulesRegistry::StageChecker( |
| 718 conditions.get(), actions.get(), &error)); | 717 conditions.get(), actions.get(), &error)); |
| 719 EXPECT_THAT(error, HasSubstr("no time in the request life-cycle")); | 718 EXPECT_THAT(error, HasSubstr("no time in the request life-cycle")); |
| 720 EXPECT_THAT(error, HasSubstr(actions->actions().back()->GetName())); | 719 EXPECT_THAT(error, HasSubstr(actions->actions().back()->GetName())); |
| 721 } | 720 } |
| 722 | 721 |
| 723 TEST(WebRequestRulesRegistrySimpleTest, HostPermissionsChecker) { | 722 TEST(WebRequestRulesRegistrySimpleTest, HostPermissionsChecker) { |
| 724 const char kAction[] = // This action requires all URLs host permission. | 723 const char kAction[] = // This action requires all URLs host permission. |
| 725 "{ \n" | 724 "{ \n" |
| 726 " \"instanceType\": \"declarativeWebRequest.RedirectRequest\",\n" | 725 " \"instanceType\": \"declarativeWebRequest.RedirectRequest\",\n" |
| 727 " \"redirectUrl\": \"http://bar.com\" \n" | 726 " \"redirectUrl\": \"http://bar.com\" \n" |
| 728 "} "; | 727 "} "; |
| 729 scoped_ptr<base::Value> action_value = base::JSONReader::Read(kAction); | 728 std::unique_ptr<base::Value> action_value = base::JSONReader::Read(kAction); |
| 730 ASSERT_TRUE(action_value); | 729 ASSERT_TRUE(action_value); |
| 731 | 730 |
| 732 WebRequestActionSet::Values actions; | 731 WebRequestActionSet::Values actions; |
| 733 actions.push_back(std::move(action_value)); | 732 actions.push_back(std::move(action_value)); |
| 734 ASSERT_TRUE(actions.back().get()); | 733 ASSERT_TRUE(actions.back().get()); |
| 735 | 734 |
| 736 std::string error; | 735 std::string error; |
| 737 bool bad_message = false; | 736 bool bad_message = false; |
| 738 scoped_ptr<WebRequestActionSet> action_set( | 737 std::unique_ptr<WebRequestActionSet> action_set( |
| 739 WebRequestActionSet::Create(NULL, NULL, actions, &error, &bad_message)); | 738 WebRequestActionSet::Create(NULL, NULL, actions, &error, &bad_message)); |
| 740 ASSERT_TRUE(error.empty()) << error; | 739 ASSERT_TRUE(error.empty()) << error; |
| 741 ASSERT_FALSE(bad_message); | 740 ASSERT_FALSE(bad_message); |
| 742 ASSERT_TRUE(action_set); | 741 ASSERT_TRUE(action_set); |
| 743 | 742 |
| 744 scoped_refptr<Extension> extension_no_url( | 743 scoped_refptr<Extension> extension_no_url( |
| 745 LoadManifest("permissions", "web_request_no_host.json")); | 744 LoadManifest("permissions", "web_request_no_host.json")); |
| 746 scoped_refptr<Extension> extension_some_urls( | 745 scoped_refptr<Extension> extension_some_urls( |
| 747 LoadManifest("permissions", "web_request_com_host_permissions.json")); | 746 LoadManifest("permissions", "web_request_com_host_permissions.json")); |
| 748 scoped_refptr<Extension> extension_all_urls( | 747 scoped_refptr<Extension> extension_all_urls( |
| (...skipping 26 matching lines...) Expand all Loading... |
| 775 " ], \n" | 774 " ], \n" |
| 776 " \"actions\": [ \n" | 775 " \"actions\": [ \n" |
| 777 " { \n" | 776 " { \n" |
| 778 " \"instanceType\": \"declarativeWebRequest.RedirectRequest\",\n" | 777 " \"instanceType\": \"declarativeWebRequest.RedirectRequest\",\n" |
| 779 " \"redirectUrl\": \"http://bar.com\" \n" | 778 " \"redirectUrl\": \"http://bar.com\" \n" |
| 780 " } \n" | 779 " } \n" |
| 781 " ], \n" | 780 " ], \n" |
| 782 " \"priority\": 200 \n" | 781 " \"priority\": 200 \n" |
| 783 "} "; | 782 "} "; |
| 784 | 783 |
| 785 scoped_ptr<base::Value> value = base::JSONReader::Read(kRule); | 784 std::unique_ptr<base::Value> value = base::JSONReader::Read(kRule); |
| 786 ASSERT_TRUE(value.get()); | 785 ASSERT_TRUE(value.get()); |
| 787 | 786 |
| 788 std::vector<linked_ptr<api::events::Rule>> rules; | 787 std::vector<linked_ptr<api::events::Rule>> rules; |
| 789 rules.push_back(make_linked_ptr(new api::events::Rule)); | 788 rules.push_back(make_linked_ptr(new api::events::Rule)); |
| 790 ASSERT_TRUE(api::events::Rule::Populate(*value, rules.back().get())); | 789 ASSERT_TRUE(api::events::Rule::Populate(*value, rules.back().get())); |
| 791 | 790 |
| 792 scoped_refptr<WebRequestRulesRegistry> registry( | 791 scoped_refptr<WebRequestRulesRegistry> registry( |
| 793 new TestWebRequestRulesRegistry(extension_info_map_)); | 792 new TestWebRequestRulesRegistry(extension_info_map_)); |
| 794 | 793 |
| 795 URLMatcher matcher; | 794 URLMatcher matcher; |
| 796 std::string error = registry->AddRulesImpl(kExtensionId, rules); | 795 std::string error = registry->AddRulesImpl(kExtensionId, rules); |
| 797 EXPECT_EQ("", error); | 796 EXPECT_EQ("", error); |
| 798 | 797 |
| 799 net::TestURLRequestContext context; | 798 net::TestURLRequestContext context; |
| 800 std::list<LinkedPtrEventResponseDelta> deltas; | 799 std::list<LinkedPtrEventResponseDelta> deltas; |
| 801 | 800 |
| 802 // No match because match is in the query parameter. | 801 // No match because match is in the query parameter. |
| 803 GURL url1("http://bar.com/index.html?foo.com"); | 802 GURL url1("http://bar.com/index.html?foo.com"); |
| 804 scoped_ptr<net::URLRequest> request1(context.CreateRequest( | 803 std::unique_ptr<net::URLRequest> request1( |
| 805 url1, net::DEFAULT_PRIORITY, NULL)); | 804 context.CreateRequest(url1, net::DEFAULT_PRIORITY, NULL)); |
| 806 WebRequestData request_data1(request1.get(), ON_BEFORE_REQUEST); | 805 WebRequestData request_data1(request1.get(), ON_BEFORE_REQUEST); |
| 807 deltas = registry->CreateDeltas(NULL, request_data1, false); | 806 deltas = registry->CreateDeltas(NULL, request_data1, false); |
| 808 EXPECT_EQ(0u, deltas.size()); | 807 EXPECT_EQ(0u, deltas.size()); |
| 809 | 808 |
| 810 // This is a correct match. | 809 // This is a correct match. |
| 811 GURL url2("http://foo.com/index.html"); | 810 GURL url2("http://foo.com/index.html"); |
| 812 scoped_ptr<net::URLRequest> request2(context.CreateRequest( | 811 std::unique_ptr<net::URLRequest> request2( |
| 813 url2, net::DEFAULT_PRIORITY, NULL)); | 812 context.CreateRequest(url2, net::DEFAULT_PRIORITY, NULL)); |
| 814 WebRequestData request_data2(request2.get(), ON_BEFORE_REQUEST); | 813 WebRequestData request_data2(request2.get(), ON_BEFORE_REQUEST); |
| 815 deltas = registry->CreateDeltas(NULL, request_data2, false); | 814 deltas = registry->CreateDeltas(NULL, request_data2, false); |
| 816 EXPECT_EQ(1u, deltas.size()); | 815 EXPECT_EQ(1u, deltas.size()); |
| 817 } | 816 } |
| 818 | 817 |
| 819 } // namespace extensions | 818 } // namespace extensions |
| OLD | NEW |