| 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/media/router/discovery/dial/dial_device_data.h" |
| 12 #include "chrome/browser/extensions/api/dial/dial_registry.h" | 12 #include "chrome/browser/media/router/discovery/dial/dial_registry.h" |
| 13 #include "chrome/common/extensions/api/dial.h" | 13 #include "chrome/common/extensions/api/dial.h" |
| 14 #include "components/keyed_service/core/refcounted_keyed_service.h" | 14 #include "components/keyed_service/core/refcounted_keyed_service.h" |
| 15 #include "extensions/browser/api/async_api_function.h" | 15 #include "extensions/browser/api/async_api_function.h" |
| 16 #include "extensions/browser/event_router.h" | 16 #include "extensions/browser/event_router.h" |
| 17 | 17 |
| 18 namespace media_router { |
| 19 class DeviceDescriptionFetcher; |
| 20 } // namespace media_router |
| 21 |
| 18 namespace extensions { | 22 namespace extensions { |
| 19 | 23 |
| 20 namespace api { | |
| 21 namespace dial { | |
| 22 class DeviceDescriptionFetcher; | |
| 23 } // namespace dial | |
| 24 } // namespace api | |
| 25 | |
| 26 class DialFetchDeviceDescriptionFunction; | 24 class DialFetchDeviceDescriptionFunction; |
| 27 | 25 |
| 28 // Dial API which is a ref-counted KeyedService that manages | 26 // Dial API which is a ref-counted KeyedService that manages |
| 29 // the DIAL registry. It takes care of creating the registry on the IO thread | 27 // the DIAL registry. It takes care of creating the registry on the IO thread |
| 30 // and is an observer of the registry. It makes sure devices events are sent out | 28 // and is an observer of the registry. It makes sure devices events are sent out |
| 31 // to extension listeners on the right thread. | 29 // to extension listeners on the right thread. |
| 32 // | 30 // |
| 33 // TODO(mfoltz): This should probably inherit from BrowserContextKeyedAPI | 31 // TODO(mfoltz): This should probably inherit from BrowserContextKeyedAPI |
| 34 // instead; ShutdownOnUIThread below is a no-op, which is the whole point of | 32 // instead; ShutdownOnUIThread below is a no-op, which is the whole point of |
| 35 // RefcountedKeyedService. | 33 // RefcountedKeyedService. |
| 36 // | 34 // |
| 37 // TODO(mfoltz): The threading model for this API needs to be rethought. At a | 35 // TODO(mfoltz): The threading model for this API needs to be rethought. At a |
| 38 // minimum, DialRegistry should move to the UI thread to avoid extra thread hops | 36 // minimum, DialRegistry should move to the UI thread to avoid extra thread hops |
| 39 // here. This would also allow a straightforward GetDeviceList implementation | 37 // here. This would also allow a straightforward GetDeviceList implementation |
| 40 // (crbug.com/576817), cleanup some long-tail crashes (crbug.com/640011) that | 38 // (crbug.com/576817), cleanup some long-tail crashes (crbug.com/640011) that |
| 41 // are likely due to lifetime issues, and simplify unit tests | 39 // are likely due to lifetime issues, and simplify unit tests |
| 42 // (crbug.com/661457). | 40 // (crbug.com/661457). |
| 43 // | 41 // |
| 44 // Also, DialRegistry should be an interface that can be mocked and injected for | 42 // Also, DialRegistry should be an interface that can be mocked and injected for |
| 45 // tests; this would allow us to remove code that injects test data into the | 43 // tests; this would allow us to remove code that injects test data into the |
| 46 // real DialRegsitry. | 44 // real DialRegsitry. |
| 47 class DialAPI : public RefcountedKeyedService, | 45 class DialAPI : public RefcountedKeyedService, |
| 48 public EventRouter::Observer, | 46 public EventRouter::Observer, |
| 49 public api::dial::DialRegistry::Observer { | 47 public media_router::DialRegistry::Observer { |
| 50 public: | 48 public: |
| 51 explicit DialAPI(Profile* profile); | 49 explicit DialAPI(Profile* profile); |
| 52 | 50 |
| 53 // The DialRegistry for the API. This must always be used only from the IO | 51 // The DialRegistry for the API. This must always be used only from the IO |
| 54 // thread. | 52 // thread. |
| 55 api::dial::DialRegistry* dial_registry(); | 53 media_router::DialRegistry* dial_registry(); |
| 56 | 54 |
| 57 // Called by the DialRegistry on the IO thread so that the DialAPI dispatches | 55 // Called by the DialRegistry on the IO thread so that the DialAPI dispatches |
| 58 // the event to listeners on the UI thread. | 56 // the event to listeners on the UI thread. |
| 59 void SendEventOnUIThread(const api::dial::DialRegistry::DeviceList& devices); | 57 void SendEventOnUIThread( |
| 60 void SendErrorOnUIThread(const api::dial::DialRegistry::DialErrorCode type); | 58 const media_router::DialRegistry::DeviceList& devices); |
| 59 void SendErrorOnUIThread( |
| 60 const media_router::DialRegistry::DialErrorCode type); |
| 61 | 61 |
| 62 // Sets test device data. | 62 // Sets test device data. |
| 63 void SetDeviceForTest( | 63 void SetDeviceForTest( |
| 64 const api::dial::DialDeviceData& device_data, | 64 const media_router::DialDeviceData& device_data, |
| 65 const api::dial::DialDeviceDescriptionData& device_description); | 65 const media_router::DialDeviceDescriptionData& device_description); |
| 66 | 66 |
| 67 private: | 67 private: |
| 68 ~DialAPI() override; | 68 ~DialAPI() override; |
| 69 | 69 |
| 70 friend class DialFetchDeviceDescriptionFunction; | 70 friend class DialFetchDeviceDescriptionFunction; |
| 71 | 71 |
| 72 // RefcountedKeyedService: | 72 // RefcountedKeyedService: |
| 73 void ShutdownOnUIThread() override; | 73 void ShutdownOnUIThread() override; |
| 74 | 74 |
| 75 // EventRouter::Observer: | 75 // EventRouter::Observer: |
| 76 void OnListenerAdded(const EventListenerInfo& details) override; | 76 void OnListenerAdded(const EventListenerInfo& details) override; |
| 77 void OnListenerRemoved(const EventListenerInfo& details) override; | 77 void OnListenerRemoved(const EventListenerInfo& details) override; |
| 78 | 78 |
| 79 // DialRegistry::Observer: | 79 // DialRegistry::Observer: |
| 80 void OnDialDeviceEvent( | 80 void OnDialDeviceEvent( |
| 81 const api::dial::DialRegistry::DeviceList& devices) override; | 81 const media_router::DialRegistry::DeviceList& devices) override; |
| 82 void OnDialError(api::dial::DialRegistry::DialErrorCode type) override; | 82 void OnDialError(media_router::DialRegistry::DialErrorCode type) override; |
| 83 | 83 |
| 84 // Methods to notify the DialRegistry on the correct thread of new/removed | 84 // Methods to notify the DialRegistry on the correct thread of new/removed |
| 85 // listeners. | 85 // listeners. |
| 86 void NotifyListenerAddedOnIOThread(); | 86 void NotifyListenerAddedOnIOThread(); |
| 87 void NotifyListenerRemovedOnIOThread(); | 87 void NotifyListenerRemovedOnIOThread(); |
| 88 | 88 |
| 89 // Fills the |device| API struct from |device_data|. |
| 90 void FillDialDevice(const media_router::DialDeviceData& device_data, |
| 91 api::dial::DialDevice* device) const; |
| 92 |
| 89 Profile* profile_; | 93 Profile* profile_; |
| 90 | 94 |
| 91 // Created lazily on first access on the IO thread. | 95 // Created lazily on first access on the IO thread. |
| 92 std::unique_ptr<api::dial::DialRegistry> dial_registry_; | 96 std::unique_ptr<media_router::DialRegistry> dial_registry_; |
| 93 | 97 |
| 94 // Device data for testing. | 98 // Device data for testing. |
| 95 std::unique_ptr<api::dial::DialDeviceData> test_device_data_; | 99 std::unique_ptr<media_router::DialDeviceData> test_device_data_; |
| 96 std::unique_ptr<api::dial::DialDeviceDescriptionData> | 100 std::unique_ptr<media_router::DialDeviceDescriptionData> |
| 97 test_device_description_; | 101 test_device_description_; |
| 98 | 102 |
| 99 DISALLOW_COPY_AND_ASSIGN(DialAPI); | 103 DISALLOW_COPY_AND_ASSIGN(DialAPI); |
| 100 }; | 104 }; |
| 101 | 105 |
| 102 // DiscoverNow function. This function needs a round-trip from the IO thread | 106 // DiscoverNow function. This function needs a round-trip from the IO thread |
| 103 // because it needs to grab a pointer to the DIAL API in order to get a | 107 // because it needs to grab a pointer to the DIAL API in order to get a |
| 104 // reference to the DialRegistry while on the IO thread. Then, the result | 108 // reference to the DialRegistry while on the IO thread. Then, the result |
| 105 // must be returned on the UI thread. | 109 // must be returned on the UI thread. |
| 106 class DialDiscoverNowFunction : public AsyncApiFunction { | 110 class DialDiscoverNowFunction : public AsyncApiFunction { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 | 142 |
| 139 // AsyncExtensionFunction: | 143 // AsyncExtensionFunction: |
| 140 bool RunAsync() override; | 144 bool RunAsync() override; |
| 141 | 145 |
| 142 private: | 146 private: |
| 143 DECLARE_EXTENSION_FUNCTION("dial.fetchDeviceDescription", | 147 DECLARE_EXTENSION_FUNCTION("dial.fetchDeviceDescription", |
| 144 DIAL_FETCHDEVICEDESCRIPTION) | 148 DIAL_FETCHDEVICEDESCRIPTION) |
| 145 | 149 |
| 146 void GetDeviceDescriptionUrlOnIOThread(const std::string& label); | 150 void GetDeviceDescriptionUrlOnIOThread(const std::string& label); |
| 147 void MaybeStartFetch(const GURL& url); | 151 void MaybeStartFetch(const GURL& url); |
| 148 void OnFetchComplete(const api::dial::DialDeviceDescriptionData& result); | 152 void OnFetchComplete(const media_router::DialDeviceDescriptionData& result); |
| 149 void OnFetchError(const std::string& result); | 153 void OnFetchError(const std::string& result); |
| 150 | 154 |
| 151 std::unique_ptr<api::dial::FetchDeviceDescription::Params> params_; | 155 std::unique_ptr<api::dial::FetchDeviceDescription::Params> params_; |
| 152 std::unique_ptr<api::dial::DeviceDescriptionFetcher> | 156 std::unique_ptr<media_router::DeviceDescriptionFetcher> |
| 153 device_description_fetcher_; | 157 device_description_fetcher_; |
| 154 | 158 |
| 155 DialAPI* dial_; | 159 DialAPI* dial_; |
| 156 | 160 |
| 157 DISALLOW_COPY_AND_ASSIGN(DialFetchDeviceDescriptionFunction); | 161 DISALLOW_COPY_AND_ASSIGN(DialFetchDeviceDescriptionFunction); |
| 158 }; | 162 }; |
| 159 | 163 |
| 160 } // namespace extensions | 164 } // namespace extensions |
| 161 | 165 |
| 162 #endif // CHROME_BROWSER_EXTENSIONS_API_DIAL_DIAL_API_H_ | 166 #endif // CHROME_BROWSER_EXTENSIONS_API_DIAL_DIAL_API_H_ |
| OLD | NEW |