2017-10-28 01:55:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2017-10-28 02:10:06 +03:00
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2016-08-04 21:33:42 +03:00
|
|
|
/* 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 "gfxVars.h"
|
|
|
|
#include "gfxVarReceiver.h"
|
|
|
|
#include "mozilla/dom/ContentChild.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
|
|
|
|
|
|
|
StaticAutoPtr<gfxVars> gfxVars::sInstance;
|
|
|
|
StaticAutoPtr<nsTArray<gfxVars::VarBase*>> gfxVars::sVarList;
|
|
|
|
|
2017-06-28 00:04:17 +03:00
|
|
|
StaticAutoPtr<nsTArray<GfxVarUpdate>> gGfxVarInitUpdates;
|
|
|
|
|
|
|
|
void gfxVars::SetValuesForInitialize(
|
|
|
|
const nsTArray<GfxVarUpdate>& aInitUpdates) {
|
|
|
|
// This should only be called once
|
|
|
|
MOZ_RELEASE_ASSERT(!gGfxVarInitUpdates);
|
|
|
|
|
|
|
|
// We expect aInitUpdates to be provided before any other gfxVars operation,
|
|
|
|
// and for sInstance to be null here, but handle the alternative.
|
|
|
|
if (sInstance) {
|
|
|
|
// Apply the updates, the object has been created already
|
|
|
|
for (const auto& varUpdate : aInitUpdates) {
|
|
|
|
ApplyUpdate(varUpdate);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Save the values for Initialize call
|
|
|
|
gGfxVarInitUpdates = new nsTArray<GfxVarUpdate>(aInitUpdates);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-14 06:13:31 +03:00
|
|
|
void gfxVars::Initialize() {
|
2016-08-04 21:33:42 +03:00
|
|
|
if (sInstance) {
|
2017-06-28 00:04:17 +03:00
|
|
|
MOZ_RELEASE_ASSERT(
|
|
|
|
!gGfxVarInitUpdates,
|
|
|
|
"Initial updates should not be present after any gfxVars operation");
|
2016-08-04 21:33:42 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// sVarList must be initialized first since it's used in the constructor for
|
|
|
|
// sInstance.
|
|
|
|
sVarList = new nsTArray<gfxVars::VarBase*>();
|
|
|
|
sInstance = new gfxVars;
|
|
|
|
|
2017-06-28 00:04:17 +03:00
|
|
|
// Note the GPU process is not handled here - it cannot send sync
|
2016-08-04 21:33:42 +03:00
|
|
|
// messages, so instead the initial data is pushed down.
|
|
|
|
if (XRE_IsContentProcess()) {
|
2017-06-28 00:04:17 +03:00
|
|
|
MOZ_ASSERT(gGfxVarInitUpdates,
|
|
|
|
"Initial updates should be provided in content process");
|
|
|
|
if (!gGfxVarInitUpdates) {
|
|
|
|
// No provided initial updates, sync-request them from parent.
|
2019-07-10 08:25:54 +03:00
|
|
|
nsTArray<GfxVarUpdate> initUpdates;
|
2017-06-28 00:04:17 +03:00
|
|
|
dom::ContentChild::GetSingleton()->SendGetGfxVars(&initUpdates);
|
2018-05-30 22:15:35 +03:00
|
|
|
gGfxVarInitUpdates = new nsTArray<GfxVarUpdate>(std::move(initUpdates));
|
2016-08-04 21:33:42 +03:00
|
|
|
}
|
2017-06-28 00:04:17 +03:00
|
|
|
for (const auto& varUpdate : *gGfxVarInitUpdates) {
|
|
|
|
ApplyUpdate(varUpdate);
|
|
|
|
}
|
|
|
|
gGfxVarInitUpdates = nullptr;
|
2016-08-04 21:33:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxVars::gfxVars() {}
|
|
|
|
|
|
|
|
void gfxVars::Shutdown() {
|
|
|
|
sInstance = nullptr;
|
|
|
|
sVarList = nullptr;
|
2017-06-28 00:04:17 +03:00
|
|
|
gGfxVarInitUpdates = nullptr;
|
2016-08-04 21:33:42 +03:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:07:19 +03:00
|
|
|
/* static */
|
|
|
|
void gfxVars::ApplyUpdate(const GfxVarUpdate& aUpdate) {
|
2016-08-04 21:33:42 +03:00
|
|
|
// Only subprocesses receive updates and apply them locally.
|
|
|
|
MOZ_ASSERT(!XRE_IsParentProcess());
|
2017-06-28 00:04:17 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(sVarList || gGfxVarInitUpdates);
|
|
|
|
if (sVarList) {
|
|
|
|
sVarList->ElementAt(aUpdate.index())->SetValue(aUpdate.value());
|
|
|
|
} else if (gGfxVarInitUpdates) {
|
|
|
|
// Too early, we haven't been initialized, so just add to
|
|
|
|
// the array waiting for the initialization...
|
|
|
|
gGfxVarInitUpdates->AppendElement(aUpdate);
|
|
|
|
}
|
2016-08-04 21:33:42 +03:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:07:19 +03:00
|
|
|
/* static */
|
|
|
|
void gfxVars::AddReceiver(gfxVarReceiver* aReceiver) {
|
2016-08-04 21:33:42 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
// Don't double-add receivers, in case a broken content process sends two
|
|
|
|
// init messages.
|
|
|
|
if (!sInstance->mReceivers.Contains(aReceiver)) {
|
|
|
|
sInstance->mReceivers.AppendElement(aReceiver);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:07:19 +03:00
|
|
|
/* static */
|
|
|
|
void gfxVars::RemoveReceiver(gfxVarReceiver* aReceiver) {
|
2016-08-04 21:33:42 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (sInstance) {
|
|
|
|
sInstance->mReceivers.RemoveElement(aReceiver);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:07:19 +03:00
|
|
|
/* static */
|
|
|
|
nsTArray<GfxVarUpdate> gfxVars::FetchNonDefaultVars() {
|
2016-08-04 21:33:42 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(sVarList);
|
|
|
|
|
|
|
|
nsTArray<GfxVarUpdate> updates;
|
|
|
|
for (size_t i = 0; i < sVarList->Length(); i++) {
|
|
|
|
VarBase* var = sVarList->ElementAt(i);
|
|
|
|
if (var->HasDefaultValue()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
GfxVarValue value;
|
|
|
|
var->GetValue(&value);
|
|
|
|
|
|
|
|
updates.AppendElement(GfxVarUpdate(i, value));
|
|
|
|
}
|
|
|
|
|
|
|
|
return updates;
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxVars::VarBase::VarBase() {
|
|
|
|
mIndex = gfxVars::sVarList->Length();
|
|
|
|
gfxVars::sVarList->AppendElement(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void gfxVars::NotifyReceivers(VarBase* aVar) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
GfxVarValue value;
|
|
|
|
aVar->GetValue(&value);
|
|
|
|
|
|
|
|
GfxVarUpdate update(aVar->Index(), value);
|
|
|
|
for (auto& receiver : mReceivers) {
|
|
|
|
receiver->OnVarChanged(update);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|