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/redirect_info.h"
15 #include "net/url_request/url_request_status.h"
19 SaveFileResourceHandler::SaveFileResourceHandler(net::URLRequest
* request
,
20 int render_process_host_id
,
23 SaveFileManager
* manager
)
24 : ResourceHandler(request
),
26 render_process_id_(render_process_host_id
),
27 render_view_id_(render_view_id
),
30 save_manager_(manager
) {
33 SaveFileResourceHandler::~SaveFileResourceHandler() {
36 bool SaveFileResourceHandler::OnUploadProgress(uint64 position
, uint64 size
) {
40 bool SaveFileResourceHandler::OnRequestRedirected(
41 const net::RedirectInfo
& redirect_info
,
42 ResourceResponse
* response
,
44 final_url_
= redirect_info
.new_url
;
48 bool SaveFileResourceHandler::OnResponseStarted(ResourceResponse
* response
,
50 save_id_
= save_manager_
->GetNextId();
51 // |save_manager_| consumes (deletes):
52 SaveFileCreateInfo
* info
= new SaveFileCreateInfo
;
54 info
->final_url
= final_url_
;
55 info
->total_bytes
= content_length_
;
56 info
->save_id
= save_id_
;
57 info
->render_process_id
= render_process_id_
;
58 info
->render_view_id
= render_view_id_
;
59 info
->request_id
= GetRequestID();
60 info
->content_disposition
= content_disposition_
;
61 info
->save_source
= SaveFileCreateInfo::SAVE_FILE_FROM_NET
;
62 BrowserThread::PostTask(
63 BrowserThread::FILE, FROM_HERE
,
64 base::Bind(&SaveFileManager::StartSave
, save_manager_
, info
));
68 bool SaveFileResourceHandler::OnWillStart(const GURL
& url
, bool* defer
) {
72 bool SaveFileResourceHandler::OnBeforeNetworkStart(const GURL
& url
,
77 bool SaveFileResourceHandler::OnWillRead(scoped_refptr
<net::IOBuffer
>* buf
,
80 DCHECK(buf
&& buf_size
);
81 if (!read_buffer_
.get()) {
82 *buf_size
= min_size
< 0 ? kReadBufSize
: min_size
;
83 read_buffer_
= new net::IOBuffer(*buf_size
);
85 *buf
= read_buffer_
.get();
89 bool SaveFileResourceHandler::OnReadCompleted(int bytes_read
, bool* defer
) {
90 DCHECK(read_buffer_
.get());
91 // We are passing ownership of this buffer to the save file manager.
92 scoped_refptr
<net::IOBuffer
> buffer
;
93 read_buffer_
.swap(buffer
);
94 BrowserThread::PostTask(
95 BrowserThread::FILE, FROM_HERE
,
96 base::Bind(&SaveFileManager::UpdateSaveProgress
,
97 save_manager_
, save_id_
, buffer
, bytes_read
));
101 void SaveFileResourceHandler::OnResponseCompleted(
102 const net::URLRequestStatus
& status
,
103 const std::string
& security_info
,
105 BrowserThread::PostTask(
106 BrowserThread::FILE, FROM_HERE
,
107 base::Bind(&SaveFileManager::SaveFinished
, save_manager_
, save_id_
, url_
,
108 render_process_id_
, status
.is_success() && !status
.is_io_pending()));
112 void SaveFileResourceHandler::OnDataDownloaded(int bytes_downloaded
) {
116 void SaveFileResourceHandler::set_content_length(
117 const std::string
& content_length
) {
118 base::StringToInt64(content_length
, &content_length_
);
121 } // namespace content