Bug 1685822 [wpt PR 27117] - [Import Maps] Add tests for rejecting multiple import...
[gecko.git] / dom / media / MediaPlaybackDelayPolicy.h
blobc66d1b69e8759d1e6cbc8dc0e9803d747f54e1eb
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_mediaplaybackdelaypolicy_h__
8 #define mozilla_dom_mediaplaybackdelaypolicy_h__
10 #include "AudioChannelAgent.h"
11 #include "AudioChannelService.h"
12 #include "mozilla/MozPromise.h"
13 #include "mozilla/RefPtr.h"
14 #include "nsISupportsImpl.h"
16 typedef uint32_t SuspendTypes;
18 namespace mozilla {
19 namespace dom {
21 class HTMLMediaElement;
22 /**
23 * We usaually start AudioChannelAgent when media starts and stop it when media
24 * stops. However, when we decide to delay media playback for unvisited tab, we
25 * would start AudioChannelAgent even if media doesn't start in order to
26 * register the agent to AudioChannelService, so that the service could notify
27 * us when we are able to resume media playback. Therefore,
28 * ResumeDelayedPlaybackAgent is used to handle this special use case of
29 * AudioChannelAgent.
30 * - Use `GetResumePromise()` to require resume-promise and then do follow-up
31 * resume behavior when promise is resolved.
32 * - Use `UpdateAudibleState()` to update audible state only when media info
33 * changes. As having audio track or not is the only thing for us to decide
34 * whether we would show the delayed media playback icon on the tab bar.
36 class ResumeDelayedPlaybackAgent {
37 public:
38 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ResumeDelayedPlaybackAgent);
39 ResumeDelayedPlaybackAgent() = default;
41 using ResumePromise = MozPromise<bool, bool, true /* IsExclusive */>;
42 RefPtr<ResumePromise> GetResumePromise();
43 void UpdateAudibleState(const HTMLMediaElement* aElement, bool aIsAudible);
45 private:
46 friend class MediaPlaybackDelayPolicy;
48 ~ResumeDelayedPlaybackAgent();
49 bool InitDelegate(const HTMLMediaElement* aElement, bool aIsAudible);
51 class ResumePlayDelegate final : public nsIAudioChannelAgentCallback {
52 public:
53 NS_DECL_ISUPPORTS
55 ResumePlayDelegate() = default;
57 bool Init(const HTMLMediaElement* aElement, bool aIsAudible);
58 void UpdateAudibleState(const HTMLMediaElement* aElement, bool aIsAudible);
59 RefPtr<ResumePromise> GetResumePromise();
60 void Clear();
62 NS_IMETHODIMP WindowVolumeChanged(float aVolume, bool aMuted) override;
63 NS_IMETHODIMP WindowAudioCaptureChanged(bool aCapture) override;
64 NS_IMETHODIMP WindowSuspendChanged(SuspendTypes aSuspend) override;
66 private:
67 virtual ~ResumePlayDelegate();
69 MozPromiseHolder<ResumePromise> mPromise;
70 RefPtr<AudioChannelAgent> mAudioChannelAgent;
73 RefPtr<ResumePlayDelegate> mDelegate;
76 class MediaPlaybackDelayPolicy {
77 public:
78 static bool ShouldDelayPlayback(const HTMLMediaElement* aElement);
79 static RefPtr<ResumeDelayedPlaybackAgent> CreateResumeDelayedPlaybackAgent(
80 const HTMLMediaElement* aElement, bool aIsAudible);
83 } // namespace dom
84 } // namespace mozilla
86 #endif