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 "content/browser/loader/resource_handler.h"
13 #include "content/public/browser/download_interrupt_reasons.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"
23 class ByteStreamReader
;
24 class ByteStreamWriter
;
25 class DownloadRequestHandle
;
26 class PowerSaveBlocker
;
27 struct DownloadCreateInfo
;
29 // Forwards data to the download thread.
30 class CONTENT_EXPORT DownloadResourceHandler
31 : public ResourceHandler
,
32 public base::SupportsWeakPtr
<DownloadResourceHandler
> {
34 struct DownloadTabInfo
;
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 bool OnUploadProgress(uint64 position
, uint64 size
) override
;
50 bool OnRequestRedirected(const net::RedirectInfo
& redirect_info
,
51 ResourceResponse
* response
,
52 bool* defer
) override
;
54 // Send the download creation information to the download thread.
55 bool OnResponseStarted(ResourceResponse
* response
, bool* defer
) override
;
57 // Pass-through implementation.
58 bool OnWillStart(const GURL
& url
, bool* defer
) override
;
60 // Pass-through implementation.
61 bool OnBeforeNetworkStart(const GURL
& url
, bool* defer
) override
;
63 // Create a new buffer, which will be handed to the download thread for file
64 // writing and deletion.
65 bool OnWillRead(scoped_refptr
<net::IOBuffer
>* buf
,
67 int min_size
) override
;
69 bool OnReadCompleted(int bytes_read
, bool* defer
) override
;
71 void OnResponseCompleted(const net::URLRequestStatus
& status
,
72 const std::string
& security_info
,
73 bool* defer
) override
;
75 // N/A to this flavor of DownloadHandler.
76 void OnDataDownloaded(int bytes_downloaded
) override
;
81 // May result in this object being deleted by its owner.
84 std::string
DebugString() const;
87 ~DownloadResourceHandler() override
;
89 // Arrange for started_cb_ to be called on the UI thread with the
90 // below values, nulling out started_cb_. Should only be called
92 void CallStartedCB(DownloadItem
* item
,
93 DownloadInterruptReason interrupt_reason
);
96 // This is read only on the IO thread, but may only
97 // be called on the UI thread.
98 DownloadUrlParameters::OnStartedCallback started_cb_
;
99 scoped_ptr
<DownloadSaveInfo
> save_info_
;
101 // Stores information about the download that must be acquired on the UI
102 // thread before StartOnUIThread is called.
103 // Created on IO thread, but only accessed on UI thread. |tab_info_| holds
104 // the pointer until we pass it off to StartOnUIThread or DeleteSoon.
105 DownloadTabInfo
* tab_info_
;
108 scoped_refptr
<net::IOBuffer
> read_buffer_
; // From URLRequest.
109 scoped_ptr
<ByteStreamWriter
> stream_writer_
; // To rest of system.
111 // Keeps the system from sleeping while this ResourceHandler is alive. If the
112 // system enters power saving mode while a request is alive, it can cause the
113 // request to fail and the associated download will be interrupted.
114 scoped_ptr
<PowerSaveBlocker
> power_save_blocker_
;
116 // The following are used to collect stats.
117 base::TimeTicks download_start_time_
;
118 base::TimeTicks last_read_time_
;
119 base::TimeTicks last_stream_pause_time_
;
120 base::TimeDelta total_pause_time_
;
121 size_t last_buffer_size_
;
128 bool on_response_started_called_
;
130 static const int kReadBufSize
= 32768; // bytes
131 static const int kThrottleTimeMs
= 200; // milliseconds
133 DISALLOW_COPY_AND_ASSIGN(DownloadResourceHandler
);
136 } // namespace content
138 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_RESOURCE_HANDLER_H_