blob: e6939e125e231bc43b9e567753781080a352dc88 [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
12#include <map>
13#include <string>
14
15#include <curl/curl.h>
16
17#include "base/scoped_ptr.h"
18#include "update_engine/action.h"
19#include "update_engine/decompressing_file_writer.h"
20#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"
24
25// The Download Action downloads a requested url to a specified path on disk.
adlr@google.comc98a7ed2009-12-04 18:54:03 +000026// The url and output path are determined by the InstallPlan passed in.
rspangler@google.com49fdf182009-10-10 00:57:34 +000027
28using std::map;
29using std::string;
30
31namespace chromeos_update_engine {
32
33class DownloadAction;
34class NoneType;
35
36template<>
37class ActionTraits<DownloadAction> {
38 public:
adlr@google.comc98a7ed2009-12-04 18:54:03 +000039 // Takes and returns an InstallPlan
40 typedef InstallPlan InputObjectType;
41 typedef InstallPlan OutputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +000042};
43
44class DownloadAction : public Action<DownloadAction>,
45 public HttpFetcherDelegate {
46 public:
47 // Takes ownership of the passed in HttpFetcher. Useful for testing.
48 // A good calling pattern is:
adlr@google.comc98a7ed2009-12-04 18:54:03 +000049 // DownloadAction(new WhateverHttpFetcher);
50 DownloadAction(HttpFetcher* http_fetcher);
rspangler@google.com49fdf182009-10-10 00:57:34 +000051 virtual ~DownloadAction();
52 typedef ActionTraits<DownloadAction>::InputObjectType InputObjectType;
53 typedef ActionTraits<DownloadAction>::OutputObjectType OutputObjectType;
54 void PerformAction();
55 void TerminateProcessing();
56
57 // Debugging/logging
adlr@google.comc98a7ed2009-12-04 18:54:03 +000058 static std::string StaticType() { return "DownloadAction"; }
59 std::string Type() const { return StaticType(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +000060
61 // Delegate methods (see http_fetcher.h)
62 virtual void ReceivedBytes(HttpFetcher *fetcher,
63 const char* bytes, int length);
64 virtual void TransferComplete(HttpFetcher *fetcher, bool successful);
65
66 private:
67 // Expected size of the file (will be used for progress info)
68 const size_t size_;
69
70 // URL to download
adlr@google.comc98a7ed2009-12-04 18:54:03 +000071 std::string url_;
rspangler@google.com49fdf182009-10-10 00:57:34 +000072
73 // Path to save URL to
adlr@google.comc98a7ed2009-12-04 18:54:03 +000074 std::string output_path_;
rspangler@google.com49fdf182009-10-10 00:57:34 +000075
76 // Expected hash of the file. The hash must match for this action to
77 // succeed.
adlr@google.comc98a7ed2009-12-04 18:54:03 +000078 std::string hash_;
rspangler@google.com49fdf182009-10-10 00:57:34 +000079
80 // Whether the caller requested that we decompress the downloaded data.
adlr@google.comc98a7ed2009-12-04 18:54:03 +000081 bool should_decompress_;
rspangler@google.com49fdf182009-10-10 00:57:34 +000082
83 // The FileWriter that downloaded data should be written to. It will
84 // either point to *decompressing_file_writer_ or *direct_file_writer_.
85 FileWriter* writer_;
86
87 // If non-null, a FileWriter used for gzip decompressing downloaded data
88 scoped_ptr<GzipDecompressingFileWriter> decompressing_file_writer_;
89
90 // Used to write out the downloaded file
91 scoped_ptr<DirectFileWriter> direct_file_writer_;
92
93 // pointer to the HttpFetcher that does the http work
94 scoped_ptr<HttpFetcher> http_fetcher_;
95
96 // Used to find the hash of the bytes downloaded
97 OmahaHashCalculator omaha_hash_calculator_;
98 DISALLOW_COPY_AND_ASSIGN(DownloadAction);
99};
100
101// We want to be sure that we're compiled with large file support on linux,
102// just in case we find ourselves downloading large images.
103COMPILE_ASSERT(8 == sizeof(off_t), off_t_not_64_bit);
104
105} // namespace chromeos_update_engine
106
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000107#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_DOWNLOAD_ACTION_H__