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_action
.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 |
| 12 namespace extensions { |
| 13 |
| 14 namespace { |
| 15 // Constants from the JavaScript API. |
| 16 const char kInstanceType[] = "instanceType"; |
| 17 const char kInstanceCancel[] = "experimental.webRequest.CancelRequest"; |
| 18 |
| 19 // Error messages. |
| 20 const char kExpectedDictionary[] = "Expected a dictionary as action."; |
| 21 const char kInvalidInstanceTypeError[] = |
| 22 "An action has an invalid instanceType: %s"; |
| 23 } // namespace |
| 24 |
| 25 // |
| 26 // WebRequestAction |
| 27 // |
| 28 |
| 29 WebRequestAction::WebRequestAction() {} |
| 30 |
| 31 WebRequestAction::~WebRequestAction() {} |
| 32 |
| 33 // static |
| 34 scoped_ptr<WebRequestAction> WebRequestAction::Create( |
| 35 const base::Value& json_action, |
| 36 std::string* error) { |
| 37 const base::DictionaryValue* action_dict = NULL; |
| 38 if (!json_action.GetAsDictionary(&action_dict)) { |
| 39 *error = kExpectedDictionary; |
| 40 return scoped_ptr<WebRequestAction>(NULL); |
| 41 } |
| 42 |
| 43 std::string instance_type = "No instanceType"; |
| 44 if (!action_dict->GetString(kInstanceType, &instance_type)) { |
| 45 *error = base::StringPrintf(kInvalidInstanceTypeError, |
| 46 instance_type.c_str()); |
| 47 return scoped_ptr<WebRequestAction>(NULL); |
| 48 } |
| 49 |
| 50 if (instance_type == kInstanceCancel) { |
| 51 *error = ""; |
| 52 return scoped_ptr<WebRequestAction>(new WebRequestCancelAction); |
| 53 } |
| 54 |
| 55 *error = base::StringPrintf(kInvalidInstanceTypeError, instance_type.c_str()); |
| 56 return scoped_ptr<WebRequestAction>(); |
| 57 } |
| 58 |
| 59 |
| 60 // |
| 61 // WebRequestActionSet |
| 62 // |
| 63 |
| 64 WebRequestActionSet::WebRequestActionSet(const Actions& actions) |
| 65 : actions_(actions) {} |
| 66 |
| 67 WebRequestActionSet::~WebRequestActionSet() {} |
| 68 |
| 69 // static |
| 70 scoped_ptr<WebRequestActionSet> WebRequestActionSet::Create( |
| 71 const AnyVector& actions, |
| 72 std::string* error) { |
| 73 Actions result; |
| 74 |
| 75 for (AnyVector::const_iterator i = actions.begin(); |
| 76 i != actions.end(); ++i) { |
| 77 CHECK(i->get()); |
| 78 scoped_ptr<WebRequestAction> action = |
| 79 WebRequestAction::Create((*i)->value(), error); |
| 80 if (!error->empty()) |
| 81 return scoped_ptr<WebRequestActionSet>(NULL); |
| 82 result.push_back(make_linked_ptr(action.release())); |
| 83 } |
| 84 |
| 85 return scoped_ptr<WebRequestActionSet>(new WebRequestActionSet(result)); |
| 86 } |
| 87 |
| 88 |
| 89 // |
| 90 // WebRequestCancelAction |
| 91 // |
| 92 |
| 93 WebRequestCancelAction::WebRequestCancelAction() {} |
| 94 |
| 95 WebRequestCancelAction::~WebRequestCancelAction() {} |
| 96 |
| 97 int WebRequestCancelAction::GetStages() const { |
| 98 return ON_BEFORE_REQUEST | ON_BEFORE_SEND_HEADERS | ON_HEADERS_RECEIVED | |
| 99 ON_AUTH_REQUIRED; |
| 100 } |
| 101 |
| 102 WebRequestAction::Type WebRequestCancelAction::GetType() const { |
| 103 return WebRequestAction::ACTION_CANCEL_REQUEST; |
| 104 } |
| 105 |
| 106 } // namespace extensions |
OLD | NEW |