1 // Copyright (c) 2013 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 "net/url_request/url_fetcher_response_writer.h"
7 #include "base/file_util.h"
8 #include "base/location.h"
9 #include "base/sequenced_task_runner.h"
10 #include "base/task_runner_util.h"
11 #include "net/base/file_stream.h"
12 #include "net/base/io_buffer.h"
13 #include "net/base/net_errors.h"
17 URLFetcherStringWriter
* URLFetcherResponseWriter::AsStringWriter() {
21 URLFetcherFileWriter
* URLFetcherResponseWriter::AsFileWriter() {
25 URLFetcherStringWriter::URLFetcherStringWriter() {
28 URLFetcherStringWriter::~URLFetcherStringWriter() {
31 int URLFetcherStringWriter::Initialize(const CompletionCallback
& callback
) {
36 int URLFetcherStringWriter::Write(IOBuffer
* buffer
,
38 const CompletionCallback
& callback
) {
39 data_
.append(buffer
->data(), num_bytes
);
43 int URLFetcherStringWriter::Finish(const CompletionCallback
& callback
) {
48 URLFetcherStringWriter
* URLFetcherStringWriter::AsStringWriter() {
52 URLFetcherFileWriter::URLFetcherFileWriter(
53 scoped_refptr
<base::SequencedTaskRunner
> file_task_runner
,
54 const base::FilePath
& file_path
)
55 : file_task_runner_(file_task_runner
),
56 file_path_(file_path
),
59 DCHECK(file_task_runner_
.get());
62 URLFetcherFileWriter::~URLFetcherFileWriter() {
66 int URLFetcherFileWriter::Initialize(const CompletionCallback
& callback
) {
67 file_stream_
.reset(new FileStream(file_task_runner_
));
69 int result
= ERR_IO_PENDING
;
70 if (file_path_
.empty()) {
71 base::FilePath
* temp_file_path
= new base::FilePath
;
72 base::PostTaskAndReplyWithResult(
73 file_task_runner_
.get(),
75 base::Bind(&base::CreateTemporaryFile
, temp_file_path
),
76 base::Bind(&URLFetcherFileWriter::DidCreateTempFile
,
77 weak_factory_
.GetWeakPtr(),
79 base::Owned(temp_file_path
)));
81 result
= file_stream_
->Open(
83 base::File::FLAG_WRITE
| base::File::FLAG_ASYNC
|
84 base::File::FLAG_CREATE_ALWAYS
,
85 base::Bind(&URLFetcherFileWriter::DidOpenFile
,
86 weak_factory_
.GetWeakPtr(),
88 DCHECK_NE(OK
, result
);
93 int URLFetcherFileWriter::Write(IOBuffer
* buffer
,
95 const CompletionCallback
& callback
) {
99 int result
= file_stream_
->Write(buffer
, num_bytes
,
100 base::Bind(&URLFetcherFileWriter::DidWrite
,
101 weak_factory_
.GetWeakPtr(),
103 if (result
< 0 && result
!= ERR_IO_PENDING
)
104 CloseAndDeleteFile();
109 int URLFetcherFileWriter::Finish(const CompletionCallback
& callback
) {
110 // If the file_stream_ still exists at this point, close it.
112 int result
= file_stream_
->Close(base::Bind(
113 &URLFetcherFileWriter::CloseComplete
,
114 weak_factory_
.GetWeakPtr(), callback
));
115 if (result
!= ERR_IO_PENDING
)
116 file_stream_
.reset();
122 URLFetcherFileWriter
* URLFetcherFileWriter::AsFileWriter() {
126 void URLFetcherFileWriter::DisownFile() {
127 // Disowning is done by the delegate's OnURLFetchComplete method.
128 // The file should be closed by the time that method is called.
129 DCHECK(!file_stream_
);
134 void URLFetcherFileWriter::DidWrite(const CompletionCallback
& callback
,
137 CloseAndDeleteFile();
139 callback
.Run(result
);
142 void URLFetcherFileWriter::CloseAndDeleteFile() {
146 file_stream_
.reset();
148 file_task_runner_
->PostTask(FROM_HERE
,
149 base::Bind(base::IgnoreResult(&base::DeleteFile
),
151 false /* recursive */));
154 void URLFetcherFileWriter::DidCreateTempFile(const CompletionCallback
& callback
,
155 base::FilePath
* temp_file_path
,
158 callback
.Run(ERR_FILE_NOT_FOUND
);
161 file_path_
= *temp_file_path
;
163 const int result
= file_stream_
->Open(
165 base::File::FLAG_WRITE
| base::File::FLAG_ASYNC
|
166 base::File::FLAG_OPEN
,
167 base::Bind(&URLFetcherFileWriter::DidOpenFile
,
168 weak_factory_
.GetWeakPtr(),
170 if (result
!= ERR_IO_PENDING
)
171 DidOpenFile(callback
, result
);
174 void URLFetcherFileWriter::DidOpenFile(const CompletionCallback
& callback
,
179 CloseAndDeleteFile();
181 callback
.Run(result
);
184 void URLFetcherFileWriter::CloseComplete(const CompletionCallback
& callback
,
186 // Destroy |file_stream_| whether or not the close succeeded.
187 file_stream_
.reset();
188 callback
.Run(result
);