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