Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 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 | |
Chris Masone | d903c3b | 2011-05-12 15:35:46 -0700 | [diff] [blame] | 14 | #include <base/memory/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" |
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/http_fetcher.h" |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 21 | #include "update_engine/install_plan.h" |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 22 | #include "update_engine/system_state.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 23 | |
Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 24 | // The Download Action downloads a specified url to disk. The url should point |
| 25 | // to an update in a delta payload format. The payload will be piped into a |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 26 | // DeltaPerformer that will apply the delta to the disk. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 27 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 28 | namespace chromeos_update_engine { |
| 29 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 30 | class DownloadActionDelegate { |
| 31 | public: |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 32 | // Called right before starting the download with |active| set to |
| 33 | // true. Called after completing the download with |active| set to |
| 34 | // false. |
| 35 | virtual void SetDownloadStatus(bool active) = 0; |
| 36 | |
| 37 | // Called periodically after bytes are received. This method will be |
| 38 | // invoked only if the download is active. |bytes_received| is the |
| 39 | // number of bytes downloaded thus far. |total| is the number of |
| 40 | // bytes expected. |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 41 | virtual void BytesReceived(uint64_t bytes_received, uint64_t total) = 0; |
| 42 | }; |
| 43 | |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 44 | class PrefsInterface; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 45 | |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 46 | class DownloadAction : public InstallPlanAction, |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 47 | public HttpFetcherDelegate { |
| 48 | public: |
| 49 | // Takes ownership of the passed in HttpFetcher. Useful for testing. |
| 50 | // A good calling pattern is: |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 51 | // DownloadAction(prefs, system_state, new WhateverHttpFetcher); |
| 52 | DownloadAction(PrefsInterface* prefs, |
| 53 | SystemState* system_state, |
| 54 | HttpFetcher* http_fetcher); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 55 | virtual ~DownloadAction(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 56 | void PerformAction(); |
| 57 | void TerminateProcessing(); |
| 58 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 59 | // Testing |
| 60 | void SetTestFileWriter(FileWriter* writer) { |
| 61 | writer_ = writer; |
| 62 | } |
| 63 | |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 64 | int GetHTTPResponseCode() { return http_fetcher_->http_response_code(); } |
| 65 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 66 | // Debugging/logging |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 67 | static std::string StaticType() { return "DownloadAction"; } |
| 68 | std::string Type() const { return StaticType(); } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 69 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 70 | // HttpFetcherDelegate methods (see http_fetcher.h) |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 71 | virtual void ReceivedBytes(HttpFetcher *fetcher, |
| 72 | const char* bytes, int length); |
Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 73 | virtual void SeekToOffset(off_t offset); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 74 | virtual void TransferComplete(HttpFetcher *fetcher, bool successful); |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 75 | virtual void TransferTerminated(HttpFetcher *fetcher); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 76 | |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 77 | DownloadActionDelegate* delegate() const { return delegate_; } |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 78 | void set_delegate(DownloadActionDelegate* delegate) { |
| 79 | delegate_ = delegate; |
| 80 | } |
| 81 | |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 82 | HttpFetcher* http_fetcher() { return http_fetcher_.get(); } |
| 83 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 84 | // Returns the p2p file id for the file being written or the empty |
| 85 | // string if we're not writing to a p2p file. |
| 86 | std::string p2p_file_id() { return p2p_file_id_; }; |
| 87 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 88 | private: |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 89 | // Closes the file descriptor for the p2p file being written and |
| 90 | // clears |p2p_file_id_| to indicate that we're no longer sharing |
| 91 | // the file. If |delete_p2p_file| is True, also deletes the file. |
| 92 | // If there is no p2p file descriptor, this method does nothing. |
| 93 | void CloseP2PSharingFd(bool delete_p2p_file); |
| 94 | |
| 95 | // Starts sharing the p2p file. Must be called before |
| 96 | // WriteToP2PFile(). Returns True if this worked. |
| 97 | bool SetupP2PSharingFd(); |
| 98 | |
| 99 | // Writes |length| bytes of payload from |data| into |file_offset| |
| 100 | // of the p2p file. Also does sanity checks; for example ensures we |
| 101 | // don't end up with a file with holes in it. |
| 102 | // |
| 103 | // This method does nothing if SetupP2PSharingFd() hasn't been |
| 104 | // called or if CloseP2PSharingFd() has been called. |
| 105 | void WriteToP2PFile(const char *data, size_t length, off_t file_offset); |
| 106 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 107 | // The InstallPlan passed in |
| 108 | InstallPlan install_plan_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 109 | |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 110 | // Update Engine preference store. |
| 111 | PrefsInterface* prefs_; |
| 112 | |
Jay Srinivasan | edce283 | 2012-10-24 18:57:47 -0700 | [diff] [blame] | 113 | // Global context for the system. |
| 114 | SystemState* system_state_; |
| 115 | |
| 116 | // Pointer to the HttpFetcher that does the http work. |
| 117 | scoped_ptr<HttpFetcher> http_fetcher_; |
| 118 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 119 | // 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] | 120 | // either point to *decompressing_file_writer_ or *delta_performer_. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 121 | FileWriter* writer_; |
| 122 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 123 | scoped_ptr<DeltaPerformer> delta_performer_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 124 | |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 125 | // Used by TransferTerminated to figure if this action terminated itself or |
| 126 | // was terminated by the action processor. |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 127 | ErrorCode code_; |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 128 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 129 | // For reporting status to outsiders |
| 130 | DownloadActionDelegate* delegate_; |
| 131 | uint64_t bytes_received_; |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 132 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 133 | // The file-id for the file we're sharing or the empty string |
| 134 | // if we're not using p2p to share. |
| 135 | std::string p2p_file_id_; |
| 136 | |
| 137 | // The file descriptor for the p2p file used for caching the payload or -1 |
| 138 | // if we're not using p2p to share. |
| 139 | int p2p_sharing_fd_; |
| 140 | |
| 141 | // Set to |false| if p2p file is not visible. |
| 142 | bool p2p_visible_; |
| 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__ |