Save errno for logging before potentially overwriting it.
[chromium-blink-merge.git] / content / browser / download / save_file_resource_handler.cc
blob91bd55cb63cc90d1f81d958606cb92b7e61729f7
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.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"
16 namespace content {
18 SaveFileResourceHandler::SaveFileResourceHandler(int render_process_host_id,
19 int render_view_id,
20 const GURL& url,
21 SaveFileManager* manager)
22 : save_id_(-1),
23 render_process_id_(render_process_host_id),
24 render_view_id_(render_view_id),
25 url_(url),
26 content_length_(0),
27 save_manager_(manager) {
30 SaveFileResourceHandler::~SaveFileResourceHandler() {
33 bool SaveFileResourceHandler::OnUploadProgress(int request_id,
34 uint64 position,
35 uint64 size) {
36 return true;
39 bool SaveFileResourceHandler::OnRequestRedirected(
40 int request_id,
41 const GURL& url,
42 ResourceResponse* response,
43 bool* defer) {
44 final_url_ = url;
45 return true;
48 bool SaveFileResourceHandler::OnResponseStarted(
49 int request_id,
50 ResourceResponse* response,
51 bool* defer) {
52 save_id_ = save_manager_->GetNextId();
53 // |save_manager_| consumes (deletes):
54 SaveFileCreateInfo* info = new SaveFileCreateInfo;
55 info->url = url_;
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));
67 return true;
70 bool SaveFileResourceHandler::OnWillStart(int request_id,
71 const GURL& url,
72 bool* defer) {
73 return true;
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();
84 return true;
87 bool SaveFileResourceHandler::OnReadCompleted(int request_id, int bytes_read,
88 bool* defer) {
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));
97 return true;
100 bool SaveFileResourceHandler::OnResponseCompleted(
101 int request_id,
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()));
108 read_buffer_ = NULL;
109 return true;
112 void SaveFileResourceHandler::OnDataDownloaded(
113 int request_id,
114 int bytes_downloaded) {
115 NOTREACHED();
118 void SaveFileResourceHandler::set_content_length(
119 const std::string& content_length) {
120 base::StringToInt64(content_length, &content_length_);
123 } // namespace content