Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / content / browser / theme_helper_mac.mm
blob9f0f86d1a59dd253b3f52cf74f66e3b391fef770
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";
25 #endif
27 @interface ScrollbarPrefsObserver : NSObject
29 + (void)registerAsObserver;
30 + (void)appearancePrefsChanged:(NSNotification*)notification;
31 + (void)behaviorPrefsChanged:(NSNotification*)notification;
32 + (void)notifyPrefsChangedWithRedraw:(BOOL)redraw;
34 @end
36 @implementation ScrollbarPrefsObserver
38 + (void)registerAsObserver {
39   [[NSDistributedNotificationCenter defaultCenter]
40       addObserver:self
41          selector:@selector(appearancePrefsChanged:)
42              name:@"AppleAquaScrollBarVariantChanged"
43            object:nil
44 suspensionBehavior:NSNotificationSuspensionBehaviorDeliverImmediately];
46   [[NSDistributedNotificationCenter defaultCenter]
47       addObserver:self
48          selector:@selector(behaviorPrefsChanged:)
49              name:@"AppleNoRedisplayAppearancePreferenceChanged"
50            object:nil
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]
58         addObserver:self
59            selector:@selector(behaviorPrefsChanged:)
60                name:NSPreferredScrollerStyleDidChangeNotification
61              object:nil];
62   }
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(),
83       redraw);
86 @end
88 namespace content {
90 // static
91 ThemeHelperMac* ThemeHelperMac::GetInstance() {
92   return Singleton<ThemeHelperMac,
93       LeakySingletonTraits<ThemeHelperMac> >::get();
96 // static
97 blink::ScrollerStyle ThemeHelperMac::GetPreferredScrollerStyle() {
98   if (![NSScroller respondsToSelector:@selector(preferredScrollerStyle)])
99     return blink::ScrollerStyleLegacy;
100   return static_cast<blink::ScrollerStyle>([NSScroller preferredScrollerStyle]);
103 // static
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,
109     bool redraw) {
110   for (RenderProcessHost::iterator it(RenderProcessHost::AllHostsIterator());
111        !it.IsAtEnd();
112        it.Advance()) {
113     it.GetCurrentValue()->Send(new ViewMsg_UpdateScrollbarTheme(
114         initial_button_delay,
115         autoscroll_button_delay,
116         jump_on_track_click,
117         preferred_scroller_style,
118         redraw));
119   }
122 ThemeHelperMac::ThemeHelperMac() {
123   [ScrollbarPrefsObserver registerAsObserver];
124   registrar_.Add(this,
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(),
147       false));
150 }  // namespace content