| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #include <list> | 9 #include <list> |
| 10 | 10 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 return false; \ | 30 return false; \ |
| 31 } while (0) | 31 } while (0) |
| 32 | 32 |
| 33 #define DECLARE_EXTENSION_FUNCTION_NAME(name) \ | 33 #define DECLARE_EXTENSION_FUNCTION_NAME(name) \ |
| 34 public: static const char* function_name() { return name; } | 34 public: static const char* function_name() { return name; } |
| 35 | 35 |
| 36 // Abstract base class for extension functions the ExtensionFunctionDispatcher | 36 // Abstract base class for extension functions the ExtensionFunctionDispatcher |
| 37 // knows how to dispatch to. | 37 // knows how to dispatch to. |
| 38 class ExtensionFunction : public base::RefCountedThreadSafe<ExtensionFunction> { | 38 class ExtensionFunction : public base::RefCountedThreadSafe<ExtensionFunction> { |
| 39 public: | 39 public: |
| 40 ExtensionFunction() : request_id_(-1), name_(""), has_callback_(false) {} | 40 ExtensionFunction(); |
| 41 | 41 |
| 42 // Specifies the name of the function. | 42 // Specifies the name of the function. |
| 43 void set_name(const std::string& name) { name_ = name; } | 43 void set_name(const std::string& name) { name_ = name; } |
| 44 const std::string name() const { return name_; } | 44 const std::string name() const { return name_; } |
| 45 | 45 |
| 46 // Set the profile which contains the extension that has originated this | 46 // Set the profile which contains the extension that has originated this |
| 47 // function call. | 47 // function call. |
| 48 void set_profile(Profile* profile) { profile_ = profile; } | 48 void set_profile(Profile* profile) { profile_ = profile; } |
| 49 Profile* profile() const { return profile_; } | 49 Profile* profile() const { return profile_; } |
| 50 | 50 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 | 90 |
| 91 // Execute the API. Clients should call set_raw_args() and | 91 // Execute the API. Clients should call set_raw_args() and |
| 92 // set_request_id() before calling this method. Derived classes should be | 92 // set_request_id() before calling this method. Derived classes should be |
| 93 // ready to return raw_result() and error() before returning from this | 93 // ready to return raw_result() and error() before returning from this |
| 94 // function. | 94 // function. |
| 95 virtual void Run() = 0; | 95 virtual void Run() = 0; |
| 96 | 96 |
| 97 protected: | 97 protected: |
| 98 friend class base::RefCountedThreadSafe<ExtensionFunction>; | 98 friend class base::RefCountedThreadSafe<ExtensionFunction>; |
| 99 | 99 |
| 100 virtual ~ExtensionFunction() {} | 100 virtual ~ExtensionFunction(); |
| 101 | 101 |
| 102 // Gets the extension that called this function. This can return NULL for | 102 // Gets the extension that called this function. This can return NULL for |
| 103 // async functions, for example if the extension is unloaded while the | 103 // async functions, for example if the extension is unloaded while the |
| 104 // function is running. | 104 // function is running. |
| 105 Extension* GetExtension(); | 105 Extension* GetExtension(); |
| 106 | 106 |
| 107 // Gets the "current" browser, if any. | 107 // Gets the "current" browser, if any. |
| 108 // | 108 // |
| 109 // Many extension APIs operate relative to the current browser, which is the | 109 // Many extension APIs operate relative to the current browser, which is the |
| 110 // browser the calling code is running inside of. For example, popups, tabs, | 110 // browser the calling code is running inside of. For example, popups, tabs, |
| 111 // and infobars all have a containing browser, but background pages and | 111 // and infobars all have a containing browser, but background pages and |
| 112 // notification bubbles do not. | 112 // notification bubbles do not. |
| 113 // | 113 // |
| 114 // If there is no containing window, the current browser defaults to the | 114 // If there is no containing window, the current browser defaults to the |
| 115 // foremost one. | 115 // foremost one. |
| 116 // | 116 // |
| 117 // Incognito browsers are not considered unless the calling extension has | 117 // Incognito browsers are not considered unless the calling extension has |
| 118 // incognito access enabled. | 118 // incognito access enabled. |
| 119 // | 119 // |
| 120 // This method can return NULL if there is no matching browser, which can | 120 // This method can return NULL if there is no matching browser, which can |
| 121 // happen if only incognito windows are open, or early in startup or shutdown | 121 // happen if only incognito windows are open, or early in startup or shutdown |
| 122 // shutdown when there are no active windows. | 122 // shutdown when there are no active windows. |
| 123 Browser* GetCurrentBrowser() { | 123 Browser* GetCurrentBrowser(); |
| 124 return dispatcher()->GetCurrentBrowser(include_incognito_); | |
| 125 } | |
| 126 | 124 |
| 127 // The peer to the dispatcher that will service this extension function call. | 125 // The peer to the dispatcher that will service this extension function call. |
| 128 scoped_refptr<ExtensionFunctionDispatcher::Peer> peer_; | 126 scoped_refptr<ExtensionFunctionDispatcher::Peer> peer_; |
| 129 | 127 |
| 130 // Id of this request, used to map the response back to the caller. | 128 // Id of this request, used to map the response back to the caller. |
| 131 int request_id_; | 129 int request_id_; |
| 132 | 130 |
| 133 // The Profile of this function's extension. | 131 // The Profile of this function's extension. |
| 134 Profile* profile_; | 132 Profile* profile_; |
| 135 | 133 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 153 }; | 151 }; |
| 154 | 152 |
| 155 // Base class for an extension function that runs asynchronously *relative to | 153 // Base class for an extension function that runs asynchronously *relative to |
| 156 // the browser's UI thread*. | 154 // the browser's UI thread*. |
| 157 // Note that once Run() returns, dispatcher() can be NULL, so be sure to | 155 // Note that once Run() returns, dispatcher() can be NULL, so be sure to |
| 158 // NULL-check. | 156 // NULL-check. |
| 159 // TODO(aa) Remove this extra level of inheritance once the browser stops | 157 // TODO(aa) Remove this extra level of inheritance once the browser stops |
| 160 // parsing JSON (and instead uses custom serialization of Value objects). | 158 // parsing JSON (and instead uses custom serialization of Value objects). |
| 161 class AsyncExtensionFunction : public ExtensionFunction { | 159 class AsyncExtensionFunction : public ExtensionFunction { |
| 162 public: | 160 public: |
| 163 AsyncExtensionFunction() : args_(NULL), bad_message_(false) {} | 161 AsyncExtensionFunction(); |
| 164 | 162 |
| 165 virtual void SetArgs(const ListValue* args); | 163 virtual void SetArgs(const ListValue* args); |
| 166 virtual const std::string GetResult(); | 164 virtual const std::string GetResult(); |
| 167 virtual const std::string GetError() { return error_; } | 165 virtual const std::string GetError(); |
| 168 virtual void Run() { | 166 virtual void Run(); |
| 169 if (!RunImpl()) | |
| 170 SendResponse(false); | |
| 171 } | |
| 172 | 167 |
| 173 // Derived classes should implement this method to do their work and return | 168 // Derived classes should implement this method to do their work and return |
| 174 // success/failure. | 169 // success/failure. |
| 175 virtual bool RunImpl() = 0; | 170 virtual bool RunImpl() = 0; |
| 176 | 171 |
| 177 protected: | 172 protected: |
| 178 virtual ~AsyncExtensionFunction() {} | 173 virtual ~AsyncExtensionFunction(); |
| 179 | 174 |
| 180 void SendResponse(bool success); | 175 void SendResponse(bool success); |
| 181 | 176 |
| 182 // Return true if the argument to this function at |index| was provided and | 177 // Return true if the argument to this function at |index| was provided and |
| 183 // is non-null. | 178 // is non-null. |
| 184 bool HasOptionalArgument(size_t index); | 179 bool HasOptionalArgument(size_t index); |
| 185 | 180 |
| 186 // The arguments to the API. Only non-null if argument were specified. | 181 // The arguments to the API. Only non-null if argument were specified. |
| 187 scoped_ptr<ListValue> args_; | 182 scoped_ptr<ListValue> args_; |
| 188 | 183 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 203 | 198 |
| 204 // A SyncExtensionFunction is an ExtensionFunction that runs synchronously | 199 // A SyncExtensionFunction is an ExtensionFunction that runs synchronously |
| 205 // *relative to the browser's UI thread*. Note that this has nothing to do with | 200 // *relative to the browser's UI thread*. Note that this has nothing to do with |
| 206 // running synchronously relative to the extension process. From the extension | 201 // running synchronously relative to the extension process. From the extension |
| 207 // process's point of view, the function is still asynchronous. | 202 // process's point of view, the function is still asynchronous. |
| 208 // | 203 // |
| 209 // This kind of function is convenient for implementing simple APIs that just | 204 // This kind of function is convenient for implementing simple APIs that just |
| 210 // need to interact with things on the browser UI thread. | 205 // need to interact with things on the browser UI thread. |
| 211 class SyncExtensionFunction : public AsyncExtensionFunction { | 206 class SyncExtensionFunction : public AsyncExtensionFunction { |
| 212 public: | 207 public: |
| 213 SyncExtensionFunction() {} | 208 SyncExtensionFunction(); |
| 214 | 209 |
| 215 // Derived classes should implement this method to do their work and return | 210 // Derived classes should implement this method to do their work and return |
| 216 // success/failure. | 211 // success/failure. |
| 217 virtual bool RunImpl() = 0; | 212 virtual bool RunImpl() = 0; |
| 218 | 213 |
| 219 virtual void Run() { | 214 virtual void Run(); |
| 220 SendResponse(RunImpl()); | |
| 221 } | |
| 222 | 215 |
| 223 protected: | 216 protected: |
| 224 virtual ~SyncExtensionFunction() {} | 217 virtual ~SyncExtensionFunction(); |
| 225 | 218 |
| 226 private: | 219 private: |
| 227 DISALLOW_COPY_AND_ASSIGN(SyncExtensionFunction); | 220 DISALLOW_COPY_AND_ASSIGN(SyncExtensionFunction); |
| 228 }; | 221 }; |
| 229 | 222 |
| 230 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_H_ | 223 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_H_ |
| OLD | NEW |