1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
// 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.
#ifndef COMPONENTS_GLOBAL_MEDIA_CONTROLS_PUBLIC_MEDIA_SESSION_NOTIFICATION_ITEM_H_
#define COMPONENTS_GLOBAL_MEDIA_CONTROLS_PUBLIC_MEDIA_SESSION_NOTIFICATION_ITEM_H_
#include <optional>
#include <string>
#include "base/component_export.h"
#include "base/containers/flat_set.h"
#include "base/functional/callback.h"
#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/unguessable_token.h"
#include "components/media_message_center/media_notification_item.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/media_session/public/mojom/media_controller.mojom.h"
#include "services/media_session/public/mojom/media_session.mojom.h"
#include "ui/gfx/image/image_skia.h"
#include "url/origin.h"
namespace media_message_center {
class MediaNotificationView;
} // namespace media_message_center
namespace global_media_controls {
// MediaSessionNotificationItem manages hiding/showing a media notification and
// updating the metadata for a single media session.
class COMPONENT_EXPORT(GLOBAL_MEDIA_CONTROLS) MediaSessionNotificationItem
: public media_message_center::MediaNotificationItem,
public media_session::mojom::MediaControllerObserver,
public media_session::mojom::MediaControllerImageObserver {
public:
class Delegate {
public:
// The given item meets the criteria for being displayed.
virtual void ActivateItem(const std::string& id) = 0;
// The given item no longer meets the criteria for being displayed, but may
// be reactivated.
virtual void HideItem(const std::string& id) = 0;
// The given item should be destroyed.
virtual void RemoveItem(const std::string& id) = 0;
// The given item's UI should be refreshed.
virtual void RefreshItem(const std::string& id) = 0;
// The given button has been pressed, and therefore the action should be
// recorded.
virtual void LogMediaSessionActionButtonPressed(
const std::string& id,
media_session::mojom::MediaSessionAction action) = 0;
protected:
virtual ~Delegate() = default;
};
MediaSessionNotificationItem(
Delegate* delegate,
const std::string& request_id,
const std::string& source_name,
const std::optional<base::UnguessableToken>& source_id,
mojo::Remote<media_session::mojom::MediaController> controller,
media_session::mojom::MediaSessionInfoPtr session_info,
bool always_hidden);
MediaSessionNotificationItem(const MediaSessionNotificationItem&) = delete;
MediaSessionNotificationItem& operator=(const MediaSessionNotificationItem&) =
delete;
~MediaSessionNotificationItem() override;
// media_session::mojom::MediaControllerObserver:
void MediaSessionInfoChanged(
media_session::mojom::MediaSessionInfoPtr session_info) override;
void MediaSessionMetadataChanged(
const std::optional<media_session::MediaMetadata>& metadata) override;
void MediaSessionActionsChanged(
const std::vector<media_session::mojom::MediaSessionAction>& actions)
override;
void MediaSessionChanged(
const std::optional<base::UnguessableToken>& request_id) override {}
void MediaSessionPositionChanged(
const std::optional<media_session::MediaPosition>& position) override;
// Called when a media session item is associated with a presentation request
// to show the origin associated with the request rather than that for the
// top frame.
void UpdatePresentationRequestOrigin(const url::Origin& origin);
// Called during the creation of the footer view to show / set sink name if
// there is an active casting session associated with `this` media item.
void UpdateDeviceName(const std::optional<std::string>& device_name);
// media_session::mojom::MediaControllerImageObserver:
void MediaControllerImageChanged(
media_session::mojom::MediaSessionImageType type,
const SkBitmap& bitmap) override;
void MediaControllerChapterImageChanged(int chapter_index,
const SkBitmap& bitmap) override;
// media_message_center::MediaNotificationItem:
void SetView(media_message_center::MediaNotificationView* view) override;
void OnMediaSessionActionButtonPressed(
media_session::mojom::MediaSessionAction action) override;
void SeekTo(base::TimeDelta time) override;
// This will stop the media session associated with this item. The item will
// then call |MediaNotificationController::RemoveItem()| to ensure removal.
void Dismiss() override;
void SetVolume(float volume) override {}
void SetMute(bool mute) override;
bool RequestMediaRemoting() override;
media_message_center::Source GetSource() const override;
media_message_center::SourceType GetSourceType() const override;
std::optional<base::UnguessableToken> GetSourceId() const override;
// Stops the media session.
void Stop();
// Calls |Raise()| on the underlying MediaSession, which will focus the
// WebContents if the MediaSession is associated with one.
void Raise();
base::WeakPtr<MediaSessionNotificationItem> GetWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
void SetController(
mojo::Remote<media_session::mojom::MediaController> controller,
media_session::mojom::MediaSessionInfoPtr session_info);
// This will freeze the item and start a timer to destroy the item after
// some time has passed. If and when the item unfreezes, |unfrozen_callback|
// will be run. If the item does not unfreeze before timing out, then
// |unfrozen_callback| will not be called.
void Freeze(base::OnceClosure unfrozen_callback);
bool frozen() const { return frozen_; }
std::optional<std::string> device_name() const { return device_name_; }
// Returns nullptr if `remote_playback_disabled` is true in `session_info_` or
// the media duration is too short.
media_session::mojom::RemotePlaybackMetadataPtr GetRemotePlaybackMetadata()
const;
// Returns whether the item is actively playing.
bool IsPlaying() const;
void FlushForTesting();
private:
FRIEND_TEST_ALL_PREFIXES(MediaSessionNotificationItemTest,
GetSessionMetadata);
#if !BUILDFLAG(IS_CHROMEOS)
FRIEND_TEST_ALL_PREFIXES(MediaSessionNotificationItemTest,
GetSessionMetadataForUpdatedUI);
#endif
FRIEND_TEST_ALL_PREFIXES(MediaSessionNotificationItemTest,
GetMediaSessionActions);
FRIEND_TEST_ALL_PREFIXES(MediaSessionNotificationItemTest,
ShouldShowNotification);
media_session::MediaMetadata GetSessionMetadata() const;
base::flat_set<media_session::mojom::MediaSessionAction>
GetMediaSessionActions() const;
bool ShouldShowNotification() const;
void MaybeUnfreeze();
void UnfreezeNonArtwork();
void UnfreezeArtwork();
bool HasActions() const;
bool HasArtwork() const;
// Returns true if there's an image at the chapter `index`.
bool HasChapterArtwork(int index) const;
void OnFreezeTimerFired();
void MaybeHideOrShowNotification();
void UpdateViewCommon();
// Returns true if we're currently frozen and the frozen view contains
// non-null artwork at any chapter index.
bool FrozenWithChapterArtwork();
const raw_ptr<Delegate> delegate_;
bool is_bound_ = true;
// Weak reference to the view of the currently shown media notification.
raw_ptr<media_message_center::MediaNotificationView> view_ = nullptr;
// The |request_id_| is the request id of the media session and is guaranteed
// to be globally unique.
const std::string request_id_;
// The source of the media session.
const media_message_center::Source source_;
// The ID assigned to `source_`.
std::optional<base::UnguessableToken> source_id_;
mojo::Remote<media_session::mojom::MediaController> media_controller_remote_;
media_session::mojom::MediaSessionInfoPtr session_info_;
media_session::MediaMetadata session_metadata_;
// When a media session item is associated with a presentation request, we
// must show the origin associated with the request rather than that for the
// top frame. So, in case of having a presentation request, this field is set
// to hold the origin of that presentation request.
std::optional<url::Origin> optional_presentation_request_origin_;
// This name is used when the playback is happening on a non-default playback
// device.
std::optional<std::string> device_name_;
base::flat_set<media_session::mojom::MediaSessionAction> session_actions_;
std::optional<media_session::MediaPosition> session_position_;
std::optional<gfx::ImageSkia> session_artwork_;
std::optional<gfx::ImageSkia> session_favicon_;
// This map carries the index as the key and the image at this chapter index
// as the value.
base::flat_map<int, gfx::ImageSkia> chapter_artwork_;
// True if the metadata needs to be updated on |view_|. Used to prevent
// updating |view_|'s metadata twice on a single change.
bool view_needs_metadata_update_ = false;
// When the item is frozen the |view_| will not receive any updates to the
// data and no actions will be executed.
bool frozen_ = false;
// True if we're currently frozen and the frozen view contains at least 1
// action.
bool frozen_with_actions_ = false;
// True if we have the necessary metadata to unfreeze, but we're waiting for
// new actions.
bool waiting_for_actions_ = false;
// True if we're currently frozen and the frozen view contains non-null
// artwork.
bool frozen_with_artwork_ = false;
// True if the notification item should always be hidden.
const bool always_hidden_;
// The value is true if we're currently frozen and the frozen view contains
// non-null artwork at the chapter index.
base::flat_map<int, bool> frozen_with_chapter_artwork_;
// The timer that will notify the controller to destroy this item after it
// has been frozen for a certain period of time.
base::OneShotTimer freeze_timer_;
// Called when the item unfreezes.
base::OnceClosure unfrozen_callback_;
mojo::Receiver<media_session::mojom::MediaControllerObserver>
observer_receiver_{this};
mojo::Receiver<media_session::mojom::MediaControllerImageObserver>
artwork_observer_receiver_{this};
mojo::Receiver<media_session::mojom::MediaControllerImageObserver>
favicon_observer_receiver_{this};
mojo::Receiver<media_session::mojom::MediaControllerImageObserver>
chapter_observer_receiver_{this};
base::WeakPtrFactory<MediaSessionNotificationItem> weak_ptr_factory_{this};
};
} // namespace global_media_controls
#endif // COMPONENTS_GLOBAL_MEDIA_CONTROLS_PUBLIC_MEDIA_SESSION_NOTIFICATION_ITEM_H_
|