| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_DIAL_DIAL_API_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_DIAL_DIAL_API_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_DIAL_DIAL_API_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_DIAL_DIAL_API_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "chrome/browser/extensions/api/dial/dial_device_data.h" | 11 #include "chrome/browser/extensions/api/dial/dial_device_data.h" |
| 12 #include "chrome/browser/extensions/api/dial/dial_registry.h" | 12 #include "chrome/browser/extensions/api/dial/dial_registry.h" |
| 13 #include "chrome/common/extensions/api/dial.h" |
| 13 #include "components/keyed_service/core/refcounted_keyed_service.h" | 14 #include "components/keyed_service/core/refcounted_keyed_service.h" |
| 14 #include "extensions/browser/api/async_api_function.h" | 15 #include "extensions/browser/api/async_api_function.h" |
| 15 #include "extensions/browser/event_router.h" | 16 #include "extensions/browser/event_router.h" |
| 16 | 17 |
| 17 namespace extensions { | 18 namespace extensions { |
| 18 | 19 |
| 20 class DeviceDescriptionFetcher; |
| 19 class DialRegistry; | 21 class DialRegistry; |
| 20 | 22 |
| 23 namespace api { |
| 24 class DialFetchDeviceDescriptionFunction; |
| 25 } // namespace api |
| 26 |
| 21 // Dial API which is a ref-counted KeyedService that manages | 27 // Dial API which is a ref-counted KeyedService that manages |
| 22 // the DIAL registry. It takes care of creating the registry on the IO thread | 28 // the DIAL registry. It takes care of creating the registry on the IO thread |
| 23 // and is an observer of the registry. It makes sure devices events are sent out | 29 // and is an observer of the registry. It makes sure devices events are sent out |
| 24 // to extension listeners on the right thread. | 30 // to extension listeners on the right thread. |
| 25 // | 31 // |
| 26 // TODO(mfoltz): This should probably inherit from BrowserContextKeyedAPI | 32 // TODO(mfoltz): This should probably inherit from BrowserContextKeyedAPI |
| 27 // instead; ShutdownOnUIThread below is a no-op, which is the whole point of | 33 // instead; ShutdownOnUIThread below is a no-op, which is the whole point of |
| 28 // RefcountedKeyedService. | 34 // RefcountedKeyedService. |
| 35 // |
| 36 // TODO(mfoltz): The threading model for this API needs to be rethought. At a |
| 37 // minimum, DialRegistry should move to the UI thread to avoid extra thread hops |
| 38 // here, allow a straightforward GetDeviceList implementation |
| 39 // (crbug.com/576817), cleanup some long-tail crashes (crbug.com/640011) that |
| 40 // are likely due to lifetime issues, and simplify unit tests |
| 41 // (crbug.com/661457). |
| 29 class DialAPI : public RefcountedKeyedService, | 42 class DialAPI : public RefcountedKeyedService, |
| 30 public EventRouter::Observer, | 43 public EventRouter::Observer, |
| 31 public DialRegistry::Observer { | 44 public DialRegistry::Observer { |
| 32 public: | 45 public: |
| 33 explicit DialAPI(Profile* profile); | 46 explicit DialAPI(Profile* profile); |
| 34 | 47 |
| 35 // The DialRegistry for the API. This must always be used only from the IO | 48 // The DialRegistry for the API. This must always be used only from the IO |
| 36 // thread. | 49 // thread. |
| 37 DialRegistry* dial_registry(); | 50 DialRegistry* dial_registry(); |
| 38 | 51 |
| 39 // Called by the DialRegistry on the IO thread so that the DialAPI dispatches | 52 // Called by the DialRegistry on the IO thread so that the DialAPI dispatches |
| 40 // the event to listeners on the UI thread. | 53 // the event to listeners on the UI thread. |
| 41 void SendEventOnUIThread(const DialRegistry::DeviceList& devices); | 54 void SendEventOnUIThread(const DialRegistry::DeviceList& devices); |
| 42 void SendErrorOnUIThread(const DialRegistry::DialErrorCode type); | 55 void SendErrorOnUIThread(const DialRegistry::DialErrorCode type); |
| 43 | 56 |
| 57 // Sets test device data. For tests only. |
| 58 void SetDeviceForTest(const DialDeviceData& device_data, |
| 59 const DialDeviceDescription& device_description); |
| 60 |
| 44 private: | 61 private: |
| 45 ~DialAPI() override; | 62 ~DialAPI() override; |
| 46 | 63 |
| 64 friend class api::DialFetchDeviceDescriptionFunction; |
| 65 |
| 47 // RefcountedKeyedService: | 66 // RefcountedKeyedService: |
| 48 void ShutdownOnUIThread() override; | 67 void ShutdownOnUIThread() override; |
| 49 | 68 |
| 50 // EventRouter::Observer: | 69 // EventRouter::Observer: |
| 51 void OnListenerAdded(const EventListenerInfo& details) override; | 70 void OnListenerAdded(const EventListenerInfo& details) override; |
| 52 void OnListenerRemoved(const EventListenerInfo& details) override; | 71 void OnListenerRemoved(const EventListenerInfo& details) override; |
| 53 | 72 |
| 54 // DialRegistry::Observer: | 73 // DialRegistry::Observer: |
| 55 void OnDialDeviceEvent(const DialRegistry::DeviceList& devices) override; | 74 void OnDialDeviceEvent(const DialRegistry::DeviceList& devices) override; |
| 56 void OnDialError(DialRegistry::DialErrorCode type) override; | 75 void OnDialError(DialRegistry::DialErrorCode type) override; |
| 57 | 76 |
| 58 // Methods to notify the DialRegistry on the correct thread of new/removed | 77 // Methods to notify the DialRegistry on the correct thread of new/removed |
| 59 // listeners. | 78 // listeners. |
| 60 void NotifyListenerAddedOnIOThread(); | 79 void NotifyListenerAddedOnIOThread(); |
| 61 void NotifyListenerRemovedOnIOThread(); | 80 void NotifyListenerRemovedOnIOThread(); |
| 62 | 81 |
| 63 Profile* profile_; | 82 Profile* profile_; |
| 64 | 83 |
| 65 // Created lazily on first access on the IO thread. | 84 // Created lazily on first access on the IO thread. |
| 66 std::unique_ptr<DialRegistry> dial_registry_; | 85 std::unique_ptr<DialRegistry> dial_registry_; |
| 67 | 86 |
| 87 // Device data for testing. |
| 88 DialDeviceData test_device_data_; |
| 89 DialDeviceDescription test_device_description_; |
| 90 |
| 68 DISALLOW_COPY_AND_ASSIGN(DialAPI); | 91 DISALLOW_COPY_AND_ASSIGN(DialAPI); |
| 69 }; | 92 }; |
| 70 | 93 |
| 71 namespace api { | 94 namespace api { |
| 72 | 95 |
| 73 // DiscoverNow function. This function needs a round-trip from the IO thread | 96 // DiscoverNow function. This function needs a round-trip from the IO thread |
| 74 // because it needs to grab a pointer to the DIAL API in order to get a | 97 // because it needs to grab a pointer to the DIAL API in order to get a |
| 75 // reference to the DialRegistry while on the IO thread. Then, the result | 98 // reference to the DialRegistry while on the IO thread. Then, the result |
| 76 // must be returned on the UI thread. | 99 // must be returned on the UI thread. |
| 77 class DialDiscoverNowFunction : public AsyncApiFunction { | 100 class DialDiscoverNowFunction : public AsyncApiFunction { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 93 DialAPI* dial_; | 116 DialAPI* dial_; |
| 94 | 117 |
| 95 // Result of the discoverNow call to the DIAL registry. This result is | 118 // Result of the discoverNow call to the DIAL registry. This result is |
| 96 // retrieved on the IO thread but the function result is returned on the UI | 119 // retrieved on the IO thread but the function result is returned on the UI |
| 97 // thread. | 120 // thread. |
| 98 bool result_; | 121 bool result_; |
| 99 | 122 |
| 100 DISALLOW_COPY_AND_ASSIGN(DialDiscoverNowFunction); | 123 DISALLOW_COPY_AND_ASSIGN(DialDiscoverNowFunction); |
| 101 }; | 124 }; |
| 102 | 125 |
| 126 class DialFetchDeviceDescriptionFunction : public AsyncExtensionFunction { |
| 127 public: |
| 128 DialFetchDeviceDescriptionFunction(); |
| 129 |
| 130 protected: |
| 131 ~DialFetchDeviceDescriptionFunction() override; |
| 132 |
| 133 // AsyncExtensionFunction: |
| 134 bool RunAsync() override; |
| 135 |
| 136 private: |
| 137 DECLARE_EXTENSION_FUNCTION("dial.fetchDeviceDescription", |
| 138 DIAL_FETCHDEVICEDESCRIPTION) |
| 139 |
| 140 void GetDeviceDescriptionUrlOnIOThread(const std::string& label); |
| 141 void MaybeStartFetch(const GURL& url); |
| 142 void OnFetchComplete(const DialDeviceDescription& result); |
| 143 void OnFetchError(const std::string& result); |
| 144 |
| 145 std::unique_ptr<api::dial::FetchDeviceDescription::Params> params_; |
| 146 std::unique_ptr<DeviceDescriptionFetcher> device_description_fetcher_; |
| 147 |
| 148 DialAPI* dial_; |
| 149 |
| 150 DISALLOW_COPY_AND_ASSIGN(DialFetchDeviceDescriptionFunction); |
| 151 }; |
| 152 |
| 103 } // namespace api | 153 } // namespace api |
| 104 | 154 |
| 105 } // namespace extensions | 155 } // namespace extensions |
| 106 | 156 |
| 107 #endif // CHROME_BROWSER_EXTENSIONS_API_DIAL_DIAL_API_H_ | 157 #endif // CHROME_BROWSER_EXTENSIONS_API_DIAL_DIAL_API_H_ |
| OLD | NEW |