summaryrefslogtreecommitdiffstats
path: root/content/browser/download/download_file_impl.h
blob: 10831323c37ea82ca03654d8260e69ef4071acc2 (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
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright (c) 2012 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 CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_IMPL_H_
#define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_IMPL_H_

#include "content/browser/download/download_file.h"

#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/time.h"
#include "base/timer.h"
#include "content/browser/download/base_file.h"
#include "content/browser/download/byte_stream.h"
#include "content/browser/download/download_request_handle.h"
#include "net/base/net_log.h"

struct DownloadCreateInfo;

namespace content {
class ByteStreamReader;
class DownloadManager;
class PowerSaveBlocker;
}

class CONTENT_EXPORT DownloadFileImpl : virtual public content::DownloadFile {
 public:
  // Takes ownership of the object pointed to by |request_handle|.
  // |bound_net_log| will be used for logging the download file's events.
  DownloadFileImpl(const DownloadCreateInfo* info,
                   scoped_ptr<content::ByteStreamReader> stream,
                   DownloadRequestHandleInterface* request_handle,
                   scoped_refptr<content::DownloadManager> download_manager,
                   bool calculate_hash,
                   scoped_ptr<content::PowerSaveBlocker> power_save_blocker,
                   const net::BoundNetLog& bound_net_log);
  virtual ~DownloadFileImpl();

  // DownloadFile functions.
  virtual content::DownloadInterruptReason Initialize() OVERRIDE;
  virtual content::DownloadInterruptReason Rename(
      const FilePath& full_path) OVERRIDE;
  virtual void Detach() OVERRIDE;
  virtual void Cancel() OVERRIDE;
  virtual void AnnotateWithSourceInformation() OVERRIDE;
  virtual FilePath FullPath() const OVERRIDE;
  virtual bool InProgress() const OVERRIDE;
  virtual int64 BytesSoFar() const OVERRIDE;
  virtual int64 CurrentSpeed() const OVERRIDE;
  virtual bool GetHash(std::string* hash) OVERRIDE;
  virtual std::string GetHashState() OVERRIDE;
  virtual void CancelDownloadRequest() OVERRIDE;
  virtual int Id() const OVERRIDE;
  virtual content::DownloadManager* GetDownloadManager() OVERRIDE;
  virtual const content::DownloadId& GlobalId() const OVERRIDE;
  virtual std::string DebugString() const OVERRIDE;

 protected:
  // For test class overrides.
  virtual content::DownloadInterruptReason AppendDataToFile(
      const char* data, size_t data_len);

 private:
  // Called when there's some activity on stream_reader_ that needs to be
  // handled.
  void StreamActive();

  // Send updates on our progress.
  void SendUpdate();

  // The base file instance.
  BaseFile file_;

  // The stream through which data comes.
  // TODO(rdsmith): Move this into BaseFile; requires using the same
  // stream semantics in SavePackage.  Alternatively, replace SaveFile
  // with DownloadFile and get rid of BaseFile.
  scoped_ptr<content::ByteStreamReader> stream_reader_;

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

  // Used to trigger progress updates.
  scoped_ptr<base::RepeatingTimer<DownloadFileImpl> > update_timer_;

  // The handle to the request information.  Used for operations outside the
  // download system, specifically canceling a download.
  scoped_ptr<DownloadRequestHandleInterface> request_handle_;

  // DownloadManager this download belongs to.
  scoped_refptr<content::DownloadManager> download_manager_;

  // Statistics
  size_t bytes_seen_;
  base::TimeDelta disk_writes_time_;
  base::TimeTicks download_start_;

  net::BoundNetLog bound_net_log_;

  base::WeakPtrFactory<DownloadFileImpl> weak_factory_;

  // RAII handle to keep the system from sleeping while we're downloading.
  scoped_ptr<content::PowerSaveBlocker> power_save_blocker_;

  DISALLOW_COPY_AND_ASSIGN(DownloadFileImpl);
};

#endif  // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_IMPL_H_