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/registry_controlled_domains/registry_controlled_domain.h" |
18 #include "net/http/http_util.h" | 19 #include "net/http/http_util.h" |
19 #include "net/http/http_request_headers.h" | 20 #include "net/http/http_request_headers.h" |
20 #include "net/url_request/url_request.h" | 21 #include "net/url_request/url_request.h" |
21 | 22 |
22 using base::CaseInsensitiveCompareASCII; | 23 using base::CaseInsensitiveCompareASCII; |
23 using base::DictionaryValue; | 24 using base::DictionaryValue; |
24 using base::ListValue; | 25 using base::ListValue; |
25 using base::StringValue; | 26 using base::StringValue; |
26 using base::Value; | 27 using base::Value; |
27 | 28 |
(...skipping 17 matching lines...) Expand all Loading... |
45 | 46 |
46 WebRequestConditionAttribute::~WebRequestConditionAttribute() {} | 47 WebRequestConditionAttribute::~WebRequestConditionAttribute() {} |
47 | 48 |
48 // static | 49 // static |
49 bool WebRequestConditionAttribute::IsKnownType( | 50 bool WebRequestConditionAttribute::IsKnownType( |
50 const std::string& instance_type) { | 51 const std::string& instance_type) { |
51 return | 52 return |
52 WebRequestConditionAttributeResourceType::IsMatchingType(instance_type) || | 53 WebRequestConditionAttributeResourceType::IsMatchingType(instance_type) || |
53 WebRequestConditionAttributeContentType::IsMatchingType(instance_type) || | 54 WebRequestConditionAttributeContentType::IsMatchingType(instance_type) || |
54 WebRequestConditionAttributeResponseHeaders::IsMatchingType( | 55 WebRequestConditionAttributeResponseHeaders::IsMatchingType( |
55 instance_type); | 56 instance_type) || |
| 57 WebRequestConditionAttributeThirdParty::IsMatchingType(instance_type); |
56 } | 58 } |
57 | 59 |
58 // static | 60 // static |
59 scoped_ptr<WebRequestConditionAttribute> | 61 scoped_ptr<WebRequestConditionAttribute> |
60 WebRequestConditionAttribute::Create( | 62 WebRequestConditionAttribute::Create( |
61 const std::string& name, | 63 const std::string& name, |
62 const base::Value* value, | 64 const base::Value* value, |
63 std::string* error) { | 65 std::string* error) { |
64 CHECK(value != NULL && error != NULL); | 66 CHECK(value != NULL && error != NULL); |
65 if (WebRequestConditionAttributeResourceType::IsMatchingType(name)) { | 67 if (WebRequestConditionAttributeResourceType::IsMatchingType(name)) { |
66 return WebRequestConditionAttributeResourceType::Create(name, value, error); | 68 return WebRequestConditionAttributeResourceType::Create(name, value, error); |
67 } else if (WebRequestConditionAttributeContentType::IsMatchingType(name)) { | 69 } else if (WebRequestConditionAttributeContentType::IsMatchingType(name)) { |
68 return WebRequestConditionAttributeContentType::Create(name, value, error); | 70 return WebRequestConditionAttributeContentType::Create(name, value, error); |
69 } else if (WebRequestConditionAttributeResponseHeaders::IsMatchingType( | 71 } else if (WebRequestConditionAttributeResponseHeaders::IsMatchingType( |
70 name)) { | 72 name)) { |
71 return WebRequestConditionAttributeResponseHeaders::Create( | 73 return WebRequestConditionAttributeResponseHeaders::Create( |
72 name, value, error); | 74 name, value, error); |
| 75 } else if (WebRequestConditionAttributeThirdParty::IsMatchingType(name)) { |
| 76 return WebRequestConditionAttributeThirdParty::Create(name, value, error); |
73 } | 77 } |
74 | 78 |
75 *error = ExtensionErrorUtils::FormatErrorMessage(kUnknownConditionAttribute, | 79 *error = ExtensionErrorUtils::FormatErrorMessage(kUnknownConditionAttribute, |
76 name); | 80 name); |
77 return scoped_ptr<WebRequestConditionAttribute>(NULL); | 81 return scoped_ptr<WebRequestConditionAttribute>(NULL); |
78 } | 82 } |
79 | 83 |
80 // | 84 // |
81 // WebRequestConditionAttributeResourceType | 85 // WebRequestConditionAttributeResourceType |
82 // | 86 // |
(...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
557 } | 561 } |
558 | 562 |
559 return (positive_ ? passed : !passed); | 563 return (positive_ ? passed : !passed); |
560 } | 564 } |
561 | 565 |
562 WebRequestConditionAttribute::Type | 566 WebRequestConditionAttribute::Type |
563 WebRequestConditionAttributeResponseHeaders::GetType() const { | 567 WebRequestConditionAttributeResponseHeaders::GetType() const { |
564 return CONDITION_RESPONSE_HEADERS; | 568 return CONDITION_RESPONSE_HEADERS; |
565 } | 569 } |
566 | 570 |
| 571 // |
| 572 // WebRequestConditionAttributeThirdParty |
| 573 // |
| 574 |
| 575 WebRequestConditionAttributeThirdParty:: |
| 576 WebRequestConditionAttributeThirdParty(bool match_third_party) |
| 577 : match_third_party_(match_third_party) {} |
| 578 |
| 579 WebRequestConditionAttributeThirdParty:: |
| 580 ~WebRequestConditionAttributeThirdParty() {} |
| 581 |
| 582 // static |
| 583 bool WebRequestConditionAttributeThirdParty::IsMatchingType( |
| 584 const std::string& instance_type) { |
| 585 return instance_type == keys::kThirdPartyKey; |
| 586 } |
| 587 |
| 588 // static |
| 589 scoped_ptr<WebRequestConditionAttribute> |
| 590 WebRequestConditionAttributeThirdParty::Create( |
| 591 const std::string& name, |
| 592 const base::Value* value, |
| 593 std::string* error) { |
| 594 DCHECK(IsMatchingType(name)); |
| 595 |
| 596 bool third_party = false; // Dummy value, gets overwritten. |
| 597 if (!value->GetAsBoolean(&third_party)) { |
| 598 *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidValue, |
| 599 keys::kThirdPartyKey); |
| 600 return scoped_ptr<WebRequestConditionAttribute>(NULL); |
| 601 } |
| 602 |
| 603 return scoped_ptr<WebRequestConditionAttribute>( |
| 604 new WebRequestConditionAttributeThirdParty(third_party)); |
| 605 } |
| 606 |
| 607 int WebRequestConditionAttributeThirdParty::GetStages() const { |
| 608 return ON_BEFORE_REQUEST | ON_BEFORE_SEND_HEADERS | ON_SEND_HEADERS | |
| 609 ON_HEADERS_RECEIVED | ON_AUTH_REQUIRED | ON_BEFORE_REDIRECT | |
| 610 ON_RESPONSE_STARTED | ON_COMPLETED | ON_ERROR; |
| 611 } |
| 612 |
| 613 bool WebRequestConditionAttributeThirdParty::IsFulfilled( |
| 614 const WebRequestRule::RequestData& request_data) const { |
| 615 if (!(request_data.stage & GetStages())) |
| 616 return false; |
| 617 |
| 618 const bool is_first_party = |
| 619 net::RegistryControlledDomainService::SameDomainOrHost( |
| 620 request_data.request->url(), |
| 621 request_data.request->first_party_for_cookies()); |
| 622 return match_third_party_ ? !is_first_party : is_first_party; |
| 623 } |
| 624 |
| 625 WebRequestConditionAttribute::Type |
| 626 WebRequestConditionAttributeThirdParty::GetType() const { |
| 627 return CONDITION_THIRD_PARTY; |
| 628 } |
| 629 |
567 } // namespace extensions | 630 } // namespace extensions |
OLD | NEW |