Remove PlatformFile from profile_browsertest
[chromium-blink-merge.git] / content / browser / download / save_file_resource_handler.cc
blob18abc7dfd79f6d62dec0e21a26ac48e755f11b5c
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/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 : ResourceHandler(NULL),
23 save_id_(-1),
24 render_process_id_(render_process_host_id),
25 render_view_id_(render_view_id),
26 url_(url),
27 content_length_(0),
28 save_manager_(manager) {
31 SaveFileResourceHandler::~SaveFileResourceHandler() {
34 bool SaveFileResourceHandler::OnUploadProgress(int request_id,
35 uint64 position,
36 uint64 size) {
37 return true;
40 bool SaveFileResourceHandler::OnRequestRedirected(
41 int request_id,
42 const GURL& url,
43 ResourceResponse* response,
44 bool* defer) {
45 final_url_ = url;
46 return true;
49 bool SaveFileResourceHandler::OnResponseStarted(
50 int request_id,
51 ResourceResponse* response,
52 bool* defer) {
53 save_id_ = save_manager_->GetNextId();
54 // |save_manager_| consumes (deletes):
55 SaveFileCreateInfo* info = new SaveFileCreateInfo;
56 info->url = url_;
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));
68 return true;
71 bool SaveFileResourceHandler::OnWillStart(int request_id,
72 const GURL& url,
73 bool* defer) {
74 return true;
77 bool SaveFileResourceHandler::OnBeforeNetworkStart(int request_id,
78 const GURL& url,
79 bool* defer) {
80 return true;
83 bool SaveFileResourceHandler::OnWillRead(int request_id,
84 scoped_refptr<net::IOBuffer>* buf,
85 int* buf_size,
86 int min_size) {
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();
93 return true;
96 bool SaveFileResourceHandler::OnReadCompleted(int request_id, int bytes_read,
97 bool* defer) {
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));
106 return true;
109 void SaveFileResourceHandler::OnResponseCompleted(
110 int request_id,
111 const net::URLRequestStatus& status,
112 const std::string& security_info,
113 bool* defer) {
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()));
118 read_buffer_ = NULL;
121 void SaveFileResourceHandler::OnDataDownloaded(
122 int request_id,
123 int bytes_downloaded) {
124 NOTREACHED();
127 void SaveFileResourceHandler::set_content_length(
128 const std::string& content_length) {
129 base::StringToInt64(content_length, &content_length_);
132 } // namespace content