We started redesigning GpuMemoryBuffer interface to handle multiple buffers [0].
[chromium-blink-merge.git] / storage / browser / fileapi / file_system_url_request_job.cc
blobedc1dde045d777a2654ff329ebc88c72b6adcd2a
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"
7 #include <vector>
9 #include "base/bind.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"
32 #include "url/gurl.h"
34 using net::NetworkDelegate;
35 using net::URLRequest;
36 using net::URLRequestJob;
37 using net::URLRequestStatus;
39 namespace storage {
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);
54 return headers;
57 FileSystemURLRequestJob::FileSystemURLRequestJob(
58 URLRequest* request,
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),
65 is_directory_(false),
66 remaining_bytes_(0),
67 weak_factory_(this) {
70 FileSystemURLRequestJob::~FileSystemURLRequestJob() {}
72 void FileSystemURLRequestJob::Start() {
73 base::MessageLoop::current()->PostTask(
74 FROM_HERE,
75 base::Bind(&FileSystemURLRequestJob::StartAsync,
76 weak_factory_.GetWeakPtr()));
79 void FileSystemURLRequestJob::Kill() {
80 reader_.reset();
81 URLRequestJob::Kill();
82 weak_factory_.InvalidateWeakPtrs();
85 bool FileSystemURLRequestJob::ReadRawData(net::IOBuffer* dest,
86 int dest_size,
87 int* bytes_read) {
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);
94 DCHECK(bytes_read);
95 DCHECK_GE(remaining_bytes_, 0);
97 if (reader_.get() == NULL)
98 return false;
100 if (remaining_bytes_ < dest_size)
101 dest_size = static_cast<int>(remaining_bytes_);
103 if (!dest_size) {
104 *bytes_read = 0;
105 return true;
108 const int rv = reader_->Read(dest, dest_size,
109 base::Bind(&FileSystemURLRequestJob::DidRead,
110 weak_factory_.GetWeakPtr()));
111 if (rv >= 0) {
112 // Data is immediately available.
113 *bytes_read = rv;
114 remaining_bytes_ -= rv;
115 DCHECK_GE(remaining_bytes_, 0);
116 return true;
118 if (rv == net::ERR_IO_PENDING)
119 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
120 else
121 NotifyFailed(rv);
122 return false;
125 bool FileSystemURLRequestJob::GetMimeType(std::string* mime_type) const {
126 DCHECK(request_);
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];
142 } else {
143 // We don't support multiple range requests in one single URL request.
144 // TODO(adamk): decide whether we want to support multiple range
145 // requests.
146 NotifyFailed(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE);
152 void FileSystemURLRequestJob::GetResponseInfo(net::HttpResponseInfo* info) {
153 if (response_info_)
154 *info = *response_info_;
157 int FileSystemURLRequestJob::GetResponseCode() const {
158 if (response_info_)
159 return 200;
160 return URLRequestJob::GetResponseCode();
163 void FileSystemURLRequestJob::StartAsync() {
164 if (!request_)
165 return;
166 DCHECK(!reader_.get());
167 url_ = file_system_context_->CrackURL(request_->url());
168 if (!url_.is_valid()) {
169 file_system_context_->AttemptAutoMountForURLRequest(
170 request_,
171 storage_domain_,
172 base::Bind(&FileSystemURLRequestJob::DidAttemptAutoMount,
173 weak_factory_.GetWeakPtr()));
174 return;
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);
179 return;
181 file_system_context_->operation_runner()->GetMetadata(
182 url_,
183 base::Bind(&FileSystemURLRequestJob::DidGetMetadata,
184 weak_factory_.GetWeakPtr()));
187 void FileSystemURLRequestJob::DidAttemptAutoMount(base::File::Error result) {
188 if (result >= 0 &&
189 file_system_context_->CrackURL(request_->url()).is_valid()) {
190 StartAsync();
191 } else {
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);
202 return;
205 // We may have been orphaned...
206 if (!request_)
207 return;
209 is_directory_ = file_info.is_directory;
211 if (!byte_range_.ComputeBounds(file_info.size)) {
212 NotifyFailed(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE);
213 return;
216 if (is_directory_) {
217 NotifyHeadersComplete();
218 return;
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) {
236 if (result > 0)
237 SetStatus(URLRequestStatus()); // Clear the IO_PENDING status
238 else if (result == 0)
239 NotifyDone(URLRequestStatus());
240 else
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) {
251 if (is_directory_) {
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
260 return true;
263 return false;
266 void FileSystemURLRequestJob::NotifyFailed(int rv) {
267 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv));
270 } // namespace storage