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" |
| 19 | #include "update_engine/file_writer.h" |
| 20 | #include "update_engine/http_fetcher.h" |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 21 | #include "update_engine/install_plan.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 22 | #include "update_engine/omaha_hash_calculator.h" |
| 23 | |
| 24 | // The Download Action downloads a requested url to a specified path on disk. |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 25 | // The url and output path are determined by the InstallPlan passed in. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 26 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 27 | namespace chromeos_update_engine { |
| 28 | |
| 29 | class DownloadAction; |
| 30 | class NoneType; |
| 31 | |
| 32 | template<> |
| 33 | class ActionTraits<DownloadAction> { |
| 34 | public: |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 35 | // Takes and returns an InstallPlan |
| 36 | typedef InstallPlan InputObjectType; |
| 37 | typedef InstallPlan OutputObjectType; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | class DownloadAction : public Action<DownloadAction>, |
| 41 | public HttpFetcherDelegate { |
| 42 | public: |
| 43 | // Takes ownership of the passed in HttpFetcher. Useful for testing. |
| 44 | // A good calling pattern is: |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 45 | // DownloadAction(new WhateverHttpFetcher); |
| 46 | DownloadAction(HttpFetcher* http_fetcher); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 47 | virtual ~DownloadAction(); |
| 48 | typedef ActionTraits<DownloadAction>::InputObjectType InputObjectType; |
| 49 | typedef ActionTraits<DownloadAction>::OutputObjectType OutputObjectType; |
| 50 | void PerformAction(); |
| 51 | void TerminateProcessing(); |
| 52 | |
| 53 | // Debugging/logging |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 54 | static std::string StaticType() { return "DownloadAction"; } |
| 55 | std::string Type() const { return StaticType(); } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 56 | |
| 57 | // Delegate methods (see http_fetcher.h) |
| 58 | virtual void ReceivedBytes(HttpFetcher *fetcher, |
| 59 | const char* bytes, int length); |
| 60 | virtual void TransferComplete(HttpFetcher *fetcher, bool successful); |
| 61 | |
| 62 | private: |
| 63 | // Expected size of the file (will be used for progress info) |
| 64 | const size_t size_; |
| 65 | |
| 66 | // URL to download |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 67 | std::string url_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 68 | |
| 69 | // Path to save URL to |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 70 | std::string output_path_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 71 | |
| 72 | // Expected hash of the file. The hash must match for this action to |
| 73 | // succeed. |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 74 | std::string hash_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 75 | |
| 76 | // Whether the caller requested that we decompress the downloaded data. |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 77 | bool should_decompress_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 78 | |
| 79 | // The FileWriter that downloaded data should be written to. It will |
| 80 | // either point to *decompressing_file_writer_ or *direct_file_writer_. |
| 81 | FileWriter* writer_; |
| 82 | |
| 83 | // If non-null, a FileWriter used for gzip decompressing downloaded data |
| 84 | scoped_ptr<GzipDecompressingFileWriter> decompressing_file_writer_; |
| 85 | |
| 86 | // Used to write out the downloaded file |
| 87 | scoped_ptr<DirectFileWriter> direct_file_writer_; |
| 88 | |
| 89 | // pointer to the HttpFetcher that does the http work |
| 90 | scoped_ptr<HttpFetcher> http_fetcher_; |
| 91 | |
| 92 | // Used to find the hash of the bytes downloaded |
| 93 | OmahaHashCalculator omaha_hash_calculator_; |
| 94 | DISALLOW_COPY_AND_ASSIGN(DownloadAction); |
| 95 | }; |
| 96 | |
| 97 | // We want to be sure that we're compiled with large file support on linux, |
| 98 | // just in case we find ourselves downloading large images. |
| 99 | COMPILE_ASSERT(8 == sizeof(off_t), off_t_not_64_bit); |
| 100 | |
| 101 | } // namespace chromeos_update_engine |
| 102 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 103 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_DOWNLOAD_ACTION_H__ |