Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / content / browser / theme_helper_mac.mm
blobba91bcd703ec855e7d3ad225959a3c8ef6ab3115
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 @interface ScrollbarPrefsObserver : NSObject
20 + (void)registerAsObserver;
21 + (void)appearancePrefsChanged:(NSNotification*)notification;
22 + (void)behaviorPrefsChanged:(NSNotification*)notification;
23 + (void)notifyPrefsChangedWithRedraw:(BOOL)redraw;
25 @end
27 @implementation ScrollbarPrefsObserver
29 + (void)registerAsObserver {
30   [[NSDistributedNotificationCenter defaultCenter]
31       addObserver:self
32          selector:@selector(appearancePrefsChanged:)
33              name:@"AppleAquaScrollBarVariantChanged"
34            object:nil
35 suspensionBehavior:NSNotificationSuspensionBehaviorDeliverImmediately];
37   [[NSDistributedNotificationCenter defaultCenter]
38       addObserver:self
39          selector:@selector(behaviorPrefsChanged:)
40              name:@"AppleNoRedisplayAppearancePreferenceChanged"
41            object:nil
42 suspensionBehavior:NSNotificationSuspensionBehaviorCoalesce];
44   // In single-process mode, renderers will catch these notifications
45   // themselves and listening for them here may trigger the DCHECK in Observe().
46   if ([NSScroller respondsToSelector:@selector(preferredScrollerStyle)] &&
47       !base::CommandLine::ForCurrentProcess()->HasSwitch(
48           switches::kSingleProcess)) {
49     [[NSNotificationCenter defaultCenter]
50         addObserver:self
51            selector:@selector(behaviorPrefsChanged:)
52                name:NSPreferredScrollerStyleDidChangeNotification
53              object:nil];
54   }
57 + (void)appearancePrefsChanged:(NSNotification*)notification {
58   [self notifyPrefsChangedWithRedraw:YES];
61 + (void)behaviorPrefsChanged:(NSNotification*)notification {
62   [self notifyPrefsChangedWithRedraw:NO];
65 + (void)notifyPrefsChangedWithRedraw:(BOOL)redraw {
66   DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
67   NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
68   [defaults synchronize];
70   content::ThemeHelperMac::SendThemeChangeToAllRenderers(
71       [defaults floatForKey:@"NSScrollerButtonDelay"],
72       [defaults floatForKey:@"NSScrollerButtonPeriod"],
73       [defaults boolForKey:@"AppleScrollerPagingBehavior"],
74       content::ThemeHelperMac::GetPreferredScrollerStyle(),
75       redraw);
78 @end
80 namespace content {
82 // static
83 ThemeHelperMac* ThemeHelperMac::GetInstance() {
84   return Singleton<ThemeHelperMac,
85       LeakySingletonTraits<ThemeHelperMac> >::get();
88 // static
89 blink::ScrollerStyle ThemeHelperMac::GetPreferredScrollerStyle() {
90   if (![NSScroller respondsToSelector:@selector(preferredScrollerStyle)])
91     return blink::ScrollerStyleLegacy;
92   return static_cast<blink::ScrollerStyle>([NSScroller preferredScrollerStyle]);
95 // static
96 void ThemeHelperMac::SendThemeChangeToAllRenderers(
97     float initial_button_delay,
98     float autoscroll_button_delay,
99     bool jump_on_track_click,
100     blink::ScrollerStyle preferred_scroller_style,
101     bool redraw) {
102   ViewMsg_UpdateScrollbarTheme_Params params;
103   params.initial_button_delay = initial_button_delay;
104   params.autoscroll_button_delay = autoscroll_button_delay;
105   params.jump_on_track_click = jump_on_track_click;
106   params.preferred_scroller_style = preferred_scroller_style;
107   params.redraw = redraw;
109   for (RenderProcessHost::iterator it(RenderProcessHost::AllHostsIterator());
110        !it.IsAtEnd();
111        it.Advance()) {
112     it.GetCurrentValue()->Send(new ViewMsg_UpdateScrollbarTheme(params));
113   }
116 ThemeHelperMac::ThemeHelperMac() {
117   [ScrollbarPrefsObserver registerAsObserver];
118   registrar_.Add(this,
119                  NOTIFICATION_RENDERER_PROCESS_CREATED,
120                  NotificationService::AllSources());
123 ThemeHelperMac::~ThemeHelperMac() {
126 void ThemeHelperMac::Observe(int type,
127                              const NotificationSource& source,
128                              const NotificationDetails& details) {
129   DCHECK_EQ(NOTIFICATION_RENDERER_PROCESS_CREATED, type);
131   DCHECK_CURRENTLY_ON(BrowserThread::UI);
132   NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
133   [defaults synchronize];
135   RenderProcessHost* rph = Source<RenderProcessHost>(source).ptr();
137   ViewMsg_UpdateScrollbarTheme_Params params;
138   params.initial_button_delay = [defaults floatForKey:@"NSScrollerButtonDelay"];
139   params.autoscroll_button_delay =
140       [defaults floatForKey:@"NSScrollerButtonPeriod"];
141   params.jump_on_track_click =
142       [defaults boolForKey:@"AppleScrollerPagingBehavior"];
143   params.preferred_scroller_style = GetPreferredScrollerStyle();
144   params.redraw = false;
146   rph->Send(new ViewMsg_UpdateScrollbarTheme(params));
149 }  // namespace content