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 !base::CommandLine::ForCurrentProcess()->HasSwitch(
57 switches::kSingleProcess)) {
58 [[NSNotificationCenter defaultCenter]
60 selector:@selector(behaviorPrefsChanged:)
61 name:NSPreferredScrollerStyleDidChangeNotification
66 + (void)appearancePrefsChanged:(NSNotification*)notification {
67 [self notifyPrefsChangedWithRedraw:YES];
70 + (void)behaviorPrefsChanged:(NSNotification*)notification {
71 [self notifyPrefsChangedWithRedraw:NO];
74 + (void)notifyPrefsChangedWithRedraw:(BOOL)redraw {
75 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
76 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
77 [defaults synchronize];
79 content::ThemeHelperMac::SendThemeChangeToAllRenderers(
80 [defaults floatForKey:@"NSScrollerButtonDelay"],
81 [defaults floatForKey:@"NSScrollerButtonPeriod"],
82 [defaults boolForKey:@"AppleScrollerPagingBehavior"],
83 content::ThemeHelperMac::GetPreferredScrollerStyle(),
92 ThemeHelperMac* ThemeHelperMac::GetInstance() {
93 return Singleton<ThemeHelperMac,
94 LeakySingletonTraits<ThemeHelperMac> >::get();
98 blink::ScrollerStyle ThemeHelperMac::GetPreferredScrollerStyle() {
99 if (![NSScroller respondsToSelector:@selector(preferredScrollerStyle)])
100 return blink::ScrollerStyleLegacy;
101 return static_cast<blink::ScrollerStyle>([NSScroller preferredScrollerStyle]);
105 void ThemeHelperMac::SendThemeChangeToAllRenderers(
106 float initial_button_delay,
107 float autoscroll_button_delay,
108 bool jump_on_track_click,
109 blink::ScrollerStyle preferred_scroller_style,
111 for (RenderProcessHost::iterator it(RenderProcessHost::AllHostsIterator());
114 it.GetCurrentValue()->Send(new ViewMsg_UpdateScrollbarTheme(
115 initial_button_delay,
116 autoscroll_button_delay,
118 preferred_scroller_style,
123 ThemeHelperMac::ThemeHelperMac() {
124 [ScrollbarPrefsObserver registerAsObserver];
126 NOTIFICATION_RENDERER_PROCESS_CREATED,
127 NotificationService::AllSources());
130 ThemeHelperMac::~ThemeHelperMac() {
133 void ThemeHelperMac::Observe(int type,
134 const NotificationSource& source,
135 const NotificationDetails& details) {
136 DCHECK_EQ(NOTIFICATION_RENDERER_PROCESS_CREATED, type);
138 DCHECK_CURRENTLY_ON(BrowserThread::UI);
139 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
140 [defaults synchronize];
142 RenderProcessHost* rph = Source<RenderProcessHost>(source).ptr();
143 rph->Send(new ViewMsg_UpdateScrollbarTheme(
144 [defaults floatForKey:@"NSScrollerButtonDelay"],
145 [defaults floatForKey:@"NSScrollerButtonPeriod"],
146 [defaults boolForKey:@"AppleScrollerPagingBehavior"],
147 GetPreferredScrollerStyle(),
151 } // namespace content