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