keystone_glue.h revision c407dc5cd9bdc5668497f21b26b09d988ab439de
1// Copyright (c) 2009 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#ifndef CHROME_BROWSER_COCOA_KEYSTONE_GLUE_H_
6#define CHROME_BROWSER_COCOA_KEYSTONE_GLUE_H_
7
8#include "base/string16.h"
9
10#if defined(__OBJC__)
11
12#import <Foundation/Foundation.h>
13
14#import "base/scoped_nsobject.h"
15#include "chrome/browser/cocoa/scoped_authorizationref.h"
16
17// Possible outcomes of various operations.  A version may accompany some of
18// these, but beware: a version is never required.  For statuses that can be
19// accompanied by a version, the comment indicates what version is referenced.
20// A notification posted containing an asynchronous status will always be
21// followed by a notification with a terminal status.
22enum AutoupdateStatus {
23  kAutoupdateNone = 0,        // no version (initial state only)
24  kAutoupdateRegistering,     // no version (asynchronous operation in progress)
25  kAutoupdateRegistered,      // no version
26  kAutoupdateChecking,        // no version (asynchronous operation in progress)
27  kAutoupdateCurrent,         // version of the running application
28  kAutoupdateAvailable,       // version of the update that is available
29  kAutoupdateInstalling,      // no version (asynchronous operation in progress)
30  kAutoupdateInstalled,       // version of the update that was installed
31  kAutoupdatePromoting,       // no version (asynchronous operation in progress)
32  kAutoupdatePromoted,        // no version
33  kAutoupdateRegisterFailed,  // no version
34  kAutoupdateCheckFailed,     // no version
35  kAutoupdateInstallFailed,   // no version
36  kAutoupdatePromoteFailed,   // no version
37};
38
39// kAutoupdateStatusNotification is the name of the notification posted when
40// -checkForUpdate and -installUpdate complete.  This notification will be
41// sent with with its sender object set to the KeystoneGlue instance sending
42// the notification.  Its userInfo dictionary will contain an AutoupdateStatus
43// value as an intValue at key kAutoupdateStatusStatus.  If a version is
44// available (see AutoupdateStatus), it will be present at key
45// kAutoupdateStatusVersion.
46extern NSString* const kAutoupdateStatusNotification;
47extern NSString* const kAutoupdateStatusStatus;
48extern NSString* const kAutoupdateStatusVersion;
49
50namespace {
51
52enum BrandFileType {
53  kBrandFileTypeNotDetermined = 0,
54  kBrandFileTypeNone,
55  kBrandFileTypeUser,
56  kBrandFileTypeSystem,
57};
58
59} // namespace
60
61// KeystoneGlue is an adapter around the KSRegistration class, allowing it to
62// be used without linking directly against its containing KeystoneRegistration
63// framework.  This is used in an environment where most builds (such as
64// developer builds) don't want or need Keystone support and might not even
65// have the framework available.  Enabling Keystone support in an application
66// that uses KeystoneGlue is as simple as dropping
67// KeystoneRegistration.framework in the application's Frameworks directory
68// and providing the relevant information in its Info.plist.  KeystoneGlue
69// requires that the KSUpdateURL key be set in the application's Info.plist,
70// and that it contain a string identifying the update URL to be used by
71// Keystone.
72
73@class KSRegistration;
74
75@interface KeystoneGlue : NSObject {
76 @protected
77
78  // Data for Keystone registration
79  NSString* productID_;
80  NSString* appPath_;
81  NSString* url_;
82  NSString* version_;
83  NSString* channel_;  // Logically: Dev, Beta, or Stable.
84  BrandFileType brandFileType_;
85
86  // And the Keystone registration itself, with the active timer
87  KSRegistration* registration_;  // strong
88  NSTimer* timer_;  // strong
89
90  // The most recent kAutoupdateStatusNotification notification posted.
91  scoped_nsobject<NSNotification> recentNotification_;
92
93  // The authorization object, when it needs to persist because it's being
94  // carried across threads.
95  scoped_AuthorizationRef authorization_;
96
97  // YES if a synchronous promotion operation is in progress (promotion during
98  // installation).
99  BOOL synchronousPromotion_;
100
101  // YES if an update was ever successfully installed by -installUpdate.
102  BOOL updateSuccessfullyInstalled_;
103}
104
105// Return the default Keystone Glue object.
106+ (id)defaultKeystoneGlue;
107
108// Load KeystoneRegistration.framework if present, call into it to register
109// with Keystone, and set up periodic activity pings.
110- (void)registerWithKeystone;
111
112// -checkForUpdate launches a check for updates, and -installUpdate begins
113// installing an available update.  For each, status will be communicated via
114// a kAutoupdateStatusNotification notification, and will also be available
115// through -recentNotification.
116- (void)checkForUpdate;
117- (void)installUpdate;
118
119// Accessor for recentNotification_.  Returns an autoreleased NSNotification.
120- (NSNotification*)recentNotification;
121
122// Accessor for the kAutoupdateStatusStatus field of recentNotification_'s
123// userInfo dictionary.
124- (AutoupdateStatus)recentStatus;
125
126// Returns YES if an asynchronous operation is pending: if an update check or
127// installation attempt is currently in progress.
128- (BOOL)asyncOperationPending;
129
130// Returns YES if the application is running from a read-only filesystem,
131// such as a disk image.
132- (BOOL)isOnReadOnlyFilesystem;
133
134// -needsPromotion is YES if the application needs its ticket promoted to
135// a system ticket.  This will be YES when the application is on a user
136// ticket and determines that the current user does not have sufficient
137// permission to perform the update.
138//
139// -wantsPromotion is YES if the application wants its ticket promoted to
140// a system ticket, even if it doesn't need it as determined by
141// -needsPromotion.  -wantsPromotion will always be YES if -needsPromotion is,
142// and it will additionally be YES when the application is on a user ticket
143// and appears to be installed in a system-wide location such as
144// /Applications.
145//
146// Use -needsPromotion to decide whether to show any update UI at all.  If
147// it's YES, there's no sense in asking the user to "update now" because it
148// will fail given the rights and permissions involved.  On the other hand,
149// when -needsPromotion is YES, the application can encourage the user to
150// promote the ticket so that updates will work properly.
151//
152// Use -wantsPromotion to decide whether to allow the user to promote.  The
153// user shouldn't be nagged about promotion on the basis of -wantsPromotion,
154// but if it's YES, the user should be allowed to promote the ticket.
155- (BOOL)needsPromotion;
156- (BOOL)wantsPromotion;
157
158// Promotes the Keystone ticket into the system store.  System Keystone will
159// be installed if necessary.  If synchronous is NO, the promotion may occur
160// in the background.  synchronous should be YES for promotion during
161// installation. The KeystoneGlue object assumes ownership of
162// authorization_arg.
163- (void)promoteTicketWithAuthorization:(AuthorizationRef)authorization_arg
164                           synchronous:(BOOL)synchronous;
165
166// Requests authorization and calls -promoteTicketWithAuthorization: in
167// asynchronous mode.
168- (void)promoteTicket;
169
170// Sets a new value for appPath.  Used during installation to point a ticket
171// at the installed copy.
172- (void)setAppPath:(NSString*)appPath;
173
174@end  // @interface KeystoneGlue
175
176@interface KeystoneGlue(ExposedForTesting)
177
178// Load any params we need for configuring Keystone.
179- (void)loadParameters;
180
181// Load the Keystone registration object.
182// Return NO on failure.
183- (BOOL)loadKeystoneRegistration;
184
185- (void)stopTimer;
186
187// Called when a checkForUpdate: notification completes.
188- (void)checkForUpdateComplete:(NSNotification*)notification;
189
190// Called when an installUpdate: notification completes.
191- (void)installUpdateComplete:(NSNotification*)notification;
192
193@end  // @interface KeystoneGlue(ExposedForTesting)
194
195#endif  // __OBJC__
196
197// Functions that may be accessed from non-Objective-C C/C++ code.
198namespace keystone_glue {
199
200// True if Keystone is enabled.
201bool KeystoneEnabled();
202
203// The version of the application currently installed on disk.
204string16 CurrentlyInstalledVersion();
205
206}  // namespace keystone_glue
207
208#endif  // CHROME_BROWSER_COCOA_KEYSTONE_GLUE_H_
209