blob: 9abbdcf9cc04f53e239ce0c3df6bddfdf8133923 [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
5#ifndef UPDATE_ENGINE_DOWNLOAD_ACTION_H__
6#define UPDATE_ENGINE_DOWNLOAD_ACTION_H__
7
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"
22#include "update_engine/omaha_hash_calculator.h"
23
24// The Download Action downloads a requested url to a specified path on disk.
25// It takes no input object, but if successful, passes the output path
26// to the output pipe.
27
28using std::map;
29using std::string;
30
31namespace chromeos_update_engine {
32
33class DownloadAction;
34class NoneType;
35
36template<>
37class ActionTraits<DownloadAction> {
38 public:
39 // Does not take an object for input
40 typedef NoneType InputObjectType;
41 // On success, puts the output path on output
42 typedef std::string OutputObjectType;
43};
44
45class DownloadAction : public Action<DownloadAction>,
46 public HttpFetcherDelegate {
47 public:
48 // Takes ownership of the passed in HttpFetcher. Useful for testing.
49 // A good calling pattern is:
50 // DownloadAction(..., new WhateverHttpFetcher);
51 DownloadAction(const std::string& url, const std::string& output_path,
52 off_t size, const std::string& hash,
53 const bool should_decompress,
54 HttpFetcher* http_fetcher);
55 virtual ~DownloadAction();
56 typedef ActionTraits<DownloadAction>::InputObjectType InputObjectType;
57 typedef ActionTraits<DownloadAction>::OutputObjectType OutputObjectType;
58 void PerformAction();
59 void TerminateProcessing();
60
61 // Debugging/logging
62 std::string Type() const { return "DownloadAction"; }
63
64 // Delegate methods (see http_fetcher.h)
65 virtual void ReceivedBytes(HttpFetcher *fetcher,
66 const char* bytes, int length);
67 virtual void TransferComplete(HttpFetcher *fetcher, bool successful);
68
69 private:
70 // Expected size of the file (will be used for progress info)
71 const size_t size_;
72
73 // URL to download
74 const std::string url_;
75
76 // Path to save URL to
77 const std::string output_path_;
78
79 // Expected hash of the file. The hash must match for this action to
80 // succeed.
81 const std::string hash_;
82
83 // Whether the caller requested that we decompress the downloaded data.
84 const bool should_decompress_;
85
86 // The FileWriter that downloaded data should be written to. It will
87 // either point to *decompressing_file_writer_ or *direct_file_writer_.
88 FileWriter* writer_;
89
90 // If non-null, a FileWriter used for gzip decompressing downloaded data
91 scoped_ptr<GzipDecompressingFileWriter> decompressing_file_writer_;
92
93 // Used to write out the downloaded file
94 scoped_ptr<DirectFileWriter> direct_file_writer_;
95
96 // pointer to the HttpFetcher that does the http work
97 scoped_ptr<HttpFetcher> http_fetcher_;
98
99 // Used to find the hash of the bytes downloaded
100 OmahaHashCalculator omaha_hash_calculator_;
101 DISALLOW_COPY_AND_ASSIGN(DownloadAction);
102};
103
104// We want to be sure that we're compiled with large file support on linux,
105// just in case we find ourselves downloading large images.
106COMPILE_ASSERT(8 == sizeof(off_t), off_t_not_64_bit);
107
108} // namespace chromeos_update_engine
109
110#endif // UPDATE_ENGINE_DOWNLOAD_ACTION_H__