summaryrefslogtreecommitdiffstats
path: root/app/system_monitor_mac.mm
blob: d40970b61041d57032d59b9e49aec7f20ab0479d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "app/system_monitor.h"

#import <AppKit/AppKit.h>

@interface SystemMonitorBridge : NSObject {
 @private
  SystemMonitor* systemMonitor_;  // weak
}

- (id)initWithSystemMonitor:(SystemMonitor*)monitor;
- (void)computerDidSleep:(NSNotification*)notification;
- (void)computerDidWake:(NSNotification*)notification;

@end

@implementation SystemMonitorBridge

- (id)initWithSystemMonitor:(SystemMonitor*)monitor {
  self = [super init];
  if (self) {
    systemMonitor_ = monitor;

    // See QA1340
    // <http://developer.apple.com/library/mac/#qa/qa2004/qa1340.html> for more
    // details.
    [[[NSWorkspace sharedWorkspace] notificationCenter]
        addObserver:self
           selector:@selector(computerDidSleep:)
               name:NSWorkspaceWillSleepNotification
             object:nil];
    [[[NSWorkspace sharedWorkspace] notificationCenter]
        addObserver:self
           selector:@selector(computerDidWake:)
               name:NSWorkspaceDidWakeNotification
             object:nil];
  }
  return self;
}

- (void)dealloc {
  [[[NSWorkspace sharedWorkspace] notificationCenter]
      removeObserver:self];
  [super dealloc];
}

- (void)computerDidSleep:(NSNotification*)notification {
  systemMonitor_->ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT);
}

- (void)computerDidWake:(NSNotification*)notification {
  systemMonitor_->ProcessPowerMessage(SystemMonitor::RESUME_EVENT);
}

@end

void SystemMonitor::PlatformInit() {
  system_monitor_bridge_ =
      [[SystemMonitorBridge alloc] initWithSystemMonitor:this];
}

void SystemMonitor::PlatformDestroy() {
  [system_monitor_bridge_ release];
}