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 void OnDialDeviceEvent(const DialRegistry::DeviceList& devices) OVERRIDE; |
| 49 |
| 50 // Methods to notify the DialRegistry on the correct thread of new/removed |
| 51 // listeners. |
| 52 void NotifyListenerAddedOnIOThread(); |
| 53 void NotifyListenerRemovedOnIOThread(); |
| 54 |
| 55 Profile* profile_; |
| 56 |
| 57 // Created lazily on first access on the IO thread. |
| 58 scoped_ptr<DialRegistry> dial_registry_; |
| 59 |
| 60 DISALLOW_COPY_AND_ASSIGN(DialAPI); |
| 61 }; |
| 62 |
| 63 namespace api { |
| 64 |
| 65 // DiscoverNow function. This function needs a round-trip from the IO thread |
| 66 // because it needs to grab a pointer to the DIAL API in order to get a |
| 67 // reference to the DialRegistry while on the IO thread. Then, the result |
| 68 // must be returned on the UI thread. |
| 69 class DialDiscoverNowFunction : public AsyncApiFunction { |
| 70 public: |
| 71 DialDiscoverNowFunction(); |
| 72 |
| 73 protected: |
| 74 virtual ~DialDiscoverNowFunction() {} |
| 75 |
| 76 // AsyncApiFunction: |
| 77 virtual bool Prepare() OVERRIDE; |
| 78 virtual void Work() OVERRIDE; |
| 79 virtual bool Respond() OVERRIDE; |
| 80 |
| 81 private: |
| 82 DECLARE_EXTENSION_FUNCTION_NAME("dial.discoverNow") |
| 83 |
| 84 // Pointer to the DIAL API for this profile. We get this on the UI thread. |
| 85 DialAPI* dial_; |
| 86 |
| 87 // Result of the discoverNow call to the DIAL registry. This result is |
| 88 // retrieved on the IO thread but the function result is returned on the UI |
| 89 // thread. |
| 90 bool result_; |
| 91 }; |
| 92 |
| 93 } // namespace api |
| 94 |
| 95 } // namespace extensions |
| 96 |
| 97 #endif // CHROME_BROWSER_EXTENSIONS_API_DIAL_DIAL_API_H_ |
OLD | NEW |