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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
content / browser / theme_helper_mac.mm [blame]
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/theme_helper_mac.h"
#import <Carbon/Carbon.h>
#import <Cocoa/Cocoa.h>
#include "base/command_line.h"
#include "base/containers/span.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/mac/mac_util.h"
#include "base/strings/sys_string_conversions.h"
#include "content/browser/renderer_host/render_process_host_impl.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/common/renderer.mojom.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_switches.h"
#include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h"
using content::RenderProcessHost;
using content::RenderProcessHostImpl;
using content::ThemeHelperMac;
namespace {
void FillScrollbarThemeParams(
content::mojom::UpdateScrollbarThemeParams* params) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
NSUserDefaults* defaults = NSUserDefaults.standardUserDefaults;
[defaults synchronize];
// NSScrollerButtonDelay and NSScrollerButtonPeriod are no longer initialized
// in +[NSApplication _initializeRegisteredDefaults] as of 10.15. Their values
// still seem to affect behavior, but their use is logged as an "unusual app
// config", so it's not clear how much longer they'll be implemented.
params->has_initial_button_delay =
[defaults objectForKey:@"NSScrollerButtonDelay"] != nil;
params->initial_button_delay =
[defaults floatForKey:@"NSScrollerButtonDelay"];
params->has_autoscroll_button_delay =
[defaults objectForKey:@"NSScrollerButtonPeriod"] != nil;
params->autoscroll_button_delay =
[defaults floatForKey:@"NSScrollerButtonPeriod"];
params->jump_on_track_click =
[defaults boolForKey:@"AppleScrollerPagingBehavior"];
params->preferred_scroller_style =
static_cast<blink::ScrollerStyle>([NSScroller preferredScrollerStyle]);
id rubber_band_value = [defaults objectForKey:@"NSScrollViewRubberbanding"];
params->scroll_view_rubber_banding =
rubber_band_value ? [rubber_band_value boolValue] : YES;
}
void SendSystemColorsChangedMessage(content::mojom::Renderer* renderer) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
NSUserDefaults* defaults = NSUserDefaults.standardUserDefaults;
[defaults synchronize];
renderer->OnSystemColorsChanged(
[[defaults stringForKey:@"AppleAquaColorVariant"] intValue]);
}
SkColor NSColorToSkColor(NSColor* color) {
NSColor* color_in_color_space =
[color colorUsingColorSpace:NSColorSpace.sRGBColorSpace];
if (color_in_color_space) {
// Use nextafter() to avoid rounding colors in a way that could be off-by-
// one. See https://bugs.webkit.org/show_bug.cgi?id=6129.
static const double kScaleFactor = nextafter(256.0, 0.0);
return SkColorSetARGB(
static_cast<int>(kScaleFactor * [color_in_color_space alphaComponent]),
static_cast<int>(kScaleFactor * [color_in_color_space redComponent]),
static_cast<int>(kScaleFactor * [color_in_color_space greenComponent]),
static_cast<int>(kScaleFactor * [color_in_color_space blueComponent]));
}
// This conversion above can fail if the NSColor in question is an
// NSPatternColor (as many system colors are). These colors are actually a
// repeating pattern not just a solid color. To work around this we simply
// draw a 1x1 image of the color and use that pixel's color. It might be
// better to use an average of the colors in the pattern instead.
NSBitmapImageRep* offscreen_rep =
[[NSBitmapImageRep alloc] initWithBitmapDataPlanes:nil
pixelsWide:1
pixelsHigh:1
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSDeviceRGBColorSpace
bytesPerRow:4
bitsPerPixel:32];
{
gfx::ScopedNSGraphicsContextSaveGState gstate;
NSGraphicsContext.currentContext =
[NSGraphicsContext graphicsContextWithBitmapImageRep:offscreen_rep];
[color set];
NSRectFill(NSMakeRect(0, 0, 1, 1));
}
NSUInteger pixel[4];
[offscreen_rep getPixel:pixel atX:0 y:0];
// This recursive call will not recurse again, because the color space
// the second time around is NSDeviceRGBColorSpace.
return NSColorToSkColor([NSColor colorWithDeviceRed:pixel[0] / 255.
green:pixel[1] / 255.
blue:pixel[2] / 255.
alpha:1.]);
}
} // namespace
@interface SystemThemeObserver : NSObject {
base::RepeatingClosure _colorsChangedCallback;
}
- (instancetype)initWithColorsChangedCallback:
(base::RepeatingClosure)colorsChangedCallback;
- (void)appearancePrefsChanged:(NSNotification*)notification;
- (void)behaviorPrefsChanged:(NSNotification*)notification;
- (void)notifyPrefsChangedWithRedraw:(BOOL)redraw;
@end
@implementation SystemThemeObserver
- (instancetype)initWithColorsChangedCallback:
(base::RepeatingClosure)colorsChangedCallback {
if (!(self = [super init])) {
return nil;
}
_colorsChangedCallback = std::move(colorsChangedCallback);
NSDistributedNotificationCenter* distributedCenter =
NSDistributedNotificationCenter.defaultCenter;
[distributedCenter addObserver:self
selector:@selector(appearancePrefsChanged:)
name:@"AppleAquaScrollBarVariantChanged"
object:nil
suspensionBehavior:
NSNotificationSuspensionBehaviorDeliverImmediately];
[distributedCenter addObserver:self
selector:@selector(behaviorPrefsChanged:)
name:@"AppleNoRedisplayAppearancePreferenceChanged"
object:nil
suspensionBehavior:NSNotificationSuspensionBehaviorCoalesce];
[distributedCenter addObserver:self
selector:@selector(behaviorPrefsChanged:)
name:@"NSScrollAnimationEnabled"
object:nil
suspensionBehavior:NSNotificationSuspensionBehaviorCoalesce];
[distributedCenter addObserver:self
selector:@selector(appearancePrefsChanged:)
name:@"AppleScrollBarVariant"
object:nil
suspensionBehavior:
NSNotificationSuspensionBehaviorDeliverImmediately];
[distributedCenter
addObserver:self
selector:@selector(behaviorPrefsChanged:)
name:@"NSScrollViewRubberbanding"
object:nil
suspensionBehavior:NSNotificationSuspensionBehaviorDeliverImmediately];
// In single-process mode, renderers will catch these notifications
// themselves and listening for them here may trigger the DCHECK in Observe().
if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kSingleProcess)) {
NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
[center addObserver:self
selector:@selector(behaviorPrefsChanged:)
name:NSPreferredScrollerStyleDidChangeNotification
object:nil];
[center addObserver:self
selector:@selector(systemColorsChanged:)
name:NSSystemColorsDidChangeNotification
object:nil];
}
return self;
}
- (void)dealloc {
[NSDistributedNotificationCenter.defaultCenter removeObserver:self];
}
- (void)appearancePrefsChanged:(NSNotification*)notification {
[self notifyPrefsChangedWithRedraw:YES];
}
- (void)behaviorPrefsChanged:(NSNotification*)notification {
[self notifyPrefsChangedWithRedraw:NO];
}
- (void)systemColorsChanged:(NSNotification*)notification {
_colorsChangedCallback.Run();
for (RenderProcessHost::iterator it(RenderProcessHost::AllHostsIterator());
!it.IsAtEnd();
it.Advance()) {
SendSystemColorsChangedMessage(
it.GetCurrentValue()->GetRendererInterface());
}
}
- (void)notifyPrefsChangedWithRedraw:(BOOL)redraw {
for (RenderProcessHost::iterator it(RenderProcessHost::AllHostsIterator());
!it.IsAtEnd();
it.Advance()) {
content::mojom::UpdateScrollbarThemeParamsPtr params =
content::mojom::UpdateScrollbarThemeParams::New();
FillScrollbarThemeParams(params.get());
params->redraw = redraw;
RenderProcessHostImpl* process_host =
static_cast<RenderProcessHostImpl*>(it.GetCurrentValue());
process_host->GetRendererInterface()->UpdateScrollbarTheme(
std::move(params));
}
for (content::WebContentsImpl* web_contents :
content::WebContentsImpl::GetAllWebContents()) {
web_contents->OnWebPreferencesChanged();
}
}
@end
namespace content {
struct ThemeHelperMac::ObjCStorage {
// ObjC object that observes notifications from the system.
SystemThemeObserver* __strong theme_observer;
};
// static
ThemeHelperMac* ThemeHelperMac::GetInstance() {
static ThemeHelperMac* instance = new ThemeHelperMac();
return instance;
}
base::ReadOnlySharedMemoryRegion
ThemeHelperMac::DuplicateReadOnlyColorMapRegion() {
return read_only_color_map_.Duplicate();
}
ThemeHelperMac::ThemeHelperMac()
: objc_storage_(std::make_unique<ObjCStorage>()) {
// Allocate a region for the SkColor value table and map it.
auto writable_region = base::WritableSharedMemoryRegion::Create(
sizeof(SkColor) * blink::kMacSystemColorIDCount *
blink::kMacSystemColorSchemeCount);
writable_color_map_ = writable_region.Map();
// Downgrade the region to read-only after it has been mapped.
read_only_color_map_ = base::WritableSharedMemoryRegion::ConvertToReadOnly(
std::move(writable_region));
// Store the current color scheme into the table.
LoadSystemColors();
// Start observing for changes.
objc_storage_->theme_observer = [[SystemThemeObserver alloc]
initWithColorsChangedCallback:base::BindRepeating(
&ThemeHelperMac::LoadSystemColors,
base::Unretained(this))];
}
ThemeHelperMac::~ThemeHelperMac() = default;
void ThemeHelperMac::LoadSystemColorsForCurrentAppearance(
base::span<SkColor> values) {
for (size_t i = 0; i < blink::kMacSystemColorIDCount; ++i) {
blink::MacSystemColorID color_id = static_cast<blink::MacSystemColorID>(i);
switch (color_id) {
case blink::MacSystemColorID::kControlAccentBlueColor: {
NSColor* color =
[NSColor colorWithCatalogName:@"System"
colorName:@"controlAccentBlueColor"];
if (color) {
values[i] = NSColorToSkColor(color);
} else {
// If the controlAccentBlueColor isn't available just set a black
// value.
values[i] = SK_ColorBLACK;
}
break;
}
case blink::MacSystemColorID::kControlAccentColor:
values[i] = NSColorToSkColor(NSColor.controlAccentColor);
break;
case blink::MacSystemColorID::kKeyboardFocusIndicator:
values[i] = NSColorToSkColor(NSColor.keyboardFocusIndicatorColor);
break;
case blink::MacSystemColorID::kSecondarySelectedControl:
values[i] = NSColorToSkColor(
NSColor.unemphasizedSelectedContentBackgroundColor);
break;
case blink::MacSystemColorID::kSelectedTextBackground:
values[i] = NSColorToSkColor(NSColor.selectedTextBackgroundColor);
break;
case blink::MacSystemColorID::kCount:
NOTREACHED_IN_MIGRATION();
break;
}
}
}
void ThemeHelperMac::LoadSystemColors() {
static_assert(blink::kMacSystemColorSchemeCount == 2,
"Light and dark color scheme system colors loaded.");
base::span<SkColor> values = writable_color_map_.GetMemoryAsSpan<SkColor>(
blink::kMacSystemColorIDCount * blink::kMacSystemColorSchemeCount);
[[NSAppearance appearanceNamed:NSAppearanceNameAqua]
performAsCurrentDrawingAppearance:^{
LoadSystemColorsForCurrentAppearance(values.subspan(
0, static_cast<size_t>(blink::MacSystemColorID::kCount)));
}];
[[NSAppearance appearanceNamed:NSAppearanceNameDarkAqua]
performAsCurrentDrawingAppearance:^{
LoadSystemColorsForCurrentAppearance(values.subspan(
static_cast<size_t>(blink::MacSystemColorID::kCount),
static_cast<size_t>(blink::MacSystemColorID::kCount)));
}];
}
void ThemeHelperMac::OnRenderProcessHostCreated(
content::RenderProcessHost* host) {
// When a new RenderProcess is created, send it the initial preference
// parameters.
content::mojom::UpdateScrollbarThemeParamsPtr params =
content::mojom::UpdateScrollbarThemeParams::New();
FillScrollbarThemeParams(params.get());
params->redraw = false;
RenderProcessHostImpl* process_host =
static_cast<content::RenderProcessHostImpl*>(host);
content::mojom::Renderer* renderer = process_host->GetRendererInterface();
renderer->UpdateScrollbarTheme(std::move(params));
SendSystemColorsChangedMessage(renderer);
}
} // namespace content