OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 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 CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/values.h" | 10 #include "base/values.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 } while (0) | 21 } while (0) |
22 | 22 |
23 // Abstract base class for extension functions the ExtensionFunctionDispatcher | 23 // Abstract base class for extension functions the ExtensionFunctionDispatcher |
24 // knows how to dispatch to. | 24 // knows how to dispatch to. |
25 // | 25 // |
26 // TODO(aa): This will have to become reference counted when we introduce | 26 // TODO(aa): This will have to become reference counted when we introduce |
27 // APIs that live beyond a single stack frame. | 27 // APIs that live beyond a single stack frame. |
28 class ExtensionFunction { | 28 class ExtensionFunction { |
29 public: | 29 public: |
30 ExtensionFunction() : request_id_(-1), has_callback_(false) {} | 30 ExtensionFunction() : request_id_(-1), has_callback_(false) {} |
| 31 virtual ~ExtensionFunction() {} |
31 | 32 |
32 // Specifies the name of the function. | 33 // Specifies the name of the function. |
33 virtual void SetName(const std::string& name) { } | 34 virtual void SetName(const std::string& name) { } |
34 | 35 |
35 // Specifies the raw arguments to the function, as a JSON-encoded string. | 36 // Specifies the raw arguments to the function, as a JSON-encoded string. |
36 virtual void SetArgs(const std::string& args) = 0; | 37 virtual void SetArgs(const std::string& args) = 0; |
37 | 38 |
38 // Retrieves the results of the function as a JSON-encoded string (may | 39 // Retrieves the results of the function as a JSON-encoded string (may |
39 // be empty). | 40 // be empty). |
40 virtual const std::string GetResult() = 0; | 41 virtual const std::string GetResult() = 0; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 private: | 73 private: |
73 DISALLOW_COPY_AND_ASSIGN(ExtensionFunction); | 74 DISALLOW_COPY_AND_ASSIGN(ExtensionFunction); |
74 }; | 75 }; |
75 | 76 |
76 // Base class for an extension function that runs asynchronously *relative to | 77 // Base class for an extension function that runs asynchronously *relative to |
77 // the browser's UI thread*. | 78 // the browser's UI thread*. |
78 // TODO(aa) Remove this extra level of inheritance once the browser stops | 79 // TODO(aa) Remove this extra level of inheritance once the browser stops |
79 // parsing JSON (and instead uses custom serialization of Value objects). | 80 // parsing JSON (and instead uses custom serialization of Value objects). |
80 class AsyncExtensionFunction : public ExtensionFunction { | 81 class AsyncExtensionFunction : public ExtensionFunction { |
81 public: | 82 public: |
82 AsyncExtensionFunction() : bad_message_(false) {} | 83 AsyncExtensionFunction() : args_(NULL), bad_message_(false) {} |
83 virtual ~AsyncExtensionFunction() {} | 84 virtual ~AsyncExtensionFunction() {} |
84 | 85 |
85 virtual void SetArgs(const std::string& args); | 86 virtual void SetArgs(const std::string& args); |
86 virtual const std::string GetResult(); | 87 virtual const std::string GetResult(); |
87 virtual const std::string GetError() { return error_; } | 88 virtual const std::string GetError() { return error_; } |
88 virtual void Run() = 0; | 89 virtual void Run() = 0; |
89 | 90 |
90 protected: | 91 protected: |
91 void SendResponse(bool success); | 92 void SendResponse(bool success); |
92 | 93 |
93 std::string extension_id(); | 94 std::string extension_id(); |
94 | 95 |
95 Profile* profile(); | 96 Profile* profile(); |
96 | 97 |
97 // The arguments to the API. Only non-null if argument were specfied. | 98 // The arguments to the API. Only non-null if argument were specified. |
98 Value* args_; | 99 Value* args_; |
99 | 100 |
100 // The result of the API. This should be populated by the derived class before | 101 // The result of the API. This should be populated by the derived class before |
101 // Run() returns. | 102 // Run() returns. |
102 scoped_ptr<Value> result_; | 103 scoped_ptr<Value> result_; |
103 | 104 |
104 // Any detailed error from the API. This should be populated by the derived | 105 // Any detailed error from the API. This should be populated by the derived |
105 // class before Run() returns. | 106 // class before Run() returns. |
106 std::string error_; | 107 std::string error_; |
107 | 108 |
(...skipping 22 matching lines...) Expand all Loading... |
130 | 131 |
131 virtual void Run() { | 132 virtual void Run() { |
132 SendResponse(RunImpl()); | 133 SendResponse(RunImpl()); |
133 } | 134 } |
134 | 135 |
135 private: | 136 private: |
136 DISALLOW_COPY_AND_ASSIGN(SyncExtensionFunction); | 137 DISALLOW_COPY_AND_ASSIGN(SyncExtensionFunction); |
137 }; | 138 }; |
138 | 139 |
139 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_H_ | 140 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_H_ |
OLD | NEW |