2020-12-16 07:17:36 +03:00
|
|
|
/* -*- 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"
|
2021-03-12 00:20:54 +03:00
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2020-12-16 07:17:36 +03:00
|
|
|
#include "mozilla/Result.h"
|
|
|
|
#include "mozilla/ResultExtensions.h"
|
2020-12-22 22:26:41 +03:00
|
|
|
#include "mozilla/StaticPrefs_widget.h"
|
|
|
|
#include "nsLookAndFeel.h"
|
2020-12-16 07:17:36 +03:00
|
|
|
#include "nsXULAppAPI.h"
|
|
|
|
|
|
|
|
#include <limits>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace mozilla::widget {
|
|
|
|
|
2021-04-02 19:34:33 +03:00
|
|
|
// 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;
|
|
|
|
|
2020-12-22 22:26:41 +03:00
|
|
|
RemoteLookAndFeel::RemoteLookAndFeel(FullLookAndFeel&& aData)
|
|
|
|
: mTables(std::move(aData.tables())) {
|
2020-12-16 07:17:36 +03:00
|
|
|
MOZ_ASSERT(XRE_IsContentProcess(),
|
|
|
|
"Only content processes should be using a RemoteLookAndFeel");
|
2020-12-22 22:26:41 +03:00
|
|
|
|
|
|
|
#ifdef MOZ_WIDGET_GTK
|
2021-02-22 23:05:35 +03:00
|
|
|
if (!StaticPrefs::widget_non_native_theme_enabled()) {
|
2020-12-22 22:26:41 +03:00
|
|
|
// Configure the theme in this content process with the Gtk theme that was
|
|
|
|
// chosen by WithThemeConfiguredForContent in the parent process.
|
|
|
|
nsLookAndFeel::ConfigureTheme(aData.theme());
|
|
|
|
}
|
|
|
|
#endif
|
2020-12-16 07:17:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
RemoteLookAndFeel::~RemoteLookAndFeel() = default;
|
|
|
|
|
2020-12-22 22:26:41 +03:00
|
|
|
void RemoteLookAndFeel::SetDataImpl(FullLookAndFeel&& aData) {
|
2020-12-16 07:17:36 +03:00
|
|
|
MOZ_ASSERT(XRE_IsContentProcess(),
|
|
|
|
"Only content processes should be using a RemoteLookAndFeel");
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2020-12-22 22:26:41 +03:00
|
|
|
mTables = std::move(aData.tables());
|
|
|
|
|
|
|
|
#ifdef MOZ_WIDGET_GTK
|
2021-02-22 23:05:35 +03:00
|
|
|
if (!StaticPrefs::widget_non_native_theme_enabled()) {
|
2020-12-22 22:26:41 +03:00
|
|
|
// Configure the theme in this content process with the Gtk theme that was
|
|
|
|
// chosen by WithThemeConfiguredForContent in the parent process.
|
|
|
|
nsLookAndFeel::ConfigureTheme(aData.theme());
|
|
|
|
}
|
|
|
|
#endif
|
2020-12-16 07:17:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2021-04-06 16:44:06 +03:00
|
|
|
// Some lnf values are somewhat expensive to get, and are not needed in child
|
|
|
|
// processes, so we can avoid querying them.
|
|
|
|
bool IsNeededInChildProcess(LookAndFeel::IntID aId) {
|
|
|
|
switch (aId) {
|
|
|
|
case LookAndFeel::IntID::AlertNotificationOrigin:
|
|
|
|
return false; // see bug 1703205
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-16 07:17:36 +03:00
|
|
|
template <typename Item, typename UInt, typename ID>
|
|
|
|
Result<const Item*, nsresult> MapLookup(const nsTArray<Item>& aItems,
|
2021-03-25 01:57:36 +03:00
|
|
|
const nsTArray<UInt>& aMap, ID aID) {
|
|
|
|
UInt mapped = aMap[static_cast<size_t>(aID)];
|
2020-12-16 07:17:36 +03:00
|
|
|
|
|
|
|
if (mapped == std::numeric_limits<UInt>::max()) {
|
|
|
|
return Err(NS_ERROR_NOT_IMPLEMENTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
return &aItems[static_cast<size_t>(mapped)];
|
|
|
|
}
|
|
|
|
|
2021-03-30 11:17:38 +03:00
|
|
|
template <typename Item, typename UInt, typename Id>
|
|
|
|
void AddToMap(nsTArray<Item>& aItems, nsTArray<UInt>& aMap, Id aId,
|
2020-12-16 07:17:36 +03:00
|
|
|
Maybe<Item>&& aNewItem) {
|
2021-03-30 11:17:38 +03:00
|
|
|
auto mapIndex = size_t(aId);
|
|
|
|
aMap.EnsureLengthAtLeast(mapIndex + 1);
|
2020-12-16 07:17:36 +03:00
|
|
|
if (aNewItem.isNothing()) {
|
2021-03-30 11:17:38 +03:00
|
|
|
aMap[mapIndex] = std::numeric_limits<UInt>::max();
|
2020-12-16 07:17:36 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-30 11:17:38 +03:00
|
|
|
size_t newIndex = aItems.Length();
|
2020-12-16 07:17:36 +03:00
|
|
|
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) {
|
2021-03-30 11:17:38 +03:00
|
|
|
if (aItems[i] == aNewItem.ref()) {
|
|
|
|
aMap[mapIndex] = static_cast<UInt>(i);
|
2020-12-16 07:17:36 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 11:17:38 +03:00
|
|
|
aItems.AppendElement(aNewItem.extract());
|
|
|
|
aMap[mapIndex] = static_cast<UInt>(newIndex);
|
2020-12-16 07:17:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2021-04-03 01:17:55 +03:00
|
|
|
nsresult RemoteLookAndFeel::NativeGetColor(ColorID aID, ColorScheme aScheme,
|
|
|
|
nscolor& aResult) {
|
2020-12-16 07:17:36 +03:00
|
|
|
const nscolor* result;
|
2021-04-03 01:17:55 +03:00
|
|
|
const bool dark = aScheme == ColorScheme::Dark;
|
|
|
|
MOZ_TRY_VAR(
|
|
|
|
result,
|
|
|
|
MapLookup(dark ? mTables.darkColors() : mTables.lightColors(),
|
|
|
|
dark ? mTables.darkColorMap() : mTables.lightColorMap(), aID));
|
2020-12-16 07:17:36 +03:00
|
|
|
aResult = *result;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult RemoteLookAndFeel::NativeGetInt(IntID aID, int32_t& aResult) {
|
2021-04-06 16:44:06 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(
|
|
|
|
IsNeededInChildProcess(aID),
|
|
|
|
"Querying value that we didn't bother getting from the parent process!");
|
|
|
|
|
2020-12-16 07:17:36 +03:00
|
|
|
const int32_t* result;
|
|
|
|
MOZ_TRY_VAR(result, MapLookup(mTables.ints(), mTables.intMap(), aID));
|
|
|
|
aResult = *result;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult RemoteLookAndFeel::NativeGetFloat(FloatID aID, float& aResult) {
|
|
|
|
const float* result;
|
|
|
|
MOZ_TRY_VAR(result, MapLookup(mTables.floats(), mTables.floatMap(), aID));
|
|
|
|
aResult = *result;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RemoteLookAndFeel::NativeGetFont(FontID aID, nsString& aFontName,
|
|
|
|
gfxFontStyle& aFontStyle) {
|
2021-03-25 01:57:36 +03:00
|
|
|
auto result = MapLookup(mTables.fonts(), mTables.fontMap(), aID);
|
2020-12-16 07:17:36 +03:00
|
|
|
if (result.isErr()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const LookAndFeelFont& font = *result.unwrap();
|
2021-03-25 01:57:36 +03:00
|
|
|
return LookAndFeelFontToStyle(font, aFontName, aFontStyle);
|
2020-12-16 07:17:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
char16_t RemoteLookAndFeel::GetPasswordCharacterImpl() {
|
|
|
|
return static_cast<char16_t>(mTables.passwordChar());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RemoteLookAndFeel::GetEchoPasswordImpl() { return mTables.passwordEcho(); }
|
|
|
|
|
2021-04-30 15:36:50 +03:00
|
|
|
static bool AddIDsToMap(nsXPLookAndFeel* aImpl, FullLookAndFeel* aLf) {
|
2021-03-30 11:17:38 +03:00
|
|
|
using IntID = LookAndFeel::IntID;
|
|
|
|
using FontID = LookAndFeel::FontID;
|
|
|
|
using FloatID = LookAndFeel::FloatID;
|
|
|
|
using ColorID = LookAndFeel::ColorID;
|
2021-04-03 01:17:55 +03:00
|
|
|
using ColorScheme = LookAndFeel::ColorScheme;
|
2021-03-30 11:17:38 +03:00
|
|
|
|
|
|
|
bool anyFromOtherTheme = false;
|
|
|
|
for (auto id : MakeEnumeratedRange(IntID::End)) {
|
2021-04-06 16:44:06 +03:00
|
|
|
if (!IsNeededInChildProcess(id)) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-03-30 11:17:38 +03:00
|
|
|
int32_t theInt;
|
|
|
|
nsresult rv = aImpl->NativeGetInt(id, theInt);
|
|
|
|
AddToMap(aLf->tables().ints(), aLf->tables().intMap(), id,
|
|
|
|
NS_SUCCEEDED(rv) ? Some(theInt) : Nothing{});
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto id : MakeEnumeratedRange(ColorID::End)) {
|
|
|
|
nscolor theColor;
|
2021-04-03 01:17:55 +03:00
|
|
|
nsresult rv = aImpl->NativeGetColor(id, ColorScheme::Light, theColor);
|
|
|
|
AddToMap(aLf->tables().lightColors(), aLf->tables().lightColorMap(), id,
|
|
|
|
NS_SUCCEEDED(rv) ? Some(theColor) : Nothing{});
|
|
|
|
rv = aImpl->NativeGetColor(id, ColorScheme::Dark, theColor);
|
|
|
|
AddToMap(aLf->tables().darkColors(), aLf->tables().darkColorMap(), id,
|
2021-03-30 11:17:38 +03:00
|
|
|
NS_SUCCEEDED(rv) ? Some(theColor) : Nothing{});
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto id : MakeEnumeratedRange(FloatID::End)) {
|
|
|
|
float theFloat;
|
|
|
|
nsresult rv = aImpl->NativeGetFloat(id, theFloat);
|
|
|
|
AddToMap(aLf->tables().floats(), aLf->tables().floatMap(), id,
|
|
|
|
NS_SUCCEEDED(rv) ? Some(theFloat) : Nothing{});
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto id : MakeEnumeratedRange(FontID::End)) {
|
|
|
|
gfxFontStyle fontStyle{};
|
|
|
|
|
|
|
|
nsString name;
|
|
|
|
bool rv = aImpl->NativeGetFont(id, name, fontStyle);
|
|
|
|
Maybe<LookAndFeelFont> maybeFont;
|
|
|
|
if (rv) {
|
|
|
|
maybeFont.emplace(
|
|
|
|
nsXPLookAndFeel::StyleToLookAndFeelFont(name, fontStyle));
|
|
|
|
}
|
|
|
|
AddToMap(aLf->tables().fonts(), aLf->tables().fontMap(), id,
|
|
|
|
std::move(maybeFont));
|
|
|
|
}
|
|
|
|
|
|
|
|
return anyFromOtherTheme;
|
|
|
|
}
|
|
|
|
|
2020-12-16 07:17:36 +03:00
|
|
|
// static
|
2020-12-16 07:41:46 +03:00
|
|
|
const FullLookAndFeel* RemoteLookAndFeel::ExtractData() {
|
2020-12-16 07:17:36 +03:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess(),
|
|
|
|
"Only parent processes should be extracting LookAndFeel data");
|
|
|
|
|
2020-12-22 22:26:41 +03:00
|
|
|
if (sCachedLookAndFeelData) {
|
|
|
|
return sCachedLookAndFeelData;
|
2020-12-16 07:41:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool sInitialized = false;
|
|
|
|
if (!sInitialized) {
|
|
|
|
sInitialized = true;
|
2020-12-22 22:26:41 +03:00
|
|
|
ClearOnShutdown(&sCachedLookAndFeelData);
|
2020-12-16 07:41:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
FullLookAndFeel* lf = new FullLookAndFeel{};
|
2020-12-16 07:17:36 +03:00
|
|
|
nsXPLookAndFeel* impl = nsXPLookAndFeel::GetInstance();
|
|
|
|
|
2020-12-22 22:26:41 +03:00
|
|
|
#ifdef MOZ_WIDGET_GTK
|
2021-04-30 15:36:50 +03:00
|
|
|
impl->GetGtkContentTheme(lf->theme());
|
2020-12-22 22:26:41 +03:00
|
|
|
#endif
|
2020-12-16 07:17:36 +03:00
|
|
|
|
2021-05-03 03:03:34 +03:00
|
|
|
lf->tables().passwordChar() = impl->GetPasswordCharacterImpl();
|
|
|
|
lf->tables().passwordEcho() = impl->GetEchoPasswordImpl();
|
|
|
|
|
2021-04-30 15:36:50 +03:00
|
|
|
AddIDsToMap(impl, lf);
|
2021-03-30 11:17:38 +03:00
|
|
|
|
2020-12-22 22:26:41 +03:00
|
|
|
// This assignment to sCachedLookAndFeelData must be done after the
|
2020-12-16 07:41:46 +03:00
|
|
|
// WithThemeConfiguredForContent call, since it can end up calling RefreshImpl
|
|
|
|
// on the LookAndFeel, which will clear out sCachedTables.
|
2020-12-22 22:26:41 +03:00
|
|
|
sCachedLookAndFeelData = lf;
|
|
|
|
return sCachedLookAndFeelData;
|
2020-12-16 07:17:36 +03:00
|
|
|
}
|
|
|
|
|
2020-12-16 07:41:46 +03:00
|
|
|
void RemoteLookAndFeel::ClearCachedData() {
|
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
2020-12-22 22:26:41 +03:00
|
|
|
sCachedLookAndFeelData = nullptr;
|
2020-12-16 07:41:46 +03:00
|
|
|
}
|
|
|
|
|
2020-12-16 07:17:36 +03:00
|
|
|
} // namespace mozilla::widget
|