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.
5 #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_RESOURCE_HANDLER_H_
6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_RESOURCE_HANDLER_H_
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"
26 class ByteStreamWriter
;
27 class ByteStreamReader
;
28 class DownloadRequestHandle
;
29 struct DownloadCreateInfo
;
31 // Forwards data to the download thread.
32 class CONTENT_EXPORT DownloadResourceHandler
33 : public ResourceHandler
,
34 public base::SupportsWeakPtr
<DownloadResourceHandler
> {
36 // Size of the buffer used between the DownloadResourceHandler and the
37 // downstream receiver of its output.
38 static const int kDownloadByteStreamSize
;
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(
44 net::URLRequest
* request
,
45 const DownloadUrlParameters::OnStartedCallback
& started_cb
,
46 scoped_ptr
<DownloadSaveInfo
> save_info
);
48 virtual bool OnUploadProgress(int request_id
,
50 uint64 size
) OVERRIDE
;
52 virtual bool OnRequestRedirected(int request_id
,
54 ResourceResponse
* response
,
55 bool* defer
) OVERRIDE
;
57 // Send the download creation information to the download thread.
58 virtual bool OnResponseStarted(int request_id
,
59 ResourceResponse
* response
,
60 bool* defer
) OVERRIDE
;
62 // Pass-through implementation.
63 virtual bool OnWillStart(int request_id
,
65 bool* defer
) OVERRIDE
;
67 // Pass-through implementation.
68 virtual bool OnBeforeNetworkStart(int request_id
,
70 bool* defer
) OVERRIDE
;
72 // Create a new buffer, which will be handed to the download thread for file
73 // writing and deletion.
74 virtual bool OnWillRead(int request_id
,
75 scoped_refptr
<net::IOBuffer
>* buf
,
77 int min_size
) OVERRIDE
;
79 virtual bool OnReadCompleted(int request_id
, int bytes_read
,
80 bool* defer
) OVERRIDE
;
82 virtual void OnResponseCompleted(int request_id
,
83 const net::URLRequestStatus
& status
,
84 const std::string
& security_info
,
85 bool* defer
) OVERRIDE
;
87 // N/A to this flavor of DownloadHandler.
88 virtual void OnDataDownloaded(int request_id
, int bytes_downloaded
) OVERRIDE
;
94 std::string
DebugString() const;
97 virtual ~DownloadResourceHandler();
99 // Arrange for started_cb_ to be called on the UI thread with the
100 // below values, nulling out started_cb_. Should only be called
102 void CallStartedCB(DownloadItem
* item
, net::Error error
);
105 // This is read only on the IO thread, but may only
106 // be called on the UI thread.
107 DownloadUrlParameters::OnStartedCallback started_cb_
;
108 scoped_ptr
<DownloadSaveInfo
> save_info_
;
111 scoped_refptr
<net::IOBuffer
> read_buffer_
; // From URLRequest.
112 scoped_ptr
<ByteStreamWriter
> stream_writer_
; // To rest of system.
114 // The following are used to collect stats.
115 base::TimeTicks download_start_time_
;
116 base::TimeTicks last_read_time_
;
117 base::TimeTicks last_stream_pause_time_
;
118 base::TimeDelta total_pause_time_
;
119 size_t last_buffer_size_
;
126 bool on_response_started_called_
;
128 static const int kReadBufSize
= 32768; // bytes
129 static const int kThrottleTimeMs
= 200; // milliseconds
131 DISALLOW_COPY_AND_ASSIGN(DownloadResourceHandler
);
134 } // namespace content
136 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_RESOURCE_HANDLER_H_