1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/ash/cast_config_delegate_media_router.h"
#include <string>
#include <vector>
#include "base/macros.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/media/router/media_router.h"
#include "chrome/browser/media/router/media_router_factory.h"
#include "chrome/browser/media/router/media_router_feature.h"
#include "chrome/browser/media/router/media_routes_observer.h"
#include "chrome/browser/media/router/media_sinks_observer.h"
#include "chrome/browser/media/router/media_source_helper.h"
#include "chrome/browser/profiles/profile_manager.h"
namespace {
media_router::MediaRouter* media_router_for_test_ = nullptr;
media_router::MediaRouter* GetMediaRouter() {
if (media_router_for_test_)
return media_router_for_test_;
auto* router = media_router::MediaRouterFactory::GetApiForBrowserContext(
ProfileManager::GetPrimaryUserProfile());
DCHECK(router);
return router;
}
// The media router will sometimes append " (Tab)" to the tab title. This
// function will remove that data from the inout param |string|.
std::string StripEndingTab(const std::string& str) {
static const char ending[] = " (Tab)";
if (base::EndsWith(str, ending, base::CompareCase::SENSITIVE))
return str.substr(0, str.size() - strlen(ending));
return str;
}
} // namespace
// This class caches the values that the observers give us so we can query them
// at any point in time. It also emits a device refresh event when new data is
// available.
class CastDeviceCache : public media_router::MediaRoutesObserver,
public media_router::MediaSinksObserver {
public:
using MediaSinks = std::vector<media_router::MediaSink>;
using MediaRoutes = std::vector<media_router::MediaRoute>;
using MediaRouteIds = std::vector<media_router::MediaRoute::Id>;
explicit CastDeviceCache(ash::CastConfigDelegate* cast_config_delegate);
~CastDeviceCache() override;
// This may call cast_config_delegate->RequestDeviceRefresh() before
// returning.
void Init();
const MediaSinks& sinks() const { return sinks_; }
const MediaRoutes& routes() const { return routes_; }
private:
// media_router::MediaSinksObserver:
void OnSinksReceived(const MediaSinks& sinks) override;
// media_router::MediaRoutesObserver:
void OnRoutesUpdated(const MediaRoutes& routes,
const MediaRouteIds& unused_joinable_route_ids) override;
MediaSinks sinks_;
MediaRoutes routes_;
// Not owned.
ash::CastConfigDelegate* cast_config_delegate_;
DISALLOW_COPY_AND_ASSIGN(CastDeviceCache);
};
CastDeviceCache::CastDeviceCache(ash::CastConfigDelegate* cast_config_delegate)
: MediaRoutesObserver(GetMediaRouter()),
MediaSinksObserver(GetMediaRouter(),
media_router::MediaSourceForDesktop()),
cast_config_delegate_(cast_config_delegate) {
}
CastDeviceCache::~CastDeviceCache() {}
void CastDeviceCache::Init() {
CHECK(MediaSinksObserver::Init());
}
void CastDeviceCache::OnSinksReceived(const MediaSinks& sinks) {
sinks_ = sinks;
cast_config_delegate_->RequestDeviceRefresh();
}
void CastDeviceCache::OnRoutesUpdated(
const MediaRoutes& routes,
const MediaRouteIds& unused_joinable_route_ids) {
routes_ = routes;
cast_config_delegate_->RequestDeviceRefresh();
}
////////////////////////////////////////////////////////////////////////////////
// CastConfigDelegateMediaRouter:
// static
bool CastConfigDelegateMediaRouter::IsEnabled() {
return media_router::MediaRouterEnabled(
ProfileManager::GetPrimaryUserProfile()) ||
media_router_for_test_;
}
void CastConfigDelegateMediaRouter::SetMediaRouterForTest(
media_router::MediaRouter* media_router) {
media_router_for_test_ = media_router;
}
CastConfigDelegateMediaRouter::CastConfigDelegateMediaRouter() {}
CastConfigDelegateMediaRouter::~CastConfigDelegateMediaRouter() {}
CastDeviceCache* CastConfigDelegateMediaRouter::devices() {
// The CastDeviceCache instance is lazily allocated because the MediaRouter
// component is not ready when the constructor is invoked.
if (!devices_ && GetMediaRouter() != nullptr) {
devices_.reset(new CastDeviceCache(this));
devices_->Init();
}
return devices_.get();
}
bool CastConfigDelegateMediaRouter::HasCastExtension() const {
return true;
}
void CastConfigDelegateMediaRouter::RequestDeviceRefresh() {
// The media router component isn't ready yet.
if (!devices())
return;
// Build the old-style ReceiverAndActivity set out of the MediaRouter
// source/sink/route setup. We first map the existing sinks, and then we
// update those sinks with activity information.
ReceiversAndActivities items;
for (const media_router::MediaSink& sink : devices()->sinks()) {
ReceiverAndActivity ra;
ra.receiver.id = sink.id();
ra.receiver.name = base::UTF8ToUTF16(sink.name());
items.push_back(ra);
}
for (const media_router::MediaRoute& route : devices()->routes()) {
if (!route.for_display())
continue;
for (ReceiverAndActivity& item : items) {
if (item.receiver.id == route.media_sink_id()) {
item.activity.id = route.media_route_id();
item.activity.title =
base::UTF8ToUTF16(StripEndingTab(route.description()));
item.activity.is_local_source = route.is_local();
if (route.is_local()) {
// TODO(jdufault): Once the extension backend is removed, we can
// remove tab_id and specify the Desktop/Tab capture directly.
// crbug.com/551132.
// TODO(jdufault): We currently don't actually display DIAL casts to
// the user even though we have all the information necessary. We'll
// do this once the extension backend is gone because supporting both
// introduces extra complexity. crbug.com/551132.
// Default to a tab/app capture. This will display the media router
// description. This means we will properly support DIAL casts.
item.activity.tab_id = 0;
if (media_router::IsDesktopMirroringMediaSource(route.media_source()))
item.activity.tab_id = Activity::TabId::DESKTOP;
}
break;
}
}
}
FOR_EACH_OBSERVER(ash::CastConfigDelegate::Observer, observer_list_,
OnDevicesUpdated(items));
}
void CastConfigDelegateMediaRouter::CastToReceiver(
const std::string& receiver_id) {
// TODO(imcheng): Pass in tab casting timeout.
GetMediaRouter()->CreateRoute(
media_router::MediaSourceForDesktop().id(), receiver_id,
GURL("http://cros-cast-origin/"), nullptr,
std::vector<media_router::MediaRouteResponseCallback>(),
base::TimeDelta(), false);
}
void CastConfigDelegateMediaRouter::StopCasting(const std::string& route_id) {
GetMediaRouter()->TerminateRoute(route_id);
}
bool CastConfigDelegateMediaRouter::HasOptions() const {
// There are no plans to have an options page for the MediaRouter.
return false;
}
void CastConfigDelegateMediaRouter::LaunchCastOptions() {}
void CastConfigDelegateMediaRouter::AddObserver(
ash::CastConfigDelegate::Observer* observer) {
observer_list_.AddObserver(observer);
}
void CastConfigDelegateMediaRouter::RemoveObserver(
ash::CastConfigDelegate::Observer* observer) {
observer_list_.RemoveObserver(observer);
}
|