OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef EXTENSIONS_BROWSER_EXTENSION_FUNCTION_H_ | 5 #ifndef EXTENSIONS_BROWSER_EXTENSION_FUNCTION_H_ |
6 #define EXTENSIONS_BROWSER_EXTENSION_FUNCTION_H_ | 6 #define EXTENSIONS_BROWSER_EXTENSION_FUNCTION_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 #include <string> | 9 #include <string> |
10 | 10 |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 // | 108 // |
109 // The default implementation is to check the Extension's permissions against | 109 // The default implementation is to check the Extension's permissions against |
110 // what this function requires to run, but some APIs may require finer | 110 // what this function requires to run, but some APIs may require finer |
111 // grained control, such as tabs.executeScript being allowed for active tabs. | 111 // grained control, such as tabs.executeScript being allowed for active tabs. |
112 // | 112 // |
113 // This will be run after the function has been set up but before Run(). | 113 // This will be run after the function has been set up but before Run(). |
114 virtual bool HasPermission(); | 114 virtual bool HasPermission(); |
115 | 115 |
116 // The result of a function call. | 116 // The result of a function call. |
117 // | 117 // |
118 // Use NoArguments(), SingleArgument(), MultipleArguments(), or Error() | 118 // Use NoArguments(), OneArgument(), ArgumentList(), or Error() |
119 // rather than this class directly. | 119 // rather than this class directly. |
120 class ResponseValueObject { | 120 class ResponseValueObject { |
121 public: | 121 public: |
122 virtual ~ResponseValueObject() {} | 122 virtual ~ResponseValueObject() {} |
123 | 123 |
124 // Returns true for success, false for failure. | 124 // Returns true for success, false for failure. |
125 virtual bool Apply() = 0; | 125 virtual bool Apply() = 0; |
126 }; | 126 }; |
127 typedef scoped_ptr<ResponseValueObject> ResponseValue; | 127 typedef scoped_ptr<ResponseValueObject> ResponseValue; |
128 | 128 |
129 // The action to use when returning from RunAsync. | 129 // The action to use when returning from RunAsync. |
130 // | 130 // |
131 // Use RespondNow() or RespondLater() rather than this class directly. | 131 // Use RespondNow() or RespondLater() rather than this class directly. |
132 class ResponseActionObject { | 132 class ResponseActionObject { |
133 public: | 133 public: |
134 virtual ~ResponseActionObject() {} | 134 virtual ~ResponseActionObject() {} |
135 | 135 |
136 virtual void Execute() = 0; | 136 virtual void Execute() = 0; |
137 }; | 137 }; |
138 typedef scoped_ptr<ResponseActionObject> ResponseAction; | 138 typedef scoped_ptr<ResponseActionObject> ResponseAction; |
139 | 139 |
140 // Runs the function and returns the action to take when the caller is ready | 140 // Runs the function and returns the action to take when the caller is ready |
141 // to respond. | 141 // to respond. |
142 // | 142 // |
143 // Typical return values might be: | 143 // Typical return values might be: |
144 // * RespondNow(NoArguments()) | 144 // * RespondNow(NoArguments()) |
145 // * RespondNow(SingleArgument(42)) | 145 // * RespondNow(OneArgument(42)) |
146 // * RespondNow(MultipleArguments(my_result.ToValue())) | 146 // * RespondNow(ArgumentList(my_result.ToValue())) |
147 // * RespondNow(Error("Warp core breach")) | 147 // * RespondNow(Error("Warp core breach")) |
148 // * RespondLater(), then later, | 148 // * RespondLater(), then later, |
149 // * Respond(NoArguments()) | 149 // * Respond(NoArguments()) |
150 // * ... etc. | 150 // * ... etc. |
151 // | 151 // |
152 // | 152 // |
153 // Callers must call Execute() on the return ResponseAction at some point, | 153 // Callers must call Execute() on the return ResponseAction at some point, |
154 // exactly once. | 154 // exactly once. |
155 // | 155 // |
156 // SyncExtensionFunction and AsyncExtensionFunction implement this in terms | 156 // SyncExtensionFunction and AsyncExtensionFunction implement this in terms |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 void set_source_tab_id(int source_tab_id) { source_tab_id_ = source_tab_id; } | 241 void set_source_tab_id(int source_tab_id) { source_tab_id_ = source_tab_id; } |
242 int source_tab_id() const { return source_tab_id_; } | 242 int source_tab_id() const { return source_tab_id_; } |
243 | 243 |
244 protected: | 244 protected: |
245 friend struct ExtensionFunctionDeleteTraits; | 245 friend struct ExtensionFunctionDeleteTraits; |
246 | 246 |
247 // ResponseValues. | 247 // ResponseValues. |
248 // | 248 // |
249 // Success, no arguments to pass to caller | 249 // Success, no arguments to pass to caller |
250 ResponseValue NoArguments(); | 250 ResponseValue NoArguments(); |
251 // Success, a single argument |result| to pass to caller. TAKES OWNERSHIP. | 251 // Success, a single argument |arg| to pass to caller. TAKES OWNERSHIP -- a |
252 ResponseValue SingleArgument(base::Value* result); | 252 // raw pointer for convenience, since callers usually construct the argument |
253 // Success, a list of arguments |results| to pass to caller. TAKES OWNERSHIP. | 253 // to this by hand. |
254 ResponseValue MultipleArguments(base::ListValue* results); | 254 ResponseValue OneArgument(base::Value* arg); |
| 255 // Success, two arguments |arg1| and |arg2| to pass to caller. TAKES |
| 256 // OWNERSHIP -- raw pointers for convenience, since callers usually construct |
| 257 // the argument to this by hand. Note that use of this function may imply you |
| 258 // should be using the generated Result struct and ArgumentList. |
| 259 ResponseValue TwoArguments(base::Value* arg1, base::Value* arg2); |
| 260 // Success, a list of arguments |results| to pass to caller. TAKES OWNERSHIP |
| 261 // -- |
| 262 // a scoped_ptr<> for convenience, since callers usually get this from the |
| 263 // result of a ToValue() call on the generated Result struct. |
| 264 ResponseValue ArgumentList(scoped_ptr<base::ListValue> results); |
255 // Error. chrome.runtime.lastError.message will be set to |error|. | 265 // Error. chrome.runtime.lastError.message will be set to |error|. |
256 ResponseValue Error(const std::string& error); | 266 ResponseValue Error(const std::string& error); |
257 // Bad message. A ResponseValue equivalent to EXTENSION_FUNCTION_VALIDATE(). | 267 // Bad message. A ResponseValue equivalent to EXTENSION_FUNCTION_VALIDATE(). |
258 ResponseValue BadMessage(); | 268 ResponseValue BadMessage(); |
259 | 269 |
260 // ResponseActions. | 270 // ResponseActions. |
261 // | 271 // |
262 // Respond to the extension immediately with |result|. | 272 // Respond to the extension immediately with |result|. |
263 ResponseAction RespondNow(ResponseValue result); | 273 ResponseAction RespondNow(ResponseValue result); |
264 // Don't respond now, but promise to call Respond() later. | 274 // Don't respond now, but promise to call Respond() later. |
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
557 virtual bool RunSync() = 0; | 567 virtual bool RunSync() = 0; |
558 | 568 |
559 // ValidationFailure override to match RunSync(). | 569 // ValidationFailure override to match RunSync(). |
560 static bool ValidationFailure(SyncIOThreadExtensionFunction* function); | 570 static bool ValidationFailure(SyncIOThreadExtensionFunction* function); |
561 | 571 |
562 private: | 572 private: |
563 virtual ResponseAction Run() OVERRIDE; | 573 virtual ResponseAction Run() OVERRIDE; |
564 }; | 574 }; |
565 | 575 |
566 #endif // EXTENSIONS_BROWSER_EXTENSION_FUNCTION_H_ | 576 #endif // EXTENSIONS_BROWSER_EXTENSION_FUNCTION_H_ |
OLD | NEW |