chromium/content/browser/media/system_media_controls_notifier.cc

// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/media/system_media_controls_notifier.h"

#include <memory>
#include <utility>

#include "base/functional/bind.h"
#include "base/time/time.h"
#include "base/unguessable_token.h"
#include "build/build_config.h"
#include "components/system_media_controls/system_media_controls.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/media_session_client.h"
#include "content/public/browser/media_session_service.h"
#include "content/public/common/content_client.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/media_session/public/mojom/media_session.mojom.h"
#include "ui/gfx/image/image_skia.h"

#if BUILDFLAG(IS_WIN)
#include "ui/base/idle/idle.h"
#endif  // BUILDFLAG(IS_WIN)

namespace content {

PlaybackStatus;

const int kMinImageSize =;
const int kDesiredImageSize =;

#if BUILDFLAG(IS_WIN)
constexpr base::TimeDelta kScreenLockPollInterval = base::Seconds(1);
constexpr int kHideSmtcDelaySeconds = 5;
constexpr base::TimeDelta kHideSmtcDelay = base::Seconds(kHideSmtcDelaySeconds);
#endif  // BUILDFLAG(IS_WIN)

constexpr base::TimeDelta kDebounceDelay =;

SystemMediaControlsNotifier::SystemMediaControlsNotifier(
    system_media_controls::SystemMediaControls* system_media_controls,
    base::UnguessableToken request_id)
    :{}

SystemMediaControlsNotifier::~SystemMediaControlsNotifier() = default;

void SystemMediaControlsNotifier::MediaSessionInfoChanged(
    media_session::mojom::MediaSessionInfoPtr session_info_ptr) {}

void SystemMediaControlsNotifier::MediaSessionMetadataChanged(
    const std::optional<media_session::MediaMetadata>& metadata) {}

void SystemMediaControlsNotifier::MediaSessionActionsChanged(
    const std::vector<media_session::mojom::MediaSessionAction>& actions) {}

void SystemMediaControlsNotifier::MediaSessionChanged(
    const std::optional<base::UnguessableToken>& request_id) {}

void SystemMediaControlsNotifier::MediaControllerImageChanged(
    media_session::mojom::MediaSessionImageType type,
    const SkBitmap& bitmap) {}

void SystemMediaControlsNotifier::MediaSessionPositionChanged(
    const std::optional<media_session::MediaPosition>& position) {}

void SystemMediaControlsNotifier::DebouncePositionUpdate(
    media_session::MediaPosition position) {}

void SystemMediaControlsNotifier::DebounceMetadataUpdate(
    media_session::MediaMetadata metadata) {}

void SystemMediaControlsNotifier::DebouncePlaybackStatusUpdate(
    system_media_controls::SystemMediaControls::PlaybackStatus
        playback_status) {}

void SystemMediaControlsNotifier::DebounceIconUpdate(const SkBitmap& bitmap) {}

void SystemMediaControlsNotifier::DebounceSetIsSeekToEnabled(
    bool is_seek_to_enabled) {}

void SystemMediaControlsNotifier::MaybeScheduleMetadataUpdate() {}

void SystemMediaControlsNotifier::UpdateMetadata() {}

void SystemMediaControlsNotifier::UpdateIcon() {}

void SystemMediaControlsNotifier::ClearAllMetadata() {}

#if BUILDFLAG(IS_WIN)
void SystemMediaControlsNotifier::CheckLockState() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  bool new_state = ui::CheckIdleStateIsLocked();
  if (screen_locked_ == new_state)
    return;

  screen_locked_ = new_state;
  if (screen_locked_)
    OnScreenLocked();
  else
    OnScreenUnlocked();
}

void SystemMediaControlsNotifier::OnScreenLocked() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  // If media is currently playing, don't hide the SMTC.
  if (session_info_ptr_ &&
      session_info_ptr_->playback_state ==
          media_session::mojom::MediaPlaybackState::kPlaying) {
    return;
  }

  // Otherwise, hide them.
  system_media_controls_->SetEnabled(false);
}

void SystemMediaControlsNotifier::OnScreenUnlocked() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  StopHideSmtcTimer();
  system_media_controls_->SetEnabled(true);
}

void SystemMediaControlsNotifier::StartHideSmtcTimer() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  hide_smtc_timer_.Start(
      FROM_HERE, kHideSmtcDelay,
      base::BindOnce(&SystemMediaControlsNotifier::HideSmtcTimerFired,
                     base::Unretained(this)));
}

void SystemMediaControlsNotifier::StopHideSmtcTimer() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  hide_smtc_timer_.Stop();
}

void SystemMediaControlsNotifier::HideSmtcTimerFired() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  system_media_controls_->SetEnabled(false);
}
#endif  // BUILDFLAG(IS_WIN)

}  // namespace content