/core/externals/update-engine/Samples/EngineRunner/ERRunUpdateCommand.m

http://macfuse.googlecode.com/ · 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. #import "ERRunUpdateCommand.h"
  15. #import "GTMLogger.h"
  16. #import "KSUpdateEngine.h"
  17. @implementation ERRunUpdateCommand
  18. - (NSString *)name {
  19. return @"run";
  20. } // name
  21. - (NSString *)blurb {
  22. return @"Update a single product";
  23. } // blurb
  24. - (NSDictionary *)requiredArguments {
  25. return [NSDictionary dictionaryWithObjectsAndKeys:
  26. @"Product ID for the product to update", @"productid",
  27. @"Current version of the product", @"version",
  28. @"Server URL", @"url",
  29. nil];
  30. } // requiredArguments
  31. - (NSDictionary *)optionalArguments {
  32. return [NSDictionary dictionaryWithObjectsAndKeys:
  33. @"Existence checker path", @"xcpath",
  34. nil];
  35. } // optionalArguments
  36. - (BOOL)runWithArguments:(NSDictionary *)args {
  37. success_ = YES; // Innocent until proven guilty
  38. // Make a new ticket.
  39. NSString *productID = [args objectForKey:@"productid"];
  40. NSString *version = [args objectForKey:@"version"];
  41. NSString *urlString = [args objectForKey:@"url"];
  42. NSURL *serverURL = [NSURL URLWithString:urlString];
  43. // The existence checker is optional, so use a trueChecker if one isn't
  44. // supplied.
  45. NSString *xcpath = [args objectForKey:@"xcpath"];
  46. KSExistenceChecker *existenceChecker;
  47. if (xcpath == nil) {
  48. existenceChecker = [KSExistenceChecker trueChecker];
  49. } else {
  50. existenceChecker = [KSPathExistenceChecker checkerWithPath:xcpath];
  51. }
  52. KSTicket *ticket = [KSTicket ticketWithProductID:productID
  53. version:version
  54. existenceChecker:existenceChecker
  55. serverURL:serverURL];
  56. // And stick it into an in-memory ticket store.
  57. KSTicketStore *ticketStore = [[[KSMemoryTicketStore alloc] init] autorelease];
  58. [ticketStore storeTicket:ticket];
  59. // Make the engine to run things.
  60. KSUpdateEngine *vroom = [KSUpdateEngine engineWithTicketStore:ticketStore
  61. delegate:self];
  62. // Start the update running.
  63. [vroom updateProductWithProductID:productID];
  64. // Let Update Engine do its thing.
  65. while ([vroom isUpdating]) {
  66. NSDate *spin = [NSDate dateWithTimeIntervalSinceNow:1];
  67. [[NSRunLoop currentRunLoop] runUntilDate:spin];
  68. }
  69. // |success_| has an accumulated YES/NO value as each update completes.
  70. return success_;
  71. } // runWithArguments
  72. @end // ERRunUpdateCommand
  73. @implementation ERRunUpdateCommand (UpdateEngineDelegateMethods)
  74. // We've actually implemented a lot more of these than we need, but
  75. // it's fun to see them in action. Feel free to set breakpoints on all
  76. // of these and poke around the data that flows through them.
  77. - (void)engineStarted:(KSUpdateEngine *)engine {
  78. GTMLoggerInfo(@"starting engine");
  79. } // engineStarted
  80. - (NSArray *)engine:(KSUpdateEngine *)engine
  81. shouldPrefetchProducts:(NSArray *)products {
  82. NSArray *prefetch = products;
  83. // Go ahead and download all of the products before updating them.
  84. return prefetch;
  85. } // shouldPrefetchProducts
  86. - (NSArray *)engine:(KSUpdateEngine *)engine
  87. shouldSilentlyUpdateProducts:(NSArray *)products {
  88. // Since we're planning on being run as a command-line program from
  89. // launchd or by an administrator, no need to wait and ask for the
  90. // user's permission.
  91. return products;
  92. } // shouldSilentlyUpdateProducts
  93. - (id<KSCommandRunner>)commandRunnerForEngine:(KSUpdateEngine *)engine {
  94. return [KSTaskCommandRunner commandRunner];
  95. } // commandRunnerForEngine
  96. - (void)engine:(KSUpdateEngine *)engine
  97. starting:(KSUpdateInfo *)updateInfo {
  98. GTMLoggerInfo(@"starting update of %@", [updateInfo productID]);
  99. } // starting
  100. - (NSArray *)engine:(KSUpdateEngine *)engine
  101. shouldUpdateProducts:(NSArray *)products {
  102. // If we had a user interface, this is where we'd ask the user
  103. // about updating products, and filter out all the things that the
  104. // user rejected.
  105. return products;
  106. } // shouldUpdateProducts
  107. - (void)engine:(KSUpdateEngine *)engine
  108. finished:(KSUpdateInfo *)updateInfo
  109. wasSuccess:(BOOL)wasSuccess
  110. wantsReboot:(BOOL)wantsReboot {
  111. fprintf(stdout, "finished update of %s: %s\n",
  112. [[updateInfo productID] UTF8String],
  113. (wasSuccess) ? "Success" : "Failure");
  114. success_ = wasSuccess;
  115. } // finished
  116. - (void)engineFinished:(KSUpdateEngine *)engine wasSuccess:(BOOL)wasSuccess {
  117. GTMLoggerInfo(@"engine finished and is shutting down");
  118. } // engineFinished
  119. @end // UpdateEngineDelegateMethods