Bug 1215723 - Part 3: Propagate updates to DataStorage from the parent process to the content processes; r=keeler

This commit is contained in:
Ehsan Akhgari 2015-10-30 15:30:00 -04:00
parent c92d8a0ca2
commit 484d64ec70
5 changed files with 92 additions and 0 deletions

View File

@ -2262,6 +2262,39 @@ ContentChild::RecvPreferenceUpdate(const PrefSetting& aPref)
return true;
}
bool
ContentChild::RecvDataStoragePut(const nsString& aFilename,
const DataStorageItem& aItem)
{
RefPtr<DataStorage> storage = DataStorage::GetIfExists(aFilename);
if (storage) {
storage->Put(aItem.key(), aItem.value(), aItem.type());
}
return true;
}
bool
ContentChild::RecvDataStorageRemove(const nsString& aFilename,
const nsCString& aKey,
const DataStorageType& aType)
{
RefPtr<DataStorage> storage = DataStorage::GetIfExists(aFilename);
if (storage) {
storage->Remove(aKey, aType);
}
return true;
}
bool
ContentChild::RecvDataStorageClear(const nsString& aFilename)
{
RefPtr<DataStorage> storage = DataStorage::GetIfExists(aFilename);
if (storage) {
storage->Clear();
}
return true;
}
bool
ContentChild::RecvNotifyAlertsObserver(const nsCString& aType, const nsString& aData)
{

View File

@ -338,6 +338,13 @@ public:
virtual bool RecvPreferenceUpdate(const PrefSetting& aPref) override;
virtual bool RecvDataStoragePut(const nsString& aFilename,
const DataStorageItem& aItem) override;
virtual bool RecvDataStorageRemove(const nsString& aFilename,
const nsCString& aKey,
const DataStorageType& aType) override;
virtual bool RecvDataStorageClear(const nsString& aFilename) override;
virtual bool RecvNotifyAlertsObserver(const nsCString& aType,
const nsString& aData) override;

View File

@ -566,6 +566,10 @@ child:
PreferenceUpdate(PrefSetting pref);
DataStoragePut(nsString aFilename, DataStorageItem aItem);
DataStorageRemove(nsString aFilename, nsCString aKey, DataStorageType aType);
DataStorageClear(nsString aFilename);
NotifyAlertsObserver(nsCString topic, nsString data);
GeolocationUpdate(GeoPosition somewhere);

View File

@ -9,6 +9,7 @@
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/dom/PContent.h"
#include "mozilla/dom/ContentChild.h"
#include "mozilla/dom/ContentParent.h"
#include "mozilla/Preferences.h"
#include "mozilla/Services.h"
#include "mozilla/Telemetry.h"
@ -72,6 +73,19 @@ DataStorage::Get(const nsString& aFilename)
return storage.forget();
}
// static
already_AddRefed<DataStorage>
DataStorage::GetIfExists(const nsString& aFilename)
{
MOZ_ASSERT(NS_IsMainThread());
if (!sDataStorages) {
sDataStorages = new DataStorages();
}
RefPtr<DataStorage> storage;
sDataStorages->Get(aFilename, getter_AddRefs(storage));
return storage.forget();
}
nsresult
DataStorage::Init(bool& aDataWillPersist)
{
@ -520,6 +534,22 @@ DataStorage::MaybeEvictOneEntry(DataStorageType aType,
}
}
template <class Functor>
static
void
RunOnAllContentParents(Functor func)
{
if (!XRE_IsParentProcess()) {
return;
}
using dom::ContentParent;
nsTArray<ContentParent*> parents;
ContentParent::GetAll(parents);
for (auto& parent: parents) {
func(parent);
}
}
nsresult
DataStorage::Put(const nsCString& aKey, const nsCString& aValue,
DataStorageType aType)
@ -546,6 +576,14 @@ DataStorage::Put(const nsCString& aKey, const nsCString& aValue,
return rv;
}
RunOnAllContentParents([&](dom::ContentParent* aParent) {
DataStorageItem item;
item.key() = aKey;
item.value() = aValue;
item.type() = aType;
Unused << aParent->SendDataStoragePut(mFilename, item);
});
return NS_OK;
}
@ -576,6 +614,10 @@ DataStorage::Remove(const nsCString& aKey, DataStorageType aType)
if (aType == DataStorage_Persistent && !mPendingWrite) {
Unused << AsyncSetTimer(lock);
}
RunOnAllContentParents([&](dom::ContentParent* aParent) {
Unused << aParent->SendDataStorageRemove(mFilename, aKey, aType);
});
}
class DataStorage::Writer : public nsRunnable
@ -696,6 +738,11 @@ DataStorage::Clear()
return rv;
}
}
RunOnAllContentParents([&](dom::ContentParent* aParent) {
Unused << aParent->SendDataStorageClear(mFilename);
});
return NS_OK;
}

View File

@ -96,6 +96,7 @@ 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);
// Initializes the DataStorage. Must be called before using.
// aDataWillPersist returns whether or not data can be persistently saved.