summaryrefslogtreecommitdiffstats
path: root/chrome/browser/download/download_file.h
blob: 24025fd5542dceaaf9d42de354be3e36c379bd84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_FILE_H_
#define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_FILE_H_
#pragma once

#include "base/basictypes.h"
#include "base/file_path.h"
#include "base/hash_tables.h"
#include "base/linked_ptr.h"
#include "chrome/browser/download/download_types.h"
#include "chrome/browser/power_save_blocker.h"
#include "googleurl/src/gurl.h"

struct DownloadCreateInfo;
class ResourceDispatcherHost;

// These objects live exclusively on the download thread and handle the writing
// operations for one download. These objects live only for the duration that
// the download is 'in progress': once the download has been completed or
// cancelled, the DownloadFile is destroyed.
class DownloadFile {
 public:
  explicit DownloadFile(const DownloadCreateInfo* info);
  virtual ~DownloadFile();

  bool Initialize();

  // Write a new chunk of data to the file. Returns true on success (all bytes
  // written to the file).
  bool AppendDataToFile(const char* data, size_t data_len);

  // Abort the download and automatically close the file.
  void Cancel();

  // Rename the download file. Returns 'true' if the rename was successful.
  // path_renamed_ is set true only if |is_final_rename| is true.
  // Marked virtual for testing.
  virtual bool Rename(const FilePath& full_path, bool is_final_rename);

  // Indicate that the download has finished. No new data will be received.
  void Finish();

  // Informs the OS that this file came from the internet.
  void AnnotateWithSourceInformation();

  // Deletes its .crdownload intermediate file.
  // Marked virtual for testing.
  virtual void DeleteCrDownload();

  // Cancels the download request associated with this file.
  void CancelDownloadRequest(ResourceDispatcherHost* rdh);

  // Accessors.
  int id() const { return id_; }
  bool path_renamed() const { return path_renamed_; }
  bool in_progress() const { return file_stream_ != NULL; }

 private:
  // Open or Close the OS file stream. The stream is opened in the constructor
  // based on creation information passed to it, and automatically closed in
  // the destructor.
  void Close();
  bool Open();

  // OS file stream for writing
  linked_ptr<net::FileStream> file_stream_;

  // Source URL for the file being downloaded.
  GURL source_url_;

  // The URL where the download was initiated.
  GURL referrer_url_;

  // The unique identifier for this download, assigned at creation by
  // the DownloadFileManager for its internal record keeping.
  int id_;

  // IDs for looking up the tab we are associated with.
  int child_id_;

  // Handle for informing the ResourceDispatcherHost of a UI based cancel.
  int request_id_;

  // Full path to the downloaded file.
  FilePath full_path_;

  // Whether the download is still using its initial temporary path.
  bool path_renamed_;

  // RAII handle to keep the system from sleeping while we're downloading.
  PowerSaveBlocker dont_sleep_;

  DISALLOW_COPY_AND_ASSIGN(DownloadFile);
};

#endif  // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_FILE_H_