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/. */
6 
7 #include "gfxVars.h"
8 #include "gfxVarReceiver.h"
9 #include "mozilla/dom/ContentChild.h"
10 
11 namespace mozilla {
12 namespace gfx {
13 
14 StaticAutoPtr<gfxVars> gfxVars::sInstance;
15 StaticAutoPtr<nsTArray<gfxVars::VarBase*>> gfxVars::sVarList;
16 
17 StaticAutoPtr<nsTArray<GfxVarUpdate>> gGfxVarInitUpdates;
18 
SetValuesForInitialize(const nsTArray<GfxVarUpdate> & aInitUpdates)19 void gfxVars::SetValuesForInitialize(
20     const nsTArray<GfxVarUpdate>& aInitUpdates) {
21   // This should only be called once
22   MOZ_RELEASE_ASSERT(!gGfxVarInitUpdates);
23 
24   // We expect aInitUpdates to be provided before any other gfxVars operation,
25   // and for sInstance to be null here, but handle the alternative.
26   if (sInstance) {
27     // Apply the updates, the object has been created already
28     for (const auto& varUpdate : aInitUpdates) {
29       ApplyUpdate(varUpdate);
30     }
31   } else {
32     // Save the values for Initialize call
33     gGfxVarInitUpdates = new nsTArray<GfxVarUpdate>(aInitUpdates.Clone());
34   }
35 }
36 
Initialize()37 void gfxVars::Initialize() {
38   if (sInstance) {
39     MOZ_RELEASE_ASSERT(
40         !gGfxVarInitUpdates,
41         "Initial updates should not be present after any gfxVars operation");
42     return;
43   }
44 
45   // sVarList must be initialized first since it's used in the constructor for
46   // sInstance.
47   sVarList = new nsTArray<gfxVars::VarBase*>();
48   sInstance = new gfxVars;
49 
50   // Note the GPU process is not handled here - it cannot send sync
51   // messages, so instead the initial data is pushed down.
52   if (XRE_IsContentProcess()) {
53     MOZ_ASSERT(gGfxVarInitUpdates,
54                "Initial updates should be provided in content process");
55     if (!gGfxVarInitUpdates) {
56       // No provided initial updates, sync-request them from parent.
57       nsTArray<GfxVarUpdate> initUpdates;
58       dom::ContentChild::GetSingleton()->SendGetGfxVars(&initUpdates);
59       gGfxVarInitUpdates = new nsTArray<GfxVarUpdate>(std::move(initUpdates));
60     }
61     for (const auto& varUpdate : *gGfxVarInitUpdates) {
62       ApplyUpdate(varUpdate);
63     }
64     gGfxVarInitUpdates = nullptr;
65   }
66 }
67 
68 gfxVars::gfxVars() = default;
69 
Shutdown()70 void gfxVars::Shutdown() {
71   sInstance = nullptr;
72   sVarList = nullptr;
73   gGfxVarInitUpdates = nullptr;
74 }
75 
76 /* static */
ApplyUpdate(const GfxVarUpdate & aUpdate)77 void gfxVars::ApplyUpdate(const GfxVarUpdate& aUpdate) {
78   // Only subprocesses receive updates and apply them locally.
79   MOZ_ASSERT(!XRE_IsParentProcess());
80   MOZ_DIAGNOSTIC_ASSERT(sVarList || gGfxVarInitUpdates);
81   if (sVarList) {
82     sVarList->ElementAt(aUpdate.index())->SetValue(aUpdate.value());
83   } else if (gGfxVarInitUpdates) {
84     // Too early, we haven't been initialized, so just add to
85     // the array waiting for the initialization...
86     gGfxVarInitUpdates->AppendElement(aUpdate);
87   }
88 }
89 
90 /* static */
AddReceiver(gfxVarReceiver * aReceiver)91 void gfxVars::AddReceiver(gfxVarReceiver* aReceiver) {
92   MOZ_ASSERT(NS_IsMainThread());
93 
94   // Don't double-add receivers, in case a broken content process sends two
95   // init messages.
96   if (!sInstance->mReceivers.Contains(aReceiver)) {
97     sInstance->mReceivers.AppendElement(aReceiver);
98   }
99 }
100 
101 /* static */
RemoveReceiver(gfxVarReceiver * aReceiver)102 void gfxVars::RemoveReceiver(gfxVarReceiver* aReceiver) {
103   MOZ_ASSERT(NS_IsMainThread());
104 
105   if (sInstance) {
106     sInstance->mReceivers.RemoveElement(aReceiver);
107   }
108 }
109 
110 /* static */
FetchNonDefaultVars()111 nsTArray<GfxVarUpdate> gfxVars::FetchNonDefaultVars() {
112   MOZ_ASSERT(NS_IsMainThread());
113   MOZ_ASSERT(sVarList);
114 
115   nsTArray<GfxVarUpdate> updates;
116   for (size_t i = 0; i < sVarList->Length(); i++) {
117     VarBase* var = sVarList->ElementAt(i);
118     if (var->HasDefaultValue()) {
119       continue;
120     }
121 
122     GfxVarValue value;
123     var->GetValue(&value);
124 
125     updates.AppendElement(GfxVarUpdate(i, value));
126   }
127 
128   return updates;
129 }
130 
VarBase()131 gfxVars::VarBase::VarBase() {
132   mIndex = gfxVars::sVarList->Length();
133   gfxVars::sVarList->AppendElement(this);
134 }
135 
NotifyReceivers(VarBase * aVar)136 void gfxVars::NotifyReceivers(VarBase* aVar) {
137   MOZ_ASSERT(NS_IsMainThread());
138 
139   GfxVarValue value;
140   aVar->GetValue(&value);
141 
142   GfxVarUpdate update(aVar->Index(), value);
143   for (auto& receiver : mReceivers) {
144     receiver->OnVarChanged(update);
145   }
146 }
147 
148 }  // namespace gfx
149 }  // namespace mozilla
150