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/profiler/scoped_tracker.h"
16 #include "base/threading/thread_restrictions.h"
17 #include "base/time/time.h"
18 #include "build/build_config.h"
19 #include "net/base/file_stream.h"
20 #include "net/base/io_buffer.h"
21 #include "net/base/mime_util.h"
22 #include "net/base/net_errors.h"
23 #include "net/base/net_util.h"
24 #include "net/http/http_response_headers.h"
25 #include "net/http/http_response_info.h"
26 #include "net/http/http_util.h"
27 #include "net/url_request/url_request.h"
28 #include "storage/browser/fileapi/file_stream_reader.h"
29 #include "storage/browser/fileapi/file_system_context.h"
30 #include "storage/browser/fileapi/file_system_operation_runner.h"
31 #include "storage/common/fileapi/file_system_util.h"
34 using net::NetworkDelegate
;
35 using net::URLRequest
;
36 using net::URLRequestJob
;
37 using net::URLRequestStatus
;
41 static net::HttpResponseHeaders
* CreateHttpResponseHeaders() {
42 // HttpResponseHeaders expects its input string to be terminated by two NULs.
43 static const char kStatus
[] = "HTTP/1.1 200 OK\0";
44 static const size_t kStatusLen
= arraysize(kStatus
);
46 net::HttpResponseHeaders
* headers
=
47 new net::HttpResponseHeaders(std::string(kStatus
, kStatusLen
));
49 // Tell WebKit never to cache this content.
50 std::string
cache_control(net::HttpRequestHeaders::kCacheControl
);
51 cache_control
.append(": no-cache");
52 headers
->AddHeader(cache_control
);
57 FileSystemURLRequestJob::FileSystemURLRequestJob(
59 NetworkDelegate
* network_delegate
,
60 const std::string
& storage_domain
,
61 FileSystemContext
* file_system_context
)
62 : URLRequestJob(request
, network_delegate
),
63 storage_domain_(storage_domain
),
64 file_system_context_(file_system_context
),
70 FileSystemURLRequestJob::~FileSystemURLRequestJob() {}
72 void FileSystemURLRequestJob::Start() {
73 base::MessageLoop::current()->PostTask(
75 base::Bind(&FileSystemURLRequestJob::StartAsync
,
76 weak_factory_
.GetWeakPtr()));
79 void FileSystemURLRequestJob::Kill() {
81 URLRequestJob::Kill();
82 weak_factory_
.InvalidateWeakPtrs();
85 bool FileSystemURLRequestJob::ReadRawData(net::IOBuffer
* dest
,
88 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
89 tracked_objects::ScopedTracker
tracking_profile(
90 FROM_HERE_WITH_EXPLICIT_FUNCTION(
91 "423948 FileSystemURLRequestJob::ReadRawData"));
93 DCHECK_NE(dest_size
, 0);
95 DCHECK_GE(remaining_bytes_
, 0);
97 if (reader_
.get() == NULL
)
100 if (remaining_bytes_
< dest_size
)
101 dest_size
= static_cast<int>(remaining_bytes_
);
108 const int rv
= reader_
->Read(dest
, dest_size
,
109 base::Bind(&FileSystemURLRequestJob::DidRead
,
110 weak_factory_
.GetWeakPtr()));
112 // Data is immediately available.
114 remaining_bytes_
-= rv
;
115 DCHECK_GE(remaining_bytes_
, 0);
118 if (rv
== net::ERR_IO_PENDING
)
119 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING
, 0));
125 bool FileSystemURLRequestJob::GetMimeType(std::string
* mime_type
) const {
127 DCHECK(url_
.is_valid());
128 base::FilePath::StringType extension
= url_
.path().Extension();
129 if (!extension
.empty())
130 extension
= extension
.substr(1);
131 return net::GetWellKnownMimeTypeFromExtension(extension
, mime_type
);
134 void FileSystemURLRequestJob::SetExtraRequestHeaders(
135 const net::HttpRequestHeaders
& headers
) {
136 std::string range_header
;
137 if (headers
.GetHeader(net::HttpRequestHeaders::kRange
, &range_header
)) {
138 std::vector
<net::HttpByteRange
> ranges
;
139 if (net::HttpUtil::ParseRangeHeader(range_header
, &ranges
)) {
140 if (ranges
.size() == 1) {
141 byte_range_
= ranges
[0];
143 // We don't support multiple range requests in one single URL request.
144 // TODO(adamk): decide whether we want to support multiple range
146 NotifyFailed(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE
);
152 void FileSystemURLRequestJob::GetResponseInfo(net::HttpResponseInfo
* info
) {
154 *info
= *response_info_
;
157 int FileSystemURLRequestJob::GetResponseCode() const {
160 return URLRequestJob::GetResponseCode();
163 void FileSystemURLRequestJob::StartAsync() {
166 DCHECK(!reader_
.get());
167 url_
= file_system_context_
->CrackURL(request_
->url());
168 if (!url_
.is_valid()) {
169 file_system_context_
->AttemptAutoMountForURLRequest(
172 base::Bind(&FileSystemURLRequestJob::DidAttemptAutoMount
,
173 weak_factory_
.GetWeakPtr()));
176 if (!file_system_context_
->CanServeURLRequest(url_
)) {
177 // In incognito mode the API is not usable and there should be no data.
178 NotifyFailed(net::ERR_FILE_NOT_FOUND
);
181 file_system_context_
->operation_runner()->GetMetadata(
183 base::Bind(&FileSystemURLRequestJob::DidGetMetadata
,
184 weak_factory_
.GetWeakPtr()));
187 void FileSystemURLRequestJob::DidAttemptAutoMount(base::File::Error result
) {
189 file_system_context_
->CrackURL(request_
->url()).is_valid()) {
192 NotifyFailed(net::ERR_FILE_NOT_FOUND
);
196 void FileSystemURLRequestJob::DidGetMetadata(
197 base::File::Error error_code
,
198 const base::File::Info
& file_info
) {
199 if (error_code
!= base::File::FILE_OK
) {
200 NotifyFailed(error_code
== base::File::FILE_ERROR_INVALID_URL
?
201 net::ERR_INVALID_URL
: net::ERR_FILE_NOT_FOUND
);
205 // We may have been orphaned...
209 is_directory_
= file_info
.is_directory
;
211 if (!byte_range_
.ComputeBounds(file_info
.size
)) {
212 NotifyFailed(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE
);
217 NotifyHeadersComplete();
221 remaining_bytes_
= byte_range_
.last_byte_position() -
222 byte_range_
.first_byte_position() + 1;
223 DCHECK_GE(remaining_bytes_
, 0);
225 DCHECK(!reader_
.get());
226 reader_
= file_system_context_
->CreateFileStreamReader(
227 url_
, byte_range_
.first_byte_position(), remaining_bytes_
, base::Time());
229 set_expected_content_size(remaining_bytes_
);
230 response_info_
.reset(new net::HttpResponseInfo());
231 response_info_
->headers
= CreateHttpResponseHeaders();
232 NotifyHeadersComplete();
235 void FileSystemURLRequestJob::DidRead(int result
) {
237 SetStatus(URLRequestStatus()); // Clear the IO_PENDING status
238 else if (result
== 0)
239 NotifyDone(URLRequestStatus());
241 NotifyFailed(result
);
243 remaining_bytes_
-= result
;
244 DCHECK_GE(remaining_bytes_
, 0);
246 NotifyReadComplete(result
);
249 bool FileSystemURLRequestJob::IsRedirectResponse(GURL
* location
,
250 int* http_status_code
) {
252 // This happens when we discovered the file is a directory, so needs a
253 // slash at the end of the path.
254 std::string new_path
= request_
->url().path();
255 new_path
.push_back('/');
256 GURL::Replacements replacements
;
257 replacements
.SetPathStr(new_path
);
258 *location
= request_
->url().ReplaceComponents(replacements
);
259 *http_status_code
= 301; // simulate a permanent redirect
266 void FileSystemURLRequestJob::NotifyFailed(int rv
) {
267 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED
, rv
));
270 } // namespace storage