tor-browser

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

URLMainThread.cpp (3054B)


      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 "URLMainThread.h"
      8 
      9 #include "mozilla/dom/BindingUtils.h"
     10 #include "mozilla/dom/Blob.h"
     11 #include "mozilla/dom/BlobURLProtocolHandler.h"
     12 #include "mozilla/dom/Document.h"
     13 #include "nsContentUtils.h"
     14 #include "nsNetUtil.h"
     15 #include "nsThreadUtils.h"
     16 
     17 namespace mozilla::dom {
     18 
     19 /* static */
     20 void URLMainThread::CreateObjectURL(const GlobalObject& aGlobal,
     21                                    const BlobOrMediaSource& aObj,
     22                                    nsACString& aResult, ErrorResult& aRv) {
     23  MOZ_ASSERT(NS_IsMainThread());
     24 
     25  nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
     26  if (NS_WARN_IF(!global)) {
     27    aRv.Throw(NS_ERROR_FAILURE);
     28    return;
     29  }
     30 
     31  nsAutoString partKey;
     32  if (nsCOMPtr<nsPIDOMWindowInner> owner = do_QueryInterface(global)) {
     33    if (Document* doc = owner->GetExtantDoc()) {
     34      nsCOMPtr<nsICookieJarSettings> cookieJarSettings =
     35          doc->CookieJarSettings();
     36 
     37      cookieJarSettings->GetPartitionKey(partKey);
     38    }
     39  }
     40 
     41  nsCOMPtr<nsIPrincipal> principal =
     42      nsContentUtils::ObjectPrincipal(aGlobal.Get());
     43 
     44  if (aObj.IsBlob()) {
     45    aRv = BlobURLProtocolHandler::AddDataEntry(
     46        aObj.GetAsBlob().Impl(), principal, NS_ConvertUTF16toUTF8(partKey),
     47        aResult);
     48  } else if (aObj.IsMediaSource()) {
     49    aRv = BlobURLProtocolHandler::AddDataEntry(
     50        &aObj.GetAsMediaSource(), principal, NS_ConvertUTF16toUTF8(partKey),
     51        aResult);
     52  } else {
     53    MOZ_CRASH("Invalid type for a BlobOrMediaSource");
     54  }
     55 
     56  if (NS_WARN_IF(aRv.Failed())) {
     57    return;
     58  }
     59 
     60  global->RegisterHostObjectURI(aResult);
     61 }
     62 
     63 /* static */
     64 void URLMainThread::RevokeObjectURL(const GlobalObject& aGlobal,
     65                                    const nsACString& aURL, ErrorResult& aRv) {
     66  MOZ_ASSERT(NS_IsMainThread());
     67  nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
     68  if (!global) {
     69    aRv.Throw(NS_ERROR_FAILURE);
     70    return;
     71  }
     72 
     73  nsAutoString partKey;
     74  if (nsCOMPtr<nsPIDOMWindowInner> owner = do_QueryInterface(global)) {
     75    if (Document* doc = owner->GetExtantDoc()) {
     76      nsCOMPtr<nsICookieJarSettings> cookieJarSettings =
     77          doc->CookieJarSettings();
     78 
     79      cookieJarSettings->GetPartitionKey(partKey);
     80    }
     81  }
     82 
     83  if (BlobURLProtocolHandler::RemoveDataEntry(
     84          aURL, nsContentUtils::ObjectPrincipal(aGlobal.Get()),
     85          NS_ConvertUTF16toUTF8(partKey))) {
     86    global->UnregisterHostObjectURI(aURL);
     87  }
     88 }
     89 
     90 // static
     91 bool URLMainThread::IsBoundToBlob(const GlobalObject& aGlobal,
     92                                  const nsACString& aURL, ErrorResult& aRv) {
     93  MOZ_ASSERT(NS_IsMainThread());
     94  return BlobURLProtocolHandler::HasDataEntryTypeBlob(aURL);
     95 }
     96 
     97 }  // namespace mozilla::dom