rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium OS 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 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 5 | #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_DOWNLOAD_ACTION_H__ |
| 6 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_DOWNLOAD_ACTION_H__ |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 7 | |
| 8 | #include <sys/types.h> |
| 9 | #include <sys/stat.h> |
| 10 | #include <fcntl.h> |
| 11 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 12 | #include <string> |
| 13 | |
Darin Petkov | e971f33 | 2010-09-22 16:57:25 -0700 | [diff] [blame] | 14 | #include <base/scoped_ptr.h> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 15 | #include <curl/curl.h> |
Andrew de los Reyes | 21816e1 | 2011-04-07 14:18:56 -0700 | [diff] [blame^] | 16 | #include <google/protobuf/stubs/common.h> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 17 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 18 | #include "update_engine/action.h" |
| 19 | #include "update_engine/decompressing_file_writer.h" |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 20 | #include "update_engine/delta_performer.h" |
Darin Petkov | e971f33 | 2010-09-22 16:57:25 -0700 | [diff] [blame] | 21 | #include "update_engine/buffered_file_writer.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 22 | #include "update_engine/http_fetcher.h" |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 23 | #include "update_engine/install_plan.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 24 | #include "update_engine/omaha_hash_calculator.h" |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 25 | #include "update_engine/split_file_writer.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 26 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 27 | // The Download Action downloads a specified url to disk. The url should |
| 28 | // point to either a full or delta update. If a full update, the file will |
| 29 | // be piped into a SplitFileWriter, which will direct it to the kernel |
| 30 | // and rootfs partitions. If it's a delta update, the destination kernel |
| 31 | // and rootfs should already contain the source-version that this delta |
| 32 | // update goes from. In this case, the update will be piped into a |
| 33 | // DeltaPerformer that will apply the delta to the disk. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 34 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 35 | namespace chromeos_update_engine { |
| 36 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 37 | class DownloadActionDelegate { |
| 38 | public: |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 39 | // Called right before starting the download with |active| set to |
| 40 | // true. Called after completing the download with |active| set to |
| 41 | // false. |
| 42 | virtual void SetDownloadStatus(bool active) = 0; |
| 43 | |
| 44 | // Called periodically after bytes are received. This method will be |
| 45 | // invoked only if the download is active. |bytes_received| is the |
| 46 | // number of bytes downloaded thus far. |total| is the number of |
| 47 | // bytes expected. |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 48 | virtual void BytesReceived(uint64_t bytes_received, uint64_t total) = 0; |
| 49 | }; |
| 50 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 51 | class DownloadAction; |
| 52 | class NoneType; |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 53 | class PrefsInterface; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 54 | |
| 55 | template<> |
| 56 | class ActionTraits<DownloadAction> { |
| 57 | public: |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 58 | // Takes and returns an InstallPlan |
| 59 | typedef InstallPlan InputObjectType; |
| 60 | typedef InstallPlan OutputObjectType; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | class DownloadAction : public Action<DownloadAction>, |
| 64 | public HttpFetcherDelegate { |
| 65 | public: |
| 66 | // Takes ownership of the passed in HttpFetcher. Useful for testing. |
| 67 | // A good calling pattern is: |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 68 | // DownloadAction(new WhateverHttpFetcher); |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 69 | DownloadAction(PrefsInterface* prefs, HttpFetcher* http_fetcher); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 70 | virtual ~DownloadAction(); |
| 71 | typedef ActionTraits<DownloadAction>::InputObjectType InputObjectType; |
| 72 | typedef ActionTraits<DownloadAction>::OutputObjectType OutputObjectType; |
| 73 | void PerformAction(); |
| 74 | void TerminateProcessing(); |
| 75 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 76 | // Testing |
| 77 | void SetTestFileWriter(FileWriter* writer) { |
| 78 | writer_ = writer; |
| 79 | } |
| 80 | |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 81 | int GetHTTPResponseCode() { return http_fetcher_->http_response_code(); } |
| 82 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 83 | // Debugging/logging |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 84 | static std::string StaticType() { return "DownloadAction"; } |
| 85 | std::string Type() const { return StaticType(); } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 86 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 87 | // HttpFetcherDelegate methods (see http_fetcher.h) |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 88 | virtual void ReceivedBytes(HttpFetcher *fetcher, |
| 89 | const char* bytes, int length); |
Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 90 | virtual void SeekToOffset(off_t offset); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 91 | virtual void TransferComplete(HttpFetcher *fetcher, bool successful); |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 92 | virtual void TransferTerminated(HttpFetcher *fetcher); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 93 | |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 94 | DownloadActionDelegate* delegate() const { return delegate_; } |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 95 | void set_delegate(DownloadActionDelegate* delegate) { |
| 96 | delegate_ = delegate; |
| 97 | } |
| 98 | |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 99 | HttpFetcher* http_fetcher() { return http_fetcher_.get(); } |
| 100 | |
Andrew de los Reyes | 21816e1 | 2011-04-07 14:18:56 -0700 | [diff] [blame^] | 101 | void set_skip_reporting_signature_fail(google::protobuf::Closure* callback) { |
| 102 | skip_reporting_signature_fail_.reset(callback); |
| 103 | } |
| 104 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 105 | private: |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 106 | // The InstallPlan passed in |
| 107 | InstallPlan install_plan_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 108 | |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 109 | // Update Engine preference store. |
| 110 | PrefsInterface* prefs_; |
| 111 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 112 | // The FileWriter that downloaded data should be written to. It will |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 113 | // either point to *decompressing_file_writer_ or *delta_performer_. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 114 | FileWriter* writer_; |
| 115 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 116 | // These are used for full updates: |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 117 | scoped_ptr<GzipDecompressingFileWriter> decompressing_file_writer_; |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 118 | scoped_ptr<SplitFileWriter> split_file_writer_; |
| 119 | scoped_ptr<DirectFileWriter> kernel_file_writer_; |
| 120 | scoped_ptr<DirectFileWriter> rootfs_file_writer_; |
Darin Petkov | e971f33 | 2010-09-22 16:57:25 -0700 | [diff] [blame] | 121 | scoped_ptr<BufferedFileWriter> kernel_buffered_file_writer_; |
| 122 | scoped_ptr<BufferedFileWriter> rootfs_buffered_file_writer_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 123 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 124 | // Used to apply a delta update: |
| 125 | scoped_ptr<DeltaPerformer> delta_performer_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 126 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 127 | // Pointer to the HttpFetcher that does the http work. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 128 | scoped_ptr<HttpFetcher> http_fetcher_; |
| 129 | |
| 130 | // Used to find the hash of the bytes downloaded |
| 131 | OmahaHashCalculator omaha_hash_calculator_; |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 132 | |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 133 | // Used by TransferTerminated to figure if this action terminated itself or |
| 134 | // was terminated by the action processor. |
| 135 | ActionExitCode code_; |
| 136 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 137 | // For reporting status to outsiders |
| 138 | DownloadActionDelegate* delegate_; |
| 139 | uint64_t bytes_received_; |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 140 | |
Andrew de los Reyes | 21816e1 | 2011-04-07 14:18:56 -0700 | [diff] [blame^] | 141 | // Called if the download fails OR (download success AND signature verifies) |
| 142 | scoped_ptr<google::protobuf::Closure> skip_reporting_signature_fail_; |
| 143 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 144 | DISALLOW_COPY_AND_ASSIGN(DownloadAction); |
| 145 | }; |
| 146 | |
| 147 | // We want to be sure that we're compiled with large file support on linux, |
| 148 | // just in case we find ourselves downloading large images. |
| 149 | COMPILE_ASSERT(8 == sizeof(off_t), off_t_not_64_bit); |
| 150 | |
| 151 | } // namespace chromeos_update_engine |
| 152 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 153 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_DOWNLOAD_ACTION_H__ |