/core/externals/update-engine/Samples/EngineRunner/ERRunUpdateCommand.m
Objective C | 170 lines | 88 code | 51 blank | 31 comment | 4 complexity | de5c2f897289cf3148c3b234df5ba108 MD5 | raw file
1// Copyright 2008 Google Inc. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15#import "ERRunUpdateCommand.h" 16 17#import "GTMLogger.h" 18#import "KSUpdateEngine.h" 19 20 21@implementation ERRunUpdateCommand 22 23- (NSString *)name { 24 return @"run"; 25} // name 26 27 28- (NSString *)blurb { 29 return @"Update a single product"; 30} // blurb 31 32 33- (NSDictionary *)requiredArguments { 34 return [NSDictionary dictionaryWithObjectsAndKeys: 35 @"Product ID for the product to update", @"productid", 36 @"Current version of the product", @"version", 37 @"Server URL", @"url", 38 nil]; 39 40} // requiredArguments 41 42 43- (NSDictionary *)optionalArguments { 44 return [NSDictionary dictionaryWithObjectsAndKeys: 45 @"Existence checker path", @"xcpath", 46 nil]; 47 48} // optionalArguments 49 50 51- (BOOL)runWithArguments:(NSDictionary *)args { 52 success_ = YES; // Innocent until proven guilty 53 54 // Make a new ticket. 55 NSString *productID = [args objectForKey:@"productid"]; 56 NSString *version = [args objectForKey:@"version"]; 57 NSString *urlString = [args objectForKey:@"url"]; 58 NSURL *serverURL = [NSURL URLWithString:urlString]; 59 60 // The existence checker is optional, so use a trueChecker if one isn't 61 // supplied. 62 NSString *xcpath = [args objectForKey:@"xcpath"]; 63 KSExistenceChecker *existenceChecker; 64 if (xcpath == nil) { 65 existenceChecker = [KSExistenceChecker trueChecker]; 66 } else { 67 existenceChecker = [KSPathExistenceChecker checkerWithPath:xcpath]; 68 } 69 70 KSTicket *ticket = [KSTicket ticketWithProductID:productID 71 version:version 72 existenceChecker:existenceChecker 73 serverURL:serverURL]; 74 75 // And stick it into an in-memory ticket store. 76 KSTicketStore *ticketStore = [[[KSMemoryTicketStore alloc] init] autorelease]; 77 [ticketStore storeTicket:ticket]; 78 79 // Make the engine to run things. 80 KSUpdateEngine *vroom = [KSUpdateEngine engineWithTicketStore:ticketStore 81 delegate:self]; 82 83 // Start the update running. 84 [vroom updateProductWithProductID:productID]; 85 86 // Let Update Engine do its thing. 87 while ([vroom isUpdating]) { 88 NSDate *spin = [NSDate dateWithTimeIntervalSinceNow:1]; 89 [[NSRunLoop currentRunLoop] runUntilDate:spin]; 90 } 91 92 // |success_| has an accumulated YES/NO value as each update completes. 93 return success_; 94 95} // runWithArguments 96 97@end // ERRunUpdateCommand 98 99 100@implementation ERRunUpdateCommand (UpdateEngineDelegateMethods) 101 102// We've actually implemented a lot more of these than we need, but 103// it's fun to see them in action. Feel free to set breakpoints on all 104// of these and poke around the data that flows through them. 105 106- (void)engineStarted:(KSUpdateEngine *)engine { 107 GTMLoggerInfo(@"starting engine"); 108} // engineStarted 109 110 111- (NSArray *)engine:(KSUpdateEngine *)engine 112 shouldPrefetchProducts:(NSArray *)products { 113 NSArray *prefetch = products; 114 115 // Go ahead and download all of the products before updating them. 116 return prefetch; 117 118} // shouldPrefetchProducts 119 120 121- (NSArray *)engine:(KSUpdateEngine *)engine 122 shouldSilentlyUpdateProducts:(NSArray *)products { 123 124 // Since we're planning on being run as a command-line program from 125 // launchd or by an administrator, no need to wait and ask for the 126 // user's permission. 127 return products; 128 129} // shouldSilentlyUpdateProducts 130 131 132- (id<KSCommandRunner>)commandRunnerForEngine:(KSUpdateEngine *)engine { 133 return [KSTaskCommandRunner commandRunner]; 134} // commandRunnerForEngine 135 136 137- (void)engine:(KSUpdateEngine *)engine 138 starting:(KSUpdateInfo *)updateInfo { 139 GTMLoggerInfo(@"starting update of %@", [updateInfo productID]); 140} // starting 141 142 143- (NSArray *)engine:(KSUpdateEngine *)engine 144 shouldUpdateProducts:(NSArray *)products { 145 // If we had a user interface, this is where we'd ask the user 146 // about updating products, and filter out all the things that the 147 // user rejected. 148 149 return products; 150 151} // shouldUpdateProducts 152 153 154- (void)engine:(KSUpdateEngine *)engine 155 finished:(KSUpdateInfo *)updateInfo 156 wasSuccess:(BOOL)wasSuccess 157 wantsReboot:(BOOL)wantsReboot { 158 159 fprintf(stdout, "finished update of %s: %s\n", 160 [[updateInfo productID] UTF8String], 161 (wasSuccess) ? "Success" : "Failure"); 162 success_ = wasSuccess; 163} // finished 164 165 166- (void)engineFinished:(KSUpdateEngine *)engine wasSuccess:(BOOL)wasSuccess { 167 GTMLoggerInfo(@"engine finished and is shutting down"); 168} // engineFinished 169 170@end // UpdateEngineDelegateMethods