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/message_loop/message_loop_proxy.h"
15 #include "base/threading/thread_restrictions.h"
16 #include "base/time/time.h"
17 #include "build/build_config.h"
18 #include "net/base/file_stream.h"
19 #include "net/base/io_buffer.h"
20 #include "net/base/mime_util.h"
21 #include "net/base/net_errors.h"
22 #include "net/base/net_util.h"
23 #include "net/http/http_response_headers.h"
24 #include "net/http/http_response_info.h"
25 #include "net/http/http_util.h"
26 #include "net/url_request/url_request.h"
27 #include "storage/browser/fileapi/file_stream_reader.h"
28 #include "storage/browser/fileapi/file_system_context.h"
29 #include "storage/browser/fileapi/file_system_operation_runner.h"
30 #include "storage/common/fileapi/file_system_util.h"
33 using net::NetworkDelegate
;
34 using net::URLRequest
;
35 using net::URLRequestJob
;
36 using net::URLRequestStatus
;
40 static net::HttpResponseHeaders
* CreateHttpResponseHeaders() {
41 // HttpResponseHeaders expects its input string to be terminated by two NULs.
42 static const char kStatus
[] = "HTTP/1.1 200 OK\0";
43 static const size_t kStatusLen
= arraysize(kStatus
);
45 net::HttpResponseHeaders
* headers
=
46 new net::HttpResponseHeaders(std::string(kStatus
, kStatusLen
));
48 // Tell WebKit never to cache this content.
49 std::string
cache_control(net::HttpRequestHeaders::kCacheControl
);
50 cache_control
.append(": no-cache");
51 headers
->AddHeader(cache_control
);
56 FileSystemURLRequestJob::FileSystemURLRequestJob(
58 NetworkDelegate
* network_delegate
,
59 const std::string
& storage_domain
,
60 FileSystemContext
* file_system_context
)
61 : URLRequestJob(request
, network_delegate
),
62 storage_domain_(storage_domain
),
63 file_system_context_(file_system_context
),
69 FileSystemURLRequestJob::~FileSystemURLRequestJob() {}
71 void FileSystemURLRequestJob::Start() {
72 base::MessageLoop::current()->PostTask(
74 base::Bind(&FileSystemURLRequestJob::StartAsync
,
75 weak_factory_
.GetWeakPtr()));
78 void FileSystemURLRequestJob::Kill() {
80 URLRequestJob::Kill();
81 weak_factory_
.InvalidateWeakPtrs();
84 bool FileSystemURLRequestJob::ReadRawData(net::IOBuffer
* dest
,
87 DCHECK_NE(dest_size
, 0);
89 DCHECK_GE(remaining_bytes_
, 0);
91 if (reader_
.get() == NULL
)
94 if (remaining_bytes_
< dest_size
)
95 dest_size
= static_cast<int>(remaining_bytes_
);
102 const int rv
= reader_
->Read(dest
, dest_size
,
103 base::Bind(&FileSystemURLRequestJob::DidRead
,
104 weak_factory_
.GetWeakPtr()));
106 // Data is immediately available.
108 remaining_bytes_
-= rv
;
109 DCHECK_GE(remaining_bytes_
, 0);
112 if (rv
== net::ERR_IO_PENDING
)
113 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING
, 0));
119 bool FileSystemURLRequestJob::GetMimeType(std::string
* mime_type
) const {
121 DCHECK(url_
.is_valid());
122 base::FilePath::StringType extension
= url_
.path().Extension();
123 if (!extension
.empty())
124 extension
= extension
.substr(1);
125 return net::GetWellKnownMimeTypeFromExtension(extension
, mime_type
);
128 void FileSystemURLRequestJob::SetExtraRequestHeaders(
129 const net::HttpRequestHeaders
& headers
) {
130 std::string range_header
;
131 if (headers
.GetHeader(net::HttpRequestHeaders::kRange
, &range_header
)) {
132 std::vector
<net::HttpByteRange
> ranges
;
133 if (net::HttpUtil::ParseRangeHeader(range_header
, &ranges
)) {
134 if (ranges
.size() == 1) {
135 byte_range_
= ranges
[0];
137 // We don't support multiple range requests in one single URL request.
138 // TODO(adamk): decide whether we want to support multiple range
140 NotifyFailed(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE
);
146 void FileSystemURLRequestJob::GetResponseInfo(net::HttpResponseInfo
* info
) {
148 *info
= *response_info_
;
151 int FileSystemURLRequestJob::GetResponseCode() const {
154 return URLRequestJob::GetResponseCode();
157 void FileSystemURLRequestJob::StartAsync() {
160 DCHECK(!reader_
.get());
161 url_
= file_system_context_
->CrackURL(request_
->url());
162 if (!url_
.is_valid()) {
163 file_system_context_
->AttemptAutoMountForURLRequest(
166 base::Bind(&FileSystemURLRequestJob::DidAttemptAutoMount
,
167 weak_factory_
.GetWeakPtr()));
170 if (!file_system_context_
->CanServeURLRequest(url_
)) {
171 // In incognito mode the API is not usable and there should be no data.
172 NotifyFailed(net::ERR_FILE_NOT_FOUND
);
175 file_system_context_
->operation_runner()->GetMetadata(
177 base::Bind(&FileSystemURLRequestJob::DidGetMetadata
,
178 weak_factory_
.GetWeakPtr()));
181 void FileSystemURLRequestJob::DidAttemptAutoMount(base::File::Error result
) {
183 file_system_context_
->CrackURL(request_
->url()).is_valid()) {
186 NotifyFailed(net::ERR_FILE_NOT_FOUND
);
190 void FileSystemURLRequestJob::DidGetMetadata(
191 base::File::Error error_code
,
192 const base::File::Info
& file_info
) {
193 if (error_code
!= base::File::FILE_OK
) {
194 NotifyFailed(error_code
== base::File::FILE_ERROR_INVALID_URL
?
195 net::ERR_INVALID_URL
: net::ERR_FILE_NOT_FOUND
);
199 // We may have been orphaned...
203 is_directory_
= file_info
.is_directory
;
205 if (!byte_range_
.ComputeBounds(file_info
.size
)) {
206 NotifyFailed(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE
);
211 NotifyHeadersComplete();
215 remaining_bytes_
= byte_range_
.last_byte_position() -
216 byte_range_
.first_byte_position() + 1;
217 DCHECK_GE(remaining_bytes_
, 0);
219 DCHECK(!reader_
.get());
220 reader_
= file_system_context_
->CreateFileStreamReader(
221 url_
, byte_range_
.first_byte_position(), remaining_bytes_
, base::Time());
223 set_expected_content_size(remaining_bytes_
);
224 response_info_
.reset(new net::HttpResponseInfo());
225 response_info_
->headers
= CreateHttpResponseHeaders();
226 NotifyHeadersComplete();
229 void FileSystemURLRequestJob::DidRead(int result
) {
231 SetStatus(URLRequestStatus()); // Clear the IO_PENDING status
232 else if (result
== 0)
233 NotifyDone(URLRequestStatus());
235 NotifyFailed(result
);
237 remaining_bytes_
-= result
;
238 DCHECK_GE(remaining_bytes_
, 0);
240 NotifyReadComplete(result
);
243 bool FileSystemURLRequestJob::IsRedirectResponse(GURL
* location
,
244 int* http_status_code
) {
246 // This happens when we discovered the file is a directory, so needs a
247 // slash at the end of the path.
248 std::string new_path
= request_
->url().path();
249 new_path
.push_back('/');
250 GURL::Replacements replacements
;
251 replacements
.SetPathStr(new_path
);
252 *location
= request_
->url().ReplaceComponents(replacements
);
253 *http_status_code
= 301; // simulate a permanent redirect
260 void FileSystemURLRequestJob::NotifyFailed(int rv
) {
261 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED
, rv
));
264 } // namespace storage