| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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_MEDIA_ROUTER_DISCOVERY_DIAL_DIAL_MEDIA_SINK_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_DIAL_DIAL_MEDIA_SINK_SERVICE_IMPL_
H_ |
| 6 #define CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_DIAL_DIAL_MEDIA_SINK_SERVICE_H_ | 6 #define CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_DIAL_DIAL_MEDIA_SINK_SERVICE_IMPL_
H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <set> | 9 #include <set> |
| 10 | 10 |
| 11 #include "chrome/browser/media/router/discovery/dial/device_description_service.
h" | 11 #include "chrome/browser/media/router/discovery/dial/device_description_service.
h" |
| 12 #include "chrome/browser/media/router/discovery/dial/dial_registry.h" | 12 #include "chrome/browser/media/router/discovery/dial/dial_registry.h" |
| 13 #include "chrome/common/media_router/discovery/media_sink_internal.h" | 13 #include "chrome/browser/media/router/discovery/media_sink_service_base.h" |
| 14 #include "chrome/common/media_router/discovery/media_sink_service.h" | |
| 15 | 14 |
| 16 namespace media_router { | 15 namespace media_router { |
| 17 | 16 |
| 18 class DeviceDescriptionService; | 17 class DeviceDescriptionService; |
| 19 class DialRegistry; | 18 class DialRegistry; |
| 20 | 19 |
| 21 // A service which can be used to start background discovery and resolution of | 20 // A service which can be used to start background discovery and resolution of |
| 22 // DIAL devices (Smart TVs, Game Consoles, etc.). | 21 // DIAL devices (Smart TVs, Game Consoles, etc.). |
| 23 // This class is not thread safe. All methods must be called from the IO thread. | 22 // This class is not thread safe. All methods must be called from the IO thread. |
| 24 class DialMediaSinkService : public MediaSinkService, | 23 class DialMediaSinkServiceImpl : public MediaSinkServiceBase, |
| 25 public DialRegistry::Observer { | 24 public DialRegistry::Observer { |
| 26 public: | 25 public: |
| 27 DialMediaSinkService(const OnSinksDiscoveredCallback& callback, | 26 DialMediaSinkServiceImpl(const OnSinksDiscoveredCallback& callback, |
| 28 net::URLRequestContextGetter* request_context); | 27 net::URLRequestContextGetter* request_context); |
| 29 ~DialMediaSinkService() override; | 28 ~DialMediaSinkServiceImpl() override; |
| 30 | |
| 31 // Stops listening for DIAL device events. | |
| 32 virtual void Stop(); | |
| 33 | 29 |
| 34 // MediaSinkService implementation | 30 // MediaSinkService implementation |
| 35 void Start() override; | 31 void Start() override; |
| 32 void Stop() override; |
| 36 | 33 |
| 37 protected: | 34 protected: |
| 38 | |
| 39 // Returns instance of device description service. Create a new one if none | 35 // Returns instance of device description service. Create a new one if none |
| 40 // exists. | 36 // exists. |
| 41 DeviceDescriptionService* GetDescriptionService(); | 37 DeviceDescriptionService* GetDescriptionService(); |
| 42 | 38 |
| 43 // Does not take ownership of |dial_registry|. | 39 // Does not take ownership of |dial_registry|. |
| 44 void SetDialRegistryForTest(DialRegistry* dial_registry); | 40 void SetDialRegistryForTest(DialRegistry* dial_registry); |
| 45 void SetDescriptionServiceForTest( | 41 void SetDescriptionServiceForTest( |
| 46 std::unique_ptr<DeviceDescriptionService> description_service); | 42 std::unique_ptr<DeviceDescriptionService> description_service); |
| 47 void SetTimerForTest(std::unique_ptr<base::Timer> timer); | |
| 48 | 43 |
| 49 private: | 44 private: |
| 50 friend class DialMediaSinkServiceTest; | 45 friend class DialMediaSinkServiceImplTest; |
| 51 FRIEND_TEST_ALL_PREFIXES(DialMediaSinkServiceTest, TestStart); | 46 FRIEND_TEST_ALL_PREFIXES(DialMediaSinkServiceImplTest, TestStart); |
| 52 FRIEND_TEST_ALL_PREFIXES(DialMediaSinkServiceTest, TestTimer); | 47 FRIEND_TEST_ALL_PREFIXES(DialMediaSinkServiceImplTest, TestTimer); |
| 53 FRIEND_TEST_ALL_PREFIXES(DialMediaSinkServiceTest, | 48 FRIEND_TEST_ALL_PREFIXES(DialMediaSinkServiceImplTest, |
| 54 TestFetchCompleted_SameSink); | |
| 55 FRIEND_TEST_ALL_PREFIXES(DialMediaSinkServiceTest, TestIsDifferent); | |
| 56 FRIEND_TEST_ALL_PREFIXES(DialMediaSinkServiceTest, | |
| 57 TestOnDeviceDescriptionAvailable); | 49 TestOnDeviceDescriptionAvailable); |
| 58 | 50 |
| 59 // api::dial::DialRegistry::Observer implementation | 51 // api::dial::DialRegistry::Observer implementation |
| 60 void OnDialDeviceEvent(const DialRegistry::DeviceList& devices) override; | 52 void OnDialDeviceEvent(const DialRegistry::DeviceList& devices) override; |
| 61 void OnDialError(DialRegistry::DialErrorCode type) override; | 53 void OnDialError(DialRegistry::DialErrorCode type) override; |
| 62 | 54 |
| 63 // Called when description service successfully fetches and parses device | 55 // Called when description service successfully fetches and parses device |
| 64 // description XML. Restart |finish_timer_| if it is not running. | 56 // description XML. Restart |finish_timer_| if it is not running. |
| 65 void OnDeviceDescriptionAvailable( | 57 void OnDeviceDescriptionAvailable( |
| 66 const DialDeviceData& device_data, | 58 const DialDeviceData& device_data, |
| 67 const ParsedDialDeviceDescription& description_data); | 59 const ParsedDialDeviceDescription& description_data); |
| 68 | 60 |
| 69 // Called when fails to fetch or parse device description XML. | 61 // Called when fails to fetch or parse device description XML. |
| 70 void OnDeviceDescriptionError(const DialDeviceData& device, | 62 void OnDeviceDescriptionError(const DialDeviceData& device, |
| 71 const std::string& error_message); | 63 const std::string& error_message); |
| 72 | 64 |
| 73 // Called when |finish_timer_| expires. | |
| 74 void OnFetchCompleted(); | |
| 75 | |
| 76 // Helper function to start |finish_timer_|. | |
| 77 void StartTimer(); | |
| 78 | |
| 79 // Timer for finishing fetching. Starts in |OnDialDeviceEvent()|, and expires | |
| 80 // 3 seconds later. If |OnDeviceDescriptionAvailable()| is called after | |
| 81 // |finish_timer_| expires, |finish_timer_| is restarted. | |
| 82 std::unique_ptr<base::Timer> finish_timer_; | |
| 83 | |
| 84 std::unique_ptr<DeviceDescriptionService> description_service_; | 65 std::unique_ptr<DeviceDescriptionService> description_service_; |
| 85 | 66 |
| 86 // Raw pointer to DialRegistry singleton. | 67 // Raw pointer to DialRegistry singleton. |
| 87 DialRegistry* dial_registry_ = nullptr; | 68 DialRegistry* dial_registry_ = nullptr; |
| 88 | 69 |
| 89 // Sorted sinks from current round of discovery. | |
| 90 std::set<MediaSinkInternal> current_sinks_; | |
| 91 | |
| 92 // Sorted sinks sent to Media Router Provider in last FetchCompleted(). | |
| 93 std::set<MediaSinkInternal> mrp_sinks_; | |
| 94 | |
| 95 // Device data list from current round of discovery. | 70 // Device data list from current round of discovery. |
| 96 DialRegistry::DeviceList current_devices_; | 71 DialRegistry::DeviceList current_devices_; |
| 97 | 72 |
| 98 scoped_refptr<net::URLRequestContextGetter> request_context_; | 73 scoped_refptr<net::URLRequestContextGetter> request_context_; |
| 99 }; | 74 }; |
| 100 | 75 |
| 101 } // namespace media_router | 76 } // namespace media_router |
| 102 | 77 |
| 103 #endif // CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_DIAL_DIAL_MEDIA_SINK_SERVICE_H_ | 78 #endif // CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_DIAL_DIAL_MEDIA_SINK_SERVICE_IM
PL_H_ |
| OLD | NEW |