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), |
107 initiator_(nullptr), | 128 initiator_(nullptr), |
108 router_(nullptr), | 129 router_(nullptr), |
109 weak_factory_(this) { | 130 weak_factory_(this) { |
110 // Create a WebUIDataSource containing the chrome://media-router page's | 131 // Create a WebUIDataSource containing the chrome://media-router page's |
111 // content. | 132 // content. |
112 scoped_ptr<content::WebUIDataSource> html_source( | 133 scoped_ptr<content::WebUIDataSource> html_source( |
113 content::WebUIDataSource::Create(chrome::kChromeUIMediaRouterHost)); | 134 content::WebUIDataSource::Create(chrome::kChromeUIMediaRouterHost)); |
114 | 135 |
115 content::WebContents* wc = web_ui->GetWebContents(); | 136 content::WebContents* wc = web_ui->GetWebContents(); |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
254 | 275 |
255 void MediaRouterUI::ClearIssue(const std::string& issue_id) { | 276 void MediaRouterUI::ClearIssue(const std::string& issue_id) { |
256 router_->ClearIssue(issue_id); | 277 router_->ClearIssue(issue_id); |
257 } | 278 } |
258 | 279 |
259 std::string MediaRouterUI::GetInitialHeaderText() const { | 280 std::string MediaRouterUI::GetInitialHeaderText() const { |
260 if (cast_modes_.empty()) | 281 if (cast_modes_.empty()) |
261 return std::string(); | 282 return std::string(); |
262 | 283 |
263 return MediaCastModeToDescription(GetPreferredCastMode(cast_modes_), | 284 return MediaCastModeToDescription(GetPreferredCastMode(cast_modes_), |
264 GetHostFromURL(frame_url_)); | 285 GetTruncatedHostFromURL(frame_url_)); |
265 } | 286 } |
266 | 287 |
267 std::string MediaRouterUI::GetInitialHeaderTextTooltip() const { | 288 std::string MediaRouterUI::GetInitialHeaderTextTooltip() const { |
268 if (cast_modes_.empty()) | 289 if (cast_modes_.empty()) |
269 return std::string(); | 290 return std::string(); |
270 | 291 |
271 return GetHostFromURL(frame_url_); | 292 return GetHostFromURL(frame_url_); |
272 } | 293 } |
273 | 294 |
274 void MediaRouterUI::OnResultsUpdated( | 295 void MediaRouterUI::OnResultsUpdated( |
275 const std::vector<MediaSinkWithCastModes>& sinks) { | 296 const std::vector<MediaSinkWithCastModes>& sinks) { |
276 sinks_ = sinks; | 297 sinks_ = sinks; |
277 if (ui_initialized_) | 298 if (ui_initialized_) |
278 handler_->UpdateSinks(sinks_); | 299 handler_->UpdateSinks(sinks_); |
279 } | 300 } |
280 | 301 |
281 void MediaRouterUI::SetIssue(const Issue* issue) { | 302 void MediaRouterUI::SetIssue(const Issue* issue) { |
282 if (ui_initialized_) | 303 if (ui_initialized_) |
283 handler_->UpdateIssue(issue); | 304 handler_->UpdateIssue(issue); |
284 } | 305 } |
285 | 306 |
286 void MediaRouterUI::OnRoutesUpdated(const std::vector<MediaRoute>& routes) { | 307 void MediaRouterUI::OnRoutesUpdated(const std::vector<MediaRoute>& routes) { |
287 routes_ = routes; | 308 routes_ = routes; |
288 if (ui_initialized_) | 309 if (ui_initialized_) |
289 handler_->UpdateRoutes(routes_); | 310 handler_->UpdateRoutes(routes_); |
290 } | 311 } |
291 | 312 |
292 void MediaRouterUI::OnRouteResponseReceived(const MediaSink::Id& sink_id, | 313 void MediaRouterUI::OnRouteResponseReceived(const MediaSink::Id& sink_id, |
314 const std::string& route_guid, | |
293 const MediaRoute* route, | 315 const MediaRoute* route, |
294 const std::string& presentation_id, | 316 const std::string& presentation_id, |
295 const std::string& error) { | 317 const std::string& error) { |
296 DVLOG(1) << "OnRouteResponseReceived"; | 318 DVLOG(1) << "OnRouteResponseReceived"; |
319 // If we receive a new route that we aren't expecting, do nothing. | |
320 if (route_guid != expected_route_guid_) | |
321 return; | |
322 | |
297 if (!route) { | 323 if (!route) { |
298 // The provider will handle sending an issue for a failed route request. | 324 // The provider will handle sending an issue for a failed route request. |
299 DVLOG(0) << "MediaRouteResponse returned error: " << error; | 325 DVLOG(0) << "MediaRouteResponse returned error: " << error; |
300 } | 326 } |
301 | 327 |
302 handler_->OnCreateRouteResponseReceived(sink_id, route); | 328 handler_->OnCreateRouteResponseReceived(sink_id, route); |
303 has_pending_route_request_ = false; | |
304 requesting_route_for_default_source_ = false; | 329 requesting_route_for_default_source_ = false; |
330 expected_route_guid_ = std::string(); | |
331 route_creation_timer_.Stop(); | |
305 } | 332 } |
306 | 333 |
307 bool MediaRouterUI::DoCreateRoute(const MediaSink::Id& sink_id, | 334 bool MediaRouterUI::DoCreateRoute(const MediaSink::Id& sink_id, |
308 MediaCastMode cast_mode) { | 335 MediaCastMode cast_mode) { |
309 DCHECK(query_result_manager_.get()); | 336 DCHECK(query_result_manager_.get()); |
310 DCHECK(initiator_); | 337 DCHECK(initiator_); |
311 | 338 |
312 // Note that there is a rarely-encountered bug, where the MediaCastMode to | 339 // Note that there is a rarely-encountered bug, where the MediaCastMode to |
313 // MediaSource mapping could have been updated, between when the user | 340 // MediaSource mapping could have been updated, between when the user |
314 // clicked on the UI to start a create route request, | 341 // clicked on the UI to start a create route request, |
315 // and when this function is called. | 342 // and when this function is called. |
316 // However, since the user does not have visibility into the MediaSource, and | 343 // 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. | 344 // that it occurs very rarely in practice, we leave it as-is for now. |
318 MediaSource source = query_result_manager_->GetSourceForCastMode(cast_mode); | 345 MediaSource source = query_result_manager_->GetSourceForCastMode(cast_mode); |
319 if (source.Empty()) { | 346 if (source.Empty()) { |
320 LOG(ERROR) << "No corresponding MediaSource for cast mode " << cast_mode; | 347 LOG(ERROR) << "No corresponding MediaSource for cast mode " << cast_mode; |
321 return false; | 348 return false; |
322 } | 349 } |
323 | 350 |
324 has_pending_route_request_ = true; | |
325 requesting_route_for_default_source_ = cast_mode == MediaCastMode::DEFAULT; | 351 requesting_route_for_default_source_ = cast_mode == MediaCastMode::DEFAULT; |
352 expected_route_guid_ = base::GenerateGUID(); | |
326 GURL origin; | 353 GURL origin; |
327 // TODO(imcheng): What is the origin if not creating route in DEFAULT mode? | 354 // TODO(imcheng): What is the origin if not creating route in DEFAULT mode? |
328 if (requesting_route_for_default_source_) { | 355 if (requesting_route_for_default_source_) { |
329 origin = frame_url_.GetOrigin(); | 356 origin = frame_url_.GetOrigin(); |
330 } else { | 357 } else { |
331 // Requesting route for mirroring. Use a placeholder URL as origin. | 358 // Requesting route for mirroring. Use a placeholder URL as origin. |
332 origin = GURL(chrome::kChromeUIMediaRouterURL); | 359 origin = GURL(chrome::kChromeUIMediaRouterURL); |
333 } | 360 } |
334 DCHECK(origin.is_valid()); | 361 DCHECK(origin.is_valid()); |
335 | 362 |
336 DVLOG(1) << "DoCreateRoute: origin: " << origin; | 363 DVLOG(1) << "DoCreateRoute: origin: " << origin; |
337 | 364 |
338 // There are 3 cases. In all cases the MediaRouterUI will need to be notified. | 365 // 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 | 366 // (1) Non-presentation route request (e.g., mirroring). No additional |
340 // notification necessary. | 367 // notification necessary. |
341 // (2) Presentation route request for a Presentation API startSession call. | 368 // (2) Presentation route request for a Presentation API startSession call. |
342 // The startSession (CreatePresentationSessionRequest) will need to be | 369 // The startSession (CreatePresentationSessionRequest) will need to be |
343 // answered with the | 370 // answered with the |
344 // route response. | 371 // route response. |
345 // (3) Browser-initiated presentation route request. If successful, | 372 // (3) Browser-initiated presentation route request. If successful, |
346 // PresentationServiceDelegateImpl will have to be notified. Note that we | 373 // PresentationServiceDelegateImpl will have to be notified. Note that we |
347 // treat subsequent route requests from a Presentation API-initiated dialogs | 374 // treat subsequent route requests from a Presentation API-initiated dialogs |
348 // as browser-initiated. | 375 // as browser-initiated. |
349 std::vector<MediaRouteResponseCallback> route_response_callbacks; | 376 std::vector<MediaRouteResponseCallback> route_response_callbacks; |
350 route_response_callbacks.push_back( | 377 route_response_callbacks.push_back( |
351 base::Bind(&MediaRouterUI::OnRouteResponseReceived, | 378 base::Bind(&MediaRouterUI::OnRouteResponseReceived, |
352 weak_factory_.GetWeakPtr(), sink_id)); | 379 weak_factory_.GetWeakPtr(), sink_id, expected_route_guid_)); |
353 if (requesting_route_for_default_source_) { | 380 if (requesting_route_for_default_source_) { |
354 if (presentation_request_) { | 381 if (presentation_request_) { |
355 // |presentation_request_| will be nullptr after this call, as the | 382 // |presentation_request_| will be nullptr after this call, as the |
356 // object will be transferred to the callback. | 383 // object will be transferred to the callback. |
357 route_response_callbacks.push_back( | 384 route_response_callbacks.push_back( |
358 base::Bind(&CreatePresentationSessionRequest::HandleRouteResponse, | 385 base::Bind(&CreatePresentationSessionRequest::HandleRouteResponse, |
359 base::Passed(&presentation_request_))); | 386 base::Passed(&presentation_request_))); |
360 } else if (presentation_service_delegate_) { | 387 } else if (presentation_service_delegate_) { |
361 route_response_callbacks.push_back( | 388 route_response_callbacks.push_back( |
362 base::Bind(&PresentationServiceDelegateImpl::OnRouteResponse, | 389 base::Bind(&PresentationServiceDelegateImpl::OnRouteResponse, |
363 presentation_service_delegate_)); | 390 presentation_service_delegate_)); |
364 } | 391 } |
365 } | 392 } |
366 | 393 |
394 // Start the timer. | |
395 route_creation_timer_.Start( | |
396 FROM_HERE, base::TimeDelta::FromSeconds(kCreateRouteTimeoutSeconds), | |
397 this, &MediaRouterUI::RouteCreationTimeout); | |
398 | |
367 router_->CreateRoute(source.id(), sink_id, origin, | 399 router_->CreateRoute(source.id(), sink_id, origin, |
368 SessionTabHelper::IdForTab(initiator_), | 400 SessionTabHelper::IdForTab(initiator_), |
369 route_response_callbacks); | 401 route_response_callbacks); |
370 return true; | 402 return true; |
371 } | 403 } |
372 | 404 |
405 void MediaRouterUI::RouteCreationTimeout() { | |
406 requesting_route_for_default_source_ = false; | |
407 expected_route_guid_ = std::string(); | |
408 | |
409 base::string16 host = base::UTF8ToUTF16(GetTruncatedHostFromURL(frame_url_)); | |
410 | |
411 // TODO(apacible): Update error messages based on current cast mode | |
412 // (e.g. desktop). | |
413 std::string issue_title = host.empty() ? | |
414 l10n_util::GetStringUTF8( | |
415 IDS_MEDIA_ROUTER_ISSUE_CREATE_ROUTE_TIMEOUT_FOR_TAB) : | |
416 l10n_util::GetStringFUTF8(IDS_MEDIA_ROUTER_ISSUE_CREATE_ROUTE_TIMEOUT, | |
417 host); | |
418 | |
419 Issue issue(issue_title, std::string(), | |
420 IssueAction(IssueAction::TYPE_DISMISS), | |
421 std::vector<IssueAction>(), std::string(), Issue::NOTIFICATION, | |
422 false, std::string()); | |
423 AddIssue(issue); | |
imcheng
2015/10/02 21:57:30
Does that mean this issue will be shown in dialogs
apacible
2015/10/02 23:45:17
Correct.
| |
424 handler_->NotifyRouteCreationTimeout(); | |
425 } | |
426 | |
373 std::string MediaRouterUI::GetFrameURLHost() const { | 427 std::string MediaRouterUI::GetFrameURLHost() const { |
374 return GetHostFromURL(frame_url_); | 428 return GetHostFromURL(frame_url_); |
375 } | 429 } |
376 | 430 |
377 const std::string& MediaRouterUI::GetRouteProviderExtensionId() const { | 431 const std::string& MediaRouterUI::GetRouteProviderExtensionId() const { |
378 return router_->media_route_provider_extension_id(); | 432 return router_->media_route_provider_extension_id(); |
379 } | 433 } |
380 | 434 |
381 } // namespace media_router | 435 } // namespace media_router |
OLD | NEW |