| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2009 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 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_H_ |
| 7 |
| 8 #include <string> |
| 9 |
| 10 #include "base/values.h" |
| 11 #include "base/scoped_ptr.h" |
| 12 |
| 13 class ExtensionFunctionDispatcher; |
| 14 |
| 15 // Base class for an extension function. |
| 16 // TODO(aa): This will have to become reference counted when we introduce APIs |
| 17 // that live beyond a single stack frame. |
| 18 class ExtensionFunction { |
| 19 public: |
| 20 virtual ~ExtensionFunction() {} |
| 21 |
| 22 void set_dispatcher(ExtensionFunctionDispatcher* dispatcher) { |
| 23 dispatcher_ = dispatcher; |
| 24 } |
| 25 void set_args(Value* args) { args_ = args; } |
| 26 |
| 27 void set_callback_id(int callback_id) { callback_id_ = callback_id; } |
| 28 int callback_id() { return callback_id_; } |
| 29 |
| 30 Value* result() { return result_.get(); } |
| 31 const std::string& error() { return error_; } |
| 32 |
| 33 // Whether the extension has registered a callback and is waiting for a |
| 34 // response. APIs can use this to avoid doing unnecessary work in the case |
| 35 // that the extension is not expecting a response. |
| 36 bool has_callback() { return callback_id_ != -1; } |
| 37 |
| 38 // Execute the API. Clients should call set_args() and set_callback_id() |
| 39 // before calling this method. Derived classes should populate result_ and |
| 40 // error_ before returning. |
| 41 virtual void Run() = 0; |
| 42 |
| 43 protected: |
| 44 void SendResponse(bool success); |
| 45 |
| 46 // The arguments to the API. Only non-null if argument were specfied. |
| 47 Value* args_; |
| 48 |
| 49 // The result of the API. This should be populated by the derived class before |
| 50 // Run() returns. |
| 51 scoped_ptr<Value> result_; |
| 52 |
| 53 // Any detailed error from the API. This should be populated by the derived |
| 54 // class before Run() returns. |
| 55 std::string error_; |
| 56 |
| 57 private: |
| 58 ExtensionFunctionDispatcher* dispatcher_; |
| 59 int callback_id_; |
| 60 }; |
| 61 |
| 62 |
| 63 // A SyncExtensionFunction is an ExtensionFunction that runs synchronously |
| 64 // *relative to the browser's UI thread*. Note that this has nothing to do with |
| 65 // running synchronously relative to the extension process. From the extension |
| 66 // process's point of view, the function is still asynchronous. |
| 67 // |
| 68 // This kind of function is convenient for implementing simple APIs that just |
| 69 // need to interact with things on the browser UI thread. |
| 70 class SyncExtensionFunction : public ExtensionFunction { |
| 71 public: |
| 72 // Derived classes should implement this method to do their work and return |
| 73 // success/failure. |
| 74 virtual bool RunImpl() = 0; |
| 75 |
| 76 virtual void Run() { |
| 77 SendResponse(RunImpl()); |
| 78 } |
| 79 }; |
| 80 |
| 81 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_H_ |
| OLD | NEW |