1// Copyright (c) 2012 The Chromium 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 CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_RESOURCE_HANDLER_H_
6#define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_RESOURCE_HANDLER_H_
7
8#include <string>
9
10#include "base/callback.h"
11#include "base/memory/scoped_ptr.h"
12#include "base/timer/timer.h"
13#include "content/browser/loader/resource_handler.h"
14#include "content/public/browser/download_manager.h"
15#include "content/public/browser/download_save_info.h"
16#include "content/public/browser/download_url_parameters.h"
17#include "content/public/browser/global_request_id.h"
18#include "net/base/net_errors.h"
19
20
21namespace net {
22class URLRequest;
23}  // namespace net
24
25namespace content {
26class ByteStreamWriter;
27class ByteStreamReader;
28class DownloadRequestHandle;
29struct DownloadCreateInfo;
30
31// Forwards data to the download thread.
32class CONTENT_EXPORT DownloadResourceHandler
33    : public ResourceHandler,
34      public base::SupportsWeakPtr<DownloadResourceHandler> {
35 public:
36  // Size of the buffer used between the DownloadResourceHandler and the
37  // downstream receiver of its output.
38  static const int kDownloadByteStreamSize;
39
40  // started_cb will be called exactly once on the UI thread.
41  // |id| should be invalid if the id should be automatically assigned.
42  DownloadResourceHandler(
43      uint32 id,
44      net::URLRequest* request,
45      const DownloadUrlParameters::OnStartedCallback& started_cb,
46      scoped_ptr<DownloadSaveInfo> save_info);
47
48  virtual bool OnUploadProgress(int request_id,
49                                uint64 position,
50                                uint64 size) OVERRIDE;
51
52  virtual bool OnRequestRedirected(int request_id,
53                                   const GURL& url,
54                                   ResourceResponse* response,
55                                   bool* defer) OVERRIDE;
56
57  // Send the download creation information to the download thread.
58  virtual bool OnResponseStarted(int request_id,
59                                 ResourceResponse* response,
60                                 bool* defer) OVERRIDE;
61
62  // Pass-through implementation.
63  virtual bool OnWillStart(int request_id,
64                           const GURL& url,
65                           bool* defer) OVERRIDE;
66
67  // Create a new buffer, which will be handed to the download thread for file
68  // writing and deletion.
69  virtual bool OnWillRead(int request_id,
70                          scoped_refptr<net::IOBuffer>* buf,
71                          int* buf_size,
72                          int min_size) OVERRIDE;
73
74  virtual bool OnReadCompleted(int request_id, int bytes_read,
75                               bool* defer) OVERRIDE;
76
77  virtual void OnResponseCompleted(int request_id,
78                                   const net::URLRequestStatus& status,
79                                   const std::string& security_info,
80                                   bool* defer) OVERRIDE;
81
82  // N/A to this flavor of DownloadHandler.
83  virtual void OnDataDownloaded(int request_id, int bytes_downloaded) OVERRIDE;
84
85  void PauseRequest();
86  void ResumeRequest();
87  void CancelRequest();
88
89  std::string DebugString() const;
90
91 private:
92  virtual ~DownloadResourceHandler();
93
94  // Arrange for started_cb_ to be called on the UI thread with the
95  // below values, nulling out started_cb_.  Should only be called
96  // on the IO thread.
97  void CallStartedCB(DownloadItem* item, net::Error error);
98
99  uint32 download_id_;
100  // This is read only on the IO thread, but may only
101  // be called on the UI thread.
102  DownloadUrlParameters::OnStartedCallback started_cb_;
103  scoped_ptr<DownloadSaveInfo> save_info_;
104
105  // Data flow
106  scoped_refptr<net::IOBuffer> read_buffer_;       // From URLRequest.
107  scoped_ptr<ByteStreamWriter> stream_writer_; // To rest of system.
108
109  // The following are used to collect stats.
110  base::TimeTicks download_start_time_;
111  base::TimeTicks last_read_time_;
112  base::TimeTicks last_stream_pause_time_;
113  base::TimeDelta total_pause_time_;
114  size_t last_buffer_size_;
115  int64 bytes_read_;
116
117  int pause_count_;
118  bool was_deferred_;
119
120  // For DCHECKing
121  bool on_response_started_called_;
122
123  static const int kReadBufSize = 32768;  // bytes
124  static const int kThrottleTimeMs = 200;  // milliseconds
125
126  DISALLOW_COPY_AND_ASSIGN(DownloadResourceHandler);
127};
128
129}  // namespace content
130
131#endif  // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_RESOURCE_HANDLER_H_
132