Bug 1943650 - Command-line --help output misformatted after --dbus-service. r=emilio
[gecko.git] / toolkit / components / credentialmanagement / IdentityCredentialRequestManager.cpp
blobcafabff589f9acac22b30e7f1d0cd4a69b84dbde
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
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "IdentityCredentialRequestManager.h"
8 #include "mozilla/dom/IdentityCredentialBinding.h"
9 #include "mozilla/dom/WindowGlobalParent.h"
10 #include "nsContentUtils.h"
11 #include "nsNetUtil.h"
12 #include "mozilla/BasePrincipal.h"
14 namespace mozilla {
16 NS_IMPL_ISUPPORTS0(IdentityCredentialRequestManager);
18 StaticRefPtr<IdentityCredentialRequestManager>
19 IdentityCredentialRequestManager::sSingleton;
21 // static
22 IdentityCredentialRequestManager*
23 IdentityCredentialRequestManager::GetInstance() {
24 if (!sSingleton) {
25 sSingleton = new IdentityCredentialRequestManager();
26 ClearOnShutdown(&sSingleton);
28 return sSingleton;
31 nsresult IdentityCredentialRequestManager::StorePendingRequest(
32 const nsCOMPtr<nsIPrincipal>& aRPPrincipal,
33 const dom::IdentityCredentialRequestOptions& aRequest,
34 const RefPtr<
35 dom::IdentityCredential::GetIPCIdentityCredentialPromise::Private>&
36 aPromise,
37 const RefPtr<dom::CanonicalBrowsingContext>& aBrowsingContext) {
38 MOZ_ASSERT(aRPPrincipal);
40 if (!aRequest.mProviders.WasPassed()) {
41 return NS_ERROR_DOM_INVALID_ACCESS_ERR;
44 for (const auto& provider : aRequest.mProviders.Value()) {
45 if (!provider.mLoginURL.WasPassed()) {
46 continue;
48 nsCOMPtr<nsIURI> idpOriginURI;
49 nsAutoCString idpOriginString;
50 if (provider.mOrigin.WasPassed()) {
51 idpOriginString = provider.mOrigin.Value();
52 } else {
53 // Infer the origin from the loginURL if one wasn't provided
54 idpOriginString = provider.mLoginURL.Value();
56 nsresult rv = NS_NewURI(getter_AddRefs(idpOriginURI), idpOriginString);
57 if (NS_WARN_IF(NS_FAILED(rv))) {
58 return NS_ERROR_DOM_BAD_URI;
60 nsCOMPtr<nsIPrincipal> idpPrincipal = BasePrincipal::CreateContentPrincipal(
61 idpOriginURI, aRPPrincipal->OriginAttributesRef());
63 NS_ENSURE_TRUE(idpPrincipal, NS_ERROR_FAILURE);
64 nsTArray<PendingRequestEntry>& list =
65 mPendingRequests.LookupOrInsert(idpPrincipal);
66 list.AppendElement(PendingRequestEntry(aRPPrincipal, aRequest, aPromise,
67 aBrowsingContext));
69 return NS_OK;
72 void IdentityCredentialRequestManager::NotifyOfStoredCredential(
73 const nsCOMPtr<nsIPrincipal>& aIDPPrincipal,
74 const dom::IPCIdentityCredential& aCredential) {
75 MOZ_ASSERT(aIDPPrincipal);
76 auto listLookup = mPendingRequests.Lookup(aIDPPrincipal);
77 if (listLookup) {
78 for (auto& entry : listLookup.Data()) {
79 if (!entry.mBrowsingContext) {
80 continue;
82 // We must (asynchronously) test if this credential should be sent down
83 // to the site.
84 dom::IdentityCredential::AllowedToCollectCredential(
85 entry.mRPPrincipal, entry.mBrowsingContext, entry.mRequestOptions,
86 aCredential)
87 ->Then(
88 GetCurrentSerialEventTarget(), __func__,
89 [aCredential, entry](bool effectiveCredential) {
90 if (effectiveCredential) {
91 entry.mPromise->Resolve(aCredential, __func__);
94 []() {});
99 } // namespace mozilla