Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(208)

Side by Side Diff: chrome/browser/media/router/discovery/dial/dial_media_sink_service.cc

Issue 2868263002: [Media Router] Add some SetForTest() functions to DialMediaSinkService and update unit tests (Closed)
Patch Set: resolve code review comments from Kevin and remove discovery_start_ flag Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include "chrome/browser/media/router/discovery/dial/dial_media_sink_service.h" 5 #include "chrome/browser/media/router/discovery/dial/dial_media_sink_service.h"
6 6
7 #include "chrome/browser/media/router/discovery/dial/dial_device_data.h" 7 #include "chrome/browser/media/router/discovery/dial/dial_device_data.h"
8 #include "chrome/browser/profiles/profile.h" 8 #include "chrome/browser/profiles/profile.h"
9 #include "content/public/browser/browser_thread.h" 9 #include "content/public/browser/browser_thread.h"
10 #include "net/url_request/url_request_context_getter.h" 10 #include "net/url_request/url_request_context_getter.h"
11 11
12 using content::BrowserThread; 12 using content::BrowserThread;
13 13
14 namespace { 14 namespace {
15 // Time interval when media sink service sends sinks to MRP. 15 // Time interval when media sink service sends sinks to MRP.
16 const int kFetchCompleteTimeoutSecs = 3; 16 const int kFetchCompleteTimeoutSecs = 3;
17 } 17 }
18 18
19 namespace media_router { 19 namespace media_router {
20 20
21 DialMediaSinkService::DialMediaSinkService( 21 DialMediaSinkService::DialMediaSinkService(
22 const OnSinksDiscoveredCallback& callback, 22 const OnSinksDiscoveredCallback& callback,
23 net::URLRequestContextGetter* request_context) 23 net::URLRequestContextGetter* request_context)
24 : MediaSinkService(callback), request_context_(request_context) { 24 : MediaSinkService(callback), request_context_(request_context) {
25 DCHECK_CURRENTLY_ON(BrowserThread::IO); 25 DCHECK_CURRENTLY_ON(BrowserThread::IO);
26 DCHECK(request_context_); 26 DCHECK(request_context_);
27 } 27 }
28 28
29 DialMediaSinkService::~DialMediaSinkService() {} 29 DialMediaSinkService::~DialMediaSinkService() {
30 DCHECK_CURRENTLY_ON(BrowserThread::IO);
31 Stop();
32 }
30 33
31 void DialMediaSinkService::Start() { 34 void DialMediaSinkService::Start() {
32 DCHECK_CURRENTLY_ON(BrowserThread::IO); 35 DCHECK_CURRENTLY_ON(BrowserThread::IO);
33 dial_registry()->RegisterObserver(this); 36 if (dial_registry_)
34 dial_registry()->StartPeriodicDiscovery(); 37 return;
38
39 dial_registry_ = DialRegistry::GetInstance();
40 dial_registry_->RegisterObserver(this);
41 dial_registry_->OnListenerAdded();
35 } 42 }
36 43
37 void DialMediaSinkService::Stop() { 44 void DialMediaSinkService::Stop() {
38 DCHECK_CURRENTLY_ON(BrowserThread::IO); 45 DCHECK_CURRENTLY_ON(BrowserThread::IO);
39 dial_registry()->UnregisterObserver(this); 46 if (!dial_registry_)
40 } 47 return;
41 48
42 DialRegistry* DialMediaSinkService::dial_registry() { 49 dial_registry_->OnListenerRemoved();
43 DCHECK_CURRENTLY_ON(BrowserThread::IO); 50 dial_registry_->UnregisterObserver(this);
44 return DialRegistry::GetInstance(); 51 dial_registry_ = nullptr;
45 } 52 }
46 53
47 DeviceDescriptionService* DialMediaSinkService::GetDescriptionService() { 54 DeviceDescriptionService* DialMediaSinkService::GetDescriptionService() {
48 DCHECK_CURRENTLY_ON(BrowserThread::IO); 55 DCHECK_CURRENTLY_ON(BrowserThread::IO);
49 if (!description_service_.get()) { 56 if (!description_service_.get()) {
50 description_service_.reset(new DeviceDescriptionService( 57 description_service_.reset(new DeviceDescriptionService(
51 base::Bind(&DialMediaSinkService::OnDeviceDescriptionAvailable, 58 base::Bind(&DialMediaSinkService::OnDeviceDescriptionAvailable,
52 base::Unretained(this)), 59 base::Unretained(this)),
53 base::Bind(&DialMediaSinkService::OnDeviceDescriptionError, 60 base::Bind(&DialMediaSinkService::OnDeviceDescriptionError,
54 base::Unretained(this)))); 61 base::Unretained(this))));
55 } 62 }
56 return description_service_.get(); 63 return description_service_.get();
57 } 64 }
58 65
66 void DialMediaSinkService::SetDialRegistryForTest(DialRegistry* dial_registry) {
67 dial_registry_ = dial_registry;
mark a. foltz 2017/05/11 23:12:14 DCHECK(!dial_registry_) before assigning? I assum
zhaobin 2017/05/12 00:36:09 Done.
68 if (dial_registry_) {
mark a. foltz 2017/05/11 23:12:14 When would it be okay to pass a nullptr? Should th
zhaobin 2017/05/12 00:36:09 Done.
69 dial_registry_->RegisterObserver(this);
70 dial_registry_->OnListenerAdded();
71 }
72 }
73
74 void DialMediaSinkService::SetDescriptionServiceForTest(
75 std::unique_ptr<DeviceDescriptionService> description_service) {
76 description_service_ = std::move(description_service);
mark a. foltz 2017/05/11 23:12:14 It seems like you don't want the test to call GetD
zhaobin 2017/05/12 00:36:09 Done.
77 }
78
79 void DialMediaSinkService::SetTimerForTest(std::unique_ptr<base::Timer> timer) {
80 finish_timer_ = std::move(timer);
mark a. foltz 2017/05/11 23:12:14 Similar comment - you don't want the test to start
zhaobin 2017/05/12 00:36:09 Done.
81 }
82
59 void DialMediaSinkService::OnDialDeviceEvent( 83 void DialMediaSinkService::OnDialDeviceEvent(
60 const DialRegistry::DeviceList& devices) { 84 const DialRegistry::DeviceList& devices) {
61 DCHECK_CURRENTLY_ON(BrowserThread::IO); 85 DCHECK_CURRENTLY_ON(BrowserThread::IO);
62 DVLOG(2) << "DialMediaSinkService::OnDialDeviceEvent found " << devices.size() 86 DVLOG(2) << "DialMediaSinkService::OnDialDeviceEvent found " << devices.size()
63 << " devices"; 87 << " devices";
64 88
65 // Add a finish timer. 89 StartTimer();
66 finish_timer_.reset(new base::OneShotTimer());
67 base::TimeDelta finish_delay =
68 base::TimeDelta::FromSeconds(kFetchCompleteTimeoutSecs);
69 finish_timer_->Start(FROM_HERE, finish_delay, this,
70 &DialMediaSinkService::OnFetchCompleted);
71 90
72 current_sinks_.clear(); 91 current_sinks_.clear();
73 current_devices_ = devices; 92 current_devices_ = devices;
74 93
75 GetDescriptionService()->GetDeviceDescriptions(devices, 94 GetDescriptionService()->GetDeviceDescriptions(devices,
76 request_context_.get()); 95 request_context_.get());
77 } 96 }
78 97
79 void DialMediaSinkService::OnDialError(DialRegistry::DialErrorCode type) { 98 void DialMediaSinkService::OnDialError(DialRegistry::DialErrorCode type) {
80 DCHECK_CURRENTLY_ON(BrowserThread::IO); 99 DCHECK_CURRENTLY_ON(BrowserThread::IO);
(...skipping 19 matching lines...) Expand all
100 extra_data.app_url = description_data.app_url; 119 extra_data.app_url = description_data.app_url;
101 extra_data.model_name = description_data.model_name; 120 extra_data.model_name = description_data.model_name;
102 std::string ip_address = device_data.device_description_url().host(); 121 std::string ip_address = device_data.device_description_url().host();
103 if (!extra_data.ip_address.AssignFromIPLiteral(ip_address)) { 122 if (!extra_data.ip_address.AssignFromIPLiteral(ip_address)) {
104 DVLOG(1) << "Invalid ip_address: " << ip_address; 123 DVLOG(1) << "Invalid ip_address: " << ip_address;
105 return; 124 return;
106 } 125 }
107 126
108 current_sinks_.insert(MediaSinkInternal(sink, extra_data)); 127 current_sinks_.insert(MediaSinkInternal(sink, extra_data));
109 128
110 if (finish_timer_)
111 return;
112
113 // Start fetch timer again if device description comes back after 129 // Start fetch timer again if device description comes back after
114 // |finish_timer_| fires. 130 // |finish_timer_| fires.
115 base::TimeDelta finish_delay = 131 DCHECK(finish_timer_);
mark a. foltz 2017/05/11 23:12:13 Is it possible for this to fail? It looks like th
zhaobin 2017/05/12 00:36:09 Done.
116 base::TimeDelta::FromSeconds(kFetchCompleteTimeoutSecs); 132 if (!finish_timer_->IsRunning())
117 finish_timer_.reset(new base::OneShotTimer()); 133 StartTimer();
118 finish_timer_->Start(FROM_HERE, finish_delay, this,
119 &DialMediaSinkService::OnFetchCompleted);
120 } 134 }
121 135
122 void DialMediaSinkService::OnDeviceDescriptionError( 136 void DialMediaSinkService::OnDeviceDescriptionError(
123 const DialDeviceData& device, 137 const DialDeviceData& device,
124 const std::string& error_message) { 138 const std::string& error_message) {
125 DCHECK_CURRENTLY_ON(BrowserThread::IO); 139 DCHECK_CURRENTLY_ON(BrowserThread::IO);
126 DVLOG(2) << "OnDescriptionFetchesError [message]: " << error_message; 140 DVLOG(2) << "OnDescriptionFetchesError [message]: " << error_message;
127 } 141 }
128 142
129 void DialMediaSinkService::OnFetchCompleted() { 143 void DialMediaSinkService::OnFetchCompleted() {
130 DCHECK_CURRENTLY_ON(BrowserThread::IO); 144 DCHECK_CURRENTLY_ON(BrowserThread::IO);
mark a. foltz 2017/05/11 23:12:14 Super nit: This is only called from a callback pas
zhaobin 2017/05/12 00:36:09 Done.
131 DCHECK(!sink_discovery_callback_.is_null()); 145 DCHECK(!sink_discovery_callback_.is_null());
132 146
133 finish_timer_.reset(); 147 if (current_sinks_ == mrp_sinks_) {
134
135 auto sinks = current_sinks_;
136 if (sinks == mrp_sinks_) {
137 DVLOG(2) << "No update to sink list."; 148 DVLOG(2) << "No update to sink list.";
138 return; 149 return;
139 } 150 }
140 151
141 DVLOG(2) << "Send sinks to media router, [size]: " << sinks.size(); 152 DVLOG(2) << "Send sinks to media router, [size]: " << current_sinks_.size();
142 sink_discovery_callback_.Run( 153 sink_discovery_callback_.Run(std::vector<MediaSinkInternal>(
143 std::vector<MediaSinkInternal>(sinks.begin(), sinks.end())); 154 current_sinks_.begin(), current_sinks_.end()));
144 mrp_sinks_ = std::move(sinks); 155 mrp_sinks_ = current_sinks_;
156 }
157
158 void DialMediaSinkService::StartTimer() {
159 DCHECK_CURRENTLY_ON(BrowserThread::IO);
mark a. foltz 2017/05/11 23:12:14 Similar nit: This is only called from within this
zhaobin 2017/05/12 00:36:09 Done.
160 // Create a finish timer.
161 if (!finish_timer_)
162 finish_timer_.reset(new base::OneShotTimer());
163
164 base::TimeDelta finish_delay =
165 base::TimeDelta::FromSeconds(kFetchCompleteTimeoutSecs);
166 finish_timer_->Start(FROM_HERE, finish_delay,
167 base::Bind(&DialMediaSinkService::OnFetchCompleted,
168 base::Unretained(this)));
145 } 169 }
146 170
147 } // namespace media_router 171 } // namespace media_router
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698