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