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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 } // namespace | 44 } // namespace |
45 | 45 |
46 namespace extensions { | 46 namespace extensions { |
47 | 47 |
48 namespace helpers = extension_web_request_api_helpers; | 48 namespace helpers = extension_web_request_api_helpers; |
49 namespace keys = declarative_webrequest_constants; | 49 namespace keys = declarative_webrequest_constants; |
50 namespace keys2 = url_matcher::url_matcher_constants; | 50 namespace keys2 = url_matcher::url_matcher_constants; |
51 | 51 |
52 class TestWebRequestRulesRegistry : public WebRequestRulesRegistry { | 52 class TestWebRequestRulesRegistry : public WebRequestRulesRegistry { |
53 public: | 53 public: |
54 TestWebRequestRulesRegistry(scoped_refptr<InfoMap> extension_info_map) | 54 explicit TestWebRequestRulesRegistry( |
| 55 scoped_refptr<InfoMap> extension_info_map) |
55 : WebRequestRulesRegistry(NULL /*profile*/, | 56 : WebRequestRulesRegistry(NULL /*profile*/, |
56 NULL /* cache_delegate */, | 57 NULL /* cache_delegate */, |
57 RulesRegistryService::kDefaultRulesRegistryID), | 58 RulesRegistryService::kDefaultRulesRegistryID), |
58 num_clear_cache_calls_(0) { | 59 num_clear_cache_calls_(0) { |
59 SetExtensionInfoMapForTesting(extension_info_map); | 60 SetExtensionInfoMapForTesting(extension_info_map); |
60 } | 61 } |
61 | 62 |
62 // Returns how often the in-memory caches of the renderers were instructed | 63 // Returns how often the in-memory caches of the renderers were instructed |
63 // to be cleared. | 64 // to be cleared. |
64 int num_clear_cache_calls() const { return num_clear_cache_calls_; } | 65 int num_clear_cache_calls() const { return num_clear_cache_calls_; } |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 | 276 |
276 error = registry->AddRules(kExtensionId, rules); | 277 error = registry->AddRules(kExtensionId, rules); |
277 EXPECT_EQ("", error); | 278 EXPECT_EQ("", error); |
278 EXPECT_EQ(1, registry->num_clear_cache_calls()); | 279 EXPECT_EQ(1, registry->num_clear_cache_calls()); |
279 | 280 |
280 std::set<const WebRequestRule*> matches; | 281 std::set<const WebRequestRule*> matches; |
281 | 282 |
282 GURL http_url("http://www.example.com"); | 283 GURL http_url("http://www.example.com"); |
283 net::TestURLRequestContext context; | 284 net::TestURLRequestContext context; |
284 scoped_ptr<net::URLRequest> http_request(context.CreateRequest( | 285 scoped_ptr<net::URLRequest> http_request(context.CreateRequest( |
285 http_url, net::DEFAULT_PRIORITY, NULL, NULL)); | 286 http_url, net::DEFAULT_PRIORITY, NULL)); |
286 WebRequestData request_data(http_request.get(), ON_BEFORE_REQUEST); | 287 WebRequestData request_data(http_request.get(), ON_BEFORE_REQUEST); |
287 matches = registry->GetMatches(request_data); | 288 matches = registry->GetMatches(request_data); |
288 EXPECT_EQ(2u, matches.size()); | 289 EXPECT_EQ(2u, matches.size()); |
289 | 290 |
290 std::set<WebRequestRule::GlobalRuleId> matches_ids; | 291 std::set<WebRequestRule::GlobalRuleId> matches_ids; |
291 for (std::set<const WebRequestRule*>::const_iterator it = matches.begin(); | 292 for (std::set<const WebRequestRule*>::const_iterator it = matches.begin(); |
292 it != matches.end(); ++it) | 293 it != matches.end(); ++it) |
293 matches_ids.insert((*it)->id()); | 294 matches_ids.insert((*it)->id()); |
294 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId1))); | 295 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId1))); |
295 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId2))); | 296 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId2))); |
296 | 297 |
297 GURL foobar_url("http://www.foobar.com"); | 298 GURL foobar_url("http://www.foobar.com"); |
298 scoped_ptr<net::URLRequest> foobar_request(context.CreateRequest( | 299 scoped_ptr<net::URLRequest> foobar_request(context.CreateRequest( |
299 foobar_url, net::DEFAULT_PRIORITY, NULL, NULL)); | 300 foobar_url, net::DEFAULT_PRIORITY, NULL)); |
300 request_data.request = foobar_request.get(); | 301 request_data.request = foobar_request.get(); |
301 matches = registry->GetMatches(request_data); | 302 matches = registry->GetMatches(request_data); |
302 EXPECT_EQ(1u, matches.size()); | 303 EXPECT_EQ(1u, matches.size()); |
303 WebRequestRule::GlobalRuleId expected_pair = | 304 WebRequestRule::GlobalRuleId expected_pair = |
304 std::make_pair(kExtensionId, kRuleId2); | 305 std::make_pair(kExtensionId, kRuleId2); |
305 EXPECT_EQ(expected_pair, (*matches.begin())->id()); | 306 EXPECT_EQ(expected_pair, (*matches.begin())->id()); |
306 } | 307 } |
307 | 308 |
308 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { | 309 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { |
309 scoped_refptr<TestWebRequestRulesRegistry> registry( | 310 scoped_refptr<TestWebRequestRulesRegistry> registry( |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
416 EXPECT_EQ("", error); | 417 EXPECT_EQ("", error); |
417 | 418 |
418 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_2(1); | 419 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_2(1); |
419 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com"); | 420 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com"); |
420 error = registry->AddRules(kExtensionId2, rules_to_add_2); | 421 error = registry->AddRules(kExtensionId2, rules_to_add_2); |
421 EXPECT_EQ("", error); | 422 EXPECT_EQ("", error); |
422 | 423 |
423 GURL url("http://www.google.com"); | 424 GURL url("http://www.google.com"); |
424 net::TestURLRequestContext context; | 425 net::TestURLRequestContext context; |
425 scoped_ptr<net::URLRequest> request(context.CreateRequest( | 426 scoped_ptr<net::URLRequest> request(context.CreateRequest( |
426 url, net::DEFAULT_PRIORITY, NULL, NULL)); | 427 url, net::DEFAULT_PRIORITY, NULL)); |
427 WebRequestData request_data(request.get(), ON_BEFORE_REQUEST); | 428 WebRequestData request_data(request.get(), ON_BEFORE_REQUEST); |
428 std::list<LinkedPtrEventResponseDelta> deltas = | 429 std::list<LinkedPtrEventResponseDelta> deltas = |
429 registry->CreateDeltas(NULL, request_data, false); | 430 registry->CreateDeltas(NULL, request_data, false); |
430 | 431 |
431 // The second extension is installed later and will win for this reason | 432 // The second extension is installed later and will win for this reason |
432 // in conflict resolution. | 433 // in conflict resolution. |
433 ASSERT_EQ(2u, deltas.size()); | 434 ASSERT_EQ(2u, deltas.size()); |
434 deltas.sort(&helpers::InDecreasingExtensionInstallationTimeOrder); | 435 deltas.sort(&helpers::InDecreasingExtensionInstallationTimeOrder); |
435 | 436 |
436 std::list<LinkedPtrEventResponseDelta>::iterator i = deltas.begin(); | 437 std::list<LinkedPtrEventResponseDelta>::iterator i = deltas.begin(); |
(...skipping 28 matching lines...) Expand all Loading... |
465 EXPECT_EQ("", error); | 466 EXPECT_EQ("", error); |
466 | 467 |
467 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_3(1); | 468 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_3(1); |
468 rules_to_add_3[0] = CreateIgnoreRule(); | 469 rules_to_add_3[0] = CreateIgnoreRule(); |
469 error = registry->AddRules(kExtensionId, rules_to_add_3); | 470 error = registry->AddRules(kExtensionId, rules_to_add_3); |
470 EXPECT_EQ("", error); | 471 EXPECT_EQ("", error); |
471 | 472 |
472 GURL url("http://www.google.com/index.html"); | 473 GURL url("http://www.google.com/index.html"); |
473 net::TestURLRequestContext context; | 474 net::TestURLRequestContext context; |
474 scoped_ptr<net::URLRequest> request(context.CreateRequest( | 475 scoped_ptr<net::URLRequest> request(context.CreateRequest( |
475 url, net::DEFAULT_PRIORITY, NULL, NULL)); | 476 url, net::DEFAULT_PRIORITY, NULL)); |
476 WebRequestData request_data(request.get(), ON_BEFORE_REQUEST); | 477 WebRequestData request_data(request.get(), ON_BEFORE_REQUEST); |
477 std::list<LinkedPtrEventResponseDelta> deltas = | 478 std::list<LinkedPtrEventResponseDelta> deltas = |
478 registry->CreateDeltas(NULL, request_data, false); | 479 registry->CreateDeltas(NULL, request_data, false); |
479 | 480 |
480 // The redirect by the first extension is ignored due to the ignore rule. | 481 // The redirect by the first extension is ignored due to the ignore rule. |
481 ASSERT_EQ(1u, deltas.size()); | 482 ASSERT_EQ(1u, deltas.size()); |
482 LinkedPtrEventResponseDelta effective_rule = *(deltas.begin()); | 483 LinkedPtrEventResponseDelta effective_rule = *(deltas.begin()); |
483 | 484 |
484 EXPECT_EQ(kExtensionId2, effective_rule->extension_id); | 485 EXPECT_EQ(kExtensionId2, effective_rule->extension_id); |
485 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2), | 486 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2), |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
539 | 540 |
540 scoped_refptr<WebRequestRulesRegistry> registry( | 541 scoped_refptr<WebRequestRulesRegistry> registry( |
541 new TestWebRequestRulesRegistry(extension_info_map_)); | 542 new TestWebRequestRulesRegistry(extension_info_map_)); |
542 std::string error = registry->AddRulesImpl(kExtensionId, rules); | 543 std::string error = registry->AddRulesImpl(kExtensionId, rules); |
543 EXPECT_EQ("", error); | 544 EXPECT_EQ("", error); |
544 EXPECT_FALSE(registry->IsEmpty()); | 545 EXPECT_FALSE(registry->IsEmpty()); |
545 | 546 |
546 GURL url("http://www.foo.com/test"); | 547 GURL url("http://www.foo.com/test"); |
547 net::TestURLRequestContext context; | 548 net::TestURLRequestContext context; |
548 scoped_ptr<net::URLRequest> request(context.CreateRequest( | 549 scoped_ptr<net::URLRequest> request(context.CreateRequest( |
549 url, net::DEFAULT_PRIORITY, NULL, NULL)); | 550 url, net::DEFAULT_PRIORITY, NULL)); |
550 WebRequestData request_data(request.get(), ON_BEFORE_REQUEST); | 551 WebRequestData request_data(request.get(), ON_BEFORE_REQUEST); |
551 std::list<LinkedPtrEventResponseDelta> deltas = | 552 std::list<LinkedPtrEventResponseDelta> deltas = |
552 registry->CreateDeltas(NULL, request_data, false); | 553 registry->CreateDeltas(NULL, request_data, false); |
553 | 554 |
554 // The redirect by the redirect rule is ignored due to the ignore rule. | 555 // The redirect by the redirect rule is ignored due to the ignore rule. |
555 std::set<const WebRequestRule*> matches = registry->GetMatches(request_data); | 556 std::set<const WebRequestRule*> matches = registry->GetMatches(request_data); |
556 EXPECT_EQ(2u, matches.size()); | 557 EXPECT_EQ(2u, matches.size()); |
557 ASSERT_EQ(0u, deltas.size()); | 558 ASSERT_EQ(0u, deltas.size()); |
558 } | 559 } |
559 | 560 |
(...skipping 29 matching lines...) Expand all Loading... |
589 | 590 |
590 error = registry->AddRules(kExtensionId, rules); | 591 error = registry->AddRules(kExtensionId, rules); |
591 EXPECT_EQ("", error); | 592 EXPECT_EQ("", error); |
592 EXPECT_EQ(1, registry->num_clear_cache_calls()); | 593 EXPECT_EQ(1, registry->num_clear_cache_calls()); |
593 | 594 |
594 std::set<const WebRequestRule*> matches; | 595 std::set<const WebRequestRule*> matches; |
595 | 596 |
596 GURL http_url("http://www.example.com"); | 597 GURL http_url("http://www.example.com"); |
597 net::TestURLRequestContext context; | 598 net::TestURLRequestContext context; |
598 scoped_ptr<net::URLRequest> http_request(context.CreateRequest( | 599 scoped_ptr<net::URLRequest> http_request(context.CreateRequest( |
599 http_url, net::DEFAULT_PRIORITY, NULL, NULL)); | 600 http_url, net::DEFAULT_PRIORITY, NULL)); |
600 WebRequestData request_data(http_request.get(), ON_BEFORE_REQUEST); | 601 WebRequestData request_data(http_request.get(), ON_BEFORE_REQUEST); |
601 matches = registry->GetMatches(request_data); | 602 matches = registry->GetMatches(request_data); |
602 EXPECT_EQ(1u, matches.size()); | 603 EXPECT_EQ(1u, matches.size()); |
603 WebRequestRule::GlobalRuleId expected_pair = std::make_pair(kExtensionId, | 604 WebRequestRule::GlobalRuleId expected_pair = std::make_pair(kExtensionId, |
604 kRuleId3); | 605 kRuleId3); |
605 EXPECT_EQ(expected_pair, (*matches.begin())->id()); | 606 EXPECT_EQ(expected_pair, (*matches.begin())->id()); |
606 } | 607 } |
607 | 608 |
608 // Test that the url and firstPartyForCookiesUrl attributes are evaluated | 609 // Test that the url and firstPartyForCookiesUrl attributes are evaluated |
609 // against corresponding URLs. Tested on requests where these URLs actually | 610 // against corresponding URLs. Tested on requests where these URLs actually |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
648 "urls and firstPartyUrls must have the same number " | 649 "urls and firstPartyUrls must have the same number " |
649 "of elements"); | 650 "of elements"); |
650 static_assert(arraysize(urls) == arraysize(matchingRuleIds), | 651 static_assert(arraysize(urls) == arraysize(matchingRuleIds), |
651 "urls and matchingRuleIds must have the same number " | 652 "urls and matchingRuleIds must have the same number " |
652 "of elements"); | 653 "of elements"); |
653 net::TestURLRequestContext context; | 654 net::TestURLRequestContext context; |
654 | 655 |
655 for (size_t i = 0; i < arraysize(matchingRuleIds); ++i) { | 656 for (size_t i = 0; i < arraysize(matchingRuleIds); ++i) { |
656 // Construct the inputs. | 657 // Construct the inputs. |
657 scoped_ptr<net::URLRequest> http_request(context.CreateRequest( | 658 scoped_ptr<net::URLRequest> http_request(context.CreateRequest( |
658 urls[i], net::DEFAULT_PRIORITY, NULL, NULL)); | 659 urls[i], net::DEFAULT_PRIORITY, NULL)); |
659 WebRequestData request_data(http_request.get(), ON_BEFORE_REQUEST); | 660 WebRequestData request_data(http_request.get(), ON_BEFORE_REQUEST); |
660 http_request->set_first_party_for_cookies(firstPartyUrls[i]); | 661 http_request->set_first_party_for_cookies(firstPartyUrls[i]); |
661 // Now run both rules on the input. | 662 // Now run both rules on the input. |
662 matches = registry->GetMatches(request_data); | 663 matches = registry->GetMatches(request_data); |
663 SCOPED_TRACE(testing::Message("i = ") << i << ", rule id = " | 664 SCOPED_TRACE(testing::Message("i = ") << i << ", rule id = " |
664 << matchingRuleIds[i]); | 665 << matchingRuleIds[i]); |
665 // Make sure that the right rule succeeded. | 666 // Make sure that the right rule succeeded. |
666 EXPECT_EQ(1u, matches.size()); | 667 EXPECT_EQ(1u, matches.size()); |
667 EXPECT_EQ(WebRequestRule::GlobalRuleId(std::make_pair(kExtensionId, | 668 EXPECT_EQ(WebRequestRule::GlobalRuleId(std::make_pair(kExtensionId, |
668 matchingRuleIds[i])), | 669 matchingRuleIds[i])), |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
798 URLMatcher matcher; | 799 URLMatcher matcher; |
799 std::string error = registry->AddRulesImpl(kExtensionId, rules); | 800 std::string error = registry->AddRulesImpl(kExtensionId, rules); |
800 EXPECT_EQ("", error); | 801 EXPECT_EQ("", error); |
801 | 802 |
802 net::TestURLRequestContext context; | 803 net::TestURLRequestContext context; |
803 std::list<LinkedPtrEventResponseDelta> deltas; | 804 std::list<LinkedPtrEventResponseDelta> deltas; |
804 | 805 |
805 // No match because match is in the query parameter. | 806 // No match because match is in the query parameter. |
806 GURL url1("http://bar.com/index.html?foo.com"); | 807 GURL url1("http://bar.com/index.html?foo.com"); |
807 scoped_ptr<net::URLRequest> request1(context.CreateRequest( | 808 scoped_ptr<net::URLRequest> request1(context.CreateRequest( |
808 url1, net::DEFAULT_PRIORITY, NULL, NULL)); | 809 url1, net::DEFAULT_PRIORITY, NULL)); |
809 WebRequestData request_data1(request1.get(), ON_BEFORE_REQUEST); | 810 WebRequestData request_data1(request1.get(), ON_BEFORE_REQUEST); |
810 deltas = registry->CreateDeltas(NULL, request_data1, false); | 811 deltas = registry->CreateDeltas(NULL, request_data1, false); |
811 EXPECT_EQ(0u, deltas.size()); | 812 EXPECT_EQ(0u, deltas.size()); |
812 | 813 |
813 // This is a correct match. | 814 // This is a correct match. |
814 GURL url2("http://foo.com/index.html"); | 815 GURL url2("http://foo.com/index.html"); |
815 scoped_ptr<net::URLRequest> request2(context.CreateRequest( | 816 scoped_ptr<net::URLRequest> request2(context.CreateRequest( |
816 url2, net::DEFAULT_PRIORITY, NULL, NULL)); | 817 url2, net::DEFAULT_PRIORITY, NULL)); |
817 WebRequestData request_data2(request2.get(), ON_BEFORE_REQUEST); | 818 WebRequestData request_data2(request2.get(), ON_BEFORE_REQUEST); |
818 deltas = registry->CreateDeltas(NULL, request_data2, false); | 819 deltas = registry->CreateDeltas(NULL, request_data2, false); |
819 EXPECT_EQ(1u, deltas.size()); | 820 EXPECT_EQ(1u, deltas.size()); |
820 } | 821 } |
821 | 822 |
822 } // namespace extensions | 823 } // namespace extensions |
OLD | NEW |