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

http://macfuse.googlecode.com/ · Objective C · 157 lines · 76 code · 50 blank · 31 comment · 5 complexity · 15a6fd77fd403b4d8378b9531e50976b 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 "ERRunUpdateTicketCommand.h"
  15. #import "GTMLogger.h"
  16. #import "KSUpdateEngine.h"
  17. @implementation ERRunUpdateTicketCommand
  18. - (NSString *)name {
  19. return @"runticket";
  20. } // name
  21. - (NSString *)blurb {
  22. return @"Update all of the products in a ticket store";
  23. } // blurb
  24. - (NSDictionary *)requiredArguments {
  25. return [NSDictionary dictionaryWithObjectsAndKeys:
  26. @"Path to the ticket store", @"store",
  27. nil];
  28. } // requiredArguments
  29. - (NSDictionary *)optionalArguments {
  30. return [NSDictionary dictionaryWithObjectsAndKeys:
  31. @"Only update this product ID", @"productid",
  32. nil];
  33. } // optionalArguments
  34. - (BOOL)runWithArguments:(NSDictionary *)args {
  35. success_ = YES; // Innocent until proven guilty
  36. // Pick up the ticket store.
  37. NSString *storePath = [args objectForKey:@"store"];
  38. KSTicketStore *ticketStore = [KSTicketStore ticketStoreWithPath:storePath];
  39. // Make the engine to run things.
  40. KSUpdateEngine *vroom = [KSUpdateEngine engineWithTicketStore:ticketStore
  41. delegate:self];
  42. // Optional argument, if we want to do the update check for just one product
  43. // rather than everything in the store.
  44. NSString *productID = [args objectForKey:@"productid"];
  45. if (productID == nil) {
  46. [vroom updateAllProducts];
  47. } else {
  48. [vroom updateProductWithProductID:productID];
  49. }
  50. // Let Update Engine do its thing.
  51. while ([vroom isUpdating]) {
  52. NSDate *spin = [NSDate dateWithTimeIntervalSinceNow:1];
  53. [[NSRunLoop currentRunLoop] runUntilDate:spin];
  54. }
  55. // |success_| has an accumulated YES/NO value as each update completes.
  56. return success_;
  57. } // runWithArguments
  58. @end // ERRunUpdateTicketCommand
  59. @implementation ERRunUpdateTicketCommand (UpdateEngineDelegateMethods)
  60. // We've actually implemented a lot more of these than we need, but
  61. // it's fun to see them in action. Feel free to set breakpoints on all
  62. // of these and poke around the data that flows through them.
  63. - (void)engineStarted:(KSUpdateEngine *)engine {
  64. GTMLoggerInfo(@"starting engine");
  65. } // engineStarted
  66. - (NSArray *)engine:(KSUpdateEngine *)engine
  67. shouldPrefetchProducts:(NSArray *)products {
  68. NSArray *prefetch = products;
  69. // Go ahead and download all of the products before updating them.
  70. return prefetch;
  71. } // shouldPrefetchProducts
  72. - (NSArray *)engine:(KSUpdateEngine *)engine
  73. shouldSilentlyUpdateProducts:(NSArray *)products {
  74. // Since we're planning on being run as a command-line program from
  75. // launchd or by an administrator, no need to wait and ask for the
  76. // user's permission.
  77. return products;
  78. } // shouldSilentlyUpdateProducts
  79. - (id<KSCommandRunner>)commandRunnerForEngine:(KSUpdateEngine *)engine {
  80. return [KSTaskCommandRunner commandRunner];
  81. } // commandRunnerForEngine
  82. - (void)engine:(KSUpdateEngine *)engine
  83. starting:(KSUpdateInfo *)updateInfo {
  84. GTMLoggerInfo(@"starting update of %@", [updateInfo productID]);
  85. } // starting
  86. - (NSArray *)engine:(KSUpdateEngine *)engine
  87. shouldUpdateProducts:(NSArray *)products {
  88. // If we had a user interface, this is where we'd ask the user
  89. // about updating products, and filter out all the things that the
  90. // user rejected.
  91. return products;
  92. } // shouldUpdateProducts
  93. - (void)engine:(KSUpdateEngine *)engine
  94. finished:(KSUpdateInfo *)updateInfo
  95. wasSuccess:(BOOL)wasSuccess
  96. wantsReboot:(BOOL)wantsReboot {
  97. fprintf(stdout, "finished update of %s: %s\n",
  98. [[updateInfo productID] UTF8String],
  99. (wasSuccess) ? "Success" : "Failure");
  100. // Once we get a single |wasSuccess| == NO, |success_| will be NO
  101. // from here on out.
  102. success_ = success_ && wasSuccess;
  103. } // finished
  104. - (void)engineFinished:(KSUpdateEngine *)engine wasSuccess:(BOOL)wasSuccess {
  105. GTMLoggerInfo(@"engine finished and is shutting down");
  106. } // engineFinished
  107. @end // UpdateEngineDelegateMethods