Bug 1347461 - Part 1: Add a C++ API for the list of DataStorage classes; r=keeler

This commit is contained in:
Ehsan Akhgari 2017-03-15 22:19:14 -04:00
Родитель d25edd4d4f
Коммит c0b6db9d07
9 изменённых файлов: 108 добавлений и 26 удалений

Просмотреть файл

@ -2089,7 +2089,7 @@ mozilla::ipc::IPCResult
ContentChild::RecvDataStoragePut(const nsString& aFilename,
const DataStorageItem& aItem)
{
RefPtr<DataStorage> storage = DataStorage::GetIfExists(aFilename);
RefPtr<DataStorage> storage = DataStorage::GetFromRawFileName(aFilename);
if (storage) {
storage->Put(aItem.key(), aItem.value(), aItem.type());
}
@ -2101,7 +2101,7 @@ ContentChild::RecvDataStorageRemove(const nsString& aFilename,
const nsCString& aKey,
const DataStorageType& aType)
{
RefPtr<DataStorage> storage = DataStorage::GetIfExists(aFilename);
RefPtr<DataStorage> storage = DataStorage::GetFromRawFileName(aFilename);
if (storage) {
storage->Remove(aKey, aType);
}
@ -2111,7 +2111,7 @@ ContentChild::RecvDataStorageRemove(const nsString& aFilename,
mozilla::ipc::IPCResult
ContentChild::RecvDataStorageClear(const nsString& aFilename)
{
RefPtr<DataStorage> storage = DataStorage::GetIfExists(aFilename);
RefPtr<DataStorage> storage = DataStorage::GetFromRawFileName(aFilename);
if (storage) {
storage->Clear();
}

Просмотреть файл

@ -2239,15 +2239,8 @@ ContentParent::InitInternal(ProcessPriority aInitialPriority,
// Ensure the SSS is initialized before we try to use its storage.
nsCOMPtr<nsISiteSecurityService> sss = do_GetService("@mozilla.org/ssservice;1");
nsTArray<nsString> storageFiles;
DataStorage::GetAllFileNames(storageFiles);
for (auto& file : storageFiles) {
dom::DataStorageEntry entry;
entry.filename() = file;
RefPtr<DataStorage> storage = DataStorage::Get(file);
storage->GetAll(&entry.items());
xpcomInit.dataStorage().AppendElement(Move(entry));
}
DataStorage::GetAllChildProcessData(xpcomInit.dataStorage());
// Must send screen info before send initialData
ScreenManager& screenManager = ScreenManager::GetSingleton();
screenManager.CopyScreensToRemote(this);

Просмотреть файл

@ -946,7 +946,7 @@ AltSvcCache::GetAltServiceMapping(const nsACString &scheme, const nsACString &ho
// DataStorage gives synchronous access to a memory based hash table
// that is backed by disk where those writes are done asynchronously
// on another thread
mStorage = DataStorage::Get(NS_LITERAL_STRING("AlternateServices.txt"));
mStorage = DataStorage::Get(DataStorageClass::AlternateServices);
if (mStorage) {
bool storageWillPersist = false;
if (NS_FAILED(mStorage->Init(storageWillPersist))) {

Просмотреть файл

@ -52,7 +52,7 @@ public:
nsTArray<nsString> fileNames;
DataStorage::GetAllFileNames(fileNames);
for (const auto& file: fileNames) {
RefPtr<DataStorage> ds = DataStorage::Get(file);
RefPtr<DataStorage> ds = DataStorage::GetFromRawFileName(file);
size_t amount = ds->SizeOfIncludingThis(MallocSizeOf);
nsPrintfCString path("explicit/data-storage/%s",
NS_ConvertUTF16toUTF8(file).get());
@ -88,7 +88,23 @@ DataStorage::~DataStorage()
// static
already_AddRefed<DataStorage>
DataStorage::Get(const nsString& aFilename)
DataStorage::Get(DataStorageClass aFilename)
{
switch (aFilename) {
#define DATA_STORAGE(_) \
case DataStorageClass::_: \
return GetFromRawFileName(NS_LITERAL_STRING(#_ ".txt"));
#include "mozilla/DataStorageList.h"
#undef DATA_STORAGE
default:
MOZ_ASSERT_UNREACHABLE("Invalid DataStorage type passed?");
return nullptr;
}
}
// static
already_AddRefed<DataStorage>
DataStorage::GetFromRawFileName(const nsString& aFilename)
{
MOZ_ASSERT(NS_IsMainThread());
if (!sDataStorages) {
@ -105,14 +121,27 @@ DataStorage::Get(const nsString& aFilename)
// static
already_AddRefed<DataStorage>
DataStorage::GetIfExists(const nsString& aFilename)
DataStorage::GetIfExists(DataStorageClass aFilename)
{
MOZ_ASSERT(NS_IsMainThread());
if (!sDataStorages) {
sDataStorages = new DataStorages();
}
nsString name;
switch (aFilename) {
#define DATA_STORAGE(_) \
case DataStorageClass::_: \
name.AssignLiteral(#_ ".txt"); \
break;
#include "mozilla/DataStorageList.h"
#undef DATA_STORAGE
default:
MOZ_ASSERT_UNREACHABLE("Invalid DataStorages type passed?");
}
RefPtr<DataStorage> storage;
sDataStorages->Get(aFilename, getter_AddRefs(storage));
if (!name.IsEmpty()) {
sDataStorages->Get(name, getter_AddRefs(storage));
}
return storage.forget();
}
@ -124,8 +153,34 @@ DataStorage::GetAllFileNames(nsTArray<nsString>& aItems)
if (!sDataStorages) {
return;
}
for (auto iter = sDataStorages->Iter(); !iter.Done(); iter.Next()) {
aItems.AppendElement(iter.Key());
#define DATA_STORAGE(_) \
aItems.AppendElement(NS_LITERAL_STRING(#_ ".txt"));
#include "mozilla/DataStorageList.h"
#undef DATA_STORAGE
}
// static
void
DataStorage::GetAllChildProcessData(
nsTArray<mozilla::dom::DataStorageEntry>& aEntries)
{
nsTArray<nsString> storageFiles;
GetAllFileNames(storageFiles);
for (auto& file : storageFiles) {
dom::DataStorageEntry entry;
entry.filename() = file;
RefPtr<DataStorage> storage = DataStorage::GetFromRawFileName(file);
if (!storage->mInitCalled) {
// Perhaps no consumer has initialized the DataStorage object yet,
// so do that now!
bool dataWillPersist = false;
nsresult rv = storage->Init(dataWillPersist);
if (NS_WARN_IF(NS_FAILED(rv))) {
return;
}
}
storage->GetAll(&entry.items());
aEntries.AppendElement(Move(entry));
}
}
@ -137,7 +192,7 @@ DataStorage::SetCachedStorageEntries(
MOZ_ASSERT(XRE_IsContentProcess());
for (auto& entry : aEntries) {
RefPtr<DataStorage> storage = DataStorage::Get(entry.filename());
RefPtr<DataStorage> storage = DataStorage::GetFromRawFileName(entry.filename());
bool dataWillPersist = false;
storage->Init(dataWillPersist, &entry.items());
}

Просмотреть файл

@ -20,9 +20,13 @@
#include "nsRefPtrHashtable.h"
#include "nsString.h"
class psm_DataStorageTest;
namespace mozilla {
class DataStorageMemoryReporter;
namespace dom {
class ContentChild;
class DataStorageEntry;
class DataStorageItem;
}
@ -88,6 +92,12 @@ enum DataStorageType {
DataStorage_Private
};
enum class DataStorageClass {
#define DATA_STORAGE(_) _,
#include "mozilla/DataStorageList.h"
#undef DATA_STORAGE
};
class DataStorage : public nsIObserver
{
typedef dom::DataStorageItem DataStorageItem;
@ -98,8 +108,8 @@ public:
// If there is a profile directory, there is or will eventually be a file
// by the name specified by aFilename there.
static already_AddRefed<DataStorage> Get(const nsString& aFilename);
static already_AddRefed<DataStorage> GetIfExists(const nsString& aFilename);
static already_AddRefed<DataStorage> Get(DataStorageClass aFilename);
static already_AddRefed<DataStorage> GetIfExists(DataStorageClass aFilename);
// Initializes the DataStorage. Must be called before using.
// aDataWillPersist returns whether or not data can be persistently saved.
@ -127,6 +137,9 @@ public:
// Read all file names that we know about.
static void GetAllFileNames(nsTArray<nsString>& aItems);
// Read all child process data that we know about.
static void GetAllChildProcessData(nsTArray<mozilla::dom::DataStorageEntry>& aEntries);
// Read all of the data items.
void GetAll(InfallibleTArray<DataStorageItem>* aItems);
@ -139,6 +152,12 @@ private:
explicit DataStorage(const nsString& aFilename);
virtual ~DataStorage();
static already_AddRefed<DataStorage> GetFromRawFileName(const nsString& aFilename);
friend class ::psm_DataStorageTest;
friend class mozilla::dom::ContentChild;
friend class mozilla::DataStorageMemoryReporter;
class Writer;
class Reader;
@ -202,9 +221,10 @@ private:
uint32_t mTimerDelay; // in milliseconds
bool mPendingWrite; // true if a write is needed but hasn't been dispatched
bool mShuttingDown;
mozilla::Atomic<bool> mInitCalled; // Indicates that Init() has been called.
// (End list of members protected by mMutex)
mozilla::Atomic<bool> mInitCalled; // Indicates that Init() has been called.
Monitor mReadyMonitor; // Do not acquire this at the same time as mMutex.
bool mReady; // Indicates that saved data has been read and Get can proceed.

Просмотреть файл

@ -0,0 +1,13 @@
/* -*- 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/. */
// This is the list of well-known PSM DataStorage classes that Gecko uses.
// These are key value data stores that are backed by a simple text-based
// storage in the profile directory.
DATA_STORAGE(AlternateServices)
DATA_STORAGE(SecurityPreloadState)
DATA_STORAGE(SiteSecurityServiceState)

Просмотреть файл

@ -81,6 +81,7 @@ EXPORTS += [
EXPORTS.mozilla += [
'DataStorage.h',
'DataStorageList.h',
'PublicSSL.h',
]

Просмотреть файл

@ -378,9 +378,9 @@ nsSiteSecurityService::Init()
mozilla::Preferences::AddStrongObserver(this,
"test.currentTimeOffsetSeconds");
mSiteStateStorage =
mozilla::DataStorage::Get(NS_LITERAL_STRING("SiteSecurityServiceState.txt"));
mozilla::DataStorage::Get(DataStorageClass::SiteSecurityServiceState);
mPreloadStateStorage =
mozilla::DataStorage::Get(NS_LITERAL_STRING("SecurityPreloadState.txt"));
mozilla::DataStorage::Get(DataStorageClass::SecurityPreloadState);
bool storageWillPersist = false;
bool preloadStorageWillPersist = false;
nsresult rv = mSiteStateStorage->Init(storageWillPersist);

Просмотреть файл

@ -24,7 +24,7 @@ protected:
const ::testing::TestInfo* const testInfo =
::testing::UnitTest::GetInstance()->current_test_info();
NS_ConvertUTF8toUTF16 testName(testInfo->name());
storage = DataStorage::Get(testName);
storage = DataStorage::GetFromRawFileName(testName);
storage->Init(dataWillPersist);
}