| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <list> | 9 #include <list> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 // returning. The calling renderer process will be killed. | 201 // returning. The calling renderer process will be killed. |
| 202 bool bad_message_; | 202 bool bad_message_; |
| 203 | 203 |
| 204 DISALLOW_COPY_AND_ASSIGN(ExtensionFunction); | 204 DISALLOW_COPY_AND_ASSIGN(ExtensionFunction); |
| 205 }; | 205 }; |
| 206 | 206 |
| 207 // Extension functions that run on the UI thread. Most functions fall into | 207 // Extension functions that run on the UI thread. Most functions fall into |
| 208 // this category. | 208 // this category. |
| 209 class UIThreadExtensionFunction : public ExtensionFunction { | 209 class UIThreadExtensionFunction : public ExtensionFunction { |
| 210 public: | 210 public: |
| 211 // A delegate for use in testing, to intercept the call to SendResponse. |
| 212 class DelegateForTests { |
| 213 public: |
| 214 virtual void OnSendResponse(UIThreadExtensionFunction* function, |
| 215 bool success) = 0; |
| 216 }; |
| 217 |
| 211 UIThreadExtensionFunction(); | 218 UIThreadExtensionFunction(); |
| 212 | 219 |
| 213 virtual UIThreadExtensionFunction* AsUIThreadExtensionFunction() OVERRIDE; | 220 virtual UIThreadExtensionFunction* AsUIThreadExtensionFunction() OVERRIDE; |
| 214 | 221 |
| 222 void set_test_delegate(DelegateForTests* delegate) { |
| 223 delegate_ = delegate; |
| 224 } |
| 215 // Set the profile which contains the extension that has originated this | 225 // Set the profile which contains the extension that has originated this |
| 216 // function call. | 226 // function call. |
| 217 void set_profile(Profile* profile) { profile_ = profile; } | 227 void set_profile(Profile* profile) { profile_ = profile; } |
| 218 Profile* profile() const { return profile_; } | 228 Profile* profile() const { return profile_; } |
| 219 | 229 |
| 220 void SetRenderViewHost(RenderViewHost* render_view_host); | 230 void SetRenderViewHost(RenderViewHost* render_view_host); |
| 221 RenderViewHost* render_view_host() const { return render_view_host_; } | 231 RenderViewHost* render_view_host() const { return render_view_host_; } |
| 222 | 232 |
| 223 void set_dispatcher( | 233 void set_dispatcher( |
| 224 const base::WeakPtr<ExtensionFunctionDispatcher>& dispatcher) { | 234 const base::WeakPtr<ExtensionFunctionDispatcher>& dispatcher) { |
| 225 dispatcher_ = dispatcher; | 235 dispatcher_ = dispatcher; |
| 226 } | 236 } |
| 227 ExtensionFunctionDispatcher* dispatcher() const { | 237 ExtensionFunctionDispatcher* dispatcher() const { |
| 228 return dispatcher_.get(); | 238 return dispatcher_.get(); |
| 229 } | 239 } |
| 230 | 240 |
| 231 protected: | 241 protected: |
| 232 friend struct content::BrowserThread::DeleteOnThread< | 242 friend struct content::BrowserThread::DeleteOnThread< |
| 233 content::BrowserThread::UI>; | 243 content::BrowserThread::UI>; |
| 234 friend class DeleteTask<UIThreadExtensionFunction>; | 244 friend class DeleteTask<UIThreadExtensionFunction>; |
| 235 | 245 |
| 236 virtual ~UIThreadExtensionFunction(); | 246 virtual ~UIThreadExtensionFunction(); |
| 237 | 247 |
| 238 virtual void SendResponse(bool success); | 248 virtual void SendResponse(bool success) OVERRIDE; |
| 239 | 249 |
| 240 // Gets the "current" browser, if any. | 250 // Gets the "current" browser, if any. |
| 241 // | 251 // |
| 242 // Many extension APIs operate relative to the current browser, which is the | 252 // Many extension APIs operate relative to the current browser, which is the |
| 243 // browser the calling code is running inside of. For example, popups, tabs, | 253 // browser the calling code is running inside of. For example, popups, tabs, |
| 244 // and infobars all have a containing browser, but background pages and | 254 // and infobars all have a containing browser, but background pages and |
| 245 // notification bubbles do not. | 255 // notification bubbles do not. |
| 246 // | 256 // |
| 247 // If there is no containing window, the current browser defaults to the | 257 // If there is no containing window, the current browser defaults to the |
| 248 // foremost one. | 258 // foremost one. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 278 virtual void Observe(int type, | 288 virtual void Observe(int type, |
| 279 const content::NotificationSource& source, | 289 const content::NotificationSource& source, |
| 280 const content::NotificationDetails& details) OVERRIDE; | 290 const content::NotificationDetails& details) OVERRIDE; |
| 281 UIThreadExtensionFunction* function_; | 291 UIThreadExtensionFunction* function_; |
| 282 content::NotificationRegistrar registrar_; | 292 content::NotificationRegistrar registrar_; |
| 283 }; | 293 }; |
| 284 | 294 |
| 285 virtual void Destruct() const; | 295 virtual void Destruct() const; |
| 286 | 296 |
| 287 scoped_ptr<RenderViewHostTracker> tracker_; | 297 scoped_ptr<RenderViewHostTracker> tracker_; |
| 298 |
| 299 DelegateForTests* delegate_; |
| 288 }; | 300 }; |
| 289 | 301 |
| 290 // Extension functions that run on the IO thread. This type of function avoids | 302 // Extension functions that run on the IO thread. This type of function avoids |
| 291 // a roundtrip to and from the UI thread (because communication with the | 303 // a roundtrip to and from the UI thread (because communication with the |
| 292 // extension process happens on the IO thread). It's intended to be used when | 304 // extension process happens on the IO thread). It's intended to be used when |
| 293 // performance is critical (e.g. the webRequest API which can block network | 305 // performance is critical (e.g. the webRequest API which can block network |
| 294 // requests). Generally, UIThreadExtensionFunction is more appropriate and will | 306 // requests). Generally, UIThreadExtensionFunction is more appropriate and will |
| 295 // be easier to use and interface with the rest of the browser. | 307 // be easier to use and interface with the rest of the browser. |
| 296 class IOThreadExtensionFunction : public ExtensionFunction { | 308 class IOThreadExtensionFunction : public ExtensionFunction { |
| 297 public: | 309 public: |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 333 base::WeakPtr<ChromeRenderMessageFilter> ipc_sender_; | 345 base::WeakPtr<ChromeRenderMessageFilter> ipc_sender_; |
| 334 int routing_id_; | 346 int routing_id_; |
| 335 | 347 |
| 336 scoped_refptr<const ExtensionInfoMap> extension_info_map_; | 348 scoped_refptr<const ExtensionInfoMap> extension_info_map_; |
| 337 }; | 349 }; |
| 338 | 350 |
| 339 // Base class for an extension function that runs asynchronously *relative to | 351 // Base class for an extension function that runs asynchronously *relative to |
| 340 // the browser's UI thread*. | 352 // the browser's UI thread*. |
| 341 class AsyncExtensionFunction : public UIThreadExtensionFunction { | 353 class AsyncExtensionFunction : public UIThreadExtensionFunction { |
| 342 public: | 354 public: |
| 343 // A delegate for use in testing, to intercept the call to SendResponse. | |
| 344 class DelegateForTests { | |
| 345 public: | |
| 346 virtual void OnSendResponse(AsyncExtensionFunction* function, | |
| 347 bool success) = 0; | |
| 348 }; | |
| 349 | |
| 350 AsyncExtensionFunction(); | 355 AsyncExtensionFunction(); |
| 351 virtual void SendResponse(bool success) OVERRIDE; | |
| 352 | |
| 353 void set_test_delegate(DelegateForTests* delegate) { | |
| 354 delegate_ = delegate; | |
| 355 } | |
| 356 | 356 |
| 357 protected: | 357 protected: |
| 358 virtual ~AsyncExtensionFunction(); | 358 virtual ~AsyncExtensionFunction(); |
| 359 | |
| 360 DelegateForTests* delegate_; | |
| 361 }; | 359 }; |
| 362 | 360 |
| 363 // A SyncExtensionFunction is an ExtensionFunction that runs synchronously | 361 // A SyncExtensionFunction is an ExtensionFunction that runs synchronously |
| 364 // *relative to the browser's UI thread*. Note that this has nothing to do with | 362 // *relative to the browser's UI thread*. Note that this has nothing to do with |
| 365 // running synchronously relative to the extension process. From the extension | 363 // running synchronously relative to the extension process. From the extension |
| 366 // process's point of view, the function is still asynchronous. | 364 // process's point of view, the function is still asynchronous. |
| 367 // | 365 // |
| 368 // This kind of function is convenient for implementing simple APIs that just | 366 // This kind of function is convenient for implementing simple APIs that just |
| 369 // need to interact with things on the browser UI thread. | 367 // need to interact with things on the browser UI thread. |
| 370 class SyncExtensionFunction : public UIThreadExtensionFunction { | 368 class SyncExtensionFunction : public UIThreadExtensionFunction { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 381 public: | 379 public: |
| 382 SyncIOThreadExtensionFunction(); | 380 SyncIOThreadExtensionFunction(); |
| 383 | 381 |
| 384 virtual void Run() OVERRIDE; | 382 virtual void Run() OVERRIDE; |
| 385 | 383 |
| 386 protected: | 384 protected: |
| 387 virtual ~SyncIOThreadExtensionFunction(); | 385 virtual ~SyncIOThreadExtensionFunction(); |
| 388 }; | 386 }; |
| 389 | 387 |
| 390 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_H_ | 388 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_H_ |
| OLD | NEW |