blob: 62dd29afb51ddb069e30f806774f01f8a69101a9 [file] [log] [blame]
rspangler@google.com49fdf182009-10-10 00:57:34 +00001// 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.comc98a7ed2009-12-04 18:54:03 +00005#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_DOWNLOAD_ACTION_H__
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_DOWNLOAD_ACTION_H__
rspangler@google.com49fdf182009-10-10 00:57:34 +00007
8#include <sys/types.h>
9#include <sys/stat.h>
10#include <fcntl.h>
11
rspangler@google.com49fdf182009-10-10 00:57:34 +000012#include <string>
13
Darin Petkove971f332010-09-22 16:57:25 -070014#include <base/scoped_ptr.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000015#include <curl/curl.h>
16
rspangler@google.com49fdf182009-10-10 00:57:34 +000017#include "update_engine/action.h"
18#include "update_engine/decompressing_file_writer.h"
Andrew de los Reyesf9185172010-05-03 11:07:05 -070019#include "update_engine/delta_performer.h"
Darin Petkove971f332010-09-22 16:57:25 -070020#include "update_engine/buffered_file_writer.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000021#include "update_engine/http_fetcher.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000022#include "update_engine/install_plan.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000023#include "update_engine/omaha_hash_calculator.h"
Andrew de los Reyesf9185172010-05-03 11:07:05 -070024#include "update_engine/split_file_writer.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000025
Andrew de los Reyesf9185172010-05-03 11:07:05 -070026// 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.com49fdf182009-10-10 00:57:34 +000033
rspangler@google.com49fdf182009-10-10 00:57:34 +000034namespace chromeos_update_engine {
35
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070036class DownloadActionDelegate {
37 public:
Darin Petkov9d911fa2010-08-19 09:36:08 -070038 // 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 Reyes63b96d72010-05-10 13:08:54 -070047 virtual void BytesReceived(uint64_t bytes_received, uint64_t total) = 0;
48};
49
rspangler@google.com49fdf182009-10-10 00:57:34 +000050class DownloadAction;
51class NoneType;
Darin Petkov73058b42010-10-06 16:32:19 -070052class PrefsInterface;
rspangler@google.com49fdf182009-10-10 00:57:34 +000053
54template<>
55class ActionTraits<DownloadAction> {
56 public:
adlr@google.comc98a7ed2009-12-04 18:54:03 +000057 // Takes and returns an InstallPlan
58 typedef InstallPlan InputObjectType;
59 typedef InstallPlan OutputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +000060};
61
62class DownloadAction : public Action<DownloadAction>,
63 public HttpFetcherDelegate {
64 public:
65 // Takes ownership of the passed in HttpFetcher. Useful for testing.
66 // A good calling pattern is:
adlr@google.comc98a7ed2009-12-04 18:54:03 +000067 // DownloadAction(new WhateverHttpFetcher);
Darin Petkov73058b42010-10-06 16:32:19 -070068 DownloadAction(PrefsInterface* prefs, HttpFetcher* http_fetcher);
rspangler@google.com49fdf182009-10-10 00:57:34 +000069 virtual ~DownloadAction();
70 typedef ActionTraits<DownloadAction>::InputObjectType InputObjectType;
71 typedef ActionTraits<DownloadAction>::OutputObjectType OutputObjectType;
72 void PerformAction();
73 void TerminateProcessing();
74
Andrew de los Reyesf9185172010-05-03 11:07:05 -070075 // Testing
76 void SetTestFileWriter(FileWriter* writer) {
77 writer_ = writer;
78 }
79
Darin Petkov1023a602010-08-30 13:47:51 -070080 int GetHTTPResponseCode() { return http_fetcher_->http_response_code(); }
81
rspangler@google.com49fdf182009-10-10 00:57:34 +000082 // Debugging/logging
adlr@google.comc98a7ed2009-12-04 18:54:03 +000083 static std::string StaticType() { return "DownloadAction"; }
84 std::string Type() const { return StaticType(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +000085
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070086 // HttpFetcherDelegate methods (see http_fetcher.h)
rspangler@google.com49fdf182009-10-10 00:57:34 +000087 virtual void ReceivedBytes(HttpFetcher *fetcher,
88 const char* bytes, int length);
89 virtual void TransferComplete(HttpFetcher *fetcher, bool successful);
90
Darin Petkovf42cc1c2010-09-01 09:03:02 -070091 DownloadActionDelegate* delegate() const { return delegate_; }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070092 void set_delegate(DownloadActionDelegate* delegate) {
93 delegate_ = delegate;
94 }
95
rspangler@google.com49fdf182009-10-10 00:57:34 +000096 private:
Andrew de los Reyesf9185172010-05-03 11:07:05 -070097 // The InstallPlan passed in
98 InstallPlan install_plan_;
rspangler@google.com49fdf182009-10-10 00:57:34 +000099
Darin Petkov73058b42010-10-06 16:32:19 -0700100 // Update Engine preference store.
101 PrefsInterface* prefs_;
102
rspangler@google.com49fdf182009-10-10 00:57:34 +0000103 // The FileWriter that downloaded data should be written to. It will
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700104 // either point to *decompressing_file_writer_ or *delta_performer_.
rspangler@google.com49fdf182009-10-10 00:57:34 +0000105 FileWriter* writer_;
106
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700107 // These are used for full updates:
rspangler@google.com49fdf182009-10-10 00:57:34 +0000108 scoped_ptr<GzipDecompressingFileWriter> decompressing_file_writer_;
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700109 scoped_ptr<SplitFileWriter> split_file_writer_;
110 scoped_ptr<DirectFileWriter> kernel_file_writer_;
111 scoped_ptr<DirectFileWriter> rootfs_file_writer_;
Darin Petkove971f332010-09-22 16:57:25 -0700112 scoped_ptr<BufferedFileWriter> kernel_buffered_file_writer_;
113 scoped_ptr<BufferedFileWriter> rootfs_buffered_file_writer_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000114
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700115 // Used to apply a delta update:
116 scoped_ptr<DeltaPerformer> delta_performer_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000117
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700118 // Pointer to the HttpFetcher that does the http work.
rspangler@google.com49fdf182009-10-10 00:57:34 +0000119 scoped_ptr<HttpFetcher> http_fetcher_;
120
121 // Used to find the hash of the bytes downloaded
122 OmahaHashCalculator omaha_hash_calculator_;
Darin Petkov9d911fa2010-08-19 09:36:08 -0700123
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700124 // For reporting status to outsiders
125 DownloadActionDelegate* delegate_;
126 uint64_t bytes_received_;
Darin Petkov9d911fa2010-08-19 09:36:08 -0700127
rspangler@google.com49fdf182009-10-10 00:57:34 +0000128 DISALLOW_COPY_AND_ASSIGN(DownloadAction);
129};
130
131// We want to be sure that we're compiled with large file support on linux,
132// just in case we find ourselves downloading large images.
133COMPILE_ASSERT(8 == sizeof(off_t), off_t_not_64_bit);
134
135} // namespace chromeos_update_engine
136
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000137#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_DOWNLOAD_ACTION_H__