OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit
ion_attribute.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/stringprintf.h" |
| 9 #include "base/values.h" |
| 10 #include "chrome/browser/extensions/api/declarative_webrequest/request_stages.h" |
| 11 #include "net/url_request/url_request.h" |
| 12 |
| 13 namespace { |
| 14 // Constants from the JavaScript API. |
| 15 const char kSchemeKey[] = "scheme"; |
| 16 |
| 17 // Error messages. |
| 18 const char kUnknownConditionAttribute[] = "Unknown matching condition: '%s'"; |
| 19 const char kInvalidValue[] = "Condition '%s' has an invalid value"; |
| 20 } |
| 21 |
| 22 namespace extensions { |
| 23 |
| 24 // |
| 25 // WebRequestConditionAttribute |
| 26 // |
| 27 |
| 28 WebRequestConditionAttribute::WebRequestConditionAttribute() {} |
| 29 |
| 30 WebRequestConditionAttribute::~WebRequestConditionAttribute() {} |
| 31 |
| 32 // static |
| 33 bool WebRequestConditionAttribute::IsKnownType( |
| 34 const std::string& instance_type) { |
| 35 return WebRequestConditionAttributeHasScheme::IsMatchingType(instance_type); |
| 36 } |
| 37 |
| 38 // static |
| 39 scoped_ptr<WebRequestConditionAttribute> |
| 40 WebRequestConditionAttribute::Create( |
| 41 const std::string& name, |
| 42 const base::Value* value, |
| 43 std::string* error) { |
| 44 if (WebRequestConditionAttributeHasScheme::IsMatchingType(name)) { |
| 45 return WebRequestConditionAttributeHasScheme::Create(name, value, error); |
| 46 } |
| 47 |
| 48 *error = base::StringPrintf(kUnknownConditionAttribute, name.c_str()); |
| 49 return scoped_ptr<WebRequestConditionAttribute>(NULL); |
| 50 } |
| 51 |
| 52 |
| 53 // |
| 54 // WebRequestConditionAttributeHasScheme |
| 55 // |
| 56 |
| 57 WebRequestConditionAttributeHasScheme::WebRequestConditionAttributeHasScheme( |
| 58 const std::string& pattern) |
| 59 : pattern_(pattern) {} |
| 60 |
| 61 WebRequestConditionAttributeHasScheme::~WebRequestConditionAttributeHasScheme() |
| 62 {} |
| 63 |
| 64 // static |
| 65 bool WebRequestConditionAttributeHasScheme::IsMatchingType( |
| 66 const std::string& instance_type) { |
| 67 return instance_type == kSchemeKey; |
| 68 } |
| 69 |
| 70 // |
| 71 // WebRequestConditionAttributeHasScheme |
| 72 // |
| 73 |
| 74 // static |
| 75 scoped_ptr<WebRequestConditionAttribute> |
| 76 WebRequestConditionAttributeHasScheme::Create( |
| 77 const std::string& name, |
| 78 const base::Value* value, |
| 79 std::string* error) { |
| 80 DCHECK(IsMatchingType(name)); |
| 81 |
| 82 std::string scheme; |
| 83 if (!value->GetAsString(&scheme)) { |
| 84 *error = base::StringPrintf(kInvalidValue, kSchemeKey); |
| 85 return scoped_ptr<WebRequestConditionAttribute>(NULL); |
| 86 } |
| 87 |
| 88 return scoped_ptr<WebRequestConditionAttribute>( |
| 89 new WebRequestConditionAttributeHasScheme(scheme)); |
| 90 } |
| 91 |
| 92 int WebRequestConditionAttributeHasScheme::GetStages() const { |
| 93 return ON_BEFORE_REQUEST | ON_BEFORE_SEND_HEADERS | ON_SEND_HEADERS | |
| 94 ON_HEADERS_RECEIVED | ON_AUTH_REQUIRED | ON_BEFORE_REDIRECT | |
| 95 ON_RESPONSE_STARTED | ON_COMPLETED | ON_ERROR; |
| 96 } |
| 97 |
| 98 bool WebRequestConditionAttributeHasScheme::IsFulfilled( |
| 99 net::URLRequest* request) { |
| 100 return request->url().scheme() == pattern_; |
| 101 } |
| 102 |
| 103 WebRequestConditionAttribute::Type |
| 104 WebRequestConditionAttributeHasScheme::GetType() const { |
| 105 return CONDITION_HAS_SCHEME; |
| 106 } |
| 107 |
| 108 } // namespace extensions |
OLD | NEW |