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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
// Copyright (c) 2009 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.
#import "chrome/app/keystone_glue.h"
#include "base/logging.h"
#include "base/mac_util.h"
#import "base/worker_pool_mac.h"
#include "chrome/common/chrome_constants.h"
namespace {
// Provide declarations of the Keystone registration bits needed here. From
// KSRegistration.h.
typedef enum { kKSPathExistenceChecker } KSExistenceCheckerType;
NSString *KSRegistrationCheckForUpdateNotification =
@"KSRegistrationCheckForUpdateNotification";
NSString *KSRegistrationStatusKey = @"Status";
NSString *KSRegistrationVersionKey = @"Version";
NSString *KSRegistrationUpdateCheckErrorKey = @"Error";
NSString *KSRegistrationStartUpdateNotification =
@"KSRegistrationStartUpdateNotification";
NSString *KSUpdateCheckSuccessfulKey = @"CheckSuccessful";
NSString *KSUpdateCheckSuccessfullyInstalledKey = @"SuccessfullyInstalled";
NSString *KSRegistrationRemoveExistingTag = @"";
#define KSRegistrationPreserveExistingTag nil
} // namespace
@interface KSRegistration : NSObject
+ (id)registrationWithProductID:(NSString*)productID;
- (BOOL)registerWithVersion:(NSString*)version
existenceCheckerType:(KSExistenceCheckerType)xctype
existenceCheckerString:(NSString*)xc
serverURLString:(NSString*)serverURLString
preserveTTToken:(BOOL)preserveToken
tag:(NSString*)tag;
- (void)setActive;
- (void)checkForUpdate;
- (void)startUpdate;
@end // @interface KSRegistration
@interface KeystoneGlue(Private)
// Called periodically to announce activity by pinging the Keystone server.
- (void)markActive:(NSTimer*)timer;
// Called when an update check or update installation is complete. Posts the
// kAutoupdateStatusNotification notification to the default notification
// center.
- (void)updateStatus:(AutoupdateStatus)status version:(NSString*)version;
// These three methods are used to determine the version of the application
// currently installed on disk, compare that to the currently-running version,
// decide whether any updates have been installed, and call
// -updateStatus:version:.
//
// In order to check the version on disk, the installed application's
// Info.plist dictionary must be read; in order to see changes as updates are
// applied, the dictionary must be read each time, bypassing any caches such
// as the one that NSBundle might be maintaining. Reading files can be a
// blocking operation, and blocking operations are to be avoided on the main
// thread. I'm not quite sure what jank means, but I bet that a blocked main
// thread would cause some of it.
//
// -determineUpdateStatusAsync is called on the main thread to initiate the
// operation. It performs initial set-up work that must be done on the main
// thread and arranges for -determineUpdateStatusAtPath: to be called on a
// work queue thread managed by NSOperationQueue.
// -determineUpdateStatusAtPath: then reads the Info.plist, gets the version
// from the CFBundleShortVersionString key, and performs
// -determineUpdateStatusForVersion: on the main thread.
// -determineUpdateStatusForVersion: does the actual comparison of the version
// on disk with the running version and calls -updateStatus:version: with the
// results of its analysis.
- (void)determineUpdateStatusAsync;
- (void)determineUpdateStatusAtPath:(NSString*)appPath;
- (void)determineUpdateStatusForVersion:(NSString*)version;
@end // @interface KeystoneGlue(Private)
const NSString* const kAutoupdateStatusNotification =
@"AutoupdateStatusNotification";
const NSString* const kAutoupdateStatusStatus = @"status";
const NSString* const kAutoupdateStatusVersion = @"version";
@implementation KeystoneGlue
// TODO(jrg): use base::SingletonObjC<KeystoneGlue>
static KeystoneGlue* sDefaultKeystoneGlue = nil; // leaked
+ (id)defaultKeystoneGlue {
if (!sDefaultKeystoneGlue) {
sDefaultKeystoneGlue = [[KeystoneGlue alloc] init];
[sDefaultKeystoneGlue loadParameters];
if (![sDefaultKeystoneGlue loadKeystoneRegistration]) {
[sDefaultKeystoneGlue release];
sDefaultKeystoneGlue = nil;
}
}
return sDefaultKeystoneGlue;
}
- (id)init {
if ((self = [super init])) {
NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
[center addObserver:self
selector:@selector(checkForUpdateComplete:)
name:KSRegistrationCheckForUpdateNotification
object:nil];
[center addObserver:self
selector:@selector(installUpdateComplete:)
name:KSRegistrationStartUpdateNotification
object:nil];
}
return self;
}
- (void)dealloc {
[url_ release];
[productID_ release];
[version_ release];
[channel_ release];
[registration_ release];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
- (NSDictionary*)infoDictionary {
// Use mac_util::MainAppBundle() to get the app framework's dictionary.
return [mac_util::MainAppBundle() infoDictionary];
}
- (void)loadParameters {
NSDictionary* infoDictionary = [self infoDictionary];
NSString* url = [infoDictionary objectForKey:@"KSUpdateURL"];
NSString* product = [infoDictionary objectForKey:@"KSProductID"];
if (product == nil) {
// Use [NSBundle mainBundle] to fall back to the app's own bundle
// identifier, not the app framework's.
product = [[NSBundle mainBundle] bundleIdentifier];
}
NSString* version = [infoDictionary objectForKey:@"KSVersion"];
if (!product || !url || !version) {
// If parameters required for Keystone are missing, don't use it.
return;
}
NSString* channel = [infoDictionary objectForKey:@"KSChannelID"];
// The stable channel has no tag. If updating to stable, remove the
// dev and beta tags since we've been "promoted".
if (channel == nil)
channel = KSRegistrationRemoveExistingTag;
url_ = [url retain];
productID_ = [product retain];
version_ = [version retain];
channel_ = [channel retain];
}
- (BOOL)loadKeystoneRegistration {
if (!productID_ || !url_ || !version_)
return NO;
// Load the KeystoneRegistration framework bundle if present. It lives
// inside the framework, so use mac_util::MainAppBundle();
NSString* ksrPath =
[[mac_util::MainAppBundle() privateFrameworksPath]
stringByAppendingPathComponent:@"KeystoneRegistration.framework"];
NSBundle* ksrBundle = [NSBundle bundleWithPath:ksrPath];
[ksrBundle load];
// Harness the KSRegistration class.
Class ksrClass = [ksrBundle classNamed:@"KSRegistration"];
KSRegistration* ksr = [ksrClass registrationWithProductID:productID_];
if (!ksr)
return NO;
registration_ = [ksr retain];
return YES;
}
- (void)registerWithKeystone {
// The existence checks should use the path to the app bundle, not the
// app framework bundle, so use [NSBundle mainBundle] instead of
// mac_util::MainBundle().
[registration_ registerWithVersion:version_
existenceCheckerType:kKSPathExistenceChecker
existenceCheckerString:[[NSBundle mainBundle] bundlePath]
serverURLString:url_
preserveTTToken:YES
tag:channel_];
// Mark an active RIGHT NOW; don't wait an hour for the first one.
[registration_ setActive];
// Set up hourly activity pings.
timer_ = [NSTimer scheduledTimerWithTimeInterval:60 * 60 // One hour
target:self
selector:@selector(markActive:)
userInfo:registration_
repeats:YES];
}
- (void)stopTimer {
[timer_ invalidate];
}
- (void)markActive:(NSTimer*)timer {
KSRegistration* ksr = [timer userInfo];
[ksr setActive];
}
- (void)checkForUpdate {
DCHECK(![self asyncOperationPending]);
if (!registration_) {
[self updateStatus:kAutoupdateCheckFailed version:nil];
return;
}
[self updateStatus:kAutoupdateChecking version:nil];
[registration_ checkForUpdate];
// Upon completion, KSRegistrationCheckForUpdateNotification will be posted,
// and -checkForUpdateComplete: will be called.
}
- (void)checkForUpdateComplete:(NSNotification*)notification {
NSDictionary* userInfo = [notification userInfo];
if ([[userInfo objectForKey:KSRegistrationUpdateCheckErrorKey] boolValue]) {
[self updateStatus:kAutoupdateCheckFailed version:nil];
} else if ([[userInfo objectForKey:KSRegistrationStatusKey] boolValue]) {
// If an update is known to be available, go straight to
// -updateStatus:version:. It doesn't matter what's currently on disk.
NSString* version = [userInfo objectForKey:KSRegistrationVersionKey];
[self updateStatus:kAutoupdateAvailable version:version];
} else {
// If no updates are available, check what's on disk, because an update
// may have already been installed. This check happens on another thread,
// and -updateStatus:version: will be called on the main thread when done.
[self determineUpdateStatusAsync];
}
}
- (void)installUpdate {
DCHECK(![self asyncOperationPending]);
if (!registration_) {
[self updateStatus:kAutoupdateInstallFailed version:nil];
return;
}
[self updateStatus:kAutoupdateInstalling version:nil];
[registration_ startUpdate];
// Upon completion, KSRegistrationStartUpdateNotification will be posted,
// and -installUpdateComplete: will be called.
}
- (void)installUpdateComplete:(NSNotification*)notification {
NSDictionary* userInfo = [notification userInfo];
if (![[userInfo objectForKey:KSUpdateCheckSuccessfulKey] boolValue] ||
![[userInfo objectForKey:KSUpdateCheckSuccessfullyInstalledKey]
intValue]) {
[self updateStatus:kAutoupdateInstallFailed version:nil];
} else {
updateSuccessfullyInstalled_ = YES;
// Nothing in the notification dictionary reports the version that was
// installed. Figure it out based on what's on disk.
[self determineUpdateStatusAsync];
}
}
// Runs on the main thread.
- (void)determineUpdateStatusAsync {
// NSBundle is not documented as being thread-safe. Do NSBundle operations
// on the main thread before jumping over to a NSOperationQueue-managed
// thread to do blocking file input.
DCHECK([NSThread isMainThread]);
SEL selector = @selector(determineUpdateStatusAtPath:);
NSString* appPath = [[NSBundle mainBundle] bundlePath];
NSInvocationOperation* operation =
[[[NSInvocationOperation alloc] initWithTarget:self
selector:selector
object:appPath] autorelease];
NSOperationQueue* operationQueue = [WorkerPoolObjC sharedOperationQueue];
[operationQueue addOperation:operation];
}
// Runs on a thread managed by NSOperationQueue.
- (void)determineUpdateStatusAtPath:(NSString*)appPath {
DCHECK(![NSThread isMainThread]);
NSString* appInfoPlistPath =
[[appPath stringByAppendingPathComponent:@"Contents"]
stringByAppendingPathComponent:@"Info.plist"];
NSDictionary* infoPlist =
[NSDictionary dictionaryWithContentsOfFile:appInfoPlistPath];
NSString* version = [infoPlist objectForKey:@"CFBundleShortVersionString"];
[self performSelectorOnMainThread:@selector(determineUpdateStatusForVersion:)
withObject:version
waitUntilDone:NO];
}
// Runs on the main thread.
- (void)determineUpdateStatusForVersion:(NSString*)version {
DCHECK([NSThread isMainThread]);
AutoupdateStatus status;
if (updateSuccessfullyInstalled_) {
// If an update was successfully installed and this object saw it happen,
// then don't even bother comparing versions.
status = kAutoupdateInstalled;
} else {
NSString* currentVersion =
[NSString stringWithUTF8String:chrome::kChromeVersion];
if (!version) {
// If the version on disk could not be determined, assume that
// whatever's running is current.
version = currentVersion;
status = kAutoupdateCurrent;
} else if ([version isEqualToString:currentVersion]) {
status = kAutoupdateCurrent;
} else {
// If the version on disk doesn't match what's currently running, an
// update must have been applied in the background, without this app's
// direct participation. Leave updateSuccessfullyInstalled_ alone
// because there's no direct knowledge of what actually happened.
status = kAutoupdateInstalled;
}
}
[self updateStatus:status version:version];
}
- (void)updateStatus:(AutoupdateStatus)status version:(NSString*)version {
NSNumber* statusNumber = [NSNumber numberWithInt:status];
NSMutableDictionary* dictionary =
[NSMutableDictionary dictionaryWithObject:statusNumber
forKey:kAutoupdateStatusStatus];
if (version) {
[dictionary setObject:version forKey:kAutoupdateStatusVersion];
}
NSNotification* notification =
[NSNotification notificationWithName:kAutoupdateStatusNotification
object:self
userInfo:dictionary];
recentNotification_.reset([notification retain]);
[[NSNotificationCenter defaultCenter] postNotification:notification];
}
- (NSNotification*)recentNotification {
return [[recentNotification_ retain] autorelease];
}
- (AutoupdateStatus)recentStatus {
NSDictionary* dictionary = [recentNotification_ userInfo];
return static_cast<AutoupdateStatus>(
[[dictionary objectForKey:kAutoupdateStatusStatus] intValue]);
}
- (BOOL)asyncOperationPending {
AutoupdateStatus status = [self recentStatus];
return status == kAutoupdateChecking || status == kAutoupdateInstalling;
}
@end // @implementation KeystoneGlue
|