OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "storage/browser/fileapi/file_system_url_request_job.h" | 5 #include "storage/browser/fileapi/file_system_url_request_job.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 FileSystemURLRequestJob::FileSystemURLRequestJob( | 55 FileSystemURLRequestJob::FileSystemURLRequestJob( |
56 URLRequest* request, | 56 URLRequest* request, |
57 NetworkDelegate* network_delegate, | 57 NetworkDelegate* network_delegate, |
58 const std::string& storage_domain, | 58 const std::string& storage_domain, |
59 FileSystemContext* file_system_context) | 59 FileSystemContext* file_system_context) |
60 : URLRequestJob(request, network_delegate), | 60 : URLRequestJob(request, network_delegate), |
61 storage_domain_(storage_domain), | 61 storage_domain_(storage_domain), |
62 file_system_context_(file_system_context), | 62 file_system_context_(file_system_context), |
63 is_directory_(false), | 63 is_directory_(false), |
64 remaining_bytes_(0), | 64 remaining_bytes_(0), |
| 65 range_parse_result_(net::OK), |
65 weak_factory_(this) {} | 66 weak_factory_(this) {} |
66 | 67 |
67 FileSystemURLRequestJob::~FileSystemURLRequestJob() {} | 68 FileSystemURLRequestJob::~FileSystemURLRequestJob() {} |
68 | 69 |
69 void FileSystemURLRequestJob::Start() { | 70 void FileSystemURLRequestJob::Start() { |
70 base::MessageLoop::current()->PostTask( | 71 base::MessageLoop::current()->PostTask( |
71 FROM_HERE, | 72 FROM_HERE, |
72 base::Bind(&FileSystemURLRequestJob::StartAsync, | 73 base::Bind(&FileSystemURLRequestJob::StartAsync, |
73 weak_factory_.GetWeakPtr())); | 74 weak_factory_.GetWeakPtr())); |
74 } | 75 } |
75 | 76 |
76 void FileSystemURLRequestJob::Kill() { | 77 void FileSystemURLRequestJob::Kill() { |
77 reader_.reset(); | 78 reader_.reset(); |
78 URLRequestJob::Kill(); | 79 URLRequestJob::Kill(); |
79 weak_factory_.InvalidateWeakPtrs(); | 80 weak_factory_.InvalidateWeakPtrs(); |
80 } | 81 } |
81 | 82 |
82 bool FileSystemURLRequestJob::ReadRawData(net::IOBuffer* dest, | 83 int FileSystemURLRequestJob::ReadRawData(net::IOBuffer* dest, int dest_size) { |
83 int dest_size, | |
84 int* bytes_read) { | |
85 DCHECK_NE(dest_size, 0); | 84 DCHECK_NE(dest_size, 0); |
86 DCHECK(bytes_read); | |
87 DCHECK_GE(remaining_bytes_, 0); | 85 DCHECK_GE(remaining_bytes_, 0); |
88 | 86 |
89 if (reader_.get() == NULL) | 87 if (reader_.get() == NULL) |
90 return false; | 88 return net::ERR_FAILED; |
91 | 89 |
92 if (remaining_bytes_ < dest_size) | 90 if (remaining_bytes_ < dest_size) |
93 dest_size = static_cast<int>(remaining_bytes_); | 91 dest_size = remaining_bytes_; |
94 | 92 |
95 if (!dest_size) { | 93 if (!dest_size) |
96 *bytes_read = 0; | 94 return 0; |
97 return true; | |
98 } | |
99 | 95 |
100 const int rv = reader_->Read(dest, dest_size, | 96 const int rv = reader_->Read(dest, dest_size, |
101 base::Bind(&FileSystemURLRequestJob::DidRead, | 97 base::Bind(&FileSystemURLRequestJob::DidRead, |
102 weak_factory_.GetWeakPtr())); | 98 weak_factory_.GetWeakPtr())); |
103 if (rv >= 0) { | 99 if (rv >= 0) { |
104 // Data is immediately available. | |
105 *bytes_read = rv; | |
106 remaining_bytes_ -= rv; | 100 remaining_bytes_ -= rv; |
107 DCHECK_GE(remaining_bytes_, 0); | 101 DCHECK_GE(remaining_bytes_, 0); |
108 return true; | |
109 } | 102 } |
110 if (rv == net::ERR_IO_PENDING) | 103 |
111 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); | 104 return rv; |
112 else | |
113 NotifyFailed(rv); | |
114 return false; | |
115 } | 105 } |
116 | 106 |
117 bool FileSystemURLRequestJob::GetMimeType(std::string* mime_type) const { | 107 bool FileSystemURLRequestJob::GetMimeType(std::string* mime_type) const { |
118 DCHECK(request_); | 108 DCHECK(request_); |
119 DCHECK(url_.is_valid()); | 109 DCHECK(url_.is_valid()); |
120 base::FilePath::StringType extension = url_.path().Extension(); | 110 base::FilePath::StringType extension = url_.path().Extension(); |
121 if (!extension.empty()) | 111 if (!extension.empty()) |
122 extension = extension.substr(1); | 112 extension = extension.substr(1); |
123 return net::GetWellKnownMimeTypeFromExtension(extension, mime_type); | 113 return net::GetWellKnownMimeTypeFromExtension(extension, mime_type); |
124 } | 114 } |
125 | 115 |
126 void FileSystemURLRequestJob::SetExtraRequestHeaders( | 116 void FileSystemURLRequestJob::SetExtraRequestHeaders( |
127 const net::HttpRequestHeaders& headers) { | 117 const net::HttpRequestHeaders& headers) { |
128 std::string range_header; | 118 std::string range_header; |
| 119 // Currently this job only cares about the Range header. Note that validation |
| 120 // is deferred to DidGetMetaData(), because NotifyStartError is not legal to |
| 121 // call since the job has not started. |
129 if (headers.GetHeader(net::HttpRequestHeaders::kRange, &range_header)) { | 122 if (headers.GetHeader(net::HttpRequestHeaders::kRange, &range_header)) { |
130 std::vector<net::HttpByteRange> ranges; | 123 std::vector<net::HttpByteRange> ranges; |
| 124 |
131 if (net::HttpUtil::ParseRangeHeader(range_header, &ranges)) { | 125 if (net::HttpUtil::ParseRangeHeader(range_header, &ranges)) { |
132 if (ranges.size() == 1) { | 126 if (ranges.size() == 1) { |
133 byte_range_ = ranges[0]; | 127 byte_range_ = ranges[0]; |
134 } else { | 128 } else { |
135 // We don't support multiple range requests in one single URL request. | 129 // We don't support multiple range requests in one single URL request. |
136 // TODO(adamk): decide whether we want to support multiple range | 130 // TODO(adamk): decide whether we want to support multiple range |
137 // requests. | 131 // requests. |
138 NotifyFailed(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE); | 132 range_parse_result_ = net::ERR_REQUEST_RANGE_NOT_SATISFIABLE; |
139 } | 133 } |
140 } | 134 } |
141 } | 135 } |
142 } | 136 } |
143 | 137 |
144 void FileSystemURLRequestJob::GetResponseInfo(net::HttpResponseInfo* info) { | 138 void FileSystemURLRequestJob::GetResponseInfo(net::HttpResponseInfo* info) { |
145 if (response_info_) | 139 if (response_info_) |
146 *info = *response_info_; | 140 *info = *response_info_; |
147 } | 141 } |
148 | 142 |
(...skipping 11 matching lines...) Expand all Loading... |
160 if (!url_.is_valid()) { | 154 if (!url_.is_valid()) { |
161 file_system_context_->AttemptAutoMountForURLRequest( | 155 file_system_context_->AttemptAutoMountForURLRequest( |
162 request_, | 156 request_, |
163 storage_domain_, | 157 storage_domain_, |
164 base::Bind(&FileSystemURLRequestJob::DidAttemptAutoMount, | 158 base::Bind(&FileSystemURLRequestJob::DidAttemptAutoMount, |
165 weak_factory_.GetWeakPtr())); | 159 weak_factory_.GetWeakPtr())); |
166 return; | 160 return; |
167 } | 161 } |
168 if (!file_system_context_->CanServeURLRequest(url_)) { | 162 if (!file_system_context_->CanServeURLRequest(url_)) { |
169 // In incognito mode the API is not usable and there should be no data. | 163 // In incognito mode the API is not usable and there should be no data. |
170 NotifyFailed(net::ERR_FILE_NOT_FOUND); | 164 NotifyStartError(URLRequestStatus::FromError(net::ERR_FILE_NOT_FOUND)); |
171 return; | 165 return; |
172 } | 166 } |
173 file_system_context_->operation_runner()->GetMetadata( | 167 file_system_context_->operation_runner()->GetMetadata( |
174 url_, | 168 url_, |
175 base::Bind(&FileSystemURLRequestJob::DidGetMetadata, | 169 base::Bind(&FileSystemURLRequestJob::DidGetMetadata, |
176 weak_factory_.GetWeakPtr())); | 170 weak_factory_.GetWeakPtr())); |
177 } | 171 } |
178 | 172 |
179 void FileSystemURLRequestJob::DidAttemptAutoMount(base::File::Error result) { | 173 void FileSystemURLRequestJob::DidAttemptAutoMount(base::File::Error result) { |
180 if (result >= 0 && | 174 if (result >= 0 && |
181 file_system_context_->CrackURL(request_->url()).is_valid()) { | 175 file_system_context_->CrackURL(request_->url()).is_valid()) { |
182 StartAsync(); | 176 StartAsync(); |
183 } else { | 177 } else { |
184 NotifyFailed(net::ERR_FILE_NOT_FOUND); | 178 NotifyStartError(URLRequestStatus::FromError(net::ERR_FILE_NOT_FOUND)); |
185 } | 179 } |
186 } | 180 } |
187 | 181 |
188 void FileSystemURLRequestJob::DidGetMetadata( | 182 void FileSystemURLRequestJob::DidGetMetadata( |
189 base::File::Error error_code, | 183 base::File::Error error_code, |
190 const base::File::Info& file_info) { | 184 const base::File::Info& file_info) { |
191 if (error_code != base::File::FILE_OK) { | 185 if (error_code != base::File::FILE_OK) { |
192 NotifyFailed(error_code == base::File::FILE_ERROR_INVALID_URL | 186 NotifyStartError(URLRequestStatus::FromError( |
193 ? net::ERR_INVALID_URL | 187 error_code == base::File::FILE_ERROR_INVALID_URL |
194 : net::ERR_FILE_NOT_FOUND); | 188 ? net::ERR_INVALID_URL |
| 189 : net::ERR_FILE_NOT_FOUND)); |
195 return; | 190 return; |
196 } | 191 } |
197 | 192 |
198 // We may have been orphaned... | 193 // We may have been orphaned... |
199 if (!request_) | 194 if (!request_) |
200 return; | 195 return; |
201 | 196 |
202 is_directory_ = file_info.is_directory; | 197 is_directory_ = file_info.is_directory; |
203 | 198 |
204 if (!byte_range_.ComputeBounds(file_info.size)) { | 199 if (range_parse_result_ != net::OK) { |
205 NotifyFailed(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE); | 200 NotifyStartError(URLRequestStatus::FromError(range_parse_result_)); |
206 return; | 201 return; |
207 } | 202 } |
208 | 203 |
| 204 if (!byte_range_.ComputeBounds(file_info.size)) { |
| 205 NotifyStartError( |
| 206 URLRequestStatus::FromError(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE)); |
| 207 return; |
| 208 } |
| 209 |
209 if (is_directory_) { | 210 if (is_directory_) { |
210 NotifyHeadersComplete(); | 211 NotifyHeadersComplete(); |
211 return; | 212 return; |
212 } | 213 } |
213 | 214 |
214 remaining_bytes_ = byte_range_.last_byte_position() - | 215 remaining_bytes_ = byte_range_.last_byte_position() - |
215 byte_range_.first_byte_position() + 1; | 216 byte_range_.first_byte_position() + 1; |
216 DCHECK_GE(remaining_bytes_, 0); | 217 DCHECK_GE(remaining_bytes_, 0); |
217 | 218 |
218 DCHECK(!reader_.get()); | 219 DCHECK(!reader_.get()); |
219 reader_ = file_system_context_->CreateFileStreamReader( | 220 reader_ = file_system_context_->CreateFileStreamReader( |
220 url_, byte_range_.first_byte_position(), remaining_bytes_, base::Time()); | 221 url_, byte_range_.first_byte_position(), remaining_bytes_, base::Time()); |
221 | 222 |
222 set_expected_content_size(remaining_bytes_); | 223 set_expected_content_size(remaining_bytes_); |
223 response_info_.reset(new net::HttpResponseInfo()); | 224 response_info_.reset(new net::HttpResponseInfo()); |
224 response_info_->headers = CreateHttpResponseHeaders(); | 225 response_info_->headers = CreateHttpResponseHeaders(); |
225 NotifyHeadersComplete(); | 226 NotifyHeadersComplete(); |
226 } | 227 } |
227 | 228 |
228 void FileSystemURLRequestJob::DidRead(int result) { | 229 void FileSystemURLRequestJob::DidRead(int result) { |
229 if (result > 0) | 230 if (result >= 0) { |
230 SetStatus(URLRequestStatus()); // Clear the IO_PENDING status | 231 remaining_bytes_ -= result; |
231 else if (result == 0) | 232 DCHECK_GE(remaining_bytes_, 0); |
232 NotifyDone(URLRequestStatus()); | 233 } |
233 else | |
234 NotifyFailed(result); | |
235 | 234 |
236 remaining_bytes_ -= result; | 235 ReadRawDataComplete(result); |
237 DCHECK_GE(remaining_bytes_, 0); | |
238 | |
239 NotifyReadComplete(result); | |
240 } | 236 } |
241 | 237 |
242 bool FileSystemURLRequestJob::IsRedirectResponse(GURL* location, | 238 bool FileSystemURLRequestJob::IsRedirectResponse(GURL* location, |
243 int* http_status_code) { | 239 int* http_status_code) { |
244 if (is_directory_) { | 240 if (is_directory_) { |
245 // This happens when we discovered the file is a directory, so needs a | 241 // This happens when we discovered the file is a directory, so needs a |
246 // slash at the end of the path. | 242 // slash at the end of the path. |
247 std::string new_path = request_->url().path(); | 243 std::string new_path = request_->url().path(); |
248 new_path.push_back('/'); | 244 new_path.push_back('/'); |
249 GURL::Replacements replacements; | 245 GURL::Replacements replacements; |
250 replacements.SetPathStr(new_path); | 246 replacements.SetPathStr(new_path); |
251 *location = request_->url().ReplaceComponents(replacements); | 247 *location = request_->url().ReplaceComponents(replacements); |
252 *http_status_code = 301; // simulate a permanent redirect | 248 *http_status_code = 301; // simulate a permanent redirect |
253 return true; | 249 return true; |
254 } | 250 } |
255 | 251 |
256 return false; | 252 return false; |
257 } | 253 } |
258 | 254 |
259 void FileSystemURLRequestJob::NotifyFailed(int rv) { | |
260 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv)); | |
261 } | |
262 | |
263 } // namespace storage | 255 } // namespace storage |
OLD | NEW |