Chromium Code Reviews| 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/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "base/strings/utf_string_conversions.h" | |
| 10 #include "chrome/browser/media/router/create_presentation_session_request.h" | 11 #include "chrome/browser/media/router/create_presentation_session_request.h" |
| 11 #include "chrome/browser/media/router/issue.h" | 12 #include "chrome/browser/media/router/issue.h" |
| 12 #include "chrome/browser/media/router/issues_observer.h" | 13 #include "chrome/browser/media/router/issues_observer.h" |
| 13 #include "chrome/browser/media/router/media_route.h" | 14 #include "chrome/browser/media/router/media_route.h" |
| 14 #include "chrome/browser/media/router/media_router.h" | 15 #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_factory.h" |
| 16 #include "chrome/browser/media/router/media_router_mojo_impl.h" | 17 #include "chrome/browser/media/router/media_router_mojo_impl.h" |
| 17 #include "chrome/browser/media/router/media_routes_observer.h" | 18 #include "chrome/browser/media/router/media_routes_observer.h" |
| 18 #include "chrome/browser/media/router/media_sink.h" | 19 #include "chrome/browser/media/router/media_sink.h" |
| 19 #include "chrome/browser/media/router/media_sinks_observer.h" | 20 #include "chrome/browser/media/router/media_sinks_observer.h" |
| 20 #include "chrome/browser/media/router/media_source.h" | 21 #include "chrome/browser/media/router/media_source.h" |
| 21 #include "chrome/browser/media/router/media_source_helper.h" | 22 #include "chrome/browser/media/router/media_source_helper.h" |
| 22 #include "chrome/browser/media/router/presentation_service_delegate_impl.h" | 23 #include "chrome/browser/media/router/presentation_service_delegate_impl.h" |
| 23 #include "chrome/browser/profiles/profile.h" | 24 #include "chrome/browser/profiles/profile.h" |
| 24 #include "chrome/browser/sessions/session_tab_helper.h" | 25 #include "chrome/browser/sessions/session_tab_helper.h" |
| 25 #include "chrome/browser/ui/webui/media_router/media_router_localized_strings_pr ovider.h" | 26 #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 " | 27 #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" | 28 #include "chrome/browser/ui/webui/media_router/media_router_webui_message_handle r.h" |
| 28 #include "chrome/common/url_constants.h" | 29 #include "chrome/common/url_constants.h" |
| 30 #include "chrome/grit/generated_resources.h" | |
| 29 #include "content/public/browser/web_contents.h" | 31 #include "content/public/browser/web_contents.h" |
| 30 #include "content/public/browser/web_ui.h" | 32 #include "content/public/browser/web_ui.h" |
| 31 #include "content/public/browser/web_ui_data_source.h" | 33 #include "content/public/browser/web_ui_data_source.h" |
| 34 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | |
| 35 #include "ui/base/l10n/l10n_util.h" | |
| 32 #include "ui/web_dialogs/web_dialog_delegate.h" | 36 #include "ui/web_dialogs/web_dialog_delegate.h" |
| 33 | 37 |
| 34 namespace media_router { | 38 namespace media_router { |
| 35 | 39 |
| 36 namespace { | 40 namespace { |
| 37 | 41 |
| 42 // The amount of time to wait for a response when creating a new route. | |
| 43 const int kCreateRouteTimeoutSeconds = 20; | |
| 44 | |
| 38 std::string GetHostFromURL(const GURL& gurl) { | 45 std::string GetHostFromURL(const GURL& gurl) { |
| 39 if (gurl.is_empty()) | 46 if (gurl.is_empty()) |
| 40 return std::string(); | 47 return std::string(); |
| 41 std::string host = gurl.host(); | 48 std::string host = gurl.host(); |
| 42 if (base::StartsWith(host, "www.", base::CompareCase::INSENSITIVE_ASCII)) | 49 if (base::StartsWith(host, "www.", base::CompareCase::INSENSITIVE_ASCII)) |
| 43 host = host.substr(4); | 50 host = host.substr(4); |
| 44 return host; | 51 return host; |
| 45 } | 52 } |
| 46 | 53 |
| 54 std::string GetTruncatedHostFromURL(const GURL& gurl) { | |
| 55 std::string host = GetHostFromURL(gurl); | |
| 56 | |
| 57 const std::string truncated = | |
| 58 net::registry_controlled_domains::GetDomainAndRegistry( | |
| 59 host, | |
| 60 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); | |
| 61 // The truncation will be empty in some scenarios (e.g. host is | |
| 62 // simply an IP address). Fail gracefully. | |
| 63 if (truncated.empty()) | |
| 64 return host; | |
| 65 return truncated; | |
| 66 } | |
| 67 | |
| 47 } // namespace | 68 } // namespace |
| 48 | 69 |
| 49 // This class calls to refresh the UI when the highest priority issue is | 70 // This class calls to refresh the UI when the highest priority issue is |
| 50 // updated. | 71 // updated. |
| 51 class MediaRouterUI::UIIssuesObserver : public IssuesObserver { | 72 class MediaRouterUI::UIIssuesObserver : public IssuesObserver { |
| 52 public: | 73 public: |
| 53 UIIssuesObserver(MediaRouter* router, MediaRouterUI* ui) | 74 UIIssuesObserver(MediaRouter* router, MediaRouterUI* ui) |
| 54 : IssuesObserver(router), ui_(ui) { | 75 : IssuesObserver(router), ui_(ui) { |
| 55 DCHECK(ui); | 76 DCHECK(ui); |
| 56 } | 77 } |
| (...skipping 197 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( |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 295 const std::string& error) { | 316 const std::string& error) { |
| 296 DVLOG(1) << "OnRouteResponseReceived"; | 317 DVLOG(1) << "OnRouteResponseReceived"; |
| 297 if (!route) { | 318 if (!route) { |
| 298 // The provider will handle sending an issue for a failed route request. | 319 // The provider will handle sending an issue for a failed route request. |
| 299 DVLOG(0) << "MediaRouteResponse returned error: " << error; | 320 DVLOG(0) << "MediaRouteResponse returned error: " << error; |
| 300 } | 321 } |
| 301 | 322 |
| 302 handler_->OnCreateRouteResponseReceived(sink_id, route); | 323 handler_->OnCreateRouteResponseReceived(sink_id, route); |
| 303 has_pending_route_request_ = false; | 324 has_pending_route_request_ = false; |
| 304 requesting_route_for_default_source_ = false; | 325 requesting_route_for_default_source_ = false; |
| 326 route_creation_timer_.Stop(); | |
| 305 } | 327 } |
| 306 | 328 |
| 307 bool MediaRouterUI::DoCreateRoute(const MediaSink::Id& sink_id, | 329 bool MediaRouterUI::DoCreateRoute(const MediaSink::Id& sink_id, |
| 308 MediaCastMode cast_mode) { | 330 MediaCastMode cast_mode) { |
| 309 DCHECK(query_result_manager_.get()); | 331 DCHECK(query_result_manager_.get()); |
| 310 DCHECK(initiator_); | 332 DCHECK(initiator_); |
| 311 | 333 |
| 312 // Note that there is a rarely-encountered bug, where the MediaCastMode to | 334 // Note that there is a rarely-encountered bug, where the MediaCastMode to |
| 313 // MediaSource mapping could have been updated, between when the user | 335 // MediaSource mapping could have been updated, between when the user |
| 314 // clicked on the UI to start a create route request, | 336 // clicked on the UI to start a create route request, |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 357 route_response_callbacks.push_back( | 379 route_response_callbacks.push_back( |
| 358 base::Bind(&CreatePresentationSessionRequest::HandleRouteResponse, | 380 base::Bind(&CreatePresentationSessionRequest::HandleRouteResponse, |
| 359 base::Passed(&presentation_request_))); | 381 base::Passed(&presentation_request_))); |
| 360 } else if (presentation_service_delegate_) { | 382 } else if (presentation_service_delegate_) { |
| 361 route_response_callbacks.push_back( | 383 route_response_callbacks.push_back( |
| 362 base::Bind(&PresentationServiceDelegateImpl::OnRouteResponse, | 384 base::Bind(&PresentationServiceDelegateImpl::OnRouteResponse, |
| 363 presentation_service_delegate_)); | 385 presentation_service_delegate_)); |
| 364 } | 386 } |
| 365 } | 387 } |
| 366 | 388 |
| 389 // Start the timer. | |
| 390 route_creation_timer_.Start( | |
| 391 FROM_HERE, base::TimeDelta::FromSeconds(kCreateRouteTimeoutSeconds), | |
| 392 this, &MediaRouterUI::RouteCreationTimeout); | |
| 393 | |
| 367 router_->CreateRoute(source.id(), sink_id, origin, | 394 router_->CreateRoute(source.id(), sink_id, origin, |
| 368 SessionTabHelper::IdForTab(initiator_), | 395 SessionTabHelper::IdForTab(initiator_), |
| 369 route_response_callbacks); | 396 route_response_callbacks); |
| 397 | |
| 370 return true; | 398 return true; |
| 371 } | 399 } |
| 372 | 400 |
| 401 void MediaRouterUI::RouteCreationTimeout() { | |
| 402 has_pending_route_request_ = false; | |
| 403 requesting_route_for_default_source_ = false; | |
| 404 | |
| 405 Issue issue( | |
| 406 l10n_util::GetStringFUTF8(IDS_MEDIA_ROUTER_ISSUE_CREATE_ROUTE_TIMEOUT, | |
| 407 base::UTF8ToUTF16(GetTruncatedHostFromURL( | |
| 408 frame_url_))), | |
| 409 std::string(), IssueAction(IssueAction::TYPE_DISMISS), | |
| 410 std::vector<IssueAction>(), std::string(), Issue::NOTIFICATION, | |
| 411 false, std::string()); | |
| 412 AddIssue(issue); | |
|
imcheng
2015/09/29 19:08:53
We can also consider doing away with the timer on
apacible
2015/09/30 18:05:46
We actually don't have a timer on the WebUI side,
| |
| 413 } | |
| 414 | |
| 373 std::string MediaRouterUI::GetFrameURLHost() const { | 415 std::string MediaRouterUI::GetFrameURLHost() const { |
| 374 return GetHostFromURL(frame_url_); | 416 return GetHostFromURL(frame_url_); |
| 375 } | 417 } |
| 376 | 418 |
| 377 const std::string& MediaRouterUI::GetRouteProviderExtensionId() const { | 419 const std::string& MediaRouterUI::GetRouteProviderExtensionId() const { |
| 378 return router_->media_route_provider_extension_id(); | 420 return router_->media_route_provider_extension_id(); |
| 379 } | 421 } |
| 380 | 422 |
| 381 } // namespace media_router | 423 } // namespace media_router |
| OLD | NEW |