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/. */
6
7 #include "MediaKeySystemAccessPermissionRequest.h"
8
9 #include "nsGlobalWindowInner.h"
10
11 namespace mozilla::dom {
12
NS_IMPL_CYCLE_COLLECTION_INHERITED(MediaKeySystemAccessPermissionRequest,ContentPermissionRequestBase)13 NS_IMPL_CYCLE_COLLECTION_INHERITED(MediaKeySystemAccessPermissionRequest,
14 ContentPermissionRequestBase)
15
16 NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED_0(
17 MediaKeySystemAccessPermissionRequest, ContentPermissionRequestBase)
18
19 /* static */
20 already_AddRefed<MediaKeySystemAccessPermissionRequest>
21 MediaKeySystemAccessPermissionRequest::Create(nsPIDOMWindowInner* aWindow) {
22 // Could conceivably be created off main thread then used on main thread
23 // later. If we really need to do that at some point we could relax this
24 // assert.
25 AssertIsOnMainThread();
26 if (!aWindow) {
27 return nullptr;
28 }
29
30 nsGlobalWindowInner* innerWindow = nsGlobalWindowInner::Cast(aWindow);
31 if (!innerWindow->GetPrincipal()) {
32 return nullptr;
33 }
34
35 RefPtr<MediaKeySystemAccessPermissionRequest> request =
36 new MediaKeySystemAccessPermissionRequest(innerWindow);
37 return request.forget();
38 }
39
MediaKeySystemAccessPermissionRequest(nsGlobalWindowInner * aWindow)40 MediaKeySystemAccessPermissionRequest::MediaKeySystemAccessPermissionRequest(
41 nsGlobalWindowInner* aWindow)
42 : ContentPermissionRequestBase(aWindow->GetPrincipal(), aWindow,
43 "media.eme.require-app-approval"_ns,
44 "media-key-system-access"_ns) {}
45
46 MediaKeySystemAccessPermissionRequest::
~MediaKeySystemAccessPermissionRequest()47 ~MediaKeySystemAccessPermissionRequest() {
48 AssertIsOnMainThread();
49 // If a request has not been serviced by the time it is destroyed, treat it
50 // as if the request was denied.
51 Cancel();
52 }
53
54 already_AddRefed<MediaKeySystemAccessPermissionRequest::RequestPromise>
GetPromise()55 MediaKeySystemAccessPermissionRequest::GetPromise() {
56 return mPromiseHolder.Ensure(__func__);
57 }
58
Start()59 nsresult MediaKeySystemAccessPermissionRequest::Start() {
60 // Check test prefs to see if we should short circuit. We want to do this
61 // before checking the cached value so we can have pref changes take effect
62 // without refreshing the page.
63 MediaKeySystemAccessPermissionRequest::PromptResult promptResult =
64 CheckPromptPrefs();
65 if (promptResult ==
66 MediaKeySystemAccessPermissionRequest::PromptResult::Granted) {
67 return Allow(JS::UndefinedHandleValue);
68 }
69 if (promptResult ==
70 MediaKeySystemAccessPermissionRequest::PromptResult::Denied) {
71 return Cancel();
72 }
73
74 return nsContentPermissionUtils::AskPermission(this, mWindow);
75 }
76
77 NS_IMETHODIMP
Allow(JS::HandleValue aChoices)78 MediaKeySystemAccessPermissionRequest::Allow(JS::HandleValue aChoices) {
79 AssertIsOnMainThread();
80 mPromiseHolder.ResolveIfExists(true, __func__);
81 return NS_OK;
82 }
83
84 NS_IMETHODIMP
Cancel()85 MediaKeySystemAccessPermissionRequest::Cancel() {
86 AssertIsOnMainThread();
87 mPromiseHolder.RejectIfExists(false, __func__);
88 return NS_OK;
89 }
90
91 } // namespace mozilla::dom
92