forked from ttscoff/nv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSecureTextEntryManager.m
181 lines (138 loc) · 5.76 KB
/
SecureTextEntryManager.m
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//
// SecureTextEntryManager.m
// Notation
//
// Created by Zachary Schneirov on 1/5/11.
/*Copyright (c) 2010, Zachary Schneirov. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this list of conditions
and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of
conditions and the following disclaimer in the documentation and/or other materials provided with
the distribution.
- Neither the name of Notational Velocity nor the names of its contributors may be used to endorse
or promote products derived from this software without specific prior written permission. */
#import "SecureTextEntryManager.h"
#include <Carbon/Carbon.h>
NSString *ShouldHideSecureTextEntryWarningKey = @"ShouldHideSecureTextEntryWarning";
static SecureTextEntryManager *sharedInstance = nil;
@implementation SecureTextEntryManager
+ (SecureTextEntryManager*)sharedInstance {
//not synchronized because there should be no need for non-main threads to access this class
//also, NSThread access potentially enables a locking
if (sharedInstance == nil)
sharedInstance = [[SecureTextEntryManager alloc] init];
return sharedInstance;
}
+ (id)allocWithZone:(NSZone *)zone {
if (sharedInstance == nil) {
sharedInstance = [super allocWithZone:zone];
return sharedInstance; // assignment and return on first allocation
}
return nil; // on subsequent allocation attempts return nil
}
- (id)init {
if ((self = [super init])) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive:)
name:NSApplicationDidBecomeActiveNotification object:NSApp];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:)
name:NSApplicationWillResignActiveNotification object:NSApp];
}
return self;
}
- (void)applicationDidBecomeActive:(NSNotification *)aNotification {
if (secureTextEntry) {
[self _enableSecureEventInput];
}
}
- (void)applicationWillResignActive:(NSNotification *)aNotification {
if (secureTextEntry) {
[self _disableSecureEventInput];
}
}
//_enableSecureEventInput/_disableSecureEventInput are private; do not call them directly
- (void)_enableSecureEventInput {
if (!_calledSecureEventInput) {
NSAssert([NSApp isActive], @"not fair; app is currently inactive");
//could also assert -[NSThread isMainThread] here
_calledSecureEventInput = YES;
//NSLog(@"%s: enabled secure input", _cmd);
EnableSecureEventInput();
}
}
- (void)_disableSecureEventInput {
if (_calledSecureEventInput) {
DisableSecureEventInput();
//NSLog(@"%s: disabled secure input", _cmd);
_calledSecureEventInput = NO;
if (IsSecureEventInputEnabled())
NSLog(@"%@: WARNING: secure input is still enabled, possibly by another app", NSStringFromSelector(_cmd));
}
}
//these enable/disable methods refer to the behavior of calling EnableSecureEventInput/DisableSecureEventInput;
//rather than being wrappers for those calls themselves
- (void)disableSecureTextEntry {
if (secureTextEntry) {
[self _disableSecureEventInput];
secureTextEntry = NO;
}
}
- (void)enableSecureTextEntry {
if (!secureTextEntry) {
//should do -[checkForIncompatibleApps] here, but that would add about 0.056 seconds of latency to launch time
if ([NSApp isActive]) {
[self _enableSecureEventInput];
}
secureTextEntry = YES;
}
}
- (NSSet*)_bundleIdentifiersOfIncompatibleApps {
return [NSSet setWithObjects:@"com.smileonmymac.textexpander", @"com.macility.typinator2", @"com.typeit4me.TypeIt4MeMenu", @"uk.co.activata.Autopilot2", @"au.com.tech.AutoTyper", nil];
}
- (void)checkForIncompatibleApps {
if (!secureTextEntry || [[NSUserDefaults standardUserDefaults] boolForKey:ShouldHideSecureTextEntryWarningKey])
return;
NSSet *identifiers = [self _bundleIdentifiersOfIncompatibleApps];
ProcessSerialNumber PSN = { 0, kNoProcess };
//walk through processes using the carbon process manager, because this is what NSWorkspace's launchedApplications method does, anyway, and we get hidden processes as well
while (GetNextProcess(&PSN) == noErr) {
CFDictionaryRef infoDict = ProcessInformationCopyDictionary(&PSN, kProcessDictionaryIncludeAllInformationMask);
if (infoDict != NULL) {
CFTypeRef identifier = CFDictionaryGetValue(infoDict, kCFBundleIdentifierKey);
if ((identifier != NULL) && [identifiers containsObject:(id)identifier]) {
CFStringRef offendingAppName = CFDictionaryGetValue(infoDict, kCFBundleNameKey);
NSAlert *alert = [NSAlert alertWithMessageText:
[NSString stringWithFormat:NSLocalizedString(@"Secure Text Entry will prevent %@, which is currently installed on this computer, from working in Notational Velocity.",
@"for warning about incompatibility with TextExpander, Typinator, etc."), offendingAppName]
defaultButton:NSLocalizedString(@"OK", nil) alternateButton:nil otherButton:nil informativeTextWithFormat:@""];
if (IsLeopardOrLater) {
[alert setShowsSuppressionButton:YES];
}
[alert runModal];
if (IsLeopardOrLater && [[alert suppressionButton] state] == NSOnState) {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:ShouldHideSecureTextEntryWarningKey];
}
CFRelease(infoDict);
break;
}
CFRelease(infoDict);
}
}
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
- (id)retain {
return self;
}
- (NSUInteger)retainCount {
return UINT_MAX; // denotes an object that cannot be released
}
- (void)release {
//do nothing
}
- (id)autorelease {
return self;
}
@end