Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / content / browser / theme_helper_mac.mm
blob29190602f8dcc9c756446507b330f86da4c7999c
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       !base::CommandLine::ForCurrentProcess()->HasSwitch(
57           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_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(),
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_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(),
148       false));
151 }  // namespace content