blob: 6e334f68fff78b85d17a60cfae87e0f0d25dc11a [file] [log] [blame]
Darin Petkov7ed561b2011-10-04 02:59:03 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
rspangler@google.com49fdf182009-10-10 00:57:34 +00002// 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
Chris Masoned903c3b2011-05-12 15:35:46 -070014#include <base/memory/scoped_ptr.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000015#include <curl/curl.h>
Andrew de los Reyes21816e12011-04-07 14:18:56 -070016#include <google/protobuf/stubs/common.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000017
rspangler@google.com49fdf182009-10-10 00:57:34 +000018#include "update_engine/action.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/http_fetcher.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000021#include "update_engine/install_plan.h"
Jay Srinivasanf0572052012-10-23 18:12:56 -070022#include "update_engine/system_state.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000023
Darin Petkov7ed561b2011-10-04 02:59:03 -070024// 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 Reyesf9185172010-05-03 11:07:05 -070026// DeltaPerformer that will apply the delta to the disk.
rspangler@google.com49fdf182009-10-10 00:57:34 +000027
rspangler@google.com49fdf182009-10-10 00:57:34 +000028namespace chromeos_update_engine {
29
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070030class DownloadActionDelegate {
31 public:
Darin Petkov9d911fa2010-08-19 09:36:08 -070032 // 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 Reyes63b96d72010-05-10 13:08:54 -070041 virtual void BytesReceived(uint64_t bytes_received, uint64_t total) = 0;
42};
43
rspangler@google.com49fdf182009-10-10 00:57:34 +000044class DownloadAction;
45class NoneType;
Darin Petkov73058b42010-10-06 16:32:19 -070046class PrefsInterface;
rspangler@google.com49fdf182009-10-10 00:57:34 +000047
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:
Jay Srinivasanf0572052012-10-23 18:12:56 -070061 // DownloadAction(prefs, system_state, new WhateverHttpFetcher);
62 DownloadAction(PrefsInterface* prefs,
63 SystemState* system_state,
64 HttpFetcher* http_fetcher);
rspangler@google.com49fdf182009-10-10 00:57:34 +000065 virtual ~DownloadAction();
66 typedef ActionTraits<DownloadAction>::InputObjectType InputObjectType;
67 typedef ActionTraits<DownloadAction>::OutputObjectType OutputObjectType;
68 void PerformAction();
69 void TerminateProcessing();
70
Andrew de los Reyesf9185172010-05-03 11:07:05 -070071 // Testing
72 void SetTestFileWriter(FileWriter* writer) {
73 writer_ = writer;
74 }
75
Darin Petkov1023a602010-08-30 13:47:51 -070076 int GetHTTPResponseCode() { return http_fetcher_->http_response_code(); }
77
rspangler@google.com49fdf182009-10-10 00:57:34 +000078 // Debugging/logging
adlr@google.comc98a7ed2009-12-04 18:54:03 +000079 static std::string StaticType() { return "DownloadAction"; }
80 std::string Type() const { return StaticType(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +000081
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070082 // HttpFetcherDelegate methods (see http_fetcher.h)
rspangler@google.com49fdf182009-10-10 00:57:34 +000083 virtual void ReceivedBytes(HttpFetcher *fetcher,
84 const char* bytes, int length);
Andrew de los Reyes34e41a12010-10-26 20:07:58 -070085 virtual void SeekToOffset(off_t offset);
rspangler@google.com49fdf182009-10-10 00:57:34 +000086 virtual void TransferComplete(HttpFetcher *fetcher, bool successful);
Darin Petkov9ce452b2010-11-17 14:33:28 -080087 virtual void TransferTerminated(HttpFetcher *fetcher);
rspangler@google.com49fdf182009-10-10 00:57:34 +000088
Darin Petkovf42cc1c2010-09-01 09:03:02 -070089 DownloadActionDelegate* delegate() const { return delegate_; }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070090 void set_delegate(DownloadActionDelegate* delegate) {
91 delegate_ = delegate;
92 }
93
Darin Petkov9b230572010-10-08 10:20:09 -070094 HttpFetcher* http_fetcher() { return http_fetcher_.get(); }
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
Jay Srinivasanedce2832012-10-24 18:57:47 -0700103 // Global context for the system.
104 SystemState* system_state_;
105
106 // Pointer to the HttpFetcher that does the http work.
107 scoped_ptr<HttpFetcher> http_fetcher_;
108
rspangler@google.com49fdf182009-10-10 00:57:34 +0000109 // The FileWriter that downloaded data should be written to. It will
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700110 // either point to *decompressing_file_writer_ or *delta_performer_.
rspangler@google.com49fdf182009-10-10 00:57:34 +0000111 FileWriter* writer_;
112
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700113 scoped_ptr<DeltaPerformer> delta_performer_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000114
Darin Petkov9ce452b2010-11-17 14:33:28 -0800115 // Used by TransferTerminated to figure if this action terminated itself or
116 // was terminated by the action processor.
117 ActionExitCode code_;
118
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700119 // For reporting status to outsiders
120 DownloadActionDelegate* delegate_;
121 uint64_t bytes_received_;
Darin Petkov9d911fa2010-08-19 09:36:08 -0700122
rspangler@google.com49fdf182009-10-10 00:57:34 +0000123 DISALLOW_COPY_AND_ASSIGN(DownloadAction);
124};
125
126// We want to be sure that we're compiled with large file support on linux,
127// just in case we find ourselves downloading large images.
128COMPILE_ASSERT(8 == sizeof(off_t), off_t_not_64_bit);
129
130} // namespace chromeos_update_engine
131
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000132#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_DOWNLOAD_ACTION_H__