application_media_info_manager.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2018 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. #include "chromecast/browser/application_media_info_manager.h"
  5. #include <utility>
  6. #include "base/logging.h"
  7. #include "chromecast/base/metrics/cast_metrics_helper.h"
  8. #include "chromecast/browser/cast_renderer_block_data.h"
  9. #include "content/public/browser/web_contents.h"
  10. namespace chromecast {
  11. namespace media {
  12. void ApplicationMediaInfoManager::Create(
  13. content::RenderFrameHost* render_frame_host,
  14. std::string application_session_id,
  15. bool mixer_audio_enabled,
  16. mojo::PendingReceiver<::media::mojom::CastApplicationMediaInfoManager>
  17. receiver) {
  18. CHECK(render_frame_host);
  19. // The created ApplicationMediaInfoManager will be deleted on connection
  20. // error, or when the frame navigates away. See DocumentService for
  21. // details.
  22. new ApplicationMediaInfoManager(*render_frame_host, std::move(receiver),
  23. std::move(application_session_id),
  24. mixer_audio_enabled);
  25. }
  26. ApplicationMediaInfoManager& ApplicationMediaInfoManager::CreateForTesting(
  27. content::RenderFrameHost& render_frame_host,
  28. std::string application_session_id,
  29. bool mixer_audio_enabled,
  30. mojo::PendingReceiver<::media::mojom::CastApplicationMediaInfoManager>
  31. receiver) {
  32. return *new ApplicationMediaInfoManager(
  33. render_frame_host, std::move(receiver), std::move(application_session_id),
  34. mixer_audio_enabled);
  35. }
  36. ApplicationMediaInfoManager::ApplicationMediaInfoManager(
  37. content::RenderFrameHost& render_frame_host,
  38. mojo::PendingReceiver<::media::mojom::CastApplicationMediaInfoManager>
  39. receiver,
  40. std::string application_session_id,
  41. bool mixer_audio_enabled)
  42. : DocumentService(render_frame_host, std::move(receiver)),
  43. application_session_id_(std::move(application_session_id)),
  44. mixer_audio_enabled_(mixer_audio_enabled),
  45. renderer_blocked_(false) {
  46. shell::CastRendererBlockData::SetApplicationMediaInfoManagerForWebContents(
  47. content::WebContents::FromRenderFrameHost(&render_frame_host), this);
  48. }
  49. ApplicationMediaInfoManager::~ApplicationMediaInfoManager() = default;
  50. void ApplicationMediaInfoManager::SetRendererBlock(bool renderer_blocked) {
  51. LOG(INFO) << "Setting blocked to: " << renderer_blocked << " from "
  52. << renderer_blocked_
  53. << "(Pending call set: " << (!pending_call_.is_null()) << ")";
  54. if (renderer_blocked_ && !renderer_blocked && pending_call_) {
  55. // Move callbacks in case CanStartRenderer() is called.
  56. std::move(pending_call_)
  57. .Run(::media::mojom::CastApplicationMediaInfo::New(
  58. application_session_id_, mixer_audio_enabled_));
  59. pending_call_.Reset();
  60. }
  61. renderer_blocked_ = renderer_blocked;
  62. }
  63. void ApplicationMediaInfoManager::GetCastApplicationMediaInfo(
  64. GetCastApplicationMediaInfoCallback callback) {
  65. LOG(INFO) << "GetCastApplicationMediaInfo called with blocked: "
  66. << renderer_blocked_;
  67. metrics::CastMetricsHelper::GetInstance()->RecordApplicationEventWithValue(
  68. "Cast.Platform.CastRenderer.MediaReady", renderer_blocked_);
  69. if (renderer_blocked_) {
  70. DCHECK(!pending_call_);
  71. pending_call_ = std::move(callback);
  72. return;
  73. }
  74. std::move(callback).Run(::media::mojom::CastApplicationMediaInfo::New(
  75. application_session_id_, mixer_audio_enabled_));
  76. }
  77. } // namespace media
  78. } // namespace chromecast