blob: e743b41a57bad9119d71a2c43b3d08906da518ee [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"
rspangler@google.com49fdf182009-10-10 00:57:34 +000022
Darin Petkov7ed561b2011-10-04 02:59:03 -070023// 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 Reyesf9185172010-05-03 11:07:05 -070025// DeltaPerformer that will apply the delta to the disk.
rspangler@google.com49fdf182009-10-10 00:57:34 +000026
rspangler@google.com49fdf182009-10-10 00:57:34 +000027namespace chromeos_update_engine {
28
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070029class DownloadActionDelegate {
30 public:
Darin Petkov9d911fa2010-08-19 09:36:08 -070031 // 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 Reyes63b96d72010-05-10 13:08:54 -070040 virtual void BytesReceived(uint64_t bytes_received, uint64_t total) = 0;
41};
42
rspangler@google.com49fdf182009-10-10 00:57:34 +000043class DownloadAction;
44class NoneType;
Darin Petkov73058b42010-10-06 16:32:19 -070045class PrefsInterface;
rspangler@google.com49fdf182009-10-10 00:57:34 +000046
47template<>
48class ActionTraits<DownloadAction> {
49 public:
adlr@google.comc98a7ed2009-12-04 18:54:03 +000050 // Takes and returns an InstallPlan
51 typedef InstallPlan InputObjectType;
52 typedef InstallPlan OutputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +000053};
54
55class DownloadAction : public Action<DownloadAction>,
56 public HttpFetcherDelegate {
57 public:
58 // Takes ownership of the passed in HttpFetcher. Useful for testing.
59 // A good calling pattern is:
adlr@google.comc98a7ed2009-12-04 18:54:03 +000060 // DownloadAction(new WhateverHttpFetcher);
Darin Petkov73058b42010-10-06 16:32:19 -070061 DownloadAction(PrefsInterface* prefs, HttpFetcher* http_fetcher);
rspangler@google.com49fdf182009-10-10 00:57:34 +000062 virtual ~DownloadAction();
63 typedef ActionTraits<DownloadAction>::InputObjectType InputObjectType;
64 typedef ActionTraits<DownloadAction>::OutputObjectType OutputObjectType;
65 void PerformAction();
66 void TerminateProcessing();
67
Andrew de los Reyesf9185172010-05-03 11:07:05 -070068 // Testing
69 void SetTestFileWriter(FileWriter* writer) {
70 writer_ = writer;
71 }
72
Darin Petkov1023a602010-08-30 13:47:51 -070073 int GetHTTPResponseCode() { return http_fetcher_->http_response_code(); }
74
rspangler@google.com49fdf182009-10-10 00:57:34 +000075 // Debugging/logging
adlr@google.comc98a7ed2009-12-04 18:54:03 +000076 static std::string StaticType() { return "DownloadAction"; }
77 std::string Type() const { return StaticType(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +000078
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070079 // HttpFetcherDelegate methods (see http_fetcher.h)
rspangler@google.com49fdf182009-10-10 00:57:34 +000080 virtual void ReceivedBytes(HttpFetcher *fetcher,
81 const char* bytes, int length);
Andrew de los Reyes34e41a12010-10-26 20:07:58 -070082 virtual void SeekToOffset(off_t offset);
rspangler@google.com49fdf182009-10-10 00:57:34 +000083 virtual void TransferComplete(HttpFetcher *fetcher, bool successful);
Darin Petkov9ce452b2010-11-17 14:33:28 -080084 virtual void TransferTerminated(HttpFetcher *fetcher);
rspangler@google.com49fdf182009-10-10 00:57:34 +000085
Darin Petkovf42cc1c2010-09-01 09:03:02 -070086 DownloadActionDelegate* delegate() const { return delegate_; }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070087 void set_delegate(DownloadActionDelegate* delegate) {
88 delegate_ = delegate;
89 }
90
Darin Petkov9b230572010-10-08 10:20:09 -070091 HttpFetcher* http_fetcher() { return http_fetcher_.get(); }
92
rspangler@google.com49fdf182009-10-10 00:57:34 +000093 private:
Andrew de los Reyesf9185172010-05-03 11:07:05 -070094 // The InstallPlan passed in
95 InstallPlan install_plan_;
rspangler@google.com49fdf182009-10-10 00:57:34 +000096
Darin Petkov73058b42010-10-06 16:32:19 -070097 // Update Engine preference store.
98 PrefsInterface* prefs_;
99
rspangler@google.com49fdf182009-10-10 00:57:34 +0000100 // The FileWriter that downloaded data should be written to. It will
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700101 // either point to *decompressing_file_writer_ or *delta_performer_.
rspangler@google.com49fdf182009-10-10 00:57:34 +0000102 FileWriter* writer_;
103
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700104 scoped_ptr<DeltaPerformer> delta_performer_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000105
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700106 // Pointer to the HttpFetcher that does the http work.
rspangler@google.com49fdf182009-10-10 00:57:34 +0000107 scoped_ptr<HttpFetcher> http_fetcher_;
108
Darin Petkov9ce452b2010-11-17 14:33:28 -0800109 // Used by TransferTerminated to figure if this action terminated itself or
110 // was terminated by the action processor.
111 ActionExitCode code_;
112
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700113 // For reporting status to outsiders
114 DownloadActionDelegate* delegate_;
115 uint64_t bytes_received_;
Darin Petkov9d911fa2010-08-19 09:36:08 -0700116
rspangler@google.com49fdf182009-10-10 00:57:34 +0000117 DISALLOW_COPY_AND_ASSIGN(DownloadAction);
118};
119
120// We want to be sure that we're compiled with large file support on linux,
121// just in case we find ourselves downloading large images.
122COMPILE_ASSERT(8 == sizeof(off_t), off_t_not_64_bit);
123
124} // namespace chromeos_update_engine
125
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000126#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_DOWNLOAD_ACTION_H__