Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / localstorage / LocalStorageCommon.cpp
blobc248030edddd382738a3d107c676ef8c26e1679f
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 "LocalStorageCommon.h"
9 #include <cstdint>
10 #include "MainThreadUtils.h"
11 #include "mozilla/Assertions.h"
12 #include "mozilla/Atomics.h"
13 #include "mozilla/Logging.h"
14 #include "mozilla/OriginAttributes.h"
15 #include "mozilla/Preferences.h"
16 #include "mozilla/RefPtr.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"
32 namespace mozilla::dom {
34 using namespace mozilla::net;
36 namespace {
38 StaticMutex gNextGenLocalStorageMutex;
39 Atomic<int32_t> gNextGenLocalStorageEnabled(-1);
40 LazyLogModule gLogger("LocalStorage");
42 } // namespace
44 const char16_t* kLocalStorageType = u"localStorage";
46 bool NextGenLocalStorageEnabled() {
47 if (XRE_IsParentProcess()) {
48 StaticMutexAutoLock lock(gNextGenLocalStorageMutex);
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();
58 gNextGenLocalStorageEnabled = enabled ? 1 : 0;
61 return !!gNextGenLocalStorageEnabled;
64 return CachedNextGenLocalStorageEnabled();
67 void RecvInitNextGenLocalStorageEnabled(const bool aEnabled) {
68 MOZ_ASSERT(!XRE_IsParentProcess());
69 MOZ_ASSERT(NS_IsMainThread());
70 MOZ_ASSERT(gNextGenLocalStorageEnabled == -1);
72 gNextGenLocalStorageEnabled = aEnabled ? 1 : 0;
75 bool CachedNextGenLocalStorageEnabled() {
76 MOZ_ASSERT(gNextGenLocalStorageEnabled != -1);
78 return !!gNextGenLocalStorageEnabled;
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);
90 Result<nsCOMPtr<nsIPrincipal>, nsresult> p =
91 PrincipalInfoToPrincipal(aPrincipalInfo);
92 if (p.isErr()) {
93 return Err(p.unwrapErr());
96 nsCOMPtr<nsIPrincipal> principal = p.unwrap();
97 if (!principal) {
98 return Err(NS_ERROR_NULL_POINTER);
101 nsCString originKey;
102 nsresult rv = principal->GetStorageOriginKey(originKey);
103 if (NS_FAILED(rv)) {
104 return Err(rv);
107 nsCString originAttrSuffix;
108 rv = principal->GetOriginSuffix(originAttrSuffix);
109 if (NS_FAILED(rv)) {
110 return Err(rv);
113 return std::make_pair(std::move(originAttrSuffix), std::move(originKey));
116 LogModule* GetLocalStorageLogger() { return gLogger; }
118 } // namespace mozilla::dom