Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(100)

Side by Side Diff: extensions/browser/extension_function.h

Issue 257333002: Drive extension functions from ExtensionFunction::Run. The (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix comment Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « extensions/browser/api/test/test_api.cc ('k') | extensions/browser/extension_function.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 111
112 // Returns true if the function has permission to run. 112 // Returns true if the function has permission to run.
113 // 113 //
114 // The default implementation is to check the Extension's permissions against 114 // The default implementation is to check the Extension's permissions against
115 // what this function requires to run, but some APIs may require finer 115 // what this function requires to run, but some APIs may require finer
116 // grained control, such as tabs.executeScript being allowed for active tabs. 116 // grained control, such as tabs.executeScript being allowed for active tabs.
117 // 117 //
118 // This will be run after the function has been set up but before Run(). 118 // This will be run after the function has been set up but before Run().
119 virtual bool HasPermission(); 119 virtual bool HasPermission();
120 120
121 // Execute the API. Clients should initialize the ExtensionFunction using 121 // The result of a function call.
122 // SetArgs(), set_request_id(), and the other setters before calling this
123 // method.
124 // 122 //
125 // Note that once Run() returns, dispatcher() can be NULL, so be sure to 123 // Use NoArguments(), SingleArgument(), MultipleArguments(), or Error()
126 // NULL-check. 124 // rather than this class directly.
127 void Run(); 125 class ResponseValueObject {
126 public:
127 virtual ~ResponseValueObject() {}
128
129 // Returns true for success, false for failure.
130 virtual bool Apply() = 0;
131 };
132 typedef scoped_ptr<ResponseValueObject> ResponseValue;
133
134 // The action to use when returning from RunAsync.
135 //
136 // Use RespondNow() or RespondLater() rather than this class directly.
137 class ResponseActionObject {
138 public:
139 virtual ~ResponseActionObject() {}
140
141 virtual void Execute() = 0;
142 };
143 typedef scoped_ptr<ResponseActionObject> ResponseAction;
144
145 // Runs the function and returns the action to take when the caller is ready
146 // to respond.
147 //
148 // Callers must call Execute() on the return ResponseAction at some point,
149 // exactly once.
150 //
151 // SyncExtensionFunction and AsyncExtensionFunction implement this in terms
152 // of SyncExtensionFunction::RunSync and AsyncExtensionFunction::RunAsync,
153 // but this is deprecated. ExtensionFunction implementations are encouraged
154 // to just implement Run.
155 virtual ResponseAction Run() WARN_UNUSED_RESULT = 0;
128 156
129 // Gets whether quota should be applied to this individual function 157 // Gets whether quota should be applied to this individual function
130 // invocation. This is different to GetQuotaLimitHeuristics which is only 158 // invocation. This is different to GetQuotaLimitHeuristics which is only
131 // invoked once and then cached. 159 // invoked once and then cached.
132 // 160 //
133 // Returns false by default. 161 // Returns false by default.
134 virtual bool ShouldSkipQuotaLimiting() const; 162 virtual bool ShouldSkipQuotaLimiting() const;
135 163
136 // Optionally adds one or multiple QuotaLimitHeuristic instances suitable for 164 // Optionally adds one or multiple QuotaLimitHeuristic instances suitable for
137 // this function to |heuristics|. The ownership of the new QuotaLimitHeuristic 165 // this function to |heuristics|. The ownership of the new QuotaLimitHeuristic
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 extensions::functions::HistogramValue histogram_value() const { 229 extensions::functions::HistogramValue histogram_value() const {
202 return histogram_value_; } 230 return histogram_value_; }
203 231
204 void set_response_callback(const ResponseCallback& callback) { 232 void set_response_callback(const ResponseCallback& callback) {
205 response_callback_ = callback; 233 response_callback_ = callback;
206 } 234 }
207 235
208 void set_source_tab_id(int source_tab_id) { source_tab_id_ = source_tab_id; } 236 void set_source_tab_id(int source_tab_id) { source_tab_id_ = source_tab_id; }
209 int source_tab_id() const { return source_tab_id_; } 237 int source_tab_id() const { return source_tab_id_; }
210 238
211 // The result of a function call.
212 //
213 // Use NoArguments(), SingleArgument(), MultipleArguments(), or Error()
214 // rather than this class directly.
215 class ResponseValueObject {
216 public:
217 virtual ~ResponseValueObject() {}
218
219 // Returns true for success, false for failure.
220 virtual bool Apply() = 0;
221 };
222 typedef scoped_ptr<ResponseValueObject> ResponseValue;
223
224 // The action to use when returning from RunImpl.
225 //
226 // Use RespondNow() or RespondLater() rather than this class directly.
227 class ResponseActionObject {
228 public:
229 virtual ~ResponseActionObject() {}
230
231 virtual void Execute() = 0;
232 };
233 typedef scoped_ptr<ResponseActionObject> ResponseAction;
234
235 protected: 239 protected:
236 friend struct ExtensionFunctionDeleteTraits; 240 friend struct ExtensionFunctionDeleteTraits;
237 241
238 // ResponseValues. 242 // ResponseValues.
239 // 243 //
240 // Success, no arguments to pass to caller 244 // Success, no arguments to pass to caller
241 ResponseValue NoArguments(); 245 ResponseValue NoArguments();
242 // Success, a single argument |result| to pass to caller. TAKES OWNERSHIP. 246 // Success, a single argument |result| to pass to caller. TAKES OWNERSHIP.
243 ResponseValue SingleArgument(base::Value* result); 247 ResponseValue SingleArgument(base::Value* result);
244 // Success, a list of arguments |results| to pass to caller. TAKES OWNERSHIP. 248 // Success, a list of arguments |results| to pass to caller. TAKES OWNERSHIP.
245 ResponseValue MultipleArguments(base::ListValue* results); 249 ResponseValue MultipleArguments(base::ListValue* results);
246 // Error. chrome.runtime.lastError.message will be set to |error|. 250 // Error. chrome.runtime.lastError.message will be set to |error|.
247 ResponseValue Error(const std::string& error); 251 ResponseValue Error(const std::string& error);
248 // Bad message. A ResponseValue equivalent to EXTENSION_FUNCTION_VALIDATE(). 252 // Bad message. A ResponseValue equivalent to EXTENSION_FUNCTION_VALIDATE().
249 ResponseValue BadMessage(); 253 ResponseValue BadMessage();
250 254
251 // ResponseActions. 255 // ResponseActions.
252 // 256 //
253 // Respond to the extension immediately with |result|. 257 // Respond to the extension immediately with |result|.
254 ResponseAction RespondNow(ResponseValue result); 258 ResponseAction RespondNow(ResponseValue result);
255 // Don't respond now, but promise to call SendResponse later. 259 // Don't respond now, but promise to call Respond() later.
256 ResponseAction RespondLater(); 260 ResponseAction RespondLater();
257 261
262 // If RespondLater() was used, functions must at some point call Respond()
263 // with |result| as their result.
264 void Respond(ResponseValue result);
265
258 virtual ~ExtensionFunction(); 266 virtual ~ExtensionFunction();
259 267
260 // Helper method for ExtensionFunctionDeleteTraits. Deletes this object. 268 // Helper method for ExtensionFunctionDeleteTraits. Deletes this object.
261 virtual void Destruct() const = 0; 269 virtual void Destruct() const = 0;
262 270
263 // Derived classes should implement one of these methods to do their work. 271 // Do not call this function directly, return the appropriate ResponseAction
272 // from Run() instead. If using RespondLater then call Respond().
264 // 273 //
265 // Returns the action to take. DO NOT USE WITH SyncExtensionFunction. 274 // Call with true to indicate success, false to indicate failure, in which
266 virtual ResponseAction RunImplTypesafe(); 275 // case please set |error_|.
267 // Deprecated. Returns true on success. SendResponse() must be called later.
268 // Return false to indicate an error and respond immediately.
269 virtual bool RunImpl();
270
271 // Sends the result back to the extension.
272 //
273 // Responds with |response|.
274 void SendResponseTypesafe(ResponseValue response);
275 // Deprecated. Call with true to indicate success, false to indicate failure,
276 // in which case please set |error_|.
277 virtual void SendResponse(bool success) = 0; 276 virtual void SendResponse(bool success) = 0;
278 277
279 // Common implementation for SendResponse. 278 // Common implementation for SendResponse.
280 void SendResponseImpl(bool success); 279 void SendResponseImpl(bool success);
281 280
282 // Return true if the argument to this function at |index| was provided and 281 // Return true if the argument to this function at |index| was provided and
283 // is non-null. 282 // is non-null.
284 bool HasOptionalArgument(size_t index); 283 bool HasOptionalArgument(size_t index);
285 284
286 // Id of this request, used to map the response back to the caller. 285 // Id of this request, used to map the response back to the caller.
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 // is invoked. 329 // is invoked.
331 extensions::functions::HistogramValue histogram_value_; 330 extensions::functions::HistogramValue histogram_value_;
332 331
333 // The callback to run once the function has done execution. 332 // The callback to run once the function has done execution.
334 ResponseCallback response_callback_; 333 ResponseCallback response_callback_;
335 334
336 // The ID of the tab triggered this function call, or -1 if there is no tab. 335 // The ID of the tab triggered this function call, or -1 if there is no tab.
337 int source_tab_id_; 336 int source_tab_id_;
338 337
339 private: 338 private:
339 void OnRespondingLater(ResponseValue response);
340
340 DISALLOW_COPY_AND_ASSIGN(ExtensionFunction); 341 DISALLOW_COPY_AND_ASSIGN(ExtensionFunction);
341 }; 342 };
342 343
343 // Extension functions that run on the UI thread. Most functions fall into 344 // Extension functions that run on the UI thread. Most functions fall into
344 // this category. 345 // this category.
345 class UIThreadExtensionFunction : public ExtensionFunction { 346 class UIThreadExtensionFunction : public ExtensionFunction {
346 public: 347 public:
347 // TODO(yzshen): We should be able to remove this interface now that we 348 // TODO(yzshen): We should be able to remove this interface now that we
348 // support overriding the response callback. 349 // support overriding the response callback.
349 // A delegate for use in testing, to intercept the call to SendResponse. 350 // A delegate for use in testing, to intercept the call to SendResponse.
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
482 }; 483 };
483 484
484 // Base class for an extension function that runs asynchronously *relative to 485 // Base class for an extension function that runs asynchronously *relative to
485 // the browser's UI thread*. 486 // the browser's UI thread*.
486 class AsyncExtensionFunction : public UIThreadExtensionFunction { 487 class AsyncExtensionFunction : public UIThreadExtensionFunction {
487 public: 488 public:
488 AsyncExtensionFunction(); 489 AsyncExtensionFunction();
489 490
490 protected: 491 protected:
491 virtual ~AsyncExtensionFunction(); 492 virtual ~AsyncExtensionFunction();
493
494 // Deprecated: Override UIThreadExtensionFunction and implement Run() instead.
495 //
496 // AsyncExtensionFunctions implement this method. Return true to indicate that
497 // nothing has gone wrong yet; SendResponse must be called later. Return true
498 // to respond immediately with an error.
499 virtual bool RunAsync() = 0;
500
501 private:
502 virtual ResponseAction Run() OVERRIDE;
492 }; 503 };
493 504
494 // A SyncExtensionFunction is an ExtensionFunction that runs synchronously 505 // A SyncExtensionFunction is an ExtensionFunction that runs synchronously
495 // *relative to the browser's UI thread*. Note that this has nothing to do with 506 // *relative to the browser's UI thread*. Note that this has nothing to do with
496 // running synchronously relative to the extension process. From the extension 507 // running synchronously relative to the extension process. From the extension
497 // process's point of view, the function is still asynchronous. 508 // process's point of view, the function is still asynchronous.
498 // 509 //
499 // This kind of function is convenient for implementing simple APIs that just 510 // This kind of function is convenient for implementing simple APIs that just
500 // need to interact with things on the browser UI thread. 511 // need to interact with things on the browser UI thread.
501 class SyncExtensionFunction : public UIThreadExtensionFunction { 512 class SyncExtensionFunction : public UIThreadExtensionFunction {
502 public: 513 public:
503 SyncExtensionFunction(); 514 SyncExtensionFunction();
504 515
505 virtual bool RunImpl() OVERRIDE; 516 protected:
517 virtual ~SyncExtensionFunction();
506 518
507 protected: 519 // Deprecated: Override UIThreadExtensionFunction and implement Run() instead.
520 //
521 // SyncExtensionFunctions implement this method. Return true to respond
522 // immediately with success, false to respond immediately with an error.
508 virtual bool RunSync() = 0; 523 virtual bool RunSync() = 0;
509 524
510 virtual ~SyncExtensionFunction(); 525 private:
526 virtual ResponseAction Run() OVERRIDE;
511 }; 527 };
512 528
513 class SyncIOThreadExtensionFunction : public IOThreadExtensionFunction { 529 class SyncIOThreadExtensionFunction : public IOThreadExtensionFunction {
514 public: 530 public:
515 SyncIOThreadExtensionFunction(); 531 SyncIOThreadExtensionFunction();
516 532
517 virtual bool RunImpl() OVERRIDE; 533 protected:
534 virtual ~SyncIOThreadExtensionFunction();
518 535
519 protected: 536 // Deprecated: Override IOThreadExtensionFunction and implement Run() instead.
537 //
538 // SyncIOThreadExtensionFunctions implement this method. Return true to
539 // respond immediately with success, false to respond immediately with an
540 // error.
520 virtual bool RunSync() = 0; 541 virtual bool RunSync() = 0;
521 542
522 virtual ~SyncIOThreadExtensionFunction(); 543 private:
544 virtual ResponseAction Run() OVERRIDE;
523 }; 545 };
524 546
525 #endif // EXTENSIONS_BROWSER_EXTENSION_FUNCTION_H_ 547 #endif // EXTENSIONS_BROWSER_EXTENSION_FUNCTION_H_
OLDNEW
« no previous file with comments | « extensions/browser/api/test/test_api.cc ('k') | extensions/browser/extension_function.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698