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