Bug 1494333 - index crons just like artifacts r=Callek
[gecko.git] / dom / html / AutoplayPermissionManager.h
bloba861f94abe034af975213348050f32ec53e5b8b2
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
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
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef __AutoplayPermissionRequestManager_h__
8 #define __AutoplayPermissionRequestManager_h__
10 #include "mozilla/MozPromise.h"
11 #include "mozilla/WeakPtr.h"
12 #include "nsIContentPermissionPrompt.h"
13 #include "nsISupports.h"
14 #include "nsIWeakReferenceUtils.h"
15 #include "nsWeakReference.h"
17 class nsGlobalWindowInner;
18 class nsIEventTarget;
20 namespace mozilla {
22 // Encapsulates requesting permission from the user to autoplay with a
23 // doorhanger. The AutoplayPermissionManager is stored on the top level window,
24 // and all documents in the tab use the top level window's
25 // AutoplayPermissionManager. The AutoplayPermissionManager ensures that
26 // multiple requests are merged into one, in order to avoid showing multiple
27 // doorhangers on one tab at once.
28 class AutoplayPermissionManager final
29 : public SupportsWeakPtr<AutoplayPermissionManager>
31 public:
32 MOZ_DECLARE_WEAKREFERENCE_TYPENAME(AutoplayPermissionManager)
33 NS_INLINE_DECL_REFCOUNTING(AutoplayPermissionManager)
35 // Creates a new AutoplayPermissionManager. Don't call this directly, use
36 // AutoplayPolicy::RequestFor() to retrieve the appropriate
37 // AutoplayPermissionManager to use for a document/window.
38 explicit AutoplayPermissionManager(nsGlobalWindowInner* aWindow);
40 // Requests permission to autoplay via a user prompt. The returned MozPromise
41 // resolves/rejects when the user grants/denies permission to autoplay.
42 // If the request doorhanger is dismissed, i.e. tab closed, ESC pressed,
43 // etc, the MozPromise is rejected. If there is a stored permission for this
44 // window's origin, the parent process will approve/deny the
45 // autoplay request using the stored permission immediately (so the MozPromise
46 // rejects in the content process after an IPC round trip).
47 RefPtr<GenericPromise> RequestWithPrompt();
49 // Called by AutoplayPermissionRequest to approve the play request,
50 // and resolve the MozPromise returned by RequestWithPrompt().
51 void ApprovePlayRequestIfExists();
52 // Called by AutoplayPermissionRequest to deny the play request or the inner
53 // window is going to be destroyed, this function would reject the MozPromise
54 // returned by RequestWithPrompt().
55 void DenyPlayRequestIfExists();
57 private:
58 ~AutoplayPermissionManager();
60 nsWeakPtr mWindow;
61 MozPromiseHolder<GenericPromise> mPromiseHolder;
62 // Tracks whether we've dispatched a request to chrome in the parent process
63 // to prompt for user permission to autoplay. This flag ensures we don't
64 // request multiple times concurrently.
65 bool mRequestDispatched = false;
68 } // namespace mozilla
70 #endif // __AutoplayPermissionRequestManager_h__