1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/theme_helper_mac.h"
7 #import <Cocoa/Cocoa.h>
9 #include "base/command_line.h"
10 #include "base/mac/sdk_forward_declarations.h"
11 #include "content/common/view_messages.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/notification_service.h"
14 #include "content/public/browser/notification_types.h"
15 #include "content/public/browser/render_process_host.h"
16 #include "content/public/common/content_switches.h"
18 @interface ScrollbarPrefsObserver : NSObject
20 + (void)registerAsObserver;
21 + (void)appearancePrefsChanged:(NSNotification*)notification;
22 + (void)behaviorPrefsChanged:(NSNotification*)notification;
23 + (void)notifyPrefsChangedWithRedraw:(BOOL)redraw;
27 @implementation ScrollbarPrefsObserver
29 + (void)registerAsObserver {
30 [[NSDistributedNotificationCenter defaultCenter]
32 selector:@selector(appearancePrefsChanged:)
33 name:@"AppleAquaScrollBarVariantChanged"
35 suspensionBehavior:NSNotificationSuspensionBehaviorDeliverImmediately];
37 [[NSDistributedNotificationCenter defaultCenter]
39 selector:@selector(behaviorPrefsChanged:)
40 name:@"AppleNoRedisplayAppearancePreferenceChanged"
42 suspensionBehavior:NSNotificationSuspensionBehaviorCoalesce];
44 // In single-process mode, renderers will catch these notifications
45 // themselves and listening for them here may trigger the DCHECK in Observe().
46 if ([NSScroller respondsToSelector:@selector(preferredScrollerStyle)] &&
47 !base::CommandLine::ForCurrentProcess()->HasSwitch(
48 switches::kSingleProcess)) {
49 [[NSNotificationCenter defaultCenter]
51 selector:@selector(behaviorPrefsChanged:)
52 name:NSPreferredScrollerStyleDidChangeNotification
57 + (void)appearancePrefsChanged:(NSNotification*)notification {
58 [self notifyPrefsChangedWithRedraw:YES];
61 + (void)behaviorPrefsChanged:(NSNotification*)notification {
62 [self notifyPrefsChangedWithRedraw:NO];
65 + (void)notifyPrefsChangedWithRedraw:(BOOL)redraw {
66 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
67 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
68 [defaults synchronize];
70 content::ThemeHelperMac::SendThemeChangeToAllRenderers(
71 [defaults floatForKey:@"NSScrollerButtonDelay"],
72 [defaults floatForKey:@"NSScrollerButtonPeriod"],
73 [defaults boolForKey:@"AppleScrollerPagingBehavior"],
74 content::ThemeHelperMac::GetPreferredScrollerStyle(),
83 ThemeHelperMac* ThemeHelperMac::GetInstance() {
84 return Singleton<ThemeHelperMac,
85 LeakySingletonTraits<ThemeHelperMac> >::get();
89 blink::ScrollerStyle ThemeHelperMac::GetPreferredScrollerStyle() {
90 if (![NSScroller respondsToSelector:@selector(preferredScrollerStyle)])
91 return blink::ScrollerStyleLegacy;
92 return static_cast<blink::ScrollerStyle>([NSScroller preferredScrollerStyle]);
96 void ThemeHelperMac::SendThemeChangeToAllRenderers(
97 float initial_button_delay,
98 float autoscroll_button_delay,
99 bool jump_on_track_click,
100 blink::ScrollerStyle preferred_scroller_style,
102 for (RenderProcessHost::iterator it(RenderProcessHost::AllHostsIterator());
105 it.GetCurrentValue()->Send(new ViewMsg_UpdateScrollbarTheme(
106 initial_button_delay,
107 autoscroll_button_delay,
109 preferred_scroller_style,
114 ThemeHelperMac::ThemeHelperMac() {
115 [ScrollbarPrefsObserver registerAsObserver];
117 NOTIFICATION_RENDERER_PROCESS_CREATED,
118 NotificationService::AllSources());
121 ThemeHelperMac::~ThemeHelperMac() {
124 void ThemeHelperMac::Observe(int type,
125 const NotificationSource& source,
126 const NotificationDetails& details) {
127 DCHECK_EQ(NOTIFICATION_RENDERER_PROCESS_CREATED, type);
129 DCHECK_CURRENTLY_ON(BrowserThread::UI);
130 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
131 [defaults synchronize];
133 RenderProcessHost* rph = Source<RenderProcessHost>(source).ptr();
134 rph->Send(new ViewMsg_UpdateScrollbarTheme(
135 [defaults floatForKey:@"NSScrollerButtonDelay"],
136 [defaults floatForKey:@"NSScrollerButtonPeriod"],
137 [defaults boolForKey:@"AppleScrollerPagingBehavior"],
138 GetPreferredScrollerStyle(),
142 } // namespace content