Update optimize_png_files.sh to work on msysgit bash.
[chromium-blink-merge.git] / content / browser / theme_helper_mac.mm
blob34e90820c2e10557bdf91f9c74335c967cf5d06e
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 "content/common/view_messages.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/notification_service.h"
13 #include "content/public/browser/notification_types.h"
14 #include "content/public/browser/render_process_host.h"
15 #include "content/public/common/content_switches.h"
17 // Declare notification names from the 10.7 SDK.
18 #if !defined(MAC_OS_X_VERSION_10_7) || \
19     MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7
20 NSString* NSPreferredScrollerStyleDidChangeNotification =
21     @"NSPreferredScrollerStyleDidChangeNotification";
23 @interface NSScroller (LionSDK)
24 + (NSInteger)preferredScrollerStyle;
25 @end
26 #endif
28 @interface ScrollbarPrefsObserver : NSObject
30 + (void)registerAsObserver;
31 + (void)appearancePrefsChanged:(NSNotification*)notification;
32 + (void)behaviorPrefsChanged:(NSNotification*)notification;
33 + (void)notifyPrefsChangedWithRedraw:(BOOL)redraw;
35 @end
37 @implementation ScrollbarPrefsObserver
39 + (void)registerAsObserver {
40   [[NSDistributedNotificationCenter defaultCenter]
41       addObserver:self
42          selector:@selector(appearancePrefsChanged:)
43              name:@"AppleAquaScrollBarVariantChanged"
44            object:nil
45 suspensionBehavior:NSNotificationSuspensionBehaviorDeliverImmediately];
47   [[NSDistributedNotificationCenter defaultCenter]
48       addObserver:self
49          selector:@selector(behaviorPrefsChanged:)
50              name:@"AppleNoRedisplayAppearancePreferenceChanged"
51            object:nil
52 suspensionBehavior:NSNotificationSuspensionBehaviorCoalesce];
54   // In single-process mode, renderers will catch these notifications
55   // themselves and listening for them here may trigger the DCHECK in Observe().
56   if ([NSScroller respondsToSelector:@selector(preferredScrollerStyle)] &&
57       !CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess)) {
58     [[NSNotificationCenter defaultCenter]
59         addObserver:self
60            selector:@selector(behaviorPrefsChanged:)
61                name:NSPreferredScrollerStyleDidChangeNotification
62              object:nil];
63   }
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(content::BrowserThread::CurrentlyOn(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(),
84       redraw);
87 @end
89 namespace content {
91 // static
92 ThemeHelperMac* ThemeHelperMac::GetInstance() {
93   return Singleton<ThemeHelperMac,
94       LeakySingletonTraits<ThemeHelperMac> >::get();
97 // static
98 blink::ScrollerStyle ThemeHelperMac::GetPreferredScrollerStyle() {
99   if (![NSScroller respondsToSelector:@selector(preferredScrollerStyle)])
100     return blink::ScrollerStyleLegacy;
101   return static_cast<blink::ScrollerStyle>([NSScroller preferredScrollerStyle]);
104 // static
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,
110     bool redraw) {
111   for (RenderProcessHost::iterator it(RenderProcessHost::AllHostsIterator());
112        !it.IsAtEnd();
113        it.Advance()) {
114     it.GetCurrentValue()->Send(new ViewMsg_UpdateScrollbarTheme(
115         initial_button_delay,
116         autoscroll_button_delay,
117         jump_on_track_click,
118         preferred_scroller_style,
119         redraw));
120   }
123 ThemeHelperMac::ThemeHelperMac() {
124   [ScrollbarPrefsObserver registerAsObserver];
125   registrar_.Add(this,
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(BrowserThread::CurrentlyOn(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(),
148       false));
151 }  // namespace content