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 "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit
ion_attribute.h" | 5 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit
ion_attribute.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "chrome/browser/extensions/api/declarative_webrequest/request_stage.h" | 13 #include "chrome/browser/extensions/api/declarative_webrequest/request_stage.h" |
14 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta
nts.h" | 14 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta
nts.h" |
15 #include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h" | 15 #include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h" |
16 #include "chrome/common/extensions/extension_error_utils.h" | 16 #include "chrome/common/extensions/extension_error_utils.h" |
17 #include "content/public/browser/resource_request_info.h" | 17 #include "content/public/browser/resource_request_info.h" |
| 18 #include "net/base/net_errors.h" |
| 19 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| 20 #include "net/base/static_cookie_policy.h" |
18 #include "net/http/http_util.h" | 21 #include "net/http/http_util.h" |
19 #include "net/http/http_request_headers.h" | 22 #include "net/http/http_request_headers.h" |
20 #include "net/url_request/url_request.h" | 23 #include "net/url_request/url_request.h" |
21 | 24 |
22 using base::CaseInsensitiveCompareASCII; | 25 using base::CaseInsensitiveCompareASCII; |
23 using base::DictionaryValue; | 26 using base::DictionaryValue; |
24 using base::ListValue; | 27 using base::ListValue; |
25 using base::StringValue; | 28 using base::StringValue; |
26 using base::Value; | 29 using base::Value; |
27 | 30 |
(...skipping 17 matching lines...) Expand all Loading... |
45 | 48 |
46 WebRequestConditionAttribute::~WebRequestConditionAttribute() {} | 49 WebRequestConditionAttribute::~WebRequestConditionAttribute() {} |
47 | 50 |
48 // static | 51 // static |
49 bool WebRequestConditionAttribute::IsKnownType( | 52 bool WebRequestConditionAttribute::IsKnownType( |
50 const std::string& instance_type) { | 53 const std::string& instance_type) { |
51 return | 54 return |
52 WebRequestConditionAttributeResourceType::IsMatchingType(instance_type) || | 55 WebRequestConditionAttributeResourceType::IsMatchingType(instance_type) || |
53 WebRequestConditionAttributeContentType::IsMatchingType(instance_type) || | 56 WebRequestConditionAttributeContentType::IsMatchingType(instance_type) || |
54 WebRequestConditionAttributeResponseHeaders::IsMatchingType( | 57 WebRequestConditionAttributeResponseHeaders::IsMatchingType( |
55 instance_type); | 58 instance_type) || |
| 59 WebRequestConditionAttributeThirdParty::IsMatchingType(instance_type); |
56 } | 60 } |
57 | 61 |
58 // static | 62 // static |
59 scoped_ptr<WebRequestConditionAttribute> | 63 scoped_ptr<WebRequestConditionAttribute> |
60 WebRequestConditionAttribute::Create( | 64 WebRequestConditionAttribute::Create( |
61 const std::string& name, | 65 const std::string& name, |
62 const base::Value* value, | 66 const base::Value* value, |
63 std::string* error) { | 67 std::string* error) { |
64 CHECK(value != NULL && error != NULL); | 68 CHECK(value != NULL && error != NULL); |
65 if (WebRequestConditionAttributeResourceType::IsMatchingType(name)) { | 69 if (WebRequestConditionAttributeResourceType::IsMatchingType(name)) { |
66 return WebRequestConditionAttributeResourceType::Create(name, value, error); | 70 return WebRequestConditionAttributeResourceType::Create(name, value, error); |
67 } else if (WebRequestConditionAttributeContentType::IsMatchingType(name)) { | 71 } else if (WebRequestConditionAttributeContentType::IsMatchingType(name)) { |
68 return WebRequestConditionAttributeContentType::Create(name, value, error); | 72 return WebRequestConditionAttributeContentType::Create(name, value, error); |
69 } else if (WebRequestConditionAttributeResponseHeaders::IsMatchingType( | 73 } else if (WebRequestConditionAttributeResponseHeaders::IsMatchingType( |
70 name)) { | 74 name)) { |
71 return WebRequestConditionAttributeResponseHeaders::Create( | 75 return WebRequestConditionAttributeResponseHeaders::Create( |
72 name, value, error); | 76 name, value, error); |
| 77 } else if (WebRequestConditionAttributeThirdParty::IsMatchingType(name)) { |
| 78 return WebRequestConditionAttributeThirdParty::Create(name, value, error); |
73 } | 79 } |
74 | 80 |
75 *error = ExtensionErrorUtils::FormatErrorMessage(kUnknownConditionAttribute, | 81 *error = ExtensionErrorUtils::FormatErrorMessage(kUnknownConditionAttribute, |
76 name); | 82 name); |
77 return scoped_ptr<WebRequestConditionAttribute>(NULL); | 83 return scoped_ptr<WebRequestConditionAttribute>(NULL); |
78 } | 84 } |
79 | 85 |
80 // | 86 // |
81 // WebRequestConditionAttributeResourceType | 87 // WebRequestConditionAttributeResourceType |
82 // | 88 // |
(...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
557 } | 563 } |
558 | 564 |
559 return (positive_ ? passed : !passed); | 565 return (positive_ ? passed : !passed); |
560 } | 566 } |
561 | 567 |
562 WebRequestConditionAttribute::Type | 568 WebRequestConditionAttribute::Type |
563 WebRequestConditionAttributeResponseHeaders::GetType() const { | 569 WebRequestConditionAttributeResponseHeaders::GetType() const { |
564 return CONDITION_RESPONSE_HEADERS; | 570 return CONDITION_RESPONSE_HEADERS; |
565 } | 571 } |
566 | 572 |
| 573 // |
| 574 // WebRequestConditionAttributeThirdParty |
| 575 // |
| 576 |
| 577 WebRequestConditionAttributeThirdParty:: |
| 578 WebRequestConditionAttributeThirdParty(bool match_third_party) |
| 579 : match_third_party_(match_third_party) {} |
| 580 |
| 581 WebRequestConditionAttributeThirdParty:: |
| 582 ~WebRequestConditionAttributeThirdParty() {} |
| 583 |
| 584 // static |
| 585 bool WebRequestConditionAttributeThirdParty::IsMatchingType( |
| 586 const std::string& instance_type) { |
| 587 return instance_type == keys::kThirdPartyKey; |
| 588 } |
| 589 |
| 590 // static |
| 591 scoped_ptr<WebRequestConditionAttribute> |
| 592 WebRequestConditionAttributeThirdParty::Create( |
| 593 const std::string& name, |
| 594 const base::Value* value, |
| 595 std::string* error) { |
| 596 DCHECK(IsMatchingType(name)); |
| 597 |
| 598 bool third_party = false; // Dummy value, gets overwritten. |
| 599 if (!value->GetAsBoolean(&third_party)) { |
| 600 *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidValue, |
| 601 keys::kThirdPartyKey); |
| 602 return scoped_ptr<WebRequestConditionAttribute>(NULL); |
| 603 } |
| 604 |
| 605 return scoped_ptr<WebRequestConditionAttribute>( |
| 606 new WebRequestConditionAttributeThirdParty(third_party)); |
| 607 } |
| 608 |
| 609 int WebRequestConditionAttributeThirdParty::GetStages() const { |
| 610 return ON_BEFORE_REQUEST | ON_BEFORE_SEND_HEADERS | ON_SEND_HEADERS | |
| 611 ON_HEADERS_RECEIVED | ON_AUTH_REQUIRED | ON_BEFORE_REDIRECT | |
| 612 ON_RESPONSE_STARTED | ON_COMPLETED | ON_ERROR; |
| 613 } |
| 614 |
| 615 bool WebRequestConditionAttributeThirdParty::IsFulfilled( |
| 616 const WebRequestRule::RequestData& request_data) const { |
| 617 if (!(request_data.stage & GetStages())) |
| 618 return false; |
| 619 |
| 620 // Request is "1st party" if it gets cookies under 3rd party-blocking policy. |
| 621 const net::StaticCookiePolicy block_third_party_policy( |
| 622 net::StaticCookiePolicy::BLOCK_ALL_THIRD_PARTY_COOKIES); |
| 623 const int can_get_cookies = block_third_party_policy.CanGetCookies( |
| 624 request_data.request->url(), |
| 625 request_data.request->first_party_for_cookies()); |
| 626 const bool is_first_party = (can_get_cookies == net::OK); |
| 627 |
| 628 return match_third_party_ ? !is_first_party : is_first_party; |
| 629 } |
| 630 |
| 631 WebRequestConditionAttribute::Type |
| 632 WebRequestConditionAttributeThirdParty::GetType() const { |
| 633 return CONDITION_THIRD_PARTY; |
| 634 } |
| 635 |
567 } // namespace extensions | 636 } // namespace extensions |
OLD | NEW |