File: RemoteLookAndFeel.cpp

package info (click to toggle)
firefox 146.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,653,260 kB
  • sloc: cpp: 7,587,892; javascript: 6,509,455; ansic: 3,755,295; python: 1,410,813; xml: 629,201; asm: 438,677; java: 186,096; sh: 62,697; makefile: 18,086; objc: 13,087; perl: 12,811; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (207 lines) | stat: -rw-r--r-- 6,759 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: sw=2 ts=8 et :
 */
/* 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 "RemoteLookAndFeel.h"

#include "gfxFont.h"
#include "MainThreadUtils.h"
#include "mozilla/Assertions.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/ResultExtensions.h"
#include "mozilla/StaticPrefs_widget.h"
#include "mozilla/Try.h"
#include "nsXULAppAPI.h"

#include <limits>
#include <type_traits>
#include <utility>

namespace mozilla::widget {

// A cached copy of the data extracted by ExtractData.
//
// Storing this lets us avoid doing most of the work of ExtractData each
// time we create a new content process.
//
// Only used in the parent process.
static StaticAutoPtr<FullLookAndFeel> sCachedLookAndFeelData;

RemoteLookAndFeel::RemoteLookAndFeel(FullLookAndFeel&& aData)
    : mTables(std::move(aData.tables())) {
  MOZ_ASSERT(XRE_IsContentProcess(),
             "Only content processes should be using a RemoteLookAndFeel");
}

RemoteLookAndFeel::~RemoteLookAndFeel() = default;

void RemoteLookAndFeel::SetDataImpl(FullLookAndFeel&& aData) {
  MOZ_ASSERT(XRE_IsContentProcess(),
             "Only content processes should be using a RemoteLookAndFeel");
  MOZ_ASSERT(NS_IsMainThread());
  mTables = std::move(aData.tables());
}

namespace {

template <typename Item, typename UInt, typename ID>
Result<const Item*, nsresult> MapLookup(const nsTArray<Item>& aItems,
                                        const nsTArray<UInt>& aMap, ID aID) {
  UInt mapped = aMap[static_cast<size_t>(aID)];

  if (mapped == std::numeric_limits<UInt>::max()) {
    return Err(NS_ERROR_NOT_IMPLEMENTED);
  }

  return &aItems[static_cast<size_t>(mapped)];
}

template <typename Item, typename UInt, typename Id>
void AddToMap(nsTArray<Item>& aItems, nsTArray<UInt>& aMap, Id aId,
              Maybe<Item>&& aNewItem) {
  auto mapIndex = size_t(aId);
  aMap.EnsureLengthAtLeast(mapIndex + 1);
  if (aNewItem.isNothing()) {
    aMap[mapIndex] = std::numeric_limits<UInt>::max();
    return;
  }

  size_t newIndex = aItems.Length();
  MOZ_ASSERT(newIndex < std::numeric_limits<UInt>::max());

  // Check if there is an existing value in aItems that we can point to.
  //
  // The arrays should be small enough and contain few enough unique
  // values that sequential search here is reasonable.
  for (size_t i = 0; i < newIndex; ++i) {
    if (aItems[i] == aNewItem.ref()) {
      aMap[mapIndex] = static_cast<UInt>(i);
      return;
    }
  }

  aItems.AppendElement(aNewItem.extract());
  aMap[mapIndex] = static_cast<UInt>(newIndex);
}

}  // namespace

nsresult RemoteLookAndFeel::NativeGetColor(ColorID aID, ColorScheme aScheme,
                                           nscolor& aResult) {
  const bool dark = aScheme == ColorScheme::Dark;
  const nscolor* result = MOZ_TRY(
      MapLookup(dark ? mTables.darkColors() : mTables.lightColors(),
                dark ? mTables.darkColorMap() : mTables.lightColorMap(), aID));
  aResult = *result;
  return NS_OK;
}

nsresult RemoteLookAndFeel::NativeGetInt(IntID aID, int32_t& aResult) {
  const int32_t* result =
      MOZ_TRY(MapLookup(mTables.ints(), mTables.intMap(), aID));
  aResult = *result;
  return NS_OK;
}

nsresult RemoteLookAndFeel::NativeGetFloat(FloatID aID, float& aResult) {
  const float* result =
      MOZ_TRY(MapLookup(mTables.floats(), mTables.floatMap(), aID));
  aResult = *result;
  return NS_OK;
}

bool RemoteLookAndFeel::NativeGetFont(FontID aID, nsString& aFontName,
                                      gfxFontStyle& aFontStyle) {
  auto result = MapLookup(mTables.fonts(), mTables.fontMap(), aID);
  if (result.isErr()) {
    return false;
  }
  const LookAndFeelFont& font = *result.unwrap();
  return LookAndFeelFontToStyle(font, aFontName, aFontStyle);
}

char16_t RemoteLookAndFeel::GetPasswordCharacterImpl() {
  return static_cast<char16_t>(mTables.passwordChar());
}

bool RemoteLookAndFeel::GetEchoPasswordImpl() { return mTables.passwordEcho(); }

// TODO(emilio): Maybe reuse more of nsXPLookAndFeel's stores...
static bool AddIDsToMap(FullLookAndFeel* aLf) {
  using IntID = LookAndFeel::IntID;
  using FontID = LookAndFeel::FontID;
  using FloatID = LookAndFeel::FloatID;
  using ColorID = LookAndFeel::ColorID;
  using ColorScheme = LookAndFeel::ColorScheme;
  using UseStandins = LookAndFeel::UseStandins;

  bool anyFromOtherTheme = false;
  for (auto id : MakeEnumeratedRange(IntID::End)) {
    int32_t theInt;
    nsresult rv = LookAndFeel::GetInt(id, &theInt);
    AddToMap(aLf->tables().ints(), aLf->tables().intMap(), id,
             NS_SUCCEEDED(rv) ? Some(theInt) : Nothing{});
  }

  for (auto id : MakeEnumeratedRange(ColorID::End)) {
    AddToMap(aLf->tables().lightColors(), aLf->tables().lightColorMap(), id,
             LookAndFeel::GetColor(id, ColorScheme::Light, UseStandins::No));
    AddToMap(aLf->tables().darkColors(), aLf->tables().darkColorMap(), id,
             LookAndFeel::GetColor(id, ColorScheme::Dark, UseStandins::No));
  }

  for (auto id : MakeEnumeratedRange(FloatID::End)) {
    float theFloat;
    nsresult rv = LookAndFeel::GetFloat(id, &theFloat);
    AddToMap(aLf->tables().floats(), aLf->tables().floatMap(), id,
             NS_SUCCEEDED(rv) ? Some(theFloat) : Nothing{});
  }

  for (auto id : MakeEnumeratedRange(FontID::End)) {
    LookAndFeelFont font;
    LookAndFeel::GetFont(id, font);
    AddToMap(aLf->tables().fonts(), aLf->tables().fontMap(), id,
             font.haveFont() ? Some(std::move(font)) : Nothing{});
  }

  return anyFromOtherTheme;
}

// static
const FullLookAndFeel* RemoteLookAndFeel::ExtractData() {
  MOZ_ASSERT(XRE_IsParentProcess(),
             "Only parent processes should be extracting LookAndFeel data");

  if (sCachedLookAndFeelData) {
    return sCachedLookAndFeelData;
  }

  static bool sInitialized = false;
  if (!sInitialized) {
    sInitialized = true;
    ClearOnShutdown(&sCachedLookAndFeelData);
  }

  FullLookAndFeel* lf = new FullLookAndFeel{};

  lf->tables().passwordChar() = LookAndFeel::GetPasswordCharacter();
  lf->tables().passwordEcho() = LookAndFeel::GetEchoPassword();

  AddIDsToMap(lf);

  // This assignment to sCachedLookAndFeelData must be done after the
  // WithThemeConfiguredForContent call, since it can end up calling RefreshImpl
  // on the LookAndFeel, which will clear out sCachedTables.
  sCachedLookAndFeelData = lf;
  return sCachedLookAndFeelData;
}

void RemoteLookAndFeel::ClearCachedData() {
  MOZ_ASSERT(XRE_IsParentProcess());
  sCachedLookAndFeelData = nullptr;
}

}  // namespace mozilla::widget