blob: 020aa9c7b6104f07c1f6e9234599019d94339b55 [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
Darin Petkov73058b42010-10-06 16:32:19 -070044class PrefsInterface;
rspangler@google.com49fdf182009-10-10 00:57:34 +000045
Chris Sosad317e402013-06-12 13:47:09 -070046class DownloadAction : public InstallPlanAction,
rspangler@google.com49fdf182009-10-10 00:57:34 +000047 public HttpFetcherDelegate {
48 public:
49 // Takes ownership of the passed in HttpFetcher. Useful for testing.
50 // A good calling pattern is:
Jay Srinivasanf0572052012-10-23 18:12:56 -070051 // DownloadAction(prefs, system_state, new WhateverHttpFetcher);
52 DownloadAction(PrefsInterface* prefs,
53 SystemState* system_state,
54 HttpFetcher* http_fetcher);
rspangler@google.com49fdf182009-10-10 00:57:34 +000055 virtual ~DownloadAction();
rspangler@google.com49fdf182009-10-10 00:57:34 +000056 void PerformAction();
57 void TerminateProcessing();
58
Andrew de los Reyesf9185172010-05-03 11:07:05 -070059 // Testing
60 void SetTestFileWriter(FileWriter* writer) {
61 writer_ = writer;
62 }
63
Darin Petkov1023a602010-08-30 13:47:51 -070064 int GetHTTPResponseCode() { return http_fetcher_->http_response_code(); }
65
rspangler@google.com49fdf182009-10-10 00:57:34 +000066 // Debugging/logging
adlr@google.comc98a7ed2009-12-04 18:54:03 +000067 static std::string StaticType() { return "DownloadAction"; }
68 std::string Type() const { return StaticType(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +000069
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070070 // HttpFetcherDelegate methods (see http_fetcher.h)
rspangler@google.com49fdf182009-10-10 00:57:34 +000071 virtual void ReceivedBytes(HttpFetcher *fetcher,
72 const char* bytes, int length);
Andrew de los Reyes34e41a12010-10-26 20:07:58 -070073 virtual void SeekToOffset(off_t offset);
rspangler@google.com49fdf182009-10-10 00:57:34 +000074 virtual void TransferComplete(HttpFetcher *fetcher, bool successful);
Darin Petkov9ce452b2010-11-17 14:33:28 -080075 virtual void TransferTerminated(HttpFetcher *fetcher);
rspangler@google.com49fdf182009-10-10 00:57:34 +000076
Darin Petkovf42cc1c2010-09-01 09:03:02 -070077 DownloadActionDelegate* delegate() const { return delegate_; }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070078 void set_delegate(DownloadActionDelegate* delegate) {
79 delegate_ = delegate;
80 }
81
Darin Petkov9b230572010-10-08 10:20:09 -070082 HttpFetcher* http_fetcher() { return http_fetcher_.get(); }
83
rspangler@google.com49fdf182009-10-10 00:57:34 +000084 private:
Andrew de los Reyesf9185172010-05-03 11:07:05 -070085 // The InstallPlan passed in
86 InstallPlan install_plan_;
rspangler@google.com49fdf182009-10-10 00:57:34 +000087
Darin Petkov73058b42010-10-06 16:32:19 -070088 // Update Engine preference store.
89 PrefsInterface* prefs_;
90
Jay Srinivasanedce2832012-10-24 18:57:47 -070091 // Global context for the system.
92 SystemState* system_state_;
93
94 // Pointer to the HttpFetcher that does the http work.
95 scoped_ptr<HttpFetcher> http_fetcher_;
96
rspangler@google.com49fdf182009-10-10 00:57:34 +000097 // The FileWriter that downloaded data should be written to. It will
Andrew de los Reyesf9185172010-05-03 11:07:05 -070098 // either point to *decompressing_file_writer_ or *delta_performer_.
rspangler@google.com49fdf182009-10-10 00:57:34 +000099 FileWriter* writer_;
100
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700101 scoped_ptr<DeltaPerformer> delta_performer_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000102
Darin Petkov9ce452b2010-11-17 14:33:28 -0800103 // Used by TransferTerminated to figure if this action terminated itself or
104 // was terminated by the action processor.
David Zeuthena99981f2013-04-29 13:42:47 -0700105 ErrorCode code_;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800106
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700107 // For reporting status to outsiders
108 DownloadActionDelegate* delegate_;
109 uint64_t bytes_received_;
Darin Petkov9d911fa2010-08-19 09:36:08 -0700110
rspangler@google.com49fdf182009-10-10 00:57:34 +0000111 DISALLOW_COPY_AND_ASSIGN(DownloadAction);
112};
113
114// We want to be sure that we're compiled with large file support on linux,
115// just in case we find ourselves downloading large images.
116COMPILE_ASSERT(8 == sizeof(off_t), off_t_not_64_bit);
117
118} // namespace chromeos_update_engine
119
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000120#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_DOWNLOAD_ACTION_H__