Bug 1530996 - Create the IPC protocol to access the database from the content process. r=jya

The database is accessible from the parent process due to to the sandbox thus it is required an IPDL protocol that will transfer the queries and the results

Differential Revision: https://phabricator.services.mozilla.com/D38313

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Alex Chronopoulos 2019-08-06 09:24:34 +00:00
Родитель 9533dca8e9
Коммит a930f0bdd1
11 изменённых файлов: 256 добавлений и 0 удалений

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

@ -17,6 +17,7 @@
#include "mozilla/Attributes.h"
#include "mozilla/BackgroundHangMonitor.h"
#include "mozilla/BenchmarkStorageChild.h"
#include "mozilla/LookAndFeel.h"
#include "mozilla/MemoryTelemetry.h"
#include "mozilla/NullPrincipal.h"
@ -2231,6 +2232,15 @@ bool ContentChild::DeallocPMediaChild(media::PMediaChild* aActor) {
return media::DeallocPMediaChild(aActor);
}
PBenchmarkStorageChild* ContentChild::AllocPBenchmarkStorageChild() {
return BenchmarkStorageChild::Instance();
}
bool ContentChild::DeallocPBenchmarkStorageChild(PBenchmarkStorageChild* aActor) {
delete aActor;
return true;
}
PSpeechSynthesisChild* ContentChild::AllocPSpeechSynthesisChild() {
#ifdef MOZ_WEBSPEECH
MOZ_CRASH("No one should be allocating PSpeechSynthesisChild actors");

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

@ -45,6 +45,7 @@ class nsFrameLoader;
namespace mozilla {
class RemoteSpellcheckEngineChild;
class ChildProfilerController;
class BenchmarkStorageChild;
using mozilla::loader::PScriptCacheChild;
@ -291,6 +292,10 @@ class ContentChild final : public PContentChild,
bool DeallocPMediaChild(PMediaChild* aActor);
PBenchmarkStorageChild* AllocPBenchmarkStorageChild();
bool DeallocPBenchmarkStorageChild(PBenchmarkStorageChild* aActor);
PPresentationChild* AllocPPresentationChild();
bool DeallocPPresentationChild(PPresentationChild* aActor);

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

@ -32,6 +32,7 @@
#endif
#include "mozilla/AntiTrackingCommon.h"
#include "mozilla/BasePrincipal.h"
#include "mozilla/BenchmarkStorageParent.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/Components.h"
#include "mozilla/StyleSheetInlines.h"
@ -3745,6 +3746,15 @@ bool ContentParent::DeallocPMediaParent(media::PMediaParent* aActor) {
return media::DeallocPMediaParent(aActor);
}
PBenchmarkStorageParent* ContentParent::AllocPBenchmarkStorageParent() {
return new BenchmarkStorageParent;
}
bool ContentParent::DeallocPBenchmarkStorageParent(PBenchmarkStorageParent* aActor) {
delete aActor;
return true;
}
PPresentationParent* ContentParent::AllocPPresentationParent() {
RefPtr<PresentationParent> actor = new PresentationParent();
return actor.forget().take();

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

@ -77,6 +77,7 @@ class SandboxBrokerPolicyFactory;
#endif
class PreallocatedProcessManagerImpl;
class BenchmarkStorageParent;
using mozilla::loader::PScriptCacheParent;
@ -917,6 +918,10 @@ class ContentParent final : public PContentParent,
bool DeallocPMediaParent(PMediaParent* aActor);
PBenchmarkStorageParent* AllocPBenchmarkStorageParent();
bool DeallocPBenchmarkStorageParent(PBenchmarkStorageParent* aActor);
PPresentationParent* AllocPPresentationParent();
bool DeallocPPresentationParent(PPresentationParent* aActor);

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

@ -45,6 +45,7 @@ include protocol PRemoteDecoderManager;
include protocol PProfiler;
include protocol PScriptCache;
include protocol PSessionStorageObserver;
include protocol PBenchmarkStorage;
include DOMTypes;
include JavaScriptTypes;
include IPCBlob;
@ -354,6 +355,7 @@ nested(upto inside_cpow) sync protocol PContent
manages PScriptCache;
manages PLoginReputation;
manages PSessionStorageObserver;
manages PBenchmarkStorage;
// Depending on exactly how the new browser is being created, it might be
// created from either the child or parent process!
@ -887,6 +889,8 @@ parent:
async PSessionStorageObserver();
async PBenchmarkStorage();
// Services remoting
async StartVisitedQuery(URIParams uri);

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

@ -0,0 +1,27 @@
/* -*- 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 "BenchmarkStorageChild.h"
#include "mozilla/dom/ContentChild.h"
namespace mozilla {
static PBenchmarkStorageChild* sChild = nullptr;
/* static */
PBenchmarkStorageChild* BenchmarkStorageChild::Instance() {
MOZ_ASSERT(NS_IsMainThread());
if (!sChild) {
sChild = new BenchmarkStorageChild();
PContentChild* contentChild = dom::ContentChild::GetSingleton();
MOZ_ASSERT(contentChild);
contentChild->SendPBenchmarkStorageConstructor();
}
MOZ_ASSERT(sChild);
return sChild;
}
} // namespace mozilla

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

@ -0,0 +1,26 @@
/* -*- 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/. */
#ifndef include_dom_media_mediacapabilities_BenchmarkStorageChild_h
#define include_dom_media_mediacapabilities_BenchmarkStorageChild_h
#include "mozilla/PBenchmarkStorageChild.h"
namespace mozilla {
class BenchmarkStorageChild : public PBenchmarkStorageChild {
public:
/* Singleton class to avoid recreating the protocol every time we need access
* to the storage. */
static PBenchmarkStorageChild* Instance();
private:
BenchmarkStorageChild() = default;
};
} // namespace mozilla
#endif // include_dom_media_mediacapabilities_BenchmarkStorageChild_h

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

@ -0,0 +1,103 @@
/* -*- 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 "BenchmarkStorageParent.h"
#include "KeyValueStorage.h"
namespace mozilla {
/* Moving average window size. */
const int32_t AVG_WINDOW = 20;
/* Calculate the moving average for the new value aValue for the current window
* aWindow and the already existing average aAverage. When the method returns
* aAverage will contain the new average and aWindow will contain the new
* window.*/
void MovingAverage(int32_t& aAverage, int32_t& aWindow, const int32_t aValue) {
if (aWindow < AVG_WINDOW) {
aAverage = (aAverage * aWindow + aValue) / (aWindow + 1);
aWindow++;
return;
}
MOZ_ASSERT(aWindow == AVG_WINDOW);
aAverage = (aAverage - aAverage / aWindow) + (aValue / aWindow);
}
/* In order to decrease the number of times the database is accessed when the
* moving average is stored or retrieved we use the same value to store _both_
* the window and the average. The range of the average is limited since it is
* a percentage (0-100), and the range of the window is limited
* (1-20). Thus the number that is stored in the database is in the form
* (digits): WWAAA. For example, the value stored when an average(A) of 88
* corresponds to a window(W) 7 is 7088. The average of 100 that corresponds to
* a window of 20 is 20100. The following methods are helpers to extract or
* construct the stored value according to the above. */
/* Stored value will be in the form WWAAA(19098). We need to extract the window
* (19) and the average score (98). The aValue will
* be parsed, the aWindow will contain the window (of the moving average) and
* the return value will contain the average itself. */
int32_t ParseStoredValue(int32_t aValue, int32_t& aWindow) {
MOZ_ASSERT(aValue > 999);
MOZ_ASSERT(aValue < 100000);
int32_t score = aValue % 1000;
aWindow = (aValue / 1000) % 100;
return score;
}
int32_t PrepareStoredValue(int32_t aScore, int32_t aWindow) {
MOZ_ASSERT(aScore >= 0);
MOZ_ASSERT(aScore <= 100);
MOZ_ASSERT(aWindow > 0);
MOZ_ASSERT(aWindow < 21);
return aWindow * 1000 + aScore;
}
BenchmarkStorageParent::BenchmarkStorageParent()
: mStorage(new KeyValueStorage) {}
IPCResult BenchmarkStorageParent::RecvPut(const nsCString& aDbName,
const nsCString& aKey,
const int32_t& aValue) {
// In order to calculate and store the new moving average, we need to get the
// stored value and window first, to calculate the new score and window, and
// then to store the new aggregated value.
mStorage->Get(aDbName, aKey)
->Then(
GetCurrentThreadSerialEventTarget(), __func__,
[storage = mStorage, aDbName, aKey, aValue](int32_t aResult) {
int32_t window = 0;
int32_t average = 0;
if (aResult >= 0) {
// The key found.
average = ParseStoredValue(aResult, window);
}
MovingAverage(average, window, aValue);
int32_t newValue = PrepareStoredValue(average, window);
storage->Put(aDbName, aKey, newValue);
},
[](nsresult rv) { /*do nothing*/ });
return IPC_OK();
}
IPCResult BenchmarkStorageParent::RecvGet(const nsCString& aDbName,
const nsCString& aKey,
GetResolver&& aResolve) {
mStorage->Get(aDbName, aKey)
->Then(
GetCurrentThreadSerialEventTarget(), __func__,
[aResolve](int32_t aResult) {
int32_t window = 0; // not used
aResolve(aResult < 0 ? -1 : ParseStoredValue(aResult, window));
},
[aResolve](nsresult rv) { aResolve(-1); });
return IPC_OK();
}
}; // namespace mozilla

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

@ -0,0 +1,35 @@
/* -*- 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/. */
#ifndef include_dom_media_mediacapabilities_BenchmarkStorageParent_h
#define include_dom_media_mediacapabilities_BenchmarkStorageParent_h
#include "mozilla/PBenchmarkStorageParent.h"
namespace mozilla {
class KeyValueStorage;
using mozilla::ipc::IPCResult;
class BenchmarkStorageParent : public PBenchmarkStorageParent {
friend class PBenchmarkStorageParent;
public:
BenchmarkStorageParent();
IPCResult RecvPut(const nsCString& aDbName, const nsCString& aKey,
const int32_t& aValue);
IPCResult RecvGet(const nsCString& aDbName, const nsCString& aKey,
GetResolver&& aResolve);
private:
RefPtr<KeyValueStorage> mStorage;
};
} // namespace mozilla
#endif // include_dom_media_mediacapabilities_BenchmarkStorageParent_h

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

@ -0,0 +1,21 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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 protocol PContent;
namespace mozilla {
async protocol PBenchmarkStorage
{
manager PContent;
parent:
async Put(nsCString aDbName, nsCString aKey, int32_t aValue);
async Get(nsCString aDbName, nsCString aKey) returns(int32_t aValue);
async __delete__();
};
} // namespace mozilla

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

@ -8,12 +8,22 @@ EXPORTS.mozilla.dom += [
]
EXPORTS.mozilla += [
'BenchmarkStorageChild.h',
'BenchmarkStorageParent.h',
'KeyValueStorage.h',
]
UNIFIED_SOURCES += [
'BenchmarkStorageChild.cpp',
'BenchmarkStorageParent.cpp',
'KeyValueStorage.cpp',
'MediaCapabilities.cpp',
]
IPDL_SOURCES += [
'PBenchmarkStorage.ipdl'
]
include('/ipc/chromium/chromium-config.mozbuild')
FINAL_LIBRARY = 'xul'