Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(326)

Side by Side Diff: chrome/browser/extensions/api/declarative_webrequest/webrequest_condition_attribute.cc

Issue 11038031: Adding condition attributes for request headers to Declarative WebRequest (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased (only). Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 WebRequestConditionAttribute::WebRequestConditionAttribute() {} 47 WebRequestConditionAttribute::WebRequestConditionAttribute() {}
48 48
49 WebRequestConditionAttribute::~WebRequestConditionAttribute() {} 49 WebRequestConditionAttribute::~WebRequestConditionAttribute() {}
50 50
51 // static 51 // static
52 bool WebRequestConditionAttribute::IsKnownType( 52 bool WebRequestConditionAttribute::IsKnownType(
53 const std::string& instance_type) { 53 const std::string& instance_type) {
54 return 54 return
55 WebRequestConditionAttributeResourceType::IsMatchingType(instance_type) || 55 WebRequestConditionAttributeResourceType::IsMatchingType(instance_type) ||
56 WebRequestConditionAttributeContentType::IsMatchingType(instance_type) || 56 WebRequestConditionAttributeContentType::IsMatchingType(instance_type) ||
57 WebRequestConditionAttributeRequestHeaders::IsMatchingType(
58 instance_type) ||
57 WebRequestConditionAttributeResponseHeaders::IsMatchingType( 59 WebRequestConditionAttributeResponseHeaders::IsMatchingType(
58 instance_type) || 60 instance_type) ||
59 WebRequestConditionAttributeThirdParty::IsMatchingType(instance_type); 61 WebRequestConditionAttributeThirdParty::IsMatchingType(instance_type);
60 } 62 }
61 63
62 // static 64 // static
63 scoped_ptr<WebRequestConditionAttribute> 65 scoped_ptr<WebRequestConditionAttribute>
64 WebRequestConditionAttribute::Create( 66 WebRequestConditionAttribute::Create(
65 const std::string& name, 67 const std::string& name,
66 const base::Value* value, 68 const base::Value* value,
67 std::string* error) { 69 std::string* error) {
68 CHECK(value != NULL && error != NULL); 70 CHECK(value != NULL && error != NULL);
69 if (WebRequestConditionAttributeResourceType::IsMatchingType(name)) { 71 if (WebRequestConditionAttributeResourceType::IsMatchingType(name)) {
70 return WebRequestConditionAttributeResourceType::Create(name, value, error); 72 return WebRequestConditionAttributeResourceType::Create(name, value, error);
71 } else if (WebRequestConditionAttributeContentType::IsMatchingType(name)) { 73 } else if (WebRequestConditionAttributeContentType::IsMatchingType(name)) {
72 return WebRequestConditionAttributeContentType::Create(name, value, error); 74 return WebRequestConditionAttributeContentType::Create(name, value, error);
75 } else if (WebRequestConditionAttributeRequestHeaders::IsMatchingType(
76 name)) {
77 return WebRequestConditionAttributeRequestHeaders::Create(
78 name, value, error);
73 } else if (WebRequestConditionAttributeResponseHeaders::IsMatchingType( 79 } else if (WebRequestConditionAttributeResponseHeaders::IsMatchingType(
74 name)) { 80 name)) {
75 return WebRequestConditionAttributeResponseHeaders::Create( 81 return WebRequestConditionAttributeResponseHeaders::Create(
76 name, value, error); 82 name, value, error);
77 } else if (WebRequestConditionAttributeThirdParty::IsMatchingType(name)) { 83 } else if (WebRequestConditionAttributeThirdParty::IsMatchingType(name)) {
78 return WebRequestConditionAttributeThirdParty::Create(name, value, error); 84 return WebRequestConditionAttributeThirdParty::Create(name, value, error);
79 } 85 }
80 86
81 *error = ExtensionErrorUtils::FormatErrorMessage(kUnknownConditionAttribute, 87 *error = ExtensionErrorUtils::FormatErrorMessage(kUnknownConditionAttribute,
82 name); 88 name);
(...skipping 400 matching lines...) Expand 10 before | Expand all | Expand 10 after
483 489
484 for (size_t i = 0; i < value_match_.size(); ++i) { 490 for (size_t i = 0; i < value_match_.size(); ++i) {
485 if (!value_match_[i]->Matches(value)) 491 if (!value_match_[i]->Matches(value))
486 return false; 492 return false;
487 } 493 }
488 494
489 return true; 495 return true;
490 } 496 }
491 497
492 // 498 //
499 // WebRequestConditionAttributeRequestHeaders
500 //
501
502 WebRequestConditionAttributeRequestHeaders::
503 WebRequestConditionAttributeRequestHeaders(
504 scoped_ptr<const HeaderMatcher> header_matcher,
505 bool positive)
506 : header_matcher_(header_matcher.Pass()),
507 positive_(positive) {}
508
509 WebRequestConditionAttributeRequestHeaders::
510 ~WebRequestConditionAttributeRequestHeaders() {}
511
512 // static
513 bool WebRequestConditionAttributeRequestHeaders::IsMatchingType(
514 const std::string& instance_type) {
515 return instance_type == keys::kRequestHeadersKey ||
516 instance_type == keys::kExcludeRequestHeadersKey;
517 }
518
519 namespace {
520
521 scoped_ptr<const HeaderMatcher> PrepareHeaderMatcher(
522 const std::string& name,
523 const base::Value* value,
524 std::string* error) {
525 const ListValue* value_as_list = NULL;
526 if (!value->GetAsList(&value_as_list)) {
527 *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidValue, name);
528 return scoped_ptr<const HeaderMatcher>(NULL);
529 }
530
531 scoped_ptr<const HeaderMatcher> header_matcher(
532 HeaderMatcher::Create(value_as_list));
533 if (header_matcher.get() == NULL)
534 *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidValue, name);
535 return header_matcher.Pass();
536 }
537
538 } // namespace
539
540 // static
541 scoped_ptr<WebRequestConditionAttribute>
542 WebRequestConditionAttributeRequestHeaders::Create(
543 const std::string& name,
544 const base::Value* value,
545 std::string* error) {
546 DCHECK(IsMatchingType(name));
547
548 scoped_ptr<const HeaderMatcher> header_matcher(
549 PrepareHeaderMatcher(name, value, error));
550 if (header_matcher.get() == NULL)
551 return scoped_ptr<WebRequestConditionAttribute>(NULL);
552
553 return scoped_ptr<WebRequestConditionAttribute>(
554 new WebRequestConditionAttributeRequestHeaders(
555 header_matcher.Pass(), name == keys::kRequestHeadersKey));
556 }
557
558 int WebRequestConditionAttributeRequestHeaders::GetStages() const {
559 // Currently we only allow matching against headers in the before-send-headers
560 // stage. The headers are accessible in other stages as well, but before
561 // allowing to match against them in further stages, we should consider
562 // caching the match result.
563 return ON_BEFORE_SEND_HEADERS;
564 }
565
566 bool WebRequestConditionAttributeRequestHeaders::IsFulfilled(
567 const WebRequestRule::RequestData& request_data) const {
568 if (!(request_data.stage & GetStages()))
569 return false;
570
571 const net::HttpRequestHeaders& headers =
572 request_data.request->extra_request_headers();
573
574 bool passed = false; // Did some header pass TestNameValue?
575 net::HttpRequestHeaders::Iterator it(headers);
576 while (!passed && it.GetNext())
577 passed |= header_matcher_->TestNameValue(it.name(), it.value());
578
579 return (positive_ ? passed : !passed);
580 }
581
582 WebRequestConditionAttribute::Type
583 WebRequestConditionAttributeRequestHeaders::GetType() const {
584 return CONDITION_REQUEST_HEADERS;
585 }
586
587 //
493 // WebRequestConditionAttributeResponseHeaders 588 // WebRequestConditionAttributeResponseHeaders
494 // 589 //
495 590
496 WebRequestConditionAttributeResponseHeaders:: 591 WebRequestConditionAttributeResponseHeaders::
497 WebRequestConditionAttributeResponseHeaders( 592 WebRequestConditionAttributeResponseHeaders(
498 scoped_ptr<const HeaderMatcher>* header_matcher, 593 scoped_ptr<const HeaderMatcher> header_matcher,
499 bool positive) 594 bool positive)
500 : header_matcher_(header_matcher->Pass()), 595 : header_matcher_(header_matcher.Pass()),
501 positive_(positive) {} 596 positive_(positive) {}
502 597
503 WebRequestConditionAttributeResponseHeaders:: 598 WebRequestConditionAttributeResponseHeaders::
504 ~WebRequestConditionAttributeResponseHeaders() {} 599 ~WebRequestConditionAttributeResponseHeaders() {}
505 600
506 // static 601 // static
507 bool WebRequestConditionAttributeResponseHeaders::IsMatchingType( 602 bool WebRequestConditionAttributeResponseHeaders::IsMatchingType(
508 const std::string& instance_type) { 603 const std::string& instance_type) {
509 return instance_type == keys::kResponseHeadersKey || 604 return instance_type == keys::kResponseHeadersKey ||
510 instance_type == keys::kExcludeResponseHeadersKey; 605 instance_type == keys::kExcludeResponseHeadersKey;
511 } 606 }
512 607
513 // static 608 // static
514 scoped_ptr<WebRequestConditionAttribute> 609 scoped_ptr<WebRequestConditionAttribute>
515 WebRequestConditionAttributeResponseHeaders::Create( 610 WebRequestConditionAttributeResponseHeaders::Create(
516 const std::string& name, 611 const std::string& name,
517 const base::Value* value, 612 const base::Value* value,
518 std::string* error) { 613 std::string* error) {
519 DCHECK(IsMatchingType(name)); 614 DCHECK(IsMatchingType(name));
520 615
521 const ListValue* value_as_list = NULL; 616 scoped_ptr<const HeaderMatcher> header_matcher(
522 if (!value->GetAsList(&value_as_list)) { 617 PrepareHeaderMatcher(name, value, error));
523 *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidValue, name); 618 if (header_matcher.get() == NULL)
524 return scoped_ptr<WebRequestConditionAttribute>(NULL); 619 return scoped_ptr<WebRequestConditionAttribute>(NULL);
525 }
526 620
527 scoped_ptr<const HeaderMatcher> header_matcher(
528 HeaderMatcher::Create(value_as_list));
529 if (header_matcher.get() == NULL) {
530 *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidValue, name);
531 return scoped_ptr<WebRequestConditionAttribute>(NULL);
532 }
533
534 const bool positive = name == keys::kResponseHeadersKey;
535 return scoped_ptr<WebRequestConditionAttribute>( 621 return scoped_ptr<WebRequestConditionAttribute>(
536 new WebRequestConditionAttributeResponseHeaders( 622 new WebRequestConditionAttributeResponseHeaders(
537 &header_matcher, positive)); 623 header_matcher.Pass(), name == keys::kResponseHeadersKey));
538 } 624 }
539 625
540 int WebRequestConditionAttributeResponseHeaders::GetStages() const { 626 int WebRequestConditionAttributeResponseHeaders::GetStages() const {
541 return ON_HEADERS_RECEIVED; 627 return ON_HEADERS_RECEIVED;
542 } 628 }
543 629
544 bool WebRequestConditionAttributeResponseHeaders::IsFulfilled( 630 bool WebRequestConditionAttributeResponseHeaders::IsFulfilled(
545 const WebRequestRule::RequestData& request_data) const { 631 const WebRequestRule::RequestData& request_data) const {
546 if (!(request_data.stage & GetStages())) 632 if (!(request_data.stage & GetStages()))
547 return false; 633 return false;
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
627 713
628 return match_third_party_ ? !is_first_party : is_first_party; 714 return match_third_party_ ? !is_first_party : is_first_party;
629 } 715 }
630 716
631 WebRequestConditionAttribute::Type 717 WebRequestConditionAttribute::Type
632 WebRequestConditionAttributeThirdParty::GetType() const { 718 WebRequestConditionAttributeThirdParty::GetType() const {
633 return CONDITION_THIRD_PARTY; 719 return CONDITION_THIRD_PARTY;
634 } 720 }
635 721
636 } // namespace extensions 722 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698