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 | |
| 14 | #include <curl/curl.h> |
| 15 | |
| 16 | #include "base/scoped_ptr.h" |
| 17 | #include "update_engine/action.h" |
| 18 | #include "update_engine/decompressing_file_writer.h" |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 19 | #include "update_engine/delta_performer.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 20 | #include "update_engine/file_writer.h" |
| 21 | #include "update_engine/http_fetcher.h" |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 22 | #include "update_engine/install_plan.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 23 | #include "update_engine/omaha_hash_calculator.h" |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 24 | #include "update_engine/split_file_writer.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 25 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 26 | // The Download Action downloads a specified url to disk. The url should |
| 27 | // point to either a full or delta update. If a full update, the file will |
| 28 | // be piped into a SplitFileWriter, which will direct it to the kernel |
| 29 | // and rootfs partitions. If it's a delta update, the destination kernel |
| 30 | // and rootfs should already contain the source-version that this delta |
| 31 | // update goes from. In this case, the update will be piped into a |
| 32 | // DeltaPerformer that will apply the delta to the disk. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 33 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 34 | namespace chromeos_update_engine { |
| 35 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 36 | class DownloadActionDelegate { |
| 37 | public: |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 38 | // Called right before starting the download with |active| set to |
| 39 | // true. Called after completing the download with |active| set to |
| 40 | // false. |
| 41 | virtual void SetDownloadStatus(bool active) = 0; |
| 42 | |
| 43 | // Called periodically after bytes are received. This method will be |
| 44 | // invoked only if the download is active. |bytes_received| is the |
| 45 | // number of bytes downloaded thus far. |total| is the number of |
| 46 | // bytes expected. |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 47 | virtual void BytesReceived(uint64_t bytes_received, uint64_t total) = 0; |
| 48 | }; |
| 49 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 50 | class DownloadAction; |
| 51 | class NoneType; |
| 52 | |
| 53 | template<> |
| 54 | class ActionTraits<DownloadAction> { |
| 55 | public: |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 56 | // Takes and returns an InstallPlan |
| 57 | typedef InstallPlan InputObjectType; |
| 58 | typedef InstallPlan OutputObjectType; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | class DownloadAction : public Action<DownloadAction>, |
| 62 | public HttpFetcherDelegate { |
| 63 | public: |
| 64 | // Takes ownership of the passed in HttpFetcher. Useful for testing. |
| 65 | // A good calling pattern is: |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 66 | // DownloadAction(new WhateverHttpFetcher); |
| 67 | DownloadAction(HttpFetcher* http_fetcher); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 68 | virtual ~DownloadAction(); |
| 69 | typedef ActionTraits<DownloadAction>::InputObjectType InputObjectType; |
| 70 | typedef ActionTraits<DownloadAction>::OutputObjectType OutputObjectType; |
| 71 | void PerformAction(); |
| 72 | void TerminateProcessing(); |
| 73 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 74 | // Testing |
| 75 | void SetTestFileWriter(FileWriter* writer) { |
| 76 | writer_ = writer; |
| 77 | } |
| 78 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 79 | // Debugging/logging |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 80 | static std::string StaticType() { return "DownloadAction"; } |
| 81 | std::string Type() const { return StaticType(); } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 82 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 83 | // HttpFetcherDelegate methods (see http_fetcher.h) |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 84 | virtual void ReceivedBytes(HttpFetcher *fetcher, |
| 85 | const char* bytes, int length); |
| 86 | virtual void TransferComplete(HttpFetcher *fetcher, bool successful); |
| 87 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 88 | void set_delegate(DownloadActionDelegate* delegate) { |
| 89 | delegate_ = delegate; |
| 90 | } |
| 91 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 92 | private: |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 93 | // The InstallPlan passed in |
| 94 | InstallPlan install_plan_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 95 | |
| 96 | // 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] | 97 | // either point to *decompressing_file_writer_ or *delta_performer_. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 98 | FileWriter* writer_; |
| 99 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 100 | // These are used for full updates: |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 101 | scoped_ptr<GzipDecompressingFileWriter> decompressing_file_writer_; |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 102 | scoped_ptr<SplitFileWriter> split_file_writer_; |
| 103 | scoped_ptr<DirectFileWriter> kernel_file_writer_; |
| 104 | scoped_ptr<DirectFileWriter> rootfs_file_writer_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 105 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 106 | // Used to apply a delta update: |
| 107 | scoped_ptr<DeltaPerformer> delta_performer_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 108 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 109 | // Pointer to the HttpFetcher that does the http work. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 110 | scoped_ptr<HttpFetcher> http_fetcher_; |
| 111 | |
| 112 | // Used to find the hash of the bytes downloaded |
| 113 | OmahaHashCalculator omaha_hash_calculator_; |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 114 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 115 | // For reporting status to outsiders |
| 116 | DownloadActionDelegate* delegate_; |
| 117 | uint64_t bytes_received_; |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 118 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 119 | DISALLOW_COPY_AND_ASSIGN(DownloadAction); |
| 120 | }; |
| 121 | |
| 122 | // We want to be sure that we're compiled with large file support on linux, |
| 123 | // just in case we find ourselves downloading large images. |
| 124 | COMPILE_ASSERT(8 == sizeof(off_t), off_t_not_64_bit); |
| 125 | |
| 126 | } // namespace chromeos_update_engine |
| 127 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 128 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_DOWNLOAD_ACTION_H__ |