[email protected] | ba3aadf | 2012-06-14 14:07:22 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 3c64537 | 2011-01-25 20:54:06 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | ba3aadf | 2012-06-14 14:07:22 | [diff] [blame] | 5 | #include "chrome/browser/idle.h" |
[email protected] | 3c64537 | 2011-01-25 20:54:06 | [diff] [blame] | 6 | |
[email protected] | ba3aadf | 2012-06-14 14:07:22 | [diff] [blame] | 7 | #include <ApplicationServices/ApplicationServices.h> |
[email protected] | 3c64537 | 2011-01-25 20:54:06 | [diff] [blame] | 8 | #import <Cocoa/Cocoa.h> |
[email protected] | 3c64537 | 2011-01-25 20:54:06 | [diff] [blame] | 9 | |
10 | @interface MacScreenMonitor : NSObject { | ||||
11 | @private | ||||
12 | BOOL screensaverRunning_; | ||||
13 | BOOL screenLocked_; | ||||
14 | } | ||||
15 | |||||
16 | @property (readonly, | ||||
17 | nonatomic, | ||||
18 | getter=isScreensaverRunning) BOOL screensaverRunning; | ||||
19 | @property (readonly, nonatomic, getter=isScreenLocked) BOOL screenLocked; | ||||
20 | |||||
21 | @end | ||||
22 | |||||
23 | @implementation MacScreenMonitor | ||||
24 | |||||
25 | @synthesize screensaverRunning = screensaverRunning_; | ||||
26 | @synthesize screenLocked = screenLocked_; | ||||
27 | |||||
28 | - (id)init { | ||||
29 | if ((self = [super init])) { | ||||
30 | NSDistributedNotificationCenter* distCenter = | ||||
31 | [NSDistributedNotificationCenter defaultCenter]; | ||||
32 | [distCenter addObserver:self | ||||
33 | selector:@selector(onScreenSaverStarted:) | ||||
34 | name:@"com.apple.screensaver.didstart" | ||||
35 | object:nil]; | ||||
36 | [distCenter addObserver:self | ||||
37 | selector:@selector(onScreenSaverStopped:) | ||||
38 | name:@"com.apple.screensaver.didstop" | ||||
39 | object:nil]; | ||||
40 | [distCenter addObserver:self | ||||
41 | selector:@selector(onScreenLocked:) | ||||
42 | name:@"com.apple.screenIsLocked" | ||||
43 | object:nil]; | ||||
44 | [distCenter addObserver:self | ||||
45 | selector:@selector(onScreenUnlocked:) | ||||
46 | name:@"com.apple.screenIsUnlocked" | ||||
47 | object:nil]; | ||||
48 | } | ||||
49 | return self; | ||||
50 | } | ||||
51 | |||||
52 | - (void)dealloc { | ||||
[email protected] | 4c120af | 2012-11-17 03:27:34 | [diff] [blame] | 53 | [[NSDistributedNotificationCenter defaultCenter] removeObserver:self]; |
[email protected] | 3c64537 | 2011-01-25 20:54:06 | [diff] [blame] | 54 | [super dealloc]; |
55 | } | ||||
56 | |||||
57 | - (void)onScreenSaverStarted:(NSNotification*)notification { | ||||
58 | screensaverRunning_ = YES; | ||||
59 | } | ||||
60 | |||||
61 | - (void)onScreenSaverStopped:(NSNotification*)notification { | ||||
62 | screensaverRunning_ = NO; | ||||
63 | } | ||||
64 | |||||
65 | - (void)onScreenLocked:(NSNotification*)notification { | ||||
66 | screenLocked_ = YES; | ||||
67 | } | ||||
68 | |||||
69 | - (void)onScreenUnlocked:(NSNotification*)notification { | ||||
70 | screenLocked_ = NO; | ||||
71 | } | ||||
72 | |||||
73 | @end | ||||
74 | |||||
75 | static MacScreenMonitor* g_screenMonitor = nil; | ||||
76 | |||||
77 | void InitIdleMonitor() { | ||||
78 | if (!g_screenMonitor) | ||||
79 | g_screenMonitor = [[MacScreenMonitor alloc] init]; | ||||
80 | } | ||||
81 | |||||
[email protected] | 4c120af | 2012-11-17 03:27:34 | [diff] [blame] | 82 | void CalculateIdleTime(IdleTimeCallback notify) { |
[email protected] | 3c64537 | 2011-01-25 20:54:06 | [diff] [blame] | 83 | CFTimeInterval idle_time = CGEventSourceSecondsSinceLastEventType( |
84 | kCGEventSourceStateCombinedSessionState, | ||||
85 | kCGAnyInputEventType); | ||||
[email protected] | 4c120af | 2012-11-17 03:27:34 | [diff] [blame] | 86 | notify.Run(static_cast<int>(idle_time)); |
[email protected] | 3c64537 | 2011-01-25 20:54:06 | [diff] [blame] | 87 | } |
[email protected] | 80722b2 | 2011-09-10 07:54:50 | [diff] [blame] | 88 | |
89 | bool CheckIdleStateIsLocked() { | ||||
90 | return [g_screenMonitor isScreensaverRunning] || | ||||
91 | [g_screenMonitor isScreenLocked]; | ||||
92 | } |