Chromium Code Reviews| 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 |
| 11 #include "base/memory/ref_counted.h" | |
| 11 #include "chrome/browser/media/router/discovery/dial/device_description_service. h" | 12 #include "chrome/browser/media/router/discovery/dial/device_description_service. h" |
| 12 #include "chrome/browser/media/router/discovery/dial/dial_registry.h" | 13 #include "chrome/browser/media/router/discovery/dial/dial_registry.h" |
| 13 #include "chrome/common/media_router/discovery/media_sink_internal.h" | 14 #include "chrome/common/media_router/discovery/media_sink_internal.h" |
| 14 #include "chrome/common/media_router/discovery/media_sink_service.h" | 15 #include "chrome/common/media_router/discovery/media_sink_service.h" |
| 16 #include "content/public/browser/browser_thread.h" | |
| 15 | 17 |
| 16 namespace media_router { | 18 namespace media_router { |
| 17 | 19 |
| 18 class DeviceDescriptionService; | 20 class DeviceDescriptionService; |
| 19 class DialRegistry; | 21 class DialRegistry; |
| 20 | 22 |
| 21 // A service which can be used to start background discovery and resolution of | 23 // A service which can be used to start background discovery and resolution of |
| 22 // DIAL devices (Smart TVs, Game Consoles, etc.). | 24 // DIAL devices (Smart TVs, Game Consoles, etc.). |
| 23 // This class is not thread safe. All methods must be called from the IO thread. | 25 // This class is thread safe. |
|
mark a. foltz
2017/05/09 18:21:13
Yes, but the public API (including the OnSinksDisc
zhaobin
2017/05/10 18:17:34
Done.
| |
| 24 class DialMediaSinkService : public MediaSinkService, | 26 class DialMediaSinkService : public MediaSinkService, |
| 25 public DialRegistry::Observer { | 27 public DialRegistry::Observer, |
| 28 public base::RefCountedThreadSafe< | |
| 29 DialMediaSinkService, | |
| 30 content::BrowserThread::DeleteOnIOThread> { | |
| 26 public: | 31 public: |
| 27 DialMediaSinkService(const OnSinksDiscoveredCallback& callback, | 32 DialMediaSinkService(const OnSinksDiscoveredCallback& callback, |
| 28 net::URLRequestContextGetter* request_context); | 33 net::URLRequestContextGetter* request_context); |
| 29 ~DialMediaSinkService() override; | 34 |
| 35 // Stop listening to DIAL device events. | |
| 36 void Stop(); | |
| 30 | 37 |
| 31 // MediaSinkService implementation | 38 // MediaSinkService implementation |
| 32 void Start() override; | 39 void Start() override; |
| 33 | 40 |
| 34 void Stop(); | |
| 35 | |
| 36 protected: | 41 protected: |
| 37 virtual DialRegistry* dial_registry(); | 42 ~DialMediaSinkService() override; |
| 38 | 43 |
| 39 // Returns instance of device description service. Create a new one if none | 44 // Returns instance of device description service. Create a new one if none |
| 40 // exists. | 45 // exists. |
| 41 virtual DeviceDescriptionService* GetDescriptionService(); | 46 DeviceDescriptionService* GetDescriptionService(); |
| 47 | |
| 48 // Does not take ownership of |dial_registry|. | |
| 49 void SetDialRegistryForTest(DialRegistry* dial_registry); | |
| 50 void SetDescriptionServiceForTest( | |
| 51 std::unique_ptr<DeviceDescriptionService> description_service); | |
| 52 void SetTimerForTest(std::unique_ptr<base::Timer> timer); | |
| 42 | 53 |
| 43 private: | 54 private: |
| 55 friend struct content::BrowserThread::DeleteOnThread< | |
| 56 content::BrowserThread::IO>; | |
| 57 friend class base::DeleteHelper<DialMediaSinkService>; | |
| 44 friend class DialMediaSinkServiceTest; | 58 friend class DialMediaSinkServiceTest; |
| 45 FRIEND_TEST_ALL_PREFIXES(DialMediaSinkServiceTest, TestStart); | 59 FRIEND_TEST_ALL_PREFIXES(DialMediaSinkServiceTest, TestStart); |
| 46 FRIEND_TEST_ALL_PREFIXES(DialMediaSinkServiceTest, TestFetchCompleted); | 60 FRIEND_TEST_ALL_PREFIXES(DialMediaSinkServiceTest, TestTimer); |
| 61 FRIEND_TEST_ALL_PREFIXES(DialMediaSinkServiceTest, | |
| 62 TestFetchCompleted_SameSink); | |
| 47 FRIEND_TEST_ALL_PREFIXES(DialMediaSinkServiceTest, TestIsDifferent); | 63 FRIEND_TEST_ALL_PREFIXES(DialMediaSinkServiceTest, TestIsDifferent); |
| 48 FRIEND_TEST_ALL_PREFIXES(DialMediaSinkServiceTest, | 64 FRIEND_TEST_ALL_PREFIXES(DialMediaSinkServiceTest, |
| 49 TestOnDeviceDescriptionAvailable); | 65 TestOnDeviceDescriptionAvailable); |
| 50 | 66 |
| 67 // Start discovery on IO thread. | |
| 68 void StartOnIOThread(); | |
| 69 | |
| 70 // Stop discovery on IO thread. | |
| 71 void StopOnIOThread(); | |
| 72 | |
| 73 // Called when |finish_timer_| expires. | |
| 74 void OnFetchCompletedOnUIThread(); | |
| 75 | |
| 51 // api::dial::DialRegistry::Observer implementation | 76 // api::dial::DialRegistry::Observer implementation |
| 52 void OnDialDeviceEvent(const DialRegistry::DeviceList& devices) override; | 77 void OnDialDeviceEvent(const DialRegistry::DeviceList& devices) override; |
| 53 void OnDialError(DialRegistry::DialErrorCode type) override; | 78 void OnDialError(DialRegistry::DialErrorCode type) override; |
| 54 | 79 |
| 55 // Called when description service successfully fetches and parses device | 80 // Called when description service successfully fetches and parses device |
| 56 // description XML. Restart |finish_timer_| if it is not running. | 81 // description XML. Restart |finish_timer_| if it is not running. |
| 57 void OnDeviceDescriptionAvailable( | 82 void OnDeviceDescriptionAvailable( |
| 58 const DialDeviceData& device_data, | 83 const DialDeviceData& device_data, |
| 59 const ParsedDialDeviceDescription& description_data); | 84 const ParsedDialDeviceDescription& description_data); |
| 60 | 85 |
| 61 // Called when fails to fetch or parse device description XML. | 86 // Called when fails to fetch or parse device description XML. |
| 62 void OnDeviceDescriptionError(const DialDeviceData& device, | 87 void OnDeviceDescriptionError(const DialDeviceData& device, |
| 63 const std::string& error_message); | 88 const std::string& error_message); |
| 64 | 89 |
| 65 // Called when |finish_timer_| expires. | 90 // Called when |finish_timer_| expires and post task on UI thread. |
| 66 void OnFetchCompleted(); | 91 void OnFetchCompleted(); |
| 67 | 92 |
| 68 // Timer for finishing fetching. Starts in |OnDialDeviceEvent()|, and expires | 93 // Timer for finishing fetching. Starts in |OnDialDeviceEvent()|, and expires |
| 69 // 3 seconds later. If |OnDeviceDescriptionAvailable()| is called after | 94 // 3 seconds later. If |OnDeviceDescriptionAvailable()| is called after |
| 70 // |finish_timer_| expires, |finish_timer_| is restarted. | 95 // |finish_timer_| expires, |finish_timer_| is restarted. |
| 71 std::unique_ptr<base::OneShotTimer> finish_timer_; | 96 std::unique_ptr<base::Timer> finish_timer_; |
| 72 | 97 |
| 73 std::unique_ptr<DeviceDescriptionService> description_service_; | 98 std::unique_ptr<DeviceDescriptionService> description_service_; |
|
mark a. foltz
2017/05/09 18:21:13
I didn't see where this was set; is this only set
zhaobin
2017/05/10 18:17:34
It is set in DialMediaSinkService::GetDescriptionS
| |
| 74 | 99 |
| 100 // Raw pointer to DialRegistry singleton. | |
| 101 DialRegistry* dial_registry_ = nullptr; | |
|
mark a. foltz
2017/05/09 18:21:13
What guarantees that the DialRegistry will outlive
zhaobin
2017/05/10 18:17:34
DialRegistry is a Leaky Singleton...
https://cs.c
| |
| 102 | |
| 75 // Sorted sinks from current round of discovery. | 103 // Sorted sinks from current round of discovery. |
| 76 std::set<MediaSinkInternal> current_sinks_; | 104 std::set<MediaSinkInternal> current_sinks_; |
| 77 | 105 |
| 78 // Sorted sinks sent to Media Router Provider in last FetchCompleted(). | 106 // Sorted sinks sent to Media Router Provider in last FetchCompleted(). |
| 79 std::set<MediaSinkInternal> mrp_sinks_; | 107 std::set<MediaSinkInternal> mrp_sinks_; |
| 80 | 108 |
| 81 // Device data list from current round of discovery. | 109 // Device data list from current round of discovery. |
| 82 DialRegistry::DeviceList current_devices_; | 110 DialRegistry::DeviceList current_devices_; |
| 83 | 111 |
| 84 scoped_refptr<net::URLRequestContextGetter> request_context_; | 112 scoped_refptr<net::URLRequestContextGetter> request_context_; |
| 85 }; | 113 }; |
| 86 | 114 |
| 87 } // namespace media_router | 115 } // namespace media_router |
| 88 | 116 |
| 89 #endif // CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_DIAL_DIAL_MEDIA_SINK_SERVICE_H_ | 117 #endif // CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_DIAL_DIAL_MEDIA_SINK_SERVICE_H_ |
| OLD | NEW |