| 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 <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <list> | 10 #include <list> |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 protected: | 146 protected: |
| 147 void SetFunctionResults(ExtensionFunction* function, | 147 void SetFunctionResults(ExtensionFunction* function, |
| 148 std::unique_ptr<base::ListValue> results); | 148 std::unique_ptr<base::ListValue> results); |
| 149 void SetFunctionError(ExtensionFunction* function, | 149 void SetFunctionError(ExtensionFunction* function, |
| 150 const std::string& error); | 150 const std::string& error); |
| 151 }; | 151 }; |
| 152 typedef std::unique_ptr<ResponseValueObject> ResponseValue; | 152 typedef std::unique_ptr<ResponseValueObject> ResponseValue; |
| 153 | 153 |
| 154 // The action to use when returning from RunAsync. | 154 // The action to use when returning from RunAsync. |
| 155 // | 155 // |
| 156 // Use RespondNow() or RespondLater() rather than this class directly. | 156 // Use RespondNow() or RespondLater() or AlreadyResponded() rather than this |
| 157 // class directly. |
| 157 class ResponseActionObject { | 158 class ResponseActionObject { |
| 158 public: | 159 public: |
| 159 virtual ~ResponseActionObject() {} | 160 virtual ~ResponseActionObject() {} |
| 160 | 161 |
| 161 virtual void Execute() = 0; | 162 virtual void Execute() = 0; |
| 162 }; | 163 }; |
| 163 typedef std::unique_ptr<ResponseActionObject> ResponseAction; | 164 typedef std::unique_ptr<ResponseActionObject> ResponseAction; |
| 164 | 165 |
| 165 // Helper class for tests to force all ExtensionFunction::user_gesture() | 166 // Helper class for tests to force all ExtensionFunction::user_gesture() |
| 166 // calls to return true as long as at least one instance of this class | 167 // calls to return true as long as at least one instance of this class |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 364 // ResponseActions. | 365 // ResponseActions. |
| 365 // | 366 // |
| 366 // These are exclusively used as return values from Run(). Call Respond(...) | 367 // These are exclusively used as return values from Run(). Call Respond(...) |
| 367 // to respond at any other time - but as described below, only after Run() | 368 // to respond at any other time - but as described below, only after Run() |
| 368 // has already executed, and only if it returned RespondLater(). | 369 // has already executed, and only if it returned RespondLater(). |
| 369 // | 370 // |
| 370 // Respond to the extension immediately with |result|. | 371 // Respond to the extension immediately with |result|. |
| 371 ResponseAction RespondNow(ResponseValue result) WARN_UNUSED_RESULT; | 372 ResponseAction RespondNow(ResponseValue result) WARN_UNUSED_RESULT; |
| 372 // Don't respond now, but promise to call Respond(...) later. | 373 // Don't respond now, but promise to call Respond(...) later. |
| 373 ResponseAction RespondLater() WARN_UNUSED_RESULT; | 374 ResponseAction RespondLater() WARN_UNUSED_RESULT; |
| 375 // Respond() was already called before Run() finished executing. |
| 376 // |
| 377 // Assume Run() uses some helper system that accepts callback that Respond()s. |
| 378 // If that helper system calls the synchronously in some cases, then use |
| 379 // this return value in those cases. |
| 380 // |
| 381 // FooExtensionFunction::Run() { |
| 382 // Helper::FetchResults(..., base::Bind(&Success)); |
| 383 // if (did_respond()) return AlreadyResponded(); |
| 384 // return RespondLater(); |
| 385 // } |
| 386 // FooExtensionFunction::Success() { |
| 387 // Respond(...); |
| 388 // } |
| 389 // |
| 390 // Helper::FetchResults(..., callback) { |
| 391 // if (...) |
| 392 // callback.Run(..); // Synchronously call |callback|. |
| 393 // else |
| 394 // // Asynchronously call |callback|. |
| 395 // } |
| 396 ResponseAction AlreadyResponded() WARN_UNUSED_RESULT; |
| 374 | 397 |
| 375 // This is the return value of the EXTENSION_FUNCTION_VALIDATE macro, which | 398 // This is the return value of the EXTENSION_FUNCTION_VALIDATE macro, which |
| 376 // needs to work from Run(), RunAsync(), and RunSync(). The former of those | 399 // needs to work from Run(), RunAsync(), and RunSync(). The former of those |
| 377 // has a different return type (ResponseAction) than the latter two (bool). | 400 // has a different return type (ResponseAction) than the latter two (bool). |
| 378 static ResponseAction ValidationFailure(ExtensionFunction* function) | 401 static ResponseAction ValidationFailure(ExtensionFunction* function) |
| 379 WARN_UNUSED_RESULT; | 402 WARN_UNUSED_RESULT; |
| 380 | 403 |
| 381 // If RespondLater() was returned from Run(), functions must at some point | 404 // If RespondLater() was returned from Run(), functions must at some point |
| 382 // call Respond() with |result| as their result. | 405 // call Respond() with |result| as their result. |
| 383 // | 406 // |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 667 private: | 690 private: |
| 668 // If you're hitting a compile error here due to "final" - great! You're | 691 // If you're hitting a compile error here due to "final" - great! You're |
| 669 // doing the right thing, you just need to extend UIThreadExtensionFunction | 692 // doing the right thing, you just need to extend UIThreadExtensionFunction |
| 670 // instead of AsyncExtensionFunction. | 693 // instead of AsyncExtensionFunction. |
| 671 ResponseAction Run() final; | 694 ResponseAction Run() final; |
| 672 | 695 |
| 673 DISALLOW_COPY_AND_ASSIGN(AsyncExtensionFunction); | 696 DISALLOW_COPY_AND_ASSIGN(AsyncExtensionFunction); |
| 674 }; | 697 }; |
| 675 | 698 |
| 676 #endif // EXTENSIONS_BROWSER_EXTENSION_FUNCTION_H_ | 699 #endif // EXTENSIONS_BROWSER_EXTENSION_FUNCTION_H_ |
| OLD | NEW |