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 #include "content/browser/download/save_file_resource_handler.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "content/browser/download/save_file_manager.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "net/base/io_buffer.h"
14 #include "net/url_request/url_request_status.h"
18 SaveFileResourceHandler::SaveFileResourceHandler(int render_process_host_id
,
21 SaveFileManager
* manager
)
22 : ResourceHandler(NULL
),
24 render_process_id_(render_process_host_id
),
25 render_view_id_(render_view_id
),
28 save_manager_(manager
) {
31 SaveFileResourceHandler::~SaveFileResourceHandler() {
34 bool SaveFileResourceHandler::OnUploadProgress(int request_id
,
40 bool SaveFileResourceHandler::OnRequestRedirected(
43 ResourceResponse
* response
,
49 bool SaveFileResourceHandler::OnResponseStarted(
51 ResourceResponse
* response
,
53 save_id_
= save_manager_
->GetNextId();
54 // |save_manager_| consumes (deletes):
55 SaveFileCreateInfo
* info
= new SaveFileCreateInfo
;
57 info
->final_url
= final_url_
;
58 info
->total_bytes
= content_length_
;
59 info
->save_id
= save_id_
;
60 info
->render_process_id
= render_process_id_
;
61 info
->render_view_id
= render_view_id_
;
62 info
->request_id
= request_id
;
63 info
->content_disposition
= content_disposition_
;
64 info
->save_source
= SaveFileCreateInfo::SAVE_FILE_FROM_NET
;
65 BrowserThread::PostTask(
66 BrowserThread::FILE, FROM_HERE
,
67 base::Bind(&SaveFileManager::StartSave
, save_manager_
, info
));
71 bool SaveFileResourceHandler::OnWillStart(int request_id
,
77 bool SaveFileResourceHandler::OnBeforeNetworkStart(int request_id
,
83 bool SaveFileResourceHandler::OnWillRead(int request_id
,
84 scoped_refptr
<net::IOBuffer
>* buf
,
87 DCHECK(buf
&& buf_size
);
88 if (!read_buffer_
.get()) {
89 *buf_size
= min_size
< 0 ? kReadBufSize
: min_size
;
90 read_buffer_
= new net::IOBuffer(*buf_size
);
92 *buf
= read_buffer_
.get();
96 bool SaveFileResourceHandler::OnReadCompleted(int request_id
, int bytes_read
,
98 DCHECK(read_buffer_
.get());
99 // We are passing ownership of this buffer to the save file manager.
100 scoped_refptr
<net::IOBuffer
> buffer
;
101 read_buffer_
.swap(buffer
);
102 BrowserThread::PostTask(
103 BrowserThread::FILE, FROM_HERE
,
104 base::Bind(&SaveFileManager::UpdateSaveProgress
,
105 save_manager_
, save_id_
, buffer
, bytes_read
));
109 void SaveFileResourceHandler::OnResponseCompleted(
111 const net::URLRequestStatus
& status
,
112 const std::string
& security_info
,
114 BrowserThread::PostTask(
115 BrowserThread::FILE, FROM_HERE
,
116 base::Bind(&SaveFileManager::SaveFinished
, save_manager_
, save_id_
, url_
,
117 render_process_id_
, status
.is_success() && !status
.is_io_pending()));
121 void SaveFileResourceHandler::OnDataDownloaded(
123 int bytes_downloaded
) {
127 void SaveFileResourceHandler::set_content_length(
128 const std::string
& content_length
) {
129 base::StringToInt64(content_length
, &content_length_
);
132 } // namespace content