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 namespace declarative_webrequest { |
| 24 |
| 25 // |
| 26 // WebRequestConditionAttribute |
| 27 // |
| 28 |
| 29 WebRequestConditionAttribute::WebRequestConditionAttribute() {} |
| 30 |
| 31 WebRequestConditionAttribute::~WebRequestConditionAttribute() {} |
| 32 |
| 33 |
| 34 // |
| 35 // WebRequestConditionAttributeFactory |
| 36 // |
| 37 |
| 38 // static |
| 39 bool WebRequestConditionAttributeFactory::IsHandledByThisFactory( |
| 40 const std::string& instance_type) { |
| 41 return WebRequestConditionAttributeHasScheme::IsMatchingType(instance_type); |
| 42 } |
| 43 |
| 44 // static |
| 45 scoped_ptr<WebRequestConditionAttribute> |
| 46 WebRequestConditionAttributeFactory::Create( |
| 47 const std::string& name, |
| 48 const base::Value* value, |
| 49 std::string* error) { |
| 50 if (WebRequestConditionAttributeHasScheme::IsMatchingType(name)) { |
| 51 return WebRequestConditionAttributeHasScheme::Create(name, value, error); |
| 52 } |
| 53 |
| 54 *error = base::StringPrintf(kUnknownConditionAttribute, name.c_str()); |
| 55 return scoped_ptr<WebRequestConditionAttribute>(NULL); |
| 56 } |
| 57 |
| 58 |
| 59 // |
| 60 // WebRequestConditionAttributeHasScheme |
| 61 // |
| 62 |
| 63 WebRequestConditionAttributeHasScheme::WebRequestConditionAttributeHasScheme( |
| 64 const std::string& pattern) |
| 65 : pattern_(pattern) {} |
| 66 |
| 67 WebRequestConditionAttributeHasScheme::~WebRequestConditionAttributeHasScheme() |
| 68 {} |
| 69 |
| 70 // static |
| 71 bool WebRequestConditionAttributeHasScheme::IsMatchingType( |
| 72 const std::string& instance_type) { |
| 73 return instance_type == kSchemeKey; |
| 74 } |
| 75 |
| 76 // static |
| 77 scoped_ptr<WebRequestConditionAttribute> |
| 78 WebRequestConditionAttributeHasScheme::Create( |
| 79 const std::string& name, |
| 80 const base::Value* value, |
| 81 std::string* error) { |
| 82 DCHECK(IsMatchingType(name)); |
| 83 |
| 84 std::string scheme = ""; |
| 85 if (!value->GetAsString(&scheme)) { |
| 86 *error = base::StringPrintf(kInvalidValue, kSchemeKey); |
| 87 return scoped_ptr<WebRequestConditionAttribute>(NULL); |
| 88 } |
| 89 |
| 90 return scoped_ptr<WebRequestConditionAttribute>( |
| 91 new WebRequestConditionAttributeHasScheme(scheme)); |
| 92 } |
| 93 |
| 94 int WebRequestConditionAttributeHasScheme::GetStages() const { |
| 95 return ON_BEFORE_REQUEST | ON_BEFORE_SEND_HEADERS | ON_SEND_HEADERS | |
| 96 ON_HEADERS_RECEIVED | ON_AUTH_REQUIRED | ON_BEFORE_REDIRECT | |
| 97 ON_RESPONSE_STARTED | ON_COMPLETED | ON_ERROR; |
| 98 } |
| 99 |
| 100 bool WebRequestConditionAttributeHasScheme::IsFulfilled( |
| 101 net::URLRequest* request) { |
| 102 return request->url().scheme() == pattern_; |
| 103 } |
| 104 |
| 105 } // namespace declarative_webrequest |
| 106 } // namespace extensions |
OLD | NEW |