tor-browser

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

LocalStorageCommon.cpp (3364B)


      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 "LocalStorageCommon.h"
      8 
      9 #include <cstdint>
     10 
     11 #include "MainThreadUtils.h"
     12 #include "mozilla/Assertions.h"
     13 #include "mozilla/Atomics.h"
     14 #include "mozilla/Logging.h"
     15 #include "mozilla/OriginAttributes.h"
     16 #include "mozilla/Preferences.h"
     17 #include "mozilla/StaticMutex.h"
     18 #include "mozilla/StaticPrefs_dom.h"
     19 #include "mozilla/dom/StorageUtils.h"
     20 #include "mozilla/dom/quota/ResultExtensions.h"
     21 #include "mozilla/ipc/PBackgroundSharedTypes.h"
     22 #include "mozilla/net/WebSocketFrame.h"
     23 #include "nsDebug.h"
     24 #include "nsError.h"
     25 #include "nsIURL.h"
     26 #include "nsNetUtil.h"
     27 #include "nsPrintfCString.h"
     28 #include "nsString.h"
     29 #include "nsStringFlags.h"
     30 #include "nsXULAppAPI.h"
     31 
     32 namespace mozilla::dom {
     33 
     34 using namespace mozilla::net;
     35 
     36 namespace {
     37 
     38 StaticMutex gNextGenLocalStorageMutex;
     39 Atomic<int32_t> gNextGenLocalStorageEnabled(-1);
     40 LazyLogModule gLogger("LocalStorage");
     41 
     42 }  // namespace
     43 
     44 const char16_t* kLocalStorageType = u"localStorage";
     45 
     46 bool NextGenLocalStorageEnabled() {
     47  if (XRE_IsParentProcess()) {
     48    StaticMutexAutoLock lock(gNextGenLocalStorageMutex);
     49 
     50    if (gNextGenLocalStorageEnabled == -1) {
     51      // Ideally all this Mutex stuff would be replaced with just using
     52      // an AtStartup StaticPref, but there are concerns about this causing
     53      // deadlocks if this access needs to init the AtStartup cache.
     54      bool enabled =
     55          !StaticPrefs::
     56              dom_storage_enable_unsupported_legacy_implementation_DoNotUseDirectly();
     57 
     58      gNextGenLocalStorageEnabled = enabled ? 1 : 0;
     59    }
     60 
     61    return !!gNextGenLocalStorageEnabled;
     62  }
     63 
     64  return CachedNextGenLocalStorageEnabled();
     65 }
     66 
     67 void RecvInitNextGenLocalStorageEnabled(const bool aEnabled) {
     68  MOZ_ASSERT(!XRE_IsParentProcess());
     69  MOZ_ASSERT(NS_IsMainThread());
     70  MOZ_ASSERT(gNextGenLocalStorageEnabled == -1);
     71 
     72  gNextGenLocalStorageEnabled = aEnabled ? 1 : 0;
     73 }
     74 
     75 bool CachedNextGenLocalStorageEnabled() {
     76  MOZ_ASSERT(gNextGenLocalStorageEnabled != -1);
     77 
     78  return !!gNextGenLocalStorageEnabled;
     79 }
     80 
     81 Result<std::pair<nsCString, nsCString>, nsresult> GenerateOriginKey2(
     82    const mozilla::ipc::PrincipalInfo& aPrincipalInfo) {
     83  if (aPrincipalInfo.type() !=
     84          mozilla::ipc::PrincipalInfo::TNullPrincipalInfo &&
     85      aPrincipalInfo.type() !=
     86          mozilla::ipc::PrincipalInfo::TContentPrincipalInfo) {
     87    return Err(NS_ERROR_UNEXPECTED);
     88  }
     89 
     90  Result<nsCOMPtr<nsIPrincipal>, nsresult> p =
     91      PrincipalInfoToPrincipal(aPrincipalInfo);
     92  if (p.isErr()) {
     93    return Err(p.unwrapErr());
     94  }
     95 
     96  nsCOMPtr<nsIPrincipal> principal = p.unwrap();
     97  if (!principal) {
     98    return Err(NS_ERROR_NULL_POINTER);
     99  }
    100 
    101  nsCString originKey;
    102  nsresult rv = principal->GetStorageOriginKey(originKey);
    103  if (NS_FAILED(rv)) {
    104    return Err(rv);
    105  }
    106 
    107  nsCString originAttrSuffix;
    108  rv = principal->GetOriginSuffix(originAttrSuffix);
    109  if (NS_FAILED(rv)) {
    110    return Err(rv);
    111  }
    112 
    113  return std::make_pair(std::move(originAttrSuffix), std::move(originKey));
    114 }
    115 
    116 LogModule* GetLocalStorageLogger() { return gLogger; }
    117 
    118 }  // namespace mozilla::dom