Home | History | Annotate | Download | only in browser
      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.
      4 
      5 #include "content/browser/theme_helper_mac.h"
      6 
      7 #include <Foundation/Foundation.h>
      8 
      9 #include "content/common/view_messages.h"
     10 #include "content/public/browser/browser_thread.h"
     11 #include "content/public/browser/notification_service.h"
     12 #include "content/public/browser/notification_types.h"
     13 #include "content/public/browser/render_process_host.h"
     14 
     15 @interface ScrollbarPrefsObserver : NSObject
     16 
     17 + (void)registerAsObserver;
     18 + (void)appearancePrefsChanged:(NSNotification*)notification;
     19 + (void)behaviorPrefsChanged:(NSNotification*)notification;
     20 + (void)notifyPrefsChangedWithRedraw:(BOOL)redraw;
     21 
     22 @end
     23 
     24 @implementation ScrollbarPrefsObserver
     25 
     26 + (void)registerAsObserver {
     27   [[NSDistributedNotificationCenter defaultCenter]
     28       addObserver:self
     29          selector:@selector(appearancePrefsChanged:)
     30              name:@"AppleAquaScrollBarVariantChanged"
     31            object:nil
     32 suspensionBehavior:NSNotificationSuspensionBehaviorDeliverImmediately];
     33 
     34   [[NSDistributedNotificationCenter defaultCenter]
     35       addObserver:self
     36          selector:@selector(behaviorPrefsChanged:)
     37              name:@"AppleNoRedisplayAppearancePreferenceChanged"
     38            object:nil
     39 suspensionBehavior:NSNotificationSuspensionBehaviorCoalesce];
     40 }
     41 
     42 + (void)appearancePrefsChanged:(NSNotification*)notification {
     43   [self notifyPrefsChangedWithRedraw:YES];
     44 }
     45 
     46 + (void)behaviorPrefsChanged:(NSNotification*)notification {
     47   [self notifyPrefsChangedWithRedraw:NO];
     48 }
     49 
     50 + (void)notifyPrefsChangedWithRedraw:(BOOL)redraw {
     51   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
     52   NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
     53   [defaults synchronize];
     54 
     55   content::ThemeHelperMac::SendThemeChangeToAllRenderers(
     56       [defaults floatForKey:@"NSScrollerButtonDelay"],
     57       [defaults floatForKey:@"NSScrollerButtonPeriod"],
     58       [defaults boolForKey:@"AppleScrollerPagingBehavior"],
     59       redraw);
     60 }
     61 
     62 @end
     63 
     64 namespace content {
     65 
     66 ThemeHelperMac::ThemeHelperMac() {
     67   [ScrollbarPrefsObserver registerAsObserver];
     68   registrar_.Add(this,
     69                  NOTIFICATION_RENDERER_PROCESS_CREATED,
     70                  NotificationService::AllSources());
     71 }
     72 
     73 ThemeHelperMac::~ThemeHelperMac() {
     74 }
     75 
     76 // static
     77 ThemeHelperMac* ThemeHelperMac::GetInstance() {
     78   return Singleton<ThemeHelperMac,
     79       LeakySingletonTraits<ThemeHelperMac> >::get();
     80 }
     81 
     82 
     83 void ThemeHelperMac::Observe(int type,
     84                              const NotificationSource& source,
     85                              const NotificationDetails& details) {
     86   DCHECK_EQ(NOTIFICATION_RENDERER_PROCESS_CREATED, type);
     87 
     88   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     89   NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
     90   [defaults synchronize];
     91 
     92   RenderProcessHost* rph = Source<RenderProcessHost>(source).ptr();
     93   rph->Send(new ViewMsg_UpdateScrollbarTheme(
     94       [defaults floatForKey:@"NSScrollerButtonDelay"],
     95       [defaults floatForKey:@"NSScrollerButtonPeriod"],
     96       [defaults boolForKey:@"AppleScrollerPagingBehavior"],
     97       false));
     98 }
     99 
    100 // static
    101 void ThemeHelperMac::SendThemeChangeToAllRenderers(
    102     float initial_button_delay,
    103     float autoscroll_button_delay,
    104     bool jump_on_track_click,
    105     bool redraw) {
    106   for (RenderProcessHost::iterator it(RenderProcessHost::AllHostsIterator());
    107        !it.IsAtEnd();
    108        it.Advance()) {
    109     it.GetCurrentValue()->Send(new ViewMsg_UpdateScrollbarTheme(
    110         initial_button_delay,
    111         autoscroll_button_delay,
    112         jump_on_track_click,
    113         redraw));
    114   }
    115 }
    116 
    117 }  // namespace content
    118