DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (b6d82b1a6b02)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "LocalStorageCommon.h"

#include "mozilla/dom/ContentChild.h"
#include "mozilla/net/MozURL.h"

namespace mozilla {
namespace dom {

using namespace mozilla::net;

namespace {

StaticMutex gNextGenLocalStorageMutex;
Atomic<int32_t> gNextGenLocalStorageEnabled(-1);
LazyLogModule gLogger("LocalStorage");

}  // namespace

const char16_t* kLocalStorageType = u"localStorage";

bool NextGenLocalStorageEnabled() {
  if (XRE_IsParentProcess()) {
    StaticMutexAutoLock lock(gNextGenLocalStorageMutex);

    if (gNextGenLocalStorageEnabled == -1) {
      bool enabled = GetCurrentNextGenPrefValue();
      gNextGenLocalStorageEnabled = enabled ? 1 : 0;
    }

    return !!gNextGenLocalStorageEnabled;
  }

  MOZ_ASSERT(NS_IsMainThread());

  if (gNextGenLocalStorageEnabled == -1) {
    bool enabled = Preferences::GetBool("dom.storage.next_gen", false);
    gNextGenLocalStorageEnabled = enabled ? 1 : 0;
  }

  return !!gNextGenLocalStorageEnabled;
}

bool CachedNextGenLocalStorageEnabled() {
  MOZ_ASSERT(gNextGenLocalStorageEnabled != -1);

  return !!gNextGenLocalStorageEnabled;
}

nsresult GenerateOriginKey2(const PrincipalInfo& aPrincipalInfo,
                            nsACString& aOriginAttrSuffix,
                            nsACString& aOriginKey) {
  OriginAttributes attrs;
  nsCString spec;

  switch (aPrincipalInfo.type()) {
    case PrincipalInfo::TNullPrincipalInfo: {
      const NullPrincipalInfo& info = aPrincipalInfo.get_NullPrincipalInfo();

      attrs = info.attrs();
      spec = info.spec();

      break;
    }

    case PrincipalInfo::TContentPrincipalInfo: {
      const ContentPrincipalInfo& info =
          aPrincipalInfo.get_ContentPrincipalInfo();

      attrs = info.attrs();
      spec = info.spec();

      break;
    }

    default: {
      spec.SetIsVoid(true);

      break;
    }
  }

  if (spec.IsVoid()) {
    return NS_ERROR_UNEXPECTED;
  }

  attrs.CreateSuffix(aOriginAttrSuffix);

  RefPtr<MozURL> specURL;
  nsresult rv = MozURL::Init(getter_AddRefs(specURL), spec);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return rv;
  }

  nsCString host(specURL->Host());
  uint32_t length = host.Length();
  if (length > 0 && host.CharAt(0) == '[' && host.CharAt(length - 1) == ']') {
    host = Substring(host, 1, length - 2);
  }

  nsCString domainOrigin(host);

  if (domainOrigin.IsEmpty()) {
    // For the file:/// protocol use the exact directory as domain.
    if (specURL->Scheme().EqualsLiteral("file")) {
      domainOrigin.Assign(specURL->Directory());
    }
  }

  // Append reversed domain
  nsAutoCString reverseDomain;
  rv = CreateReversedDomain(domainOrigin, reverseDomain);
  if (NS_FAILED(rv)) {
    return rv;
  }

  aOriginKey.Append(reverseDomain);

  // Append scheme
  aOriginKey.Append(':');
  aOriginKey.Append(specURL->Scheme());

  // Append port if any
  int32_t port = specURL->RealPort();
  if (port != -1) {
    aOriginKey.Append(nsPrintfCString(":%d", port));
  }

  return NS_OK;
}

LogModule* GetLocalStorageLogger() { return gLogger; }

}  // namespace dom
}  // namespace mozilla