Home | History | Annotate | Download | only in api
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_EXTENSIONS_API_API_FUNCTION_H_
      6 #define CHROME_BROWSER_EXTENSIONS_API_API_FUNCTION_H_
      7 
      8 #include "chrome/browser/extensions/chrome_extension_function.h"
      9 #include "content/public/browser/browser_thread.h"
     10 
     11 namespace extensions {
     12 
     13 // Base class for API functions. TODO(miket): there isn't much here anymore
     14 // since the removal of ApiResourceEventRouter. Should we promote all its
     15 // subclasses to UIThreadExtensionFunctions?
     16 class ApiFunction : public ChromeAsyncExtensionFunction {
     17  protected:
     18   ApiFunction();
     19   virtual ~ApiFunction();
     20 };
     21 
     22 // AsyncApiFunction provides convenient thread management for APIs that need to
     23 // do essentially all their work on a thread other than the UI thread.
     24 class AsyncApiFunction : public ApiFunction {
     25  protected:
     26   AsyncApiFunction();
     27   virtual ~AsyncApiFunction();
     28 
     29   // Like Prepare(). A useful place to put common work in an ApiFunction
     30   // superclass that multiple API functions want to share.
     31   virtual bool PrePrepare();
     32 
     33   // Set up for work (e.g., validate arguments). Guaranteed to happen on UI
     34   // thread.
     35   virtual bool Prepare() = 0;
     36 
     37   // Do actual work. Guaranteed to happen on the thread specified in
     38   // work_thread_id_.
     39   virtual void Work();
     40 
     41   // Start the asynchronous work. Guraranteed to happen on requested thread.
     42   virtual void AsyncWorkStart();
     43 
     44   // Notify AsyncIOApiFunction that the work is completed
     45   void AsyncWorkCompleted();
     46 
     47   // Respond. Guaranteed to happen on UI thread.
     48   virtual bool Respond() = 0;
     49 
     50   // ExtensionFunction::RunImpl()
     51   virtual bool RunImpl() OVERRIDE;
     52 
     53  protected:
     54   void set_work_thread_id(content::BrowserThread::ID work_thread_id) {
     55     work_thread_id_ = work_thread_id;
     56   }
     57 
     58  private:
     59   void WorkOnWorkThread();
     60   void RespondOnUIThread();
     61 
     62   // 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().
     64   content::BrowserThread::ID work_thread_id_;
     65 };
     66 
     67 }  // namespace extensions
     68 
     69 #endif  // CHROME_BROWSER_EXTENSIONS_API_API_FUNCTION_H_
     70