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.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
)
23 render_process_id_(render_process_host_id
),
24 render_view_id_(render_view_id
),
27 save_manager_(manager
) {
30 SaveFileResourceHandler::~SaveFileResourceHandler() {
33 bool SaveFileResourceHandler::OnUploadProgress(int request_id
,
39 bool SaveFileResourceHandler::OnRequestRedirected(
42 ResourceResponse
* response
,
48 bool SaveFileResourceHandler::OnResponseStarted(
50 ResourceResponse
* response
,
52 save_id_
= save_manager_
->GetNextId();
53 // |save_manager_| consumes (deletes):
54 SaveFileCreateInfo
* info
= new SaveFileCreateInfo
;
56 info
->final_url
= final_url_
;
57 info
->total_bytes
= content_length_
;
58 info
->save_id
= save_id_
;
59 info
->render_process_id
= render_process_id_
;
60 info
->render_view_id
= render_view_id_
;
61 info
->request_id
= request_id
;
62 info
->content_disposition
= content_disposition_
;
63 info
->save_source
= SaveFileCreateInfo::SAVE_FILE_FROM_NET
;
64 BrowserThread::PostTask(
65 BrowserThread::FILE, FROM_HERE
,
66 base::Bind(&SaveFileManager::StartSave
, save_manager_
, info
));
70 bool SaveFileResourceHandler::OnWillStart(int request_id
,
76 bool SaveFileResourceHandler::OnWillRead(int request_id
, net::IOBuffer
** buf
,
77 int* buf_size
, int min_size
) {
78 DCHECK(buf
&& buf_size
);
79 if (!read_buffer_
.get()) {
80 *buf_size
= min_size
< 0 ? kReadBufSize
: min_size
;
81 read_buffer_
= new net::IOBuffer(*buf_size
);
83 *buf
= read_buffer_
.get();
87 bool SaveFileResourceHandler::OnReadCompleted(int request_id
, int bytes_read
,
89 DCHECK(read_buffer_
.get());
90 // We are passing ownership of this buffer to the save file manager.
91 scoped_refptr
<net::IOBuffer
> buffer
;
92 read_buffer_
.swap(buffer
);
93 BrowserThread::PostTask(
94 BrowserThread::FILE, FROM_HERE
,
95 base::Bind(&SaveFileManager::UpdateSaveProgress
,
96 save_manager_
, save_id_
, buffer
, bytes_read
));
100 bool SaveFileResourceHandler::OnResponseCompleted(
102 const net::URLRequestStatus
& status
,
103 const std::string
& security_info
) {
104 BrowserThread::PostTask(
105 BrowserThread::FILE, FROM_HERE
,
106 base::Bind(&SaveFileManager::SaveFinished
, save_manager_
, save_id_
, url_
,
107 render_process_id_
, status
.is_success() && !status
.is_io_pending()));
112 void SaveFileResourceHandler::OnDataDownloaded(
114 int bytes_downloaded
) {
118 void SaveFileResourceHandler::set_content_length(
119 const std::string
& content_length
) {
120 base::StringToInt64(content_length
, &content_length_
);
123 } // namespace content