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

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: Removed 0-sized arrays. 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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 WebRequestConditionAttribute::WebRequestConditionAttribute() {} 44 WebRequestConditionAttribute::WebRequestConditionAttribute() {}
45 45
46 WebRequestConditionAttribute::~WebRequestConditionAttribute() {} 46 WebRequestConditionAttribute::~WebRequestConditionAttribute() {}
47 47
48 // static 48 // static
49 bool WebRequestConditionAttribute::IsKnownType( 49 bool WebRequestConditionAttribute::IsKnownType(
50 const std::string& instance_type) { 50 const std::string& instance_type) {
51 return 51 return
52 WebRequestConditionAttributeResourceType::IsMatchingType(instance_type) || 52 WebRequestConditionAttributeResourceType::IsMatchingType(instance_type) ||
53 WebRequestConditionAttributeContentType::IsMatchingType(instance_type) || 53 WebRequestConditionAttributeContentType::IsMatchingType(instance_type) ||
54 WebRequestConditionAttributeRequestHeaders::IsMatchingType(
55 instance_type) ||
54 WebRequestConditionAttributeResponseHeaders::IsMatchingType( 56 WebRequestConditionAttributeResponseHeaders::IsMatchingType(
55 instance_type); 57 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);
71 } else if (WebRequestConditionAttributeRequestHeaders::IsMatchingType(
72 name)) {
73 return WebRequestConditionAttributeRequestHeaders::Create(
74 name, value, error);
69 } else if (WebRequestConditionAttributeResponseHeaders::IsMatchingType( 75 } else if (WebRequestConditionAttributeResponseHeaders::IsMatchingType(
70 name)) { 76 name)) {
71 return WebRequestConditionAttributeResponseHeaders::Create( 77 return WebRequestConditionAttributeResponseHeaders::Create(
72 name, value, error); 78 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 }
(...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after
477 483
478 for (size_t i = 0; i < value_match_.size(); ++i) { 484 for (size_t i = 0; i < value_match_.size(); ++i) {
479 if (!value_match_[i]->Matches(value)) 485 if (!value_match_[i]->Matches(value))
480 return false; 486 return false;
481 } 487 }
482 488
483 return true; 489 return true;
484 } 490 }
485 491
486 // 492 //
493 // WebRequestConditionAttributeRequestHeaders
494 //
495
496 WebRequestConditionAttributeRequestHeaders::
497 WebRequestConditionAttributeRequestHeaders(
498 scoped_ptr<const HeaderMatcher>* header_matcher,
battre 2012/10/05 12:50:03 This usage of a scoped_ptr is wrong. Can you pleas
vabr (Chromium) 2012/10/05 15:56:53 Done.
499 bool positive)
500 : header_matcher_(header_matcher->Pass()),
501 positive_(positive) {}
502
503 WebRequestConditionAttributeRequestHeaders::
504 ~WebRequestConditionAttributeRequestHeaders() {}
505
506 // static
507 bool WebRequestConditionAttributeRequestHeaders::IsMatchingType(
508 const std::string& instance_type) {
509 return instance_type == keys::kRequestHeadersKey ||
510 instance_type == keys::kExcludeRequestHeadersKey;
511 }
512
513 namespace {
514
515 scoped_ptr<const HeaderMatcher> PrepareHeaderMatcher(
516 const std::string& name,
517 const base::Value* value,
518 std::string* error) {
519 const ListValue* value_as_list = NULL;
520 if (!value->GetAsList(&value_as_list)) {
521 *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidValue, name);
522 return scoped_ptr<const HeaderMatcher>(NULL);
523 }
524
525 scoped_ptr<const HeaderMatcher> header_matcher(
526 HeaderMatcher::Create(value_as_list));
527 if (header_matcher.get() == NULL)
528 *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidValue, name);
529 return header_matcher.Pass();
530 }
531
532 } // namespace
533
534 // static
535 scoped_ptr<WebRequestConditionAttribute>
536 WebRequestConditionAttributeRequestHeaders::Create(
537 const std::string& name,
538 const base::Value* value,
539 std::string* error) {
540 DCHECK(IsMatchingType(name));
541
542 scoped_ptr<const HeaderMatcher> header_matcher(
543 PrepareHeaderMatcher(name, value, error));
544 if (header_matcher.get() == NULL)
545 return scoped_ptr<WebRequestConditionAttribute>(NULL);
546
547 return scoped_ptr<WebRequestConditionAttribute>(
548 new WebRequestConditionAttributeRequestHeaders(
549 &header_matcher, name == keys::kRequestHeadersKey));
battre 2012/10/05 12:50:03 &header_matcher should be replaces with header_mat
vabr (Chromium) 2012/10/05 15:56:53 Done.
550 }
551
552 int WebRequestConditionAttributeRequestHeaders::GetStages() const {
553 // Currently we only allow matching against headers in the before-send-headers
554 // stage. The headers are accessible in other stages as well, but before
555 // allowing to match against them in further stages, we should consider
556 // caching the match result.
557 return ON_BEFORE_SEND_HEADERS;
558 }
559
560 bool WebRequestConditionAttributeRequestHeaders::IsFulfilled(
561 const WebRequestRule::RequestData& request_data) const {
562 if (!(request_data.stage & GetStages()))
563 return false;
564
565 const net::HttpRequestHeaders* headers =
566 &(request_data.request->extra_request_headers());
567
568 bool passed = false; // Did some header pass TestNameValue?
569 net::HttpRequestHeaders::Iterator it(*headers);
570 while (!passed && it.GetNext()) {
571 passed |= header_matcher_->TestNameValue(it.name(), it.value());
572 }
573
574 return (positive_ ? passed : !passed);
575 }
576
577 WebRequestConditionAttribute::Type
578 WebRequestConditionAttributeRequestHeaders::GetType() const {
579 return CONDITION_REQUEST_HEADERS;
580 }
581
582 //
487 // WebRequestConditionAttributeResponseHeaders 583 // WebRequestConditionAttributeResponseHeaders
488 // 584 //
489 585
490 WebRequestConditionAttributeResponseHeaders:: 586 WebRequestConditionAttributeResponseHeaders::
491 WebRequestConditionAttributeResponseHeaders( 587 WebRequestConditionAttributeResponseHeaders(
492 scoped_ptr<const HeaderMatcher>* header_matcher, 588 scoped_ptr<const HeaderMatcher>* header_matcher,
493 bool positive) 589 bool positive)
494 : header_matcher_(header_matcher->Pass()), 590 : header_matcher_(header_matcher->Pass()),
495 positive_(positive) {} 591 positive_(positive) {}
496 592
497 WebRequestConditionAttributeResponseHeaders:: 593 WebRequestConditionAttributeResponseHeaders::
498 ~WebRequestConditionAttributeResponseHeaders() {} 594 ~WebRequestConditionAttributeResponseHeaders() {}
499 595
500 // static 596 // static
501 bool WebRequestConditionAttributeResponseHeaders::IsMatchingType( 597 bool WebRequestConditionAttributeResponseHeaders::IsMatchingType(
502 const std::string& instance_type) { 598 const std::string& instance_type) {
503 return instance_type == keys::kResponseHeadersKey || 599 return instance_type == keys::kResponseHeadersKey ||
504 instance_type == keys::kExcludeResponseHeadersKey; 600 instance_type == keys::kExcludeResponseHeadersKey;
505 } 601 }
506 602
507 // static 603 // static
508 scoped_ptr<WebRequestConditionAttribute> 604 scoped_ptr<WebRequestConditionAttribute>
509 WebRequestConditionAttributeResponseHeaders::Create( 605 WebRequestConditionAttributeResponseHeaders::Create(
510 const std::string& name, 606 const std::string& name,
511 const base::Value* value, 607 const base::Value* value,
512 std::string* error) { 608 std::string* error) {
513 DCHECK(IsMatchingType(name)); 609 DCHECK(IsMatchingType(name));
514 610
515 const ListValue* value_as_list = NULL; 611 scoped_ptr<const HeaderMatcher> header_matcher(
516 if (!value->GetAsList(&value_as_list)) { 612 PrepareHeaderMatcher(name, value, error));
517 *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidValue, name); 613 if (header_matcher.get() == NULL)
518 return scoped_ptr<WebRequestConditionAttribute>(NULL); 614 return scoped_ptr<WebRequestConditionAttribute>(NULL);
519 }
520 615
521 scoped_ptr<const HeaderMatcher> header_matcher(
522 HeaderMatcher::Create(value_as_list));
523 if (header_matcher.get() == NULL) {
524 *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidValue, name);
525 return scoped_ptr<WebRequestConditionAttribute>(NULL);
526 }
527
528 const bool positive = name == keys::kResponseHeadersKey;
529 return scoped_ptr<WebRequestConditionAttribute>( 616 return scoped_ptr<WebRequestConditionAttribute>(
530 new WebRequestConditionAttributeResponseHeaders( 617 new WebRequestConditionAttributeResponseHeaders(
531 &header_matcher, positive)); 618 &header_matcher, name == keys::kResponseHeadersKey));
532 } 619 }
533 620
534 int WebRequestConditionAttributeResponseHeaders::GetStages() const { 621 int WebRequestConditionAttributeResponseHeaders::GetStages() const {
535 return ON_HEADERS_RECEIVED; 622 return ON_HEADERS_RECEIVED;
536 } 623 }
537 624
538 bool WebRequestConditionAttributeResponseHeaders::IsFulfilled( 625 bool WebRequestConditionAttributeResponseHeaders::IsFulfilled(
539 const WebRequestRule::RequestData& request_data) const { 626 const WebRequestRule::RequestData& request_data) const {
540 if (!(request_data.stage & GetStages())) 627 if (!(request_data.stage & GetStages()))
541 return false; 628 return false;
(...skipping 16 matching lines...) Expand all
558 645
559 return (positive_ ? passed : !passed); 646 return (positive_ ? passed : !passed);
560 } 647 }
561 648
562 WebRequestConditionAttribute::Type 649 WebRequestConditionAttribute::Type
563 WebRequestConditionAttributeResponseHeaders::GetType() const { 650 WebRequestConditionAttributeResponseHeaders::GetType() const {
564 return CONDITION_RESPONSE_HEADERS; 651 return CONDITION_RESPONSE_HEADERS;
565 } 652 }
566 653
567 } // namespace extensions 654 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698