blob: e1e8aeb37b17dd98da8fd5319d43e5b7a1728748 [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
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 Reyesf9185172010-05-03 11:07:05 -070019#include "update_engine/delta_performer.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000020#include "update_engine/file_writer.h"
21#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:
38 // Called before any bytes are received and periodically after
39 // bytes are received.
40 // bytes_received is the number of bytes downloaded thus far.
41 // total is the number of bytes expected.
42 virtual void BytesReceived(uint64_t bytes_received, uint64_t total) = 0;
43};
44
rspangler@google.com49fdf182009-10-10 00:57:34 +000045class DownloadAction;
46class NoneType;
47
48template<>
49class ActionTraits<DownloadAction> {
50 public:
adlr@google.comc98a7ed2009-12-04 18:54:03 +000051 // Takes and returns an InstallPlan
52 typedef InstallPlan InputObjectType;
53 typedef InstallPlan OutputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +000054};
55
56class DownloadAction : public Action<DownloadAction>,
57 public HttpFetcherDelegate {
58 public:
59 // Takes ownership of the passed in HttpFetcher. Useful for testing.
60 // A good calling pattern is:
adlr@google.comc98a7ed2009-12-04 18:54:03 +000061 // DownloadAction(new WhateverHttpFetcher);
62 DownloadAction(HttpFetcher* http_fetcher);
rspangler@google.com49fdf182009-10-10 00:57:34 +000063 virtual ~DownloadAction();
64 typedef ActionTraits<DownloadAction>::InputObjectType InputObjectType;
65 typedef ActionTraits<DownloadAction>::OutputObjectType OutputObjectType;
66 void PerformAction();
67 void TerminateProcessing();
68
Andrew de los Reyesf9185172010-05-03 11:07:05 -070069 // Testing
70 void SetTestFileWriter(FileWriter* writer) {
71 writer_ = writer;
72 }
73
rspangler@google.com49fdf182009-10-10 00:57:34 +000074 // Debugging/logging
adlr@google.comc98a7ed2009-12-04 18:54:03 +000075 static std::string StaticType() { return "DownloadAction"; }
76 std::string Type() const { return StaticType(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +000077
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070078 // HttpFetcherDelegate methods (see http_fetcher.h)
rspangler@google.com49fdf182009-10-10 00:57:34 +000079 virtual void ReceivedBytes(HttpFetcher *fetcher,
80 const char* bytes, int length);
81 virtual void TransferComplete(HttpFetcher *fetcher, bool successful);
82
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070083 void set_delegate(DownloadActionDelegate* delegate) {
84 delegate_ = delegate;
85 }
86
rspangler@google.com49fdf182009-10-10 00:57:34 +000087 private:
Andrew de los Reyesf9185172010-05-03 11:07:05 -070088 // The InstallPlan passed in
89 InstallPlan install_plan_;
rspangler@google.com49fdf182009-10-10 00:57:34 +000090
91 // The FileWriter that downloaded data should be written to. It will
Andrew de los Reyesf9185172010-05-03 11:07:05 -070092 // either point to *decompressing_file_writer_ or *delta_performer_.
rspangler@google.com49fdf182009-10-10 00:57:34 +000093 FileWriter* writer_;
94
Andrew de los Reyesf9185172010-05-03 11:07:05 -070095 // These are used for full updates:
rspangler@google.com49fdf182009-10-10 00:57:34 +000096 scoped_ptr<GzipDecompressingFileWriter> decompressing_file_writer_;
Andrew de los Reyesf9185172010-05-03 11:07:05 -070097 scoped_ptr<SplitFileWriter> split_file_writer_;
98 scoped_ptr<DirectFileWriter> kernel_file_writer_;
99 scoped_ptr<DirectFileWriter> rootfs_file_writer_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000100
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700101 // Used to apply a delta update:
102 scoped_ptr<DeltaPerformer> delta_performer_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000103
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700104 // Pointer to the HttpFetcher that does the http work.
rspangler@google.com49fdf182009-10-10 00:57:34 +0000105 scoped_ptr<HttpFetcher> http_fetcher_;
106
107 // Used to find the hash of the bytes downloaded
108 OmahaHashCalculator omaha_hash_calculator_;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700109
110 // For reporting status to outsiders
111 DownloadActionDelegate* delegate_;
112 uint64_t bytes_received_;
113
rspangler@google.com49fdf182009-10-10 00:57:34 +0000114 DISALLOW_COPY_AND_ASSIGN(DownloadAction);
115};
116
117// We want to be sure that we're compiled with large file support on linux,
118// just in case we find ourselves downloading large images.
119COMPILE_ASSERT(8 == sizeof(off_t), off_t_not_64_bit);
120
121} // namespace chromeos_update_engine
122
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000123#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_DOWNLOAD_ACTION_H__