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 "DataStorageManager.h"
8 #include "MainThreadUtils.h"
9 #include "nsIMemoryReporter.h"
12 using VoidPtrToSizeFn
= uintptr_t (*)(const void* ptr
);
14 extern "C" nsresult
make_data_storage(const nsAString
* basename
,
16 VoidPtrToSizeFn sizeOfOp
,
17 VoidPtrToSizeFn enclosingSizeOfOp
,
18 nsIDataStorage
** result
);
20 MOZ_DEFINE_MALLOC_SIZE_OF(DataStorageMallocSizeOf
)
21 MOZ_DEFINE_MALLOC_ENCLOSING_SIZE_OF(DataStorageMallocEnclosingSizeOf
)
25 NS_IMPL_ISUPPORTS(DataStorageManager
, nsIDataStorageManager
)
28 DataStorageManager::Get(nsIDataStorageManager::DataStorage aDataStorage
,
29 nsIDataStorage
** aResult
) {
30 if (!NS_IsMainThread()) {
31 return NS_ERROR_NOT_SAME_THREAD
;
33 nsAutoString filename
;
34 size_t valueLength
= 1024;
35 switch (aDataStorage
) {
36 case nsIDataStorageManager::AlternateServices
:
37 if (mAlternateServicesCreated
) {
38 return NS_ERROR_ALREADY_INITIALIZED
;
40 mAlternateServicesCreated
= true;
41 filename
.Assign(u
"AlternateServices"_ns
);
43 case nsIDataStorageManager::ClientAuthRememberList
:
44 if (mClientAuthRememberListCreated
) {
45 return NS_ERROR_ALREADY_INITIALIZED
;
47 mClientAuthRememberListCreated
= true;
48 filename
.Assign(u
"ClientAuthRememberList"_ns
);
50 case nsIDataStorageManager::SiteSecurityServiceState
:
51 if (mSiteSecurityServiceStateCreated
) {
52 return NS_ERROR_ALREADY_INITIALIZED
;
54 mSiteSecurityServiceStateCreated
= true;
55 filename
.Assign(u
"SiteSecurityServiceState"_ns
);
56 // For most nsIDataStorage use cases, values can be quite long (1024
57 // bytes by default). For HSTS, much less information is stored, so save
58 // space by limiting values to 24 bytes.
62 return NS_ERROR_INVALID_ARG
;
64 return make_data_storage(&filename
, valueLength
, &DataStorageMallocSizeOf
,
65 &DataStorageMallocEnclosingSizeOf
, aResult
);
68 } // namespace mozilla