Home | History | Annotate | Download | only in dial
      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_DIAL_DIAL_API_H_
      6 #define CHROME_BROWSER_EXTENSIONS_API_DIAL_DIAL_API_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "base/memory/scoped_ptr.h"
     10 #include "chrome/browser/extensions/api/dial/dial_device_data.h"
     11 #include "chrome/browser/extensions/api/dial/dial_registry.h"
     12 #include "components/keyed_service/content/refcounted_browser_context_keyed_service.h"
     13 #include "extensions/browser/api/async_api_function.h"
     14 #include "extensions/browser/event_router.h"
     15 
     16 namespace extensions {
     17 
     18 class DialRegistry;
     19 
     20 // Dial API which is a ref-counted KeyedService that manages
     21 // the DIAL registry. It takes care of creating the registry on the IO thread
     22 // and is an observer of the registry. It makes sure devices events are sent out
     23 // to extension listeners on the right thread.
     24 class DialAPI : public RefcountedBrowserContextKeyedService,
     25                 public EventRouter::Observer,
     26                 public DialRegistry::Observer {
     27  public:
     28   explicit DialAPI(Profile* profile);
     29 
     30   // The DialRegistry for the API. This must always be used only from the IO
     31   // thread.
     32   DialRegistry* dial_registry();
     33 
     34   // Called by the DialRegistry on the IO thread so that the DialAPI dispatches
     35   // the event to listeners on the UI thread.
     36   void SendEventOnUIThread(const DialRegistry::DeviceList& devices);
     37   void SendErrorOnUIThread(const DialRegistry::DialErrorCode type);
     38 
     39  private:
     40   virtual ~DialAPI();
     41 
     42   // RefcountedBrowserContextKeyedService:
     43   virtual void ShutdownOnUIThread() OVERRIDE;
     44 
     45   // EventRouter::Observer:
     46   virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE;
     47   virtual void OnListenerRemoved(const EventListenerInfo& details) OVERRIDE;
     48 
     49   // DialRegistry::Observer:
     50   virtual void OnDialDeviceEvent(
     51       const DialRegistry::DeviceList& devices) OVERRIDE;
     52   virtual void OnDialError(DialRegistry::DialErrorCode type) OVERRIDE;
     53 
     54   // Methods to notify the DialRegistry on the correct thread of new/removed
     55   // listeners.
     56   void NotifyListenerAddedOnIOThread();
     57   void NotifyListenerRemovedOnIOThread();
     58 
     59   Profile* profile_;
     60 
     61   // Created lazily on first access on the IO thread.
     62   scoped_ptr<DialRegistry> dial_registry_;
     63 
     64   DISALLOW_COPY_AND_ASSIGN(DialAPI);
     65 };
     66 
     67 namespace api {
     68 
     69 // DiscoverNow function. This function needs a round-trip from the IO thread
     70 // because it needs to grab a pointer to the DIAL API in order to get a
     71 // reference to the DialRegistry while on the IO thread. Then, the result
     72 // must be returned on the UI thread.
     73 class DialDiscoverNowFunction : public AsyncApiFunction {
     74  public:
     75   DialDiscoverNowFunction();
     76 
     77  protected:
     78   virtual ~DialDiscoverNowFunction() {}
     79 
     80   // AsyncApiFunction:
     81   virtual bool Prepare() OVERRIDE;
     82   virtual void Work() OVERRIDE;
     83   virtual bool Respond() OVERRIDE;
     84 
     85  private:
     86   DECLARE_EXTENSION_FUNCTION("dial.discoverNow", DIAL_DISCOVERNOW)
     87 
     88   // Pointer to the DIAL API for this profile. We get this on the UI thread.
     89   DialAPI* dial_;
     90 
     91   // Result of the discoverNow call to the DIAL registry. This result is
     92   // retrieved on the IO thread but the function result is returned on the UI
     93   // thread.
     94   bool result_;
     95 
     96   DISALLOW_COPY_AND_ASSIGN(DialDiscoverNowFunction);
     97 };
     98 
     99 }  // namespace api
    100 
    101 }  // namespace extensions
    102 
    103 #endif  // CHROME_BROWSER_EXTENSIONS_API_DIAL_DIAL_API_H_
    104