OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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/ui/webui/media_router/media_router_ui.h" | 5 #include "chrome/browser/ui/webui/media_router/media_router_ui.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
| 9 #include "base/guid.h" |
9 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 11 #include "base/strings/utf_string_conversions.h" |
10 #include "chrome/browser/media/router/create_presentation_session_request.h" | 12 #include "chrome/browser/media/router/create_presentation_session_request.h" |
11 #include "chrome/browser/media/router/issue.h" | 13 #include "chrome/browser/media/router/issue.h" |
12 #include "chrome/browser/media/router/issues_observer.h" | 14 #include "chrome/browser/media/router/issues_observer.h" |
13 #include "chrome/browser/media/router/media_route.h" | 15 #include "chrome/browser/media/router/media_route.h" |
14 #include "chrome/browser/media/router/media_router.h" | 16 #include "chrome/browser/media/router/media_router.h" |
15 #include "chrome/browser/media/router/media_router_factory.h" | 17 #include "chrome/browser/media/router/media_router_factory.h" |
16 #include "chrome/browser/media/router/media_router_mojo_impl.h" | 18 #include "chrome/browser/media/router/media_router_mojo_impl.h" |
17 #include "chrome/browser/media/router/media_routes_observer.h" | 19 #include "chrome/browser/media/router/media_routes_observer.h" |
18 #include "chrome/browser/media/router/media_sink.h" | 20 #include "chrome/browser/media/router/media_sink.h" |
19 #include "chrome/browser/media/router/media_sinks_observer.h" | 21 #include "chrome/browser/media/router/media_sinks_observer.h" |
20 #include "chrome/browser/media/router/media_source.h" | 22 #include "chrome/browser/media/router/media_source.h" |
21 #include "chrome/browser/media/router/media_source_helper.h" | 23 #include "chrome/browser/media/router/media_source_helper.h" |
22 #include "chrome/browser/media/router/presentation_service_delegate_impl.h" | 24 #include "chrome/browser/media/router/presentation_service_delegate_impl.h" |
23 #include "chrome/browser/profiles/profile.h" | 25 #include "chrome/browser/profiles/profile.h" |
24 #include "chrome/browser/sessions/session_tab_helper.h" | 26 #include "chrome/browser/sessions/session_tab_helper.h" |
25 #include "chrome/browser/ui/webui/media_router/media_router_localized_strings_pr
ovider.h" | 27 #include "chrome/browser/ui/webui/media_router/media_router_localized_strings_pr
ovider.h" |
26 #include "chrome/browser/ui/webui/media_router/media_router_resources_provider.h
" | 28 #include "chrome/browser/ui/webui/media_router/media_router_resources_provider.h
" |
27 #include "chrome/browser/ui/webui/media_router/media_router_webui_message_handle
r.h" | 29 #include "chrome/browser/ui/webui/media_router/media_router_webui_message_handle
r.h" |
28 #include "chrome/common/url_constants.h" | 30 #include "chrome/common/url_constants.h" |
| 31 #include "chrome/grit/generated_resources.h" |
29 #include "content/public/browser/web_contents.h" | 32 #include "content/public/browser/web_contents.h" |
30 #include "content/public/browser/web_ui.h" | 33 #include "content/public/browser/web_ui.h" |
31 #include "content/public/browser/web_ui_data_source.h" | 34 #include "content/public/browser/web_ui_data_source.h" |
| 35 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| 36 #include "ui/base/l10n/l10n_util.h" |
32 #include "ui/web_dialogs/web_dialog_delegate.h" | 37 #include "ui/web_dialogs/web_dialog_delegate.h" |
33 | 38 |
34 namespace media_router { | 39 namespace media_router { |
35 | 40 |
36 namespace { | 41 namespace { |
37 | 42 |
| 43 // The amount of time to wait for a response when creating a new route. |
| 44 const int kCreateRouteTimeoutSeconds = 20; |
| 45 |
38 std::string GetHostFromURL(const GURL& gurl) { | 46 std::string GetHostFromURL(const GURL& gurl) { |
39 if (gurl.is_empty()) | 47 if (gurl.is_empty()) |
40 return std::string(); | 48 return std::string(); |
41 std::string host = gurl.host(); | 49 std::string host = gurl.host(); |
42 if (base::StartsWith(host, "www.", base::CompareCase::INSENSITIVE_ASCII)) | 50 if (base::StartsWith(host, "www.", base::CompareCase::INSENSITIVE_ASCII)) |
43 host = host.substr(4); | 51 host = host.substr(4); |
44 return host; | 52 return host; |
45 } | 53 } |
46 | 54 |
| 55 std::string GetTruncatedHostFromURL(const GURL& gurl) { |
| 56 std::string host = GetHostFromURL(gurl); |
| 57 |
| 58 const std::string truncated = |
| 59 net::registry_controlled_domains::GetDomainAndRegistry( |
| 60 host, |
| 61 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); |
| 62 // The truncation will be empty in some scenarios (e.g. host is |
| 63 // simply an IP address). Fail gracefully. |
| 64 if (truncated.empty()) |
| 65 return host; |
| 66 return truncated; |
| 67 } |
| 68 |
47 } // namespace | 69 } // namespace |
48 | 70 |
49 // This class calls to refresh the UI when the highest priority issue is | 71 // This class calls to refresh the UI when the highest priority issue is |
50 // updated. | 72 // updated. |
51 class MediaRouterUI::UIIssuesObserver : public IssuesObserver { | 73 class MediaRouterUI::UIIssuesObserver : public IssuesObserver { |
52 public: | 74 public: |
53 UIIssuesObserver(MediaRouter* router, MediaRouterUI* ui) | 75 UIIssuesObserver(MediaRouter* router, MediaRouterUI* ui) |
54 : IssuesObserver(router), ui_(ui) { | 76 : IssuesObserver(router), ui_(ui) { |
55 DCHECK(ui); | 77 DCHECK(ui); |
56 } | 78 } |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 } | 117 } |
96 } | 118 } |
97 | 119 |
98 callback_.Run(routes_for_display); | 120 callback_.Run(routes_for_display); |
99 } | 121 } |
100 | 122 |
101 MediaRouterUI::MediaRouterUI(content::WebUI* web_ui) | 123 MediaRouterUI::MediaRouterUI(content::WebUI* web_ui) |
102 : ConstrainedWebDialogUI(web_ui), | 124 : ConstrainedWebDialogUI(web_ui), |
103 handler_(new MediaRouterWebUIMessageHandler(this)), | 125 handler_(new MediaRouterWebUIMessageHandler(this)), |
104 ui_initialized_(false), | 126 ui_initialized_(false), |
105 has_pending_route_request_(false), | |
106 requesting_route_for_default_source_(false), | 127 requesting_route_for_default_source_(false), |
| 128 current_route_request_id_(-1), |
| 129 route_request_counter_(0), |
107 initiator_(nullptr), | 130 initiator_(nullptr), |
108 router_(nullptr), | 131 router_(nullptr), |
109 weak_factory_(this) { | 132 weak_factory_(this) { |
110 // Create a WebUIDataSource containing the chrome://media-router page's | 133 // Create a WebUIDataSource containing the chrome://media-router page's |
111 // content. | 134 // content. |
112 scoped_ptr<content::WebUIDataSource> html_source( | 135 scoped_ptr<content::WebUIDataSource> html_source( |
113 content::WebUIDataSource::Create(chrome::kChromeUIMediaRouterHost)); | 136 content::WebUIDataSource::Create(chrome::kChromeUIMediaRouterHost)); |
114 | 137 |
115 content::WebContents* wc = web_ui->GetWebContents(); | 138 content::WebContents* wc = web_ui->GetWebContents(); |
116 DCHECK(wc); | 139 DCHECK(wc); |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 | 277 |
255 void MediaRouterUI::ClearIssue(const std::string& issue_id) { | 278 void MediaRouterUI::ClearIssue(const std::string& issue_id) { |
256 router_->ClearIssue(issue_id); | 279 router_->ClearIssue(issue_id); |
257 } | 280 } |
258 | 281 |
259 std::string MediaRouterUI::GetInitialHeaderText() const { | 282 std::string MediaRouterUI::GetInitialHeaderText() const { |
260 if (cast_modes_.empty()) | 283 if (cast_modes_.empty()) |
261 return std::string(); | 284 return std::string(); |
262 | 285 |
263 return MediaCastModeToDescription(GetPreferredCastMode(cast_modes_), | 286 return MediaCastModeToDescription(GetPreferredCastMode(cast_modes_), |
264 GetHostFromURL(frame_url_)); | 287 GetTruncatedHostFromURL(frame_url_)); |
265 } | 288 } |
266 | 289 |
267 std::string MediaRouterUI::GetInitialHeaderTextTooltip() const { | 290 std::string MediaRouterUI::GetInitialHeaderTextTooltip() const { |
268 if (cast_modes_.empty()) | 291 if (cast_modes_.empty()) |
269 return std::string(); | 292 return std::string(); |
270 | 293 |
271 return GetHostFromURL(frame_url_); | 294 return GetHostFromURL(frame_url_); |
272 } | 295 } |
273 | 296 |
274 void MediaRouterUI::OnResultsUpdated( | 297 void MediaRouterUI::OnResultsUpdated( |
275 const std::vector<MediaSinkWithCastModes>& sinks) { | 298 const std::vector<MediaSinkWithCastModes>& sinks) { |
276 sinks_ = sinks; | 299 sinks_ = sinks; |
277 if (ui_initialized_) | 300 if (ui_initialized_) |
278 handler_->UpdateSinks(sinks_); | 301 handler_->UpdateSinks(sinks_); |
279 } | 302 } |
280 | 303 |
281 void MediaRouterUI::SetIssue(const Issue* issue) { | 304 void MediaRouterUI::SetIssue(const Issue* issue) { |
282 if (ui_initialized_) | 305 if (ui_initialized_) |
283 handler_->UpdateIssue(issue); | 306 handler_->UpdateIssue(issue); |
284 } | 307 } |
285 | 308 |
286 void MediaRouterUI::OnRoutesUpdated(const std::vector<MediaRoute>& routes) { | 309 void MediaRouterUI::OnRoutesUpdated(const std::vector<MediaRoute>& routes) { |
287 routes_ = routes; | 310 routes_ = routes; |
288 if (ui_initialized_) | 311 if (ui_initialized_) |
289 handler_->UpdateRoutes(routes_); | 312 handler_->UpdateRoutes(routes_); |
290 } | 313 } |
291 | 314 |
292 void MediaRouterUI::OnRouteResponseReceived(const MediaSink::Id& sink_id, | 315 void MediaRouterUI::OnRouteResponseReceived(const int route_request_id, |
| 316 const MediaSink::Id& sink_id, |
293 const MediaRoute* route, | 317 const MediaRoute* route, |
294 const std::string& presentation_id, | 318 const std::string& presentation_id, |
295 const std::string& error) { | 319 const std::string& error) { |
296 DVLOG(1) << "OnRouteResponseReceived"; | 320 DVLOG(1) << "OnRouteResponseReceived"; |
| 321 // If we receive a new route that we aren't expecting, do nothing. |
| 322 if (route_request_id != current_route_request_id_) |
| 323 return; |
| 324 |
297 if (!route) { | 325 if (!route) { |
298 // The provider will handle sending an issue for a failed route request. | 326 // The provider will handle sending an issue for a failed route request. |
299 DVLOG(0) << "MediaRouteResponse returned error: " << error; | 327 DVLOG(0) << "MediaRouteResponse returned error: " << error; |
300 } | 328 } |
301 | 329 |
302 handler_->OnCreateRouteResponseReceived(sink_id, route); | 330 handler_->OnCreateRouteResponseReceived(sink_id, route); |
303 has_pending_route_request_ = false; | |
304 requesting_route_for_default_source_ = false; | 331 requesting_route_for_default_source_ = false; |
| 332 current_route_request_id_ = -1; |
| 333 route_creation_timer_.Stop(); |
305 } | 334 } |
306 | 335 |
307 bool MediaRouterUI::DoCreateRoute(const MediaSink::Id& sink_id, | 336 bool MediaRouterUI::DoCreateRoute(const MediaSink::Id& sink_id, |
308 MediaCastMode cast_mode) { | 337 MediaCastMode cast_mode) { |
309 DCHECK(query_result_manager_.get()); | 338 DCHECK(query_result_manager_.get()); |
310 DCHECK(initiator_); | 339 DCHECK(initiator_); |
311 | 340 |
312 // Note that there is a rarely-encountered bug, where the MediaCastMode to | 341 // Note that there is a rarely-encountered bug, where the MediaCastMode to |
313 // MediaSource mapping could have been updated, between when the user | 342 // MediaSource mapping could have been updated, between when the user |
314 // clicked on the UI to start a create route request, | 343 // clicked on the UI to start a create route request, |
315 // and when this function is called. | 344 // and when this function is called. |
316 // However, since the user does not have visibility into the MediaSource, and | 345 // However, since the user does not have visibility into the MediaSource, and |
317 // that it occurs very rarely in practice, we leave it as-is for now. | 346 // that it occurs very rarely in practice, we leave it as-is for now. |
318 MediaSource source = query_result_manager_->GetSourceForCastMode(cast_mode); | 347 MediaSource source = query_result_manager_->GetSourceForCastMode(cast_mode); |
319 if (source.Empty()) { | 348 if (source.Empty()) { |
320 LOG(ERROR) << "No corresponding MediaSource for cast mode " << cast_mode; | 349 LOG(ERROR) << "No corresponding MediaSource for cast mode " << cast_mode; |
321 return false; | 350 return false; |
322 } | 351 } |
323 | 352 |
324 has_pending_route_request_ = true; | |
325 requesting_route_for_default_source_ = cast_mode == MediaCastMode::DEFAULT; | 353 requesting_route_for_default_source_ = cast_mode == MediaCastMode::DEFAULT; |
| 354 current_route_request_id_ = ++route_request_counter_; |
326 GURL origin; | 355 GURL origin; |
327 // TODO(imcheng): What is the origin if not creating route in DEFAULT mode? | 356 // TODO(imcheng): What is the origin if not creating route in DEFAULT mode? |
328 if (requesting_route_for_default_source_) { | 357 if (requesting_route_for_default_source_) { |
329 origin = frame_url_.GetOrigin(); | 358 origin = frame_url_.GetOrigin(); |
330 } else { | 359 } else { |
331 // Requesting route for mirroring. Use a placeholder URL as origin. | 360 // Requesting route for mirroring. Use a placeholder URL as origin. |
332 origin = GURL(chrome::kChromeUIMediaRouterURL); | 361 origin = GURL(chrome::kChromeUIMediaRouterURL); |
333 } | 362 } |
334 DCHECK(origin.is_valid()); | 363 DCHECK(origin.is_valid()); |
335 | 364 |
336 DVLOG(1) << "DoCreateRoute: origin: " << origin; | 365 DVLOG(1) << "DoCreateRoute: origin: " << origin; |
337 | 366 |
338 // There are 3 cases. In all cases the MediaRouterUI will need to be notified. | 367 // There are 3 cases. In all cases the MediaRouterUI will need to be notified. |
339 // (1) Non-presentation route request (e.g., mirroring). No additional | 368 // (1) Non-presentation route request (e.g., mirroring). No additional |
340 // notification necessary. | 369 // notification necessary. |
341 // (2) Presentation route request for a Presentation API startSession call. | 370 // (2) Presentation route request for a Presentation API startSession call. |
342 // The startSession (CreatePresentationSessionRequest) will need to be | 371 // The startSession (CreatePresentationSessionRequest) will need to be |
343 // answered with the | 372 // answered with the |
344 // route response. | 373 // route response. |
345 // (3) Browser-initiated presentation route request. If successful, | 374 // (3) Browser-initiated presentation route request. If successful, |
346 // PresentationServiceDelegateImpl will have to be notified. Note that we | 375 // PresentationServiceDelegateImpl will have to be notified. Note that we |
347 // treat subsequent route requests from a Presentation API-initiated dialogs | 376 // treat subsequent route requests from a Presentation API-initiated dialogs |
348 // as browser-initiated. | 377 // as browser-initiated. |
349 std::vector<MediaRouteResponseCallback> route_response_callbacks; | 378 std::vector<MediaRouteResponseCallback> route_response_callbacks; |
350 route_response_callbacks.push_back( | 379 route_response_callbacks.push_back( |
351 base::Bind(&MediaRouterUI::OnRouteResponseReceived, | 380 base::Bind(&MediaRouterUI::OnRouteResponseReceived, |
352 weak_factory_.GetWeakPtr(), sink_id)); | 381 weak_factory_.GetWeakPtr(), current_route_request_id_, |
| 382 sink_id)); |
353 if (requesting_route_for_default_source_) { | 383 if (requesting_route_for_default_source_) { |
354 if (presentation_request_) { | 384 if (presentation_request_) { |
355 // |presentation_request_| will be nullptr after this call, as the | 385 // |presentation_request_| will be nullptr after this call, as the |
356 // object will be transferred to the callback. | 386 // object will be transferred to the callback. |
357 route_response_callbacks.push_back( | 387 route_response_callbacks.push_back( |
358 base::Bind(&CreatePresentationSessionRequest::HandleRouteResponse, | 388 base::Bind(&CreatePresentationSessionRequest::HandleRouteResponse, |
359 base::Passed(&presentation_request_))); | 389 base::Passed(&presentation_request_))); |
360 } else if (presentation_service_delegate_) { | 390 } else if (presentation_service_delegate_) { |
361 route_response_callbacks.push_back( | 391 route_response_callbacks.push_back( |
362 base::Bind(&PresentationServiceDelegateImpl::OnRouteResponse, | 392 base::Bind(&PresentationServiceDelegateImpl::OnRouteResponse, |
363 presentation_service_delegate_)); | 393 presentation_service_delegate_)); |
364 } | 394 } |
365 } | 395 } |
366 | 396 |
| 397 // Start the timer. |
| 398 route_creation_timer_.Start( |
| 399 FROM_HERE, base::TimeDelta::FromSeconds(kCreateRouteTimeoutSeconds), |
| 400 this, &MediaRouterUI::RouteCreationTimeout); |
| 401 |
367 router_->CreateRoute(source.id(), sink_id, origin, | 402 router_->CreateRoute(source.id(), sink_id, origin, |
368 SessionTabHelper::IdForTab(initiator_), | 403 SessionTabHelper::IdForTab(initiator_), |
369 route_response_callbacks); | 404 route_response_callbacks); |
370 return true; | 405 return true; |
371 } | 406 } |
372 | 407 |
| 408 void MediaRouterUI::RouteCreationTimeout() { |
| 409 requesting_route_for_default_source_ = false; |
| 410 current_route_request_id_ = -1; |
| 411 |
| 412 base::string16 host = base::UTF8ToUTF16(GetTruncatedHostFromURL(frame_url_)); |
| 413 |
| 414 // TODO(apacible): Update error messages based on current cast mode |
| 415 // (e.g. desktop). |
| 416 std::string issue_title = host.empty() ? |
| 417 l10n_util::GetStringUTF8( |
| 418 IDS_MEDIA_ROUTER_ISSUE_CREATE_ROUTE_TIMEOUT_FOR_TAB) : |
| 419 l10n_util::GetStringFUTF8(IDS_MEDIA_ROUTER_ISSUE_CREATE_ROUTE_TIMEOUT, |
| 420 host); |
| 421 |
| 422 Issue issue(issue_title, std::string(), |
| 423 IssueAction(IssueAction::TYPE_DISMISS), |
| 424 std::vector<IssueAction>(), std::string(), Issue::NOTIFICATION, |
| 425 false, std::string()); |
| 426 AddIssue(issue); |
| 427 handler_->NotifyRouteCreationTimeout(); |
| 428 } |
| 429 |
373 std::string MediaRouterUI::GetFrameURLHost() const { | 430 std::string MediaRouterUI::GetFrameURLHost() const { |
374 return GetHostFromURL(frame_url_); | 431 return GetHostFromURL(frame_url_); |
375 } | 432 } |
376 | 433 |
377 const std::string& MediaRouterUI::GetRouteProviderExtensionId() const { | 434 const std::string& MediaRouterUI::GetRouteProviderExtensionId() const { |
378 return router_->media_route_provider_extension_id(); | 435 return router_->media_route_provider_extension_id(); |
379 } | 436 } |
380 | 437 |
381 } // namespace media_router | 438 } // namespace media_router |
OLD | NEW |