Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / download / save_file_resource_handler.cc
blob17c29ed6593ae2be7afa0a1106f67229dc23cd8e
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"
7 #include "base/bind.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"
17 namespace content {
19 SaveFileResourceHandler::SaveFileResourceHandler(net::URLRequest* request,
20 int render_process_host_id,
21 int render_view_id,
22 const GURL& url,
23 SaveFileManager* manager)
24 : ResourceHandler(request),
25 save_id_(-1),
26 render_process_id_(render_process_host_id),
27 render_view_id_(render_view_id),
28 url_(url),
29 content_length_(0),
30 save_manager_(manager) {
33 SaveFileResourceHandler::~SaveFileResourceHandler() {
36 bool SaveFileResourceHandler::OnUploadProgress(uint64 position, uint64 size) {
37 return true;
40 bool SaveFileResourceHandler::OnRequestRedirected(
41 const net::RedirectInfo& redirect_info,
42 ResourceResponse* response,
43 bool* defer) {
44 final_url_ = redirect_info.new_url;
45 return true;
48 bool SaveFileResourceHandler::OnResponseStarted(ResourceResponse* response,
49 bool* defer) {
50 save_id_ = save_manager_->GetNextId();
51 // |save_manager_| consumes (deletes):
52 SaveFileCreateInfo* info = new SaveFileCreateInfo;
53 info->url = url_;
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));
65 return true;
68 bool SaveFileResourceHandler::OnWillStart(const GURL& url, bool* defer) {
69 return true;
72 bool SaveFileResourceHandler::OnBeforeNetworkStart(const GURL& url,
73 bool* defer) {
74 return true;
77 bool SaveFileResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf,
78 int* buf_size,
79 int min_size) {
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();
86 return true;
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));
98 return true;
101 void SaveFileResourceHandler::OnResponseCompleted(
102 const net::URLRequestStatus& status,
103 const std::string& security_info,
104 bool* defer) {
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()));
109 read_buffer_ = NULL;
112 void SaveFileResourceHandler::OnDataDownloaded(int bytes_downloaded) {
113 NOTREACHED();
116 void SaveFileResourceHandler::set_content_length(
117 const std::string& content_length) {
118 base::StringToInt64(content_length, &content_length_);
121 } // namespace content