OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/media/router/discovery/dial/dial_media_sink_service.h" | |
6 | |
7 #include "chrome/browser/media/router/discovery/dial/dial_device_data.h" | |
8 #include "chrome/browser/media/router/discovery/dial/dial_media_sink_cache_servi ce.h" | |
9 #include "chrome/browser/profiles/profile.h" | |
10 #include "content/public/browser/browser_thread.h" | |
11 | |
12 using content::BrowserThread; | |
13 | |
14 namespace { | |
15 // The maximum time a response is expected after a M-SEARCH request. | |
mark a. foltz
2017/04/12 00:17:18
Can you say a bit about why this particular value
zhaobin
2017/04/18 06:58:27
It is picked at random and needs to be revised. (I
| |
16 const int kDialResponseTimeoutSecs = 5; | |
17 } | |
18 | |
19 namespace media_router { | |
20 | |
21 DialMediaSinkService::DialMediaSinkService( | |
22 const OnSinksDiscoveredCallback& callback, | |
23 content::BrowserContext* browser_context) | |
24 : MediaSinkService(callback), | |
25 network_disconnected_(false), | |
26 browser_context_(browser_context) { | |
27 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
28 auto* profile = Profile::FromBrowserContext(browser_context_); | |
29 request_context_ = profile->GetRequestContext(); | |
30 | |
31 DCHECK(request_context_); | |
32 } | |
33 | |
34 DialMediaSinkService::~DialMediaSinkService() {} | |
35 | |
36 void DialMediaSinkService::Start() { | |
37 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
38 dial_registry()->RegisterObserver(this); | |
39 dial_registry()->DiscoverNow(); | |
40 } | |
41 | |
42 void DialMediaSinkService::Stop() { | |
43 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
44 dial_registry()->UnregisterObserver(this); | |
45 } | |
46 | |
47 void DialMediaSinkService::AddSinkQuery(MediaSinksObserver* observer) { | |
48 NOTIMPLEMENTED(); | |
mark a. foltz
2017/04/12 00:17:18
Does this need a TODO to go back and implement thi
zhaobin
2017/04/18 06:58:27
Done.
| |
49 } | |
50 | |
51 void DialMediaSinkService::RemoveSinkQuery(MediaSinksObserver* observer) { | |
52 NOTIMPLEMENTED(); | |
mark a. foltz
2017/04/12 00:17:18
Ditto
zhaobin
2017/04/18 06:58:27
Done.
| |
53 } | |
54 | |
55 DialRegistry* DialMediaSinkService::dial_registry() { | |
mark a. foltz
2017/04/12 00:17:18
I'm not sure this method adds much. Is it overrid
zhaobin
2017/04/18 06:58:27
Yes, for unit tests.
| |
56 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
57 return DialRegistry::GetInstance(); | |
58 } | |
59 | |
60 DeviceDescriptionService* DialMediaSinkService::description_service() { | |
61 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
62 if (!description_service_.get()) { | |
63 description_service_.reset(new DeviceDescriptionService( | |
64 base::Bind(&DialMediaSinkCacheService::MayAddOrUpdateSink, | |
65 base::Unretained(cache_service())), | |
66 base::Bind(&DialMediaSinkService::OnDeviceDescriptionError, | |
67 base::Unretained(this)))); | |
68 } | |
69 return description_service_.get(); | |
70 } | |
71 | |
72 DialMediaSinkCacheService* DialMediaSinkService::cache_service() { | |
mark a. foltz
2017/04/12 00:17:18
If this is creating the cache service on demand, i
zhaobin
2017/04/18 06:58:27
Done.
| |
73 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
74 if (!cache_service_.get()) { | |
75 cache_service_.reset(new DialMediaSinkCacheService()); | |
76 } | |
77 return cache_service_.get(); | |
78 } | |
79 | |
80 void DialMediaSinkService::OnDialDeviceEvent( | |
81 const DialRegistry::DeviceList& devices) { | |
82 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
83 DVLOG(2) << "DialMediaSinkService::OnDialDeviceEvent found " << devices.size() | |
84 << " devices"; | |
85 | |
86 network_disconnected_ = false; | |
imcheng
2017/04/12 19:33:51
This field doesn't seem to be used.
zhaobin
2017/04/18 06:58:27
Code removed.
| |
87 | |
88 // Add a finish timer. | |
89 base::TimeDelta finish_delay = | |
90 base::TimeDelta::FromSeconds(kDialResponseTimeoutSecs); | |
91 finish_timer_.Start(FROM_HERE, finish_delay, this, | |
92 &DialMediaSinkService::FetchCompleted); | |
93 | |
94 description_service()->GetDeviceDescriptions(devices, request_context_); | |
mark a. foltz
2017/04/12 00:17:18
What happens to device descriptions fetched after
zhaobin
2017/04/18 06:58:27
They go into CacheService, and will be returned in
| |
95 } | |
96 | |
97 void DialMediaSinkService::OnDialError(DialRegistry::DialErrorCode type) { | |
98 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
99 DVLOG(2) << "OnDialError [DialErrorCode]: " << static_cast<int>(type); | |
100 | |
101 if (type == DialRegistry::DIAL_NETWORK_DISCONNECTED) | |
102 network_disconnected_ = true; | |
103 } | |
104 | |
105 void DialMediaSinkService::OnDeviceDescriptionError( | |
106 const std::string& device_label, | |
107 const std::string& error_message) { | |
108 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
109 DVLOG(2) << "OnDescriptionFetchesError [message]: " << error_message; | |
110 } | |
111 | |
112 void DialMediaSinkService::FetchCompleted() { | |
113 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
114 DCHECK(!sink_discovery_callback_.is_null()); | |
115 | |
116 finish_timer_.Stop(); | |
117 | |
118 auto sinks = cache_service()->GetAliveSinks(); | |
119 if (!IsDifferent(sinks, mrp_sinks_)) { | |
mark a. foltz
2017/04/12 00:17:18
Can this just be !(sinks == mrp_sinks_), if we enf
zhaobin
2017/04/18 06:58:27
Done.
| |
120 DVLOG(2) << "No update to sink list."; | |
121 return; | |
122 } | |
123 | |
124 DVLOG(2) << "Send sinks to media router, [size]: " << sinks.size(); | |
125 sink_discovery_callback_.Run(sinks); | |
126 mrp_sinks_ = sinks; | |
127 } | |
128 | |
129 bool DialMediaSinkService::IsDifferent( | |
130 const std::vector<MediaSinkInternal> new_sinks, | |
131 const std::vector<MediaSinkInternal> old_sinks) { | |
132 std::map<std::string, const MediaSinkInternal*> old_sink_map; | |
133 for (auto& sink : old_sinks) | |
134 old_sink_map.insert(std::make_pair(sink.sink().id(), &sink)); | |
135 | |
136 std::vector<const MediaSinkInternal*> added; | |
137 std::vector<const MediaSinkInternal*> updated; | |
138 | |
139 for (auto& sink : new_sinks) { | |
140 const auto& old_sink_it = old_sink_map.find(sink.sink().id()); | |
141 // If |sink| does not exist in |old_sinks|. | |
142 if (old_sink_it == old_sink_map.end()) { | |
143 added.push_back(&sink); | |
144 continue; | |
145 } | |
146 | |
147 if (sink != *(old_sink_it->second)) | |
148 updated.push_back(&sink); | |
149 | |
150 old_sink_map.erase(old_sink_it); | |
151 } | |
152 | |
153 return !added.empty() || !old_sink_map.empty() || !updated.empty(); | |
154 } | |
155 | |
156 } // namespace media_router | |
OLD | NEW |