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 // Declare notification names from the 10.7 SDK.
19 #if !defined(MAC_OS_X_VERSION_10_7) || \
20 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7
22 NSString* NSPreferredScrollerStyleDidChangeNotification =
23 @"NSPreferredScrollerStyleDidChangeNotification";
27 @interface ScrollbarPrefsObserver : NSObject
29 + (void)registerAsObserver;
30 + (void)appearancePrefsChanged:(NSNotification*)notification;
31 + (void)behaviorPrefsChanged:(NSNotification*)notification;
32 + (void)notifyPrefsChangedWithRedraw:(BOOL)redraw;
36 @implementation ScrollbarPrefsObserver
38 + (void)registerAsObserver {
39 [[NSDistributedNotificationCenter defaultCenter]
41 selector:@selector(appearancePrefsChanged:)
42 name:@"AppleAquaScrollBarVariantChanged"
44 suspensionBehavior:NSNotificationSuspensionBehaviorDeliverImmediately];
46 [[NSDistributedNotificationCenter defaultCenter]
48 selector:@selector(behaviorPrefsChanged:)
49 name:@"AppleNoRedisplayAppearancePreferenceChanged"
51 suspensionBehavior:NSNotificationSuspensionBehaviorCoalesce];
53 // In single-process mode, renderers will catch these notifications
54 // themselves and listening for them here may trigger the DCHECK in Observe().
55 if ([NSScroller respondsToSelector:@selector(preferredScrollerStyle)] &&
56 !CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess)) {
57 [[NSNotificationCenter defaultCenter]
59 selector:@selector(behaviorPrefsChanged:)
60 name:NSPreferredScrollerStyleDidChangeNotification
65 + (void)appearancePrefsChanged:(NSNotification*)notification {
66 [self notifyPrefsChangedWithRedraw:YES];
69 + (void)behaviorPrefsChanged:(NSNotification*)notification {
70 [self notifyPrefsChangedWithRedraw:NO];
73 + (void)notifyPrefsChangedWithRedraw:(BOOL)redraw {
74 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
75 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
76 [defaults synchronize];
78 content::ThemeHelperMac::SendThemeChangeToAllRenderers(
79 [defaults floatForKey:@"NSScrollerButtonDelay"],
80 [defaults floatForKey:@"NSScrollerButtonPeriod"],
81 [defaults boolForKey:@"AppleScrollerPagingBehavior"],
82 content::ThemeHelperMac::GetPreferredScrollerStyle(),
91 ThemeHelperMac* ThemeHelperMac::GetInstance() {
92 return Singleton<ThemeHelperMac,
93 LeakySingletonTraits<ThemeHelperMac> >::get();
97 blink::ScrollerStyle ThemeHelperMac::GetPreferredScrollerStyle() {
98 if (![NSScroller respondsToSelector:@selector(preferredScrollerStyle)])
99 return blink::ScrollerStyleLegacy;
100 return static_cast<blink::ScrollerStyle>([NSScroller preferredScrollerStyle]);
104 void ThemeHelperMac::SendThemeChangeToAllRenderers(
105 float initial_button_delay,
106 float autoscroll_button_delay,
107 bool jump_on_track_click,
108 blink::ScrollerStyle preferred_scroller_style,
110 for (RenderProcessHost::iterator it(RenderProcessHost::AllHostsIterator());
113 it.GetCurrentValue()->Send(new ViewMsg_UpdateScrollbarTheme(
114 initial_button_delay,
115 autoscroll_button_delay,
117 preferred_scroller_style,
122 ThemeHelperMac::ThemeHelperMac() {
123 [ScrollbarPrefsObserver registerAsObserver];
125 NOTIFICATION_RENDERER_PROCESS_CREATED,
126 NotificationService::AllSources());
129 ThemeHelperMac::~ThemeHelperMac() {
132 void ThemeHelperMac::Observe(int type,
133 const NotificationSource& source,
134 const NotificationDetails& details) {
135 DCHECK_EQ(NOTIFICATION_RENDERER_PROCESS_CREATED, type);
137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
138 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
139 [defaults synchronize];
141 RenderProcessHost* rph = Source<RenderProcessHost>(source).ptr();
142 rph->Send(new ViewMsg_UpdateScrollbarTheme(
143 [defaults floatForKey:@"NSScrollerButtonDelay"],
144 [defaults floatForKey:@"NSScrollerButtonPeriod"],
145 [defaults boolForKey:@"AppleScrollerPagingBehavior"],
146 GetPreferredScrollerStyle(),
150 } // namespace content