Chromium Code Reviews| Index: chrome/browser/extensions/api/declarative_webrequest/webrequest_action.cc |
| diff --git a/chrome/browser/extensions/api/declarative_webrequest/webrequest_action.cc b/chrome/browser/extensions/api/declarative_webrequest/webrequest_action.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a15e08ccfcb7f81570a1ee0b85f0648a47ab4257 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/declarative_webrequest/webrequest_action.cc |
| @@ -0,0 +1,115 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/extensions/api/declarative_webrequest/webrequest_action.h" |
| + |
| +#include "base/logging.h" |
| +#include "base/stringprintf.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/extensions/api/declarative_webrequest/request_stages.h" |
| + |
| +namespace extensions { |
| +namespace declarative_webrequest { |
| + |
| +namespace { |
| +// Constants from the JavaScript API. |
| +const char kInstanceType[] = "instanceType"; |
| +const char kInstanceCancel[] = "experimental.webRequest.CancelRequest"; |
| + |
| +// Error messages. |
| +const char kExpectedDictionary[] = "Expected a dictionary as action."; |
| +const char kInvalidInstanceTypeError[] = |
| + "An action has an invalid instanceType: %s"; |
| +} // namespace |
| + |
| +// |
| +// WebRequestAction |
| +// |
| + |
| +WebRequestAction::WebRequestAction() {} |
| + |
| +WebRequestAction::~WebRequestAction() {} |
| + |
| + |
| +// |
| +// WebRequestActionCollection |
| +// |
| + |
| +WebRequestActionCollection::WebRequestActionCollection(const Actions& actions) |
| + : actions_(actions) {} |
| + |
| +WebRequestActionCollection::~WebRequestActionCollection() {} |
| + |
| + |
| +// |
| +// WebRequestActionFactory |
| +// |
| + |
| +// static |
| +bool WebRequestActionFactory::IsHandledByThisFactory( |
| + const std::string& action_name) { |
| + return action_name == kInstanceCancel; |
| +} |
| + |
| +// static |
| +scoped_ptr<WebRequestAction> WebRequestActionFactory::CreateAction( |
| + const base::Value& json_action, |
| + std::string* error) { |
| + const base::DictionaryValue* action_dict = NULL; |
| + if (!json_action.GetAsDictionary(&action_dict)) { |
| + *error = kExpectedDictionary; |
| + return scoped_ptr<WebRequestAction>(NULL); |
| + } |
| + |
| + std::string instance_type = "No instanceType"; |
| + if (!action_dict->GetString(kInstanceType, &instance_type)) { |
| + *error = base::StringPrintf(kInvalidInstanceTypeError, |
| + instance_type.c_str()); |
| + return scoped_ptr<WebRequestAction>(NULL); |
| + } |
| + |
| + if (instance_type == kInstanceCancel) { |
|
Matt Perry
2012/03/22 23:07:25
Take a look at ExtensionFunctionRegistry for a way
battre
2012/03/26 18:35:51
Yes, I had that in mind. I will change this code o
|
| + *error = ""; |
| + return scoped_ptr<WebRequestAction>(new WebRequestCancelAction); |
| + } |
| + |
| + *error = base::StringPrintf(kInvalidInstanceTypeError, instance_type.c_str()); |
| + return scoped_ptr<WebRequestAction>(); |
| +} |
| + |
| +// static |
| +scoped_ptr<WebRequestActionCollection> |
| +WebRequestActionFactory::CreateActionCollection( |
| + const WebRequestActionFactory::AnyVector& actions, |
| + std::string* error) { |
| + WebRequestActionCollection::Actions result; |
| + |
| + for (AnyVector::const_iterator i = actions.begin(); |
| + i != actions.end(); ++i) { |
| + CHECK(i->get()); |
| + scoped_ptr<WebRequestAction> action = CreateAction((*i)->value(), error); |
| + if (!error->empty()) |
| + return scoped_ptr<WebRequestActionCollection>(NULL); |
| + result.push_back(make_linked_ptr(action.release())); |
| + } |
| + |
| + return scoped_ptr<WebRequestActionCollection>( |
| + new WebRequestActionCollection(result)); |
| +} |
| + |
| +// |
| +// WebRequestCancelAction |
| +// |
| + |
| +WebRequestCancelAction::WebRequestCancelAction() {} |
| + |
| +WebRequestCancelAction::~WebRequestCancelAction() {} |
| + |
| +int WebRequestCancelAction::GetStages() const { |
| + return ON_BEFORE_REQUEST | ON_BEFORE_SEND_HEADERS | ON_HEADERS_RECEIVED | |
| + ON_AUTH_REQUIRED; |
| +} |
| + |
| +} // namespace declarative_webrequest |
| +} // namespace extensions |