Bug 1839170 - Refactor Snap pulling, Add Firefox Snap Core22 and GNOME 42 SDK symbols...
[gecko.git] / dom / media / eme / MediaKeySystemAccessPermissionRequest.cpp
blob063bf93f7ecc776db178c84db9de784b69784f18
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 #include "MediaKeySystemAccessPermissionRequest.h"
9 #include "nsGlobalWindowInner.h"
11 namespace mozilla::dom {
13 NS_IMPL_CYCLE_COLLECTION_INHERITED(MediaKeySystemAccessPermissionRequest,
14 ContentPermissionRequestBase)
16 NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED_0(
17 MediaKeySystemAccessPermissionRequest, ContentPermissionRequestBase)
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;
30 nsGlobalWindowInner* innerWindow = nsGlobalWindowInner::Cast(aWindow);
31 if (!innerWindow->GetPrincipal()) {
32 return nullptr;
35 RefPtr<MediaKeySystemAccessPermissionRequest> request =
36 new MediaKeySystemAccessPermissionRequest(innerWindow);
37 return request.forget();
40 MediaKeySystemAccessPermissionRequest::MediaKeySystemAccessPermissionRequest(
41 nsGlobalWindowInner* aWindow)
42 : ContentPermissionRequestBase(aWindow->GetPrincipal(), aWindow,
43 "media.eme.require-app-approval"_ns,
44 "media-key-system-access"_ns) {}
46 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();
54 already_AddRefed<MediaKeySystemAccessPermissionRequest::RequestPromise>
55 MediaKeySystemAccessPermissionRequest::GetPromise() {
56 return mPromiseHolder.Ensure(__func__);
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);
69 if (promptResult ==
70 MediaKeySystemAccessPermissionRequest::PromptResult::Denied) {
71 return Cancel();
74 return nsContentPermissionUtils::AskPermission(this, mWindow);
77 NS_IMETHODIMP
78 MediaKeySystemAccessPermissionRequest::Allow(JS::Handle<JS::Value> aChoices) {
79 AssertIsOnMainThread();
80 mPromiseHolder.ResolveIfExists(true, __func__);
81 return NS_OK;
84 NS_IMETHODIMP
85 MediaKeySystemAccessPermissionRequest::Cancel() {
86 AssertIsOnMainThread();
87 mPromiseHolder.RejectIfExists(false, __func__);
88 return NS_OK;
91 } // namespace mozilla::dom