Home | History | Annotate | Download | only in braille_display_private
      1 // Copyright 2013 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_BRAILLE_DISPLAY_PRIVATE_BRAILLE_DISPLAY_PRIVATE_API_H_
      6 #define CHROME_BROWSER_EXTENSIONS_API_BRAILLE_DISPLAY_PRIVATE_BRAILLE_DISPLAY_PRIVATE_API_H_
      7 
      8 #include "base/scoped_observer.h"
      9 #include "chrome/browser/extensions/api/braille_display_private/braille_controller.h"
     10 #include "chrome/common/extensions/api/braille_display_private.h"
     11 #include "extensions/browser/api/async_api_function.h"
     12 #include "extensions/browser/browser_context_keyed_api_factory.h"
     13 #include "extensions/browser/event_router.h"
     14 
     15 class Profile;
     16 
     17 namespace extensions {
     18 namespace api {
     19 namespace braille_display_private {
     20 class BrailleDisplayPrivateAPIUserTest;
     21 }  // namespace braille_display_private
     22 }  // namespace api
     23 
     24 // Implementation of the chrome.brailleDisplayPrivate API.
     25 class BrailleDisplayPrivateAPI : public BrowserContextKeyedAPI,
     26                                  api::braille_display_private::BrailleObserver,
     27                                  EventRouter::Observer {
     28  public:
     29   explicit BrailleDisplayPrivateAPI(content::BrowserContext* context);
     30   virtual ~BrailleDisplayPrivateAPI();
     31 
     32   // ProfileKeyedService implementation.
     33   virtual void Shutdown() OVERRIDE;
     34 
     35   // BrowserContextKeyedAPI implementation.
     36   static BrowserContextKeyedAPIFactory<BrailleDisplayPrivateAPI>*
     37       GetFactoryInstance();
     38 
     39   // BrailleObserver implementation.
     40   virtual void OnBrailleDisplayStateChanged(
     41       const api::braille_display_private::DisplayState& display_state) OVERRIDE;
     42   virtual void OnBrailleKeyEvent(
     43       const api::braille_display_private::KeyEvent& keyEvent) OVERRIDE;
     44 
     45   // EventRouter::Observer implementation.
     46   virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE;
     47   virtual void OnListenerRemoved(const EventListenerInfo& details) OVERRIDE;
     48 
     49 
     50  private:
     51   friend class BrowserContextKeyedAPIFactory<BrailleDisplayPrivateAPI>;
     52   friend class api::braille_display_private::BrailleDisplayPrivateAPIUserTest;
     53 
     54   class EventDelegate {
     55    public:
     56     virtual ~EventDelegate() {}
     57     virtual void BroadcastEvent(scoped_ptr<Event> event) = 0;
     58     virtual bool HasListener() = 0;
     59   };
     60 
     61   class DefaultEventDelegate;
     62 
     63   // Returns whether the profile that this API was created for is currently
     64   // the active profile.
     65   bool IsProfileActive();
     66 
     67   void SetEventDelegateForTest(scoped_ptr<EventDelegate> delegate);
     68 
     69   Profile* profile_;
     70   ScopedObserver<api::braille_display_private::BrailleController,
     71                  BrailleObserver> scoped_observer_;
     72   scoped_ptr<EventDelegate> event_delegate_;
     73 
     74   // BrowserContextKeyedAPI implementation.
     75   static const char* service_name() {
     76     return "BrailleDisplayPrivateAPI";
     77   }
     78   // Override the default so the service is not created in tests.
     79   static const bool kServiceIsNULLWhileTesting = true;
     80 };
     81 
     82 namespace api {
     83 
     84 class BrailleDisplayPrivateGetDisplayStateFunction : public AsyncApiFunction {
     85   DECLARE_EXTENSION_FUNCTION("brailleDisplayPrivate.getDisplayState",
     86                              BRAILLEDISPLAYPRIVATE_GETDISPLAYSTATE)
     87  protected:
     88   virtual ~BrailleDisplayPrivateGetDisplayStateFunction() {}
     89   virtual bool Prepare() OVERRIDE;
     90   virtual void Work() OVERRIDE;
     91   virtual bool Respond() OVERRIDE;
     92 };
     93 
     94 class BrailleDisplayPrivateWriteDotsFunction : public AsyncApiFunction {
     95   DECLARE_EXTENSION_FUNCTION("brailleDisplayPrivate.writeDots",
     96                              BRAILLEDISPLAYPRIVATE_WRITEDOTS);
     97  public:
     98   BrailleDisplayPrivateWriteDotsFunction();
     99 
    100  protected:
    101   virtual ~BrailleDisplayPrivateWriteDotsFunction();
    102   virtual bool Prepare() OVERRIDE;
    103   virtual void Work() OVERRIDE;
    104   virtual bool Respond() OVERRIDE;
    105 
    106  private:
    107   scoped_ptr<braille_display_private::WriteDots::Params> params_;
    108 };
    109 
    110 }  // namespace api
    111 }  // namespace extensions
    112 
    113 #endif  // CHROME_BROWSER_EXTENSIONS_API_BRAILLE_DISPLAY_PRIVATE_BRAILLE_DISPLAY_PRIVATE_API_H_
    114