From 19dbb2e44c1705ed16a0d8d441a1c76cbe9939da Mon Sep 17 00:00:00 2001 From: Bryce Seager van Dyk Date: Mon, 24 Feb 2020 21:21:09 +0000 Subject: [PATCH] Bug 1587522 - Add MediaKeySystemAccessPermissionRequest. r=alwu This class will be used to surface permission requests to GeckoView. Differential Revision: https://phabricator.services.mozilla.com/D56103 --HG-- extra : moz-landing-system : lando --- .../MediaKeySystemAccessPermissionRequest.cpp | 94 +++++++++++++++++++ .../MediaKeySystemAccessPermissionRequest.h | 74 +++++++++++++++ dom/media/eme/moz.build | 2 + 3 files changed, 170 insertions(+) create mode 100644 dom/media/eme/MediaKeySystemAccessPermissionRequest.cpp create mode 100644 dom/media/eme/MediaKeySystemAccessPermissionRequest.h diff --git a/dom/media/eme/MediaKeySystemAccessPermissionRequest.cpp b/dom/media/eme/MediaKeySystemAccessPermissionRequest.cpp new file mode 100644 index 000000000000..f5306996e65c --- /dev/null +++ b/dom/media/eme/MediaKeySystemAccessPermissionRequest.cpp @@ -0,0 +1,94 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim:set ts=2 sw=2 sts=2 et cindent: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "MediaKeySystemAccessPermissionRequest.h" + +#include "nsGlobalWindowInner.h" + +namespace mozilla { +namespace dom { + +NS_IMPL_CYCLE_COLLECTION_INHERITED(MediaKeySystemAccessPermissionRequest, + ContentPermissionRequestBase) + +NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED_0( + MediaKeySystemAccessPermissionRequest, ContentPermissionRequestBase) + +/* static */ +already_AddRefed +MediaKeySystemAccessPermissionRequest::Create(nsPIDOMWindowInner* aWindow) { + // Could conceivably be created off main thread then used on main thread + // later. If we really need to do that at some point we could relax this + // assert. + AssertIsOnMainThread(); + if (!aWindow) { + return nullptr; + } + + nsGlobalWindowInner* innerWindow = nsGlobalWindowInner::Cast(aWindow); + if (!innerWindow->GetPrincipal()) { + return nullptr; + } + + RefPtr request = + new MediaKeySystemAccessPermissionRequest(innerWindow); + return request.forget(); +} + +MediaKeySystemAccessPermissionRequest::MediaKeySystemAccessPermissionRequest( + nsGlobalWindowInner* aWindow) + : ContentPermissionRequestBase( + aWindow->GetPrincipal(), aWindow, + NS_LITERAL_CSTRING("media.eme.require-app-approval"), + NS_LITERAL_CSTRING("media-key-system-access")) {} + +MediaKeySystemAccessPermissionRequest:: + ~MediaKeySystemAccessPermissionRequest() { + AssertIsOnMainThread(); + // If a request has not been serviced by the time it is destroyed, treat it + // as if the request was denied. + Cancel(); +} + +already_AddRefed +MediaKeySystemAccessPermissionRequest::GetPromise() { + return mPromiseHolder.Ensure(__func__); +} + +nsresult MediaKeySystemAccessPermissionRequest::Start() { + // Check test prefs to see if we should short circuit. We want to do this + // before checking the cached value so we can have pref changes take effect + // without refreshing the page. + MediaKeySystemAccessPermissionRequest::PromptResult promptResult = + CheckPromptPrefs(); + if (promptResult == + MediaKeySystemAccessPermissionRequest::PromptResult::Granted) { + return Allow(JS::UndefinedHandleValue); + } + if (promptResult == + MediaKeySystemAccessPermissionRequest::PromptResult::Denied) { + return Cancel(); + } + + return nsContentPermissionUtils::AskPermission(this, mWindow); +} + +NS_IMETHODIMP +MediaKeySystemAccessPermissionRequest::Allow(JS::HandleValue aChoices) { + AssertIsOnMainThread(); + mPromiseHolder.ResolveIfExists(true, __func__); + return NS_OK; +} + +NS_IMETHODIMP +MediaKeySystemAccessPermissionRequest::Cancel() { + AssertIsOnMainThread(); + mPromiseHolder.RejectIfExists(false, __func__); + return NS_OK; +} + +} // namespace dom +} // namespace mozilla diff --git a/dom/media/eme/MediaKeySystemAccessPermissionRequest.h b/dom/media/eme/MediaKeySystemAccessPermissionRequest.h new file mode 100644 index 000000000000..e68e61424e4a --- /dev/null +++ b/dom/media/eme/MediaKeySystemAccessPermissionRequest.h @@ -0,0 +1,74 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim:set ts=2 sw=2 sts=2 et cindent: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef DOM_MEDIA_EME_MEDIAKEYSYSTEMACCESSPERMISSIONREQUEST_H_ +#define DOM_MEDIA_EME_MEDIAKEYSYSTEMACCESSPERMISSIONREQUEST_H_ + +#include "mozilla/MozPromise.h" +#include "nsContentPermissionHelper.h" + +namespace mozilla { +namespace dom { + +/** + * This class encapsulates a permission request to allow media key system + * access. The intention is not for this class to be used in all cases of EME, + * but only when we need to seek explicit approval from an application using + * Gecko, such as an application embedding via GeckoView. + * + * media.eme.require-app-approval should be used to gate this functionality in + * gecko code, and is also used as the testing pref for + * ContentPermissionRequestBase. I.e. CheckPromptPrefs() will respond to having + * `media.eme.require-app-approval.prompt.testing` and + * `media.eme.require-app-approval.prompt.testing.allow` being set to true or + * false and will return an appropriate value to allow for test code to short + * circuit showing a prompt. Note that the code using this class needs to call + * CheckPromptPrefs and implement test specific logic, it is *not* handled by + * this class or ContentPermissionRequestBase. + * + * Expects to be used on main thread as ContentPermissionRequestBase uses + * PContentPermissionRequest which is managed by PContent which is main thread + * to main thread communication. + */ +class MediaKeySystemAccessPermissionRequest + : public ContentPermissionRequestBase { + public: + NS_DECL_ISUPPORTS_INHERITED + NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED( + MediaKeySystemAccessPermissionRequest, ContentPermissionRequestBase) + + using RequestPromise = MozPromise; + + // Create a MediaKeySystemAccessPermissionRequest. + // @param aWindow The window associated with this request. + static already_AddRefed Create( + nsPIDOMWindowInner* aWindow); + + // Returns a promise that will be resolved if this request is allowed or + // rejected in the case the request is denied. If allowed the promise + // will resolve with true, otherwise it will resolve with false. + already_AddRefed GetPromise(); + + // Helper function that triggers the request. This function will check + // prefs and cancel or allow the request if the appropriate prefs are set, + // otherwise it will fire the request to the associated window. + nsresult Start(); + + // nsIContentPermissionRequest methods + NS_IMETHOD Cancel(void) override; + NS_IMETHOD Allow(JS::HandleValue choices) override; + + private: + explicit MediaKeySystemAccessPermissionRequest(nsGlobalWindowInner* aWindow); + ~MediaKeySystemAccessPermissionRequest(); + + MozPromiseHolder mPromiseHolder; +}; + +} // namespace dom +} // namespace mozilla + +#endif // DOM_MEDIA_EME_MEDIAKEYSYSTEMACCESSPERMISSIONREQUEST_H_ diff --git a/dom/media/eme/moz.build b/dom/media/eme/moz.build index 05f7a8e781fb..a2e35deb4fb1 100644 --- a/dom/media/eme/moz.build +++ b/dom/media/eme/moz.build @@ -13,6 +13,7 @@ EXPORTS.mozilla.dom += [ 'MediaKeyStatusMap.h', 'MediaKeySystemAccess.h', 'MediaKeySystemAccessManager.h', + 'MediaKeySystemAccessPermissionRequest.h', ] EXPORTS.mozilla += [ @@ -35,6 +36,7 @@ UNIFIED_SOURCES += [ 'MediaKeyStatusMap.cpp', 'MediaKeySystemAccess.cpp', 'MediaKeySystemAccessManager.cpp', + 'MediaKeySystemAccessPermissionRequest.cpp', ] if CONFIG['MOZ_WIDGET_TOOLKIT'] == 'android':