Coverage Report

Created: 2018-09-25 14:53

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