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

Side by Side Diff: chrome/browser/ui/ash/cast_config_delegate_media_router.cc

Issue 2525563003: mash: Change CastConfigDelegate to a mojoified CastConfigClient. (Closed)
Patch Set: move dep to chromeos section Created 4 years 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
(Empty)
1 // Copyright 2015 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/ui/ash/cast_config_delegate_media_router.h"
6
7 #include <string>
8 #include <vector>
9
10 #include "base/macros.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/media/router/media_router.h"
15 #include "chrome/browser/media/router/media_router_factory.h"
16 #include "chrome/browser/media/router/media_router_feature.h"
17 #include "chrome/browser/media/router/media_routes_observer.h"
18 #include "chrome/browser/media/router/media_sinks_observer.h"
19 #include "chrome/browser/media/router/media_source_helper.h"
20 #include "chrome/browser/profiles/profile_manager.h"
21 #include "chrome/common/url_constants.h"
22 #include "content/public/browser/notification_service.h"
23 #include "content/public/browser/notification_source.h"
24
25 namespace {
26
27 media_router::MediaRouter* media_router_for_test_ = nullptr;
28
29 // Returns the MediaRouter instance for the current primary profile.
30 media_router::MediaRouter* GetMediaRouter() {
31 if (media_router_for_test_)
32 return media_router_for_test_;
33
34 auto* router = media_router::MediaRouterFactory::GetApiForBrowserContext(
35 ProfileManager::GetPrimaryUserProfile());
36 DCHECK(router);
37 return router;
38 }
39
40 // The media router will sometimes append " (Tab)" to the tab title. This
41 // function will remove that data from the inout param |string|.
42 std::string StripEndingTab(const std::string& str) {
43 static const char ending[] = " (Tab)";
44 if (base::EndsWith(str, ending, base::CompareCase::SENSITIVE))
45 return str.substr(0, str.size() - strlen(ending));
46 return str;
47 }
48
49 } // namespace
50
51 // This class caches the values that the observers give us so we can query them
52 // at any point in time. It also emits a device refresh event when new data is
53 // available.
54 class CastDeviceCache : public media_router::MediaRoutesObserver,
55 public media_router::MediaSinksObserver {
56 public:
57 using MediaSinks = std::vector<media_router::MediaSink>;
58 using MediaRoutes = std::vector<media_router::MediaRoute>;
59 using MediaRouteIds = std::vector<media_router::MediaRoute::Id>;
60
61 explicit CastDeviceCache(ash::CastConfigDelegate* cast_config_delegate);
62 ~CastDeviceCache() override;
63
64 // This may call cast_config_delegate->RequestDeviceRefresh() before
65 // returning.
66 void Init();
67
68 const MediaSinks& sinks() const { return sinks_; }
69 const MediaRoutes& routes() const { return routes_; }
70
71 private:
72 // media_router::MediaSinksObserver:
73 void OnSinksReceived(const MediaSinks& sinks) override;
74
75 // media_router::MediaRoutesObserver:
76 void OnRoutesUpdated(const MediaRoutes& routes,
77 const MediaRouteIds& unused_joinable_route_ids) override;
78
79 MediaSinks sinks_;
80 MediaRoutes routes_;
81
82 // Not owned.
83 ash::CastConfigDelegate* cast_config_delegate_;
84
85 DISALLOW_COPY_AND_ASSIGN(CastDeviceCache);
86 };
87
88 CastDeviceCache::CastDeviceCache(ash::CastConfigDelegate* cast_config_delegate)
89 : MediaRoutesObserver(GetMediaRouter()),
90 MediaSinksObserver(GetMediaRouter(),
91 media_router::MediaSourceForDesktop(),
92 GURL(chrome::kChromeUIMediaRouterURL)),
93 cast_config_delegate_(cast_config_delegate) {}
94
95 CastDeviceCache::~CastDeviceCache() {}
96
97 void CastDeviceCache::Init() {
98 CHECK(MediaSinksObserver::Init());
99 }
100
101 void CastDeviceCache::OnSinksReceived(const MediaSinks& sinks) {
102 sinks_.clear();
103 for (const media_router::MediaSink& sink : sinks) {
104 // The media router adds a MediaSink instance that doesn't have a name. Make
105 // sure to filter that sink out from the UI so it is not rendered, as it
106 // will be a line that only has a icon with no apparent meaning.
107 if (sink.name().empty())
108 continue;
109
110 // Hide all sinks which have a domain (ie, castouts) to meet privacy
111 // requirements. This will be enabled once UI can display the domain. See
112 // crbug.com/624016.
113 if (!sink.domain().empty())
114 continue;
115
116 sinks_.push_back(sink);
117 }
118
119 cast_config_delegate_->RequestDeviceRefresh();
120 }
121
122 void CastDeviceCache::OnRoutesUpdated(
123 const MediaRoutes& routes,
124 const MediaRouteIds& unused_joinable_route_ids) {
125 routes_ = routes;
126 cast_config_delegate_->RequestDeviceRefresh();
127 }
128
129 ////////////////////////////////////////////////////////////////////////////////
130 // CastConfigDelegateMediaRouter:
131
132 void CastConfigDelegateMediaRouter::SetMediaRouterForTest(
133 media_router::MediaRouter* media_router) {
134 media_router_for_test_ = media_router;
135 }
136
137 CastConfigDelegateMediaRouter::CastConfigDelegateMediaRouter() {
138 // TODO(jdufault): This should use a callback interface once there is an
139 // equivalent. See crbug.com/666005.
140 registrar_.Add(this, chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED,
141 content::NotificationService::AllSources());
142 }
143
144 CastConfigDelegateMediaRouter::~CastConfigDelegateMediaRouter() {}
145
146 CastDeviceCache* CastConfigDelegateMediaRouter::devices() {
147 // The CastDeviceCache instance is lazily allocated because the MediaRouter
148 // component is not ready when the constructor is invoked.
149 if (!devices_ && GetMediaRouter()) {
150 devices_ = base::MakeUnique<CastDeviceCache>(this);
151 devices_->Init();
152 }
153
154 return devices_.get();
155 }
156
157 void CastConfigDelegateMediaRouter::RequestDeviceRefresh() {
158 // The media router component isn't ready yet.
159 if (!devices())
160 return;
161
162 // Build the old-style SinkAndRoute set out of the MediaRouter
163 // source/sink/route setup. We first map the existing sinks, and then we
164 // update those sinks with activity information.
165
166 SinksAndRoutes items;
167
168 for (const media_router::MediaSink& sink : devices()->sinks()) {
169 SinkAndRoute sr;
170 sr.sink.id = sink.id();
171 sr.sink.name = base::UTF8ToUTF16(sink.name());
172 sr.sink.domain = base::UTF8ToUTF16(sink.domain());
173 items.push_back(sr);
174 }
175
176 for (const media_router::MediaRoute& route : devices()->routes()) {
177 if (!route.for_display())
178 continue;
179
180 for (SinkAndRoute& item : items) {
181 if (item.sink.id == route.media_sink_id()) {
182 item.route.id = route.media_route_id();
183 item.route.title =
184 base::UTF8ToUTF16(StripEndingTab(route.description()));
185 item.route.is_local_source = route.is_local();
186
187 // Default to a tab/app capture. This will display the media router
188 // description. This means we will properly support DIAL casts.
189 item.route.content_source = Route::ContentSource::TAB;
190 if (media_router::IsDesktopMirroringMediaSource(route.media_source()))
191 item.route.content_source = Route::ContentSource::DESKTOP;
192
193 break;
194 }
195 }
196 }
197
198 for (ash::CastConfigDelegate::Observer& observer : observer_list_)
199 observer.OnDevicesUpdated(items);
200 }
201
202 void CastConfigDelegateMediaRouter::CastToSink(const Sink& sink) {
203 // TODO(imcheng): Pass in tab casting timeout.
204 GetMediaRouter()->CreateRoute(
205 media_router::MediaSourceForDesktop().id(), sink.id,
206 GURL("http://cros-cast-origin/"), nullptr,
207 std::vector<media_router::MediaRouteResponseCallback>(),
208 base::TimeDelta(), false);
209 }
210
211 void CastConfigDelegateMediaRouter::StopCasting(const Route& route) {
212 GetMediaRouter()->TerminateRoute(route.id);
213 }
214
215 void CastConfigDelegateMediaRouter::AddObserver(
216 ash::CastConfigDelegate::Observer* observer) {
217 observer_list_.AddObserver(observer);
218 }
219
220 void CastConfigDelegateMediaRouter::RemoveObserver(
221 ash::CastConfigDelegate::Observer* observer) {
222 observer_list_.RemoveObserver(observer);
223 }
224
225 void CastConfigDelegateMediaRouter::Observe(
226 int type,
227 const content::NotificationSource& source,
228 const content::NotificationDetails& details) {
229 switch (type) {
230 case chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED:
231 // The active profile has changed, which means that the media router has
232 // as well. Reset the device cache to ensure we are using up-to-date
233 // object instances.
234 devices_.reset();
235 RequestDeviceRefresh();
236 break;
237 }
238 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/ash/cast_config_delegate_media_router.h ('k') | chrome/browser/ui/ash/system_tray_delegate_chromeos.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698