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