tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

IdentityCredential.cpp (3220B)


      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/. */
      6 
      7 #include "mozilla/dom/IdentityCredential.h"
      8 
      9 #include "mozilla/dom/Document.h"
     10 #include "mozilla/dom/Fetch.h"
     11 #include "mozilla/dom/Promise.h"
     12 #include "mozilla/dom/WebIdentityHandler.h"
     13 #include "nsGlobalWindowInner.h"
     14 #include "nsIGlobalObject.h"
     15 #include "nsIIdentityCredentialStorageService.h"
     16 #include "nsNetUtil.h"
     17 #include "nsTArray.h"
     18 
     19 namespace mozilla::dom {
     20 
     21 IdentityCredential::~IdentityCredential() = default;
     22 
     23 JSObject* IdentityCredential::WrapObject(JSContext* aCx,
     24                                         JS::Handle<JSObject*> aGivenProto) {
     25  return IdentityCredential_Binding::Wrap(aCx, this, aGivenProto);
     26 }
     27 
     28 IdentityCredential::IdentityCredential(nsPIDOMWindowInner* aParent,
     29                                       const IPCIdentityCredential& aOther)
     30    : Credential(aParent) {
     31  CopyValuesFrom(aOther);
     32 }
     33 
     34 void IdentityCredential::CopyValuesFrom(const IPCIdentityCredential& aOther) {
     35  this->SetId(aOther.id());
     36  this->SetType(u"identity"_ns);
     37  if (aOther.token().isSome()) {
     38    this->mToken = aOther.token().value();
     39  }
     40  this->mIsAutoSelected = aOther.isAutoSelected();
     41  this->mConfigURL = aOther.configURL();
     42 }
     43 
     44 IPCIdentityCredential IdentityCredential::MakeIPCIdentityCredential() const {
     45  IPCIdentityCredential result;
     46  this->GetId(result.id());
     47  if (!this->mToken.IsEmpty()) {
     48    result.token() = Some(this->mToken);
     49  }
     50  result.isAutoSelected() = mIsAutoSelected;
     51  result.configURL() = mConfigURL;
     52  return result;
     53 }
     54 
     55 void IdentityCredential::GetToken(nsACString& aToken) const {
     56  aToken.Assign(mToken);
     57 }
     58 void IdentityCredential::SetToken(const nsACString& aToken) {
     59  mToken.Assign(aToken);
     60 }
     61 bool IdentityCredential::IsAutoSelected() const { return mIsAutoSelected; }
     62 void IdentityCredential::GetConfigURL(nsACString& aConfigURL) const {
     63  aConfigURL.Assign(mConfigURL);
     64 }
     65 void IdentityCredential::SetConfigURL(const nsACString& aConfigURL) {
     66  mConfigURL.Assign(aConfigURL);
     67 }
     68 
     69 // static
     70 already_AddRefed<Promise> IdentityCredential::Disconnect(
     71    const GlobalObject& aGlobal,
     72    const IdentityCredentialDisconnectOptions& aOptions, ErrorResult& aRv) {
     73  nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
     74  if (!global) {
     75    aRv.ThrowNotAllowedError("Must be called on an appropriate global object.");
     76    return nullptr;
     77  }
     78  nsPIDOMWindowInner* window = global->GetAsInnerWindow();
     79  if (!window) {
     80    aRv.ThrowNotAllowedError("Must be called on a window.");
     81    return nullptr;
     82  }
     83  RefPtr<Promise> promise = Promise::Create(global, aRv);
     84  if (NS_WARN_IF(aRv.Failed() || !promise)) {
     85    return nullptr;
     86  }
     87  WebIdentityHandler* identityHandler = window->GetOrCreateWebIdentityHandler();
     88  if (!identityHandler) {
     89    promise->MaybeRejectWithOperationError("");
     90    return promise.forget();
     91  }
     92  identityHandler->Disconnect(aOptions, promise);
     93  return promise.forget();
     94 }
     95 
     96 }  // namespace mozilla::dom