/core/externals/update-engine/Core/KSDownloadAction.h

http://macfuse.googlecode.com/ · C Header · 143 lines · 31 code · 12 blank · 100 comment · 0 complexity · 3c32fd18a1522ebc97bdb06da57d0863 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 <Foundation/Foundation.h>
  15. #import "KSAction.h"
  16. @class KSActionProcessor;
  17. // KSDownloadAction
  18. //
  19. // An action that downloads a URL to a file on disk, and ensures that the
  20. // downloaded file matches a known SHA-1 hash value and file size. See the
  21. // KSAction.h for more general comments about actions in general. A
  22. // KSDownloadAction is created with a URL to be downloaded, a Base64 encoded
  23. // SHA-1 hash value that should match the downloaded data (this should be
  24. // obtained from a trusted source) and the file size, and a path where the
  25. // downloaded file should be saved.
  26. //
  27. // KSDownloadAction never removes files that it downloads. If cleanup is
  28. // necessary, that is the responsibility of the caller. KSDownloadAction uses
  29. // the fact that a download may already exist at a specified path to short
  30. // circuit downloads if the download already exists. Before starting a download,
  31. // KSDownloadAction first checks to see if a file alread exists at the
  32. // destination path, and if one exists, it checks if that file's hash value
  33. // matches the hash value that the KSDownloadAction was created with. If they
  34. // match, then the desired file has already been downloaded and the download can
  35. // be skipped. If they file does not exist, or the hash values do not match,
  36. // then a regular download will be done and the file on disk will be overwritten
  37. // if it already existed.
  38. //
  39. // Input-Output
  40. //
  41. // KSDownloadAction does not use its inPipe for anything. However, when a
  42. // download finishes successfully, the path (NSString *) to the downloaded file
  43. // is stored in its outPipe. It's up to you whether or not you care about this
  44. // path.
  45. //
  46. // To use this action you will need to create an instance of KSDownloadAction,
  47. // add it to a KSActionProcessor, and tell the action processor to start
  48. // processing. Once the processing is finished, the absolute file path to the
  49. // downloaded data will be available as an NSString in the downloads |outPipe|
  50. // If the download failed, the outPipe's contents will be nil. Here is an
  51. // example that downloads the Google logo from the Google homepage.
  52. //
  53. // NSURL *url = [NSURL URLWithString:
  54. // @"http://www.google.com/intl/en_ALL/images/logo.gif"];
  55. //
  56. // NSString *hash = ... Base64 encoded SHA-1 hash of data from |url| ...
  57. // unsigned long long size = ... the size of the data from |url| ...
  58. //
  59. // KSDownloadAction *download = [KSDownloadAction actionWithURL:url
  60. // size:size
  61. // hash:hash
  62. // name:@"foo.gif"];
  63. //
  64. // KSActionProcessor *ap = [[[KSActionProcessor alloc] init] autorelease];
  65. // [ap enqueueAction:download];
  66. //
  67. // [ap startProcessing]; // This runs starts the download action
  68. //
  69. // ... spin runloop to let the download complete ...
  70. //
  71. // NSString *path = nil;
  72. // if (![download isRunning]) // Make sure the download is done
  73. // path = [[download outPipe] contents];
  74. //
  75. // ... |path| is the path to the downloaded file that matches |hash| ...
  76. @interface KSDownloadAction : KSAction {
  77. @private
  78. NSURL *url_;
  79. unsigned long long size_;
  80. NSString *hash_;
  81. NSString *path_; // See comments at the top of the .m for details about
  82. NSString *tempPath_; // the difference between path_ and tempPath_.
  83. NSTask *downloadTask_;
  84. NSString *ksurlPath_;
  85. }
  86. // Returns the absolute path to the default directory to be used for downloads.
  87. // This path is used when instances are created with the
  88. // +actionWithURL:hash:name: convenience method.
  89. //
  90. // Note that we don't use Library/Caches/Google because historically this
  91. // directory has been created with a mode of 0777 (note no sticky bit) and this
  92. // is too insecure for us. So instead we'll simply use a directory in
  93. // Library/Caches/com.google.UpdateEngine.Framework.<uid>/Downloads, which has a
  94. // mode that we're happy with. This also seems to be the standard way that Apple
  95. // apps use Library/Caches.
  96. + (NSString *)defaultDownloadDirectory;
  97. // Returns an autoreleased KSDownloadAction for the specified url, hash, and
  98. // name. The destination path where the downloaded file will be saved is
  99. // constructed by appending "name" to the path obtained from
  100. // +defaultDownloadDirectory.
  101. + (id)actionWithURL:(NSURL *)url
  102. size:(unsigned long long)size
  103. hash:(NSString *)hash
  104. name:(NSString *)name;
  105. // Returns an autoreleased KSDownloadAction for the specified url, hash, and
  106. // size, that will be saved to |path|.
  107. + (id)actionWithURL:(NSURL *)url
  108. size:(unsigned long long)size
  109. hash:(NSString *)hash
  110. path:(NSString *)path;
  111. // Designated initializer. Returns a KSDownloadAction that will download the
  112. // data at |url| and save it in a file named |path|. This action will also
  113. // verify that the downloaded file's SHA-1 hash is equal to |hash|. All
  114. // parameters are required and may not be nil.
  115. - (id)initWithURL:(NSURL *)url
  116. size:(unsigned long long)size
  117. hash:(NSString *)hash
  118. path:(NSString *)path;
  119. // Returns the URL to be downloaded. The base64 encoded SHA-1 hash value of the
  120. // data from this URL should match the value of -hash.
  121. - (NSURL *)url;
  122. // Returns the size that the data downloaded from the URL should be.
  123. - (unsigned long long)size;
  124. // Returns the hash value that downloaded data should have. This returns the
  125. // hash value that this instance was created with. It is a dumb "getter". It
  126. // does not actually calculate the hash value.
  127. - (NSString *)hash;
  128. // The path where the downloaded file should be placed once it has been
  129. // succesfully downloaded.
  130. - (NSString *)path;
  131. @end