| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_API_ASYNC_API_FUCTION_H_ | 5 #ifndef EXTENSIONS_BROWSER_API_ASYNC_API_FUCTION_H_ |
| 6 #define EXTENSIONS_BROWSER_API_ASYNC_API_FUCTION_H_ | 6 #define EXTENSIONS_BROWSER_API_ASYNC_API_FUCTION_H_ |
| 7 | 7 |
| 8 #include "content/public/browser/browser_thread.h" | 8 #include "content/public/browser/browser_thread.h" |
| 9 #include "extensions/browser/extension_function.h" | 9 #include "extensions/browser/extension_function.h" |
| 10 | 10 |
| 11 namespace extensions { | 11 namespace extensions { |
| 12 | 12 |
| 13 // AsyncApiFunction provides convenient thread management for APIs that need to | 13 // AsyncApiFunction provides convenient thread management for APIs that need to |
| 14 // do essentially all their work on a thread other than the UI thread. | 14 // do essentially all their work on a thread other than the UI thread. |
| 15 class AsyncApiFunction : public UIThreadExtensionFunction { | 15 class AsyncApiFunction : public AsyncExtensionFunction { |
| 16 protected: | 16 protected: |
| 17 AsyncApiFunction(); | 17 AsyncApiFunction(); |
| 18 virtual ~AsyncApiFunction(); | 18 virtual ~AsyncApiFunction(); |
| 19 | 19 |
| 20 // Like Prepare(). A useful place to put common work in an ApiFunction | 20 // Like Prepare(). A useful place to put common work in an ApiFunction |
| 21 // superclass that multiple API functions want to share. | 21 // superclass that multiple API functions want to share. |
| 22 virtual bool PrePrepare(); | 22 virtual bool PrePrepare(); |
| 23 | 23 |
| 24 // Set up for work (e.g., validate arguments). Guaranteed to happen on UI | 24 // Set up for work (e.g., validate arguments). Guaranteed to happen on UI |
| 25 // thread. | 25 // thread. |
| 26 virtual bool Prepare() = 0; | 26 virtual bool Prepare() = 0; |
| 27 | 27 |
| 28 // Do actual work. Guaranteed to happen on the thread specified in | 28 // Do actual work. Guaranteed to happen on the thread specified in |
| 29 // work_thread_id_. | 29 // work_thread_id_. |
| 30 virtual void Work(); | 30 virtual void Work(); |
| 31 | 31 |
| 32 // Start the asynchronous work. Guraranteed to happen on requested thread. | 32 // Start the asynchronous work. Guraranteed to happen on requested thread. |
| 33 virtual void AsyncWorkStart(); | 33 virtual void AsyncWorkStart(); |
| 34 | 34 |
| 35 // Notify AsyncIOApiFunction that the work is completed | 35 // Notify AsyncIOApiFunction that the work is completed |
| 36 void AsyncWorkCompleted(); | 36 void AsyncWorkCompleted(); |
| 37 | 37 |
| 38 // Respond. Guaranteed to happen on UI thread. | 38 // Respond. Guaranteed to happen on UI thread. |
| 39 virtual bool Respond() = 0; | 39 virtual bool Respond() = 0; |
| 40 | 40 |
| 41 // ExtensionFunction::RunImpl() | 41 // ExtensionFunction::RunAsync() |
| 42 virtual bool RunImpl() OVERRIDE; | 42 virtual bool RunAsync() OVERRIDE; |
| 43 | 43 |
| 44 protected: | 44 protected: |
| 45 content::BrowserThread::ID work_thread_id() const { return work_thread_id_; } | 45 content::BrowserThread::ID work_thread_id() const { return work_thread_id_; } |
| 46 void set_work_thread_id(content::BrowserThread::ID work_thread_id) { | 46 void set_work_thread_id(content::BrowserThread::ID work_thread_id) { |
| 47 work_thread_id_ = work_thread_id; | 47 work_thread_id_ = work_thread_id; |
| 48 } | 48 } |
| 49 | 49 |
| 50 private: | 50 private: |
| 51 void WorkOnWorkThread(); | 51 void WorkOnWorkThread(); |
| 52 void RespondOnUIThread(); | 52 void RespondOnUIThread(); |
| 53 | 53 |
| 54 // If you don't want your Work() method to happen on the IO thread, then set | 54 // If you don't want your Work() method to happen on the IO thread, then set |
| 55 // this to the thread that you do want, preferably in Prepare(). | 55 // this to the thread that you do want, preferably in Prepare(). |
| 56 content::BrowserThread::ID work_thread_id_; | 56 content::BrowserThread::ID work_thread_id_; |
| 57 }; | 57 }; |
| 58 | 58 |
| 59 } // namespace extensions | 59 } // namespace extensions |
| 60 | 60 |
| 61 #endif // EXTENSIONS_BROWSER_API_ASYNC_API_FUCTION_H_ | 61 #endif // EXTENSIONS_BROWSER_API_ASYNC_API_FUCTION_H_ |
| OLD | NEW |