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