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 "storage/browser/fileapi/file_system_url_request_job.h"
10 #include "base/compiler_specific.h"
11 #include "base/files/file_path.h"
12 #include "base/files/file_util_proxy.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/threading/thread_restrictions.h"
15 #include "base/time/time.h"
16 #include "build/build_config.h"
17 #include "net/base/file_stream.h"
18 #include "net/base/io_buffer.h"
19 #include "net/base/mime_util.h"
20 #include "net/base/net_errors.h"
21 #include "net/base/net_util.h"
22 #include "net/http/http_response_headers.h"
23 #include "net/http/http_response_info.h"
24 #include "net/http/http_util.h"
25 #include "net/url_request/url_request.h"
26 #include "storage/browser/fileapi/file_stream_reader.h"
27 #include "storage/browser/fileapi/file_system_context.h"
28 #include "storage/browser/fileapi/file_system_operation_runner.h"
29 #include "storage/common/fileapi/file_system_util.h"
32 using net::NetworkDelegate
;
33 using net::URLRequest
;
34 using net::URLRequestJob
;
35 using net::URLRequestStatus
;
39 static net::HttpResponseHeaders
* CreateHttpResponseHeaders() {
40 // HttpResponseHeaders expects its input string to be terminated by two NULs.
41 static const char kStatus
[] = "HTTP/1.1 200 OK\0";
42 static const size_t kStatusLen
= arraysize(kStatus
);
44 net::HttpResponseHeaders
* headers
=
45 new net::HttpResponseHeaders(std::string(kStatus
, kStatusLen
));
47 // Tell WebKit never to cache this content.
48 std::string
cache_control(net::HttpRequestHeaders::kCacheControl
);
49 cache_control
.append(": no-cache");
50 headers
->AddHeader(cache_control
);
55 FileSystemURLRequestJob::FileSystemURLRequestJob(
57 NetworkDelegate
* network_delegate
,
58 const std::string
& storage_domain
,
59 FileSystemContext
* file_system_context
)
60 : URLRequestJob(request
, network_delegate
),
61 storage_domain_(storage_domain
),
62 file_system_context_(file_system_context
),
68 FileSystemURLRequestJob::~FileSystemURLRequestJob() {}
70 void FileSystemURLRequestJob::Start() {
71 base::MessageLoop::current()->PostTask(
73 base::Bind(&FileSystemURLRequestJob::StartAsync
,
74 weak_factory_
.GetWeakPtr()));
77 void FileSystemURLRequestJob::Kill() {
79 URLRequestJob::Kill();
80 weak_factory_
.InvalidateWeakPtrs();
83 bool FileSystemURLRequestJob::ReadRawData(net::IOBuffer
* dest
,
86 DCHECK_NE(dest_size
, 0);
88 DCHECK_GE(remaining_bytes_
, 0);
90 if (reader_
.get() == NULL
)
93 if (remaining_bytes_
< dest_size
)
94 dest_size
= static_cast<int>(remaining_bytes_
);
101 const int rv
= reader_
->Read(dest
, dest_size
,
102 base::Bind(&FileSystemURLRequestJob::DidRead
,
103 weak_factory_
.GetWeakPtr()));
105 // Data is immediately available.
107 remaining_bytes_
-= rv
;
108 DCHECK_GE(remaining_bytes_
, 0);
111 if (rv
== net::ERR_IO_PENDING
)
112 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING
, 0));
118 bool FileSystemURLRequestJob::GetMimeType(std::string
* mime_type
) const {
120 DCHECK(url_
.is_valid());
121 base::FilePath::StringType extension
= url_
.path().Extension();
122 if (!extension
.empty())
123 extension
= extension
.substr(1);
124 return net::GetWellKnownMimeTypeFromExtension(extension
, mime_type
);
127 void FileSystemURLRequestJob::SetExtraRequestHeaders(
128 const net::HttpRequestHeaders
& headers
) {
129 std::string range_header
;
130 if (headers
.GetHeader(net::HttpRequestHeaders::kRange
, &range_header
)) {
131 std::vector
<net::HttpByteRange
> ranges
;
132 if (net::HttpUtil::ParseRangeHeader(range_header
, &ranges
)) {
133 if (ranges
.size() == 1) {
134 byte_range_
= ranges
[0];
136 // We don't support multiple range requests in one single URL request.
137 // TODO(adamk): decide whether we want to support multiple range
139 NotifyFailed(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE
);
145 void FileSystemURLRequestJob::GetResponseInfo(net::HttpResponseInfo
* info
) {
147 *info
= *response_info_
;
150 int FileSystemURLRequestJob::GetResponseCode() const {
153 return URLRequestJob::GetResponseCode();
156 void FileSystemURLRequestJob::StartAsync() {
159 DCHECK(!reader_
.get());
160 url_
= file_system_context_
->CrackURL(request_
->url());
161 if (!url_
.is_valid()) {
162 file_system_context_
->AttemptAutoMountForURLRequest(
165 base::Bind(&FileSystemURLRequestJob::DidAttemptAutoMount
,
166 weak_factory_
.GetWeakPtr()));
169 if (!file_system_context_
->CanServeURLRequest(url_
)) {
170 // In incognito mode the API is not usable and there should be no data.
171 NotifyFailed(net::ERR_FILE_NOT_FOUND
);
174 file_system_context_
->operation_runner()->GetMetadata(
176 base::Bind(&FileSystemURLRequestJob::DidGetMetadata
,
177 weak_factory_
.GetWeakPtr()));
180 void FileSystemURLRequestJob::DidAttemptAutoMount(base::File::Error result
) {
182 file_system_context_
->CrackURL(request_
->url()).is_valid()) {
185 NotifyFailed(net::ERR_FILE_NOT_FOUND
);
189 void FileSystemURLRequestJob::DidGetMetadata(
190 base::File::Error error_code
,
191 const base::File::Info
& file_info
) {
192 if (error_code
!= base::File::FILE_OK
) {
193 NotifyFailed(error_code
== base::File::FILE_ERROR_INVALID_URL
?
194 net::ERR_INVALID_URL
: net::ERR_FILE_NOT_FOUND
);
198 // We may have been orphaned...
202 is_directory_
= file_info
.is_directory
;
204 if (!byte_range_
.ComputeBounds(file_info
.size
)) {
205 NotifyFailed(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE
);
210 NotifyHeadersComplete();
214 remaining_bytes_
= byte_range_
.last_byte_position() -
215 byte_range_
.first_byte_position() + 1;
216 DCHECK_GE(remaining_bytes_
, 0);
218 DCHECK(!reader_
.get());
219 reader_
= file_system_context_
->CreateFileStreamReader(
220 url_
, byte_range_
.first_byte_position(), remaining_bytes_
, base::Time());
222 set_expected_content_size(remaining_bytes_
);
223 response_info_
.reset(new net::HttpResponseInfo());
224 response_info_
->headers
= CreateHttpResponseHeaders();
225 NotifyHeadersComplete();
228 void FileSystemURLRequestJob::DidRead(int result
) {
230 SetStatus(URLRequestStatus()); // Clear the IO_PENDING status
231 else if (result
== 0)
232 NotifyDone(URLRequestStatus());
234 NotifyFailed(result
);
236 remaining_bytes_
-= result
;
237 DCHECK_GE(remaining_bytes_
, 0);
239 NotifyReadComplete(result
);
242 bool FileSystemURLRequestJob::IsRedirectResponse(GURL
* location
,
243 int* http_status_code
) {
245 // This happens when we discovered the file is a directory, so needs a
246 // slash at the end of the path.
247 std::string new_path
= request_
->url().path();
248 new_path
.push_back('/');
249 GURL::Replacements replacements
;
250 replacements
.SetPathStr(new_path
);
251 *location
= request_
->url().ReplaceComponents(replacements
);
252 *http_status_code
= 301; // simulate a permanent redirect
259 void FileSystemURLRequestJob::NotifyFailed(int rv
) {
260 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED
, rv
));
263 } // namespace storage