Remove muting for extreme playbackRates.
[chromium-blink-merge.git] / net / url_request / url_request_file_job.cc
blobf0963b668f45d9cc554a0d3106bf19bf8f864b57
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 // For loading files, we make use of overlapped i/o to ensure that reading from
6 // the filesystem (e.g., a network filesystem) does not block the calling
7 // thread. An alternative approach would be to use a background thread or pool
8 // of threads, but it seems better to leverage the operating system's ability
9 // to do background file reads for us.
11 // Since overlapped reads require a 'static' buffer for the duration of the
12 // asynchronous read, the URLRequestFileJob keeps a buffer as a member var. In
13 // URLRequestFileJob::Read, data is simply copied from the object's buffer into
14 // the given buffer. If there is no data to copy, the URLRequestFileJob
15 // attempts to read more from the file to fill its buffer. If reading from the
16 // file does not complete synchronously, then the URLRequestFileJob waits for a
17 // signal from the OS that the overlapped read has completed. It does so by
18 // leveraging the MessageLoop::WatchObject API.
20 #include "net/url_request/url_request_file_job.h"
22 #include "base/bind.h"
23 #include "base/compiler_specific.h"
24 #include "base/file_util.h"
25 #include "base/message_loop/message_loop.h"
26 #include "base/strings/string_util.h"
27 #include "base/synchronization/lock.h"
28 #include "base/task_runner.h"
29 #include "base/threading/thread_restrictions.h"
30 #include "build/build_config.h"
31 #include "net/base/file_stream.h"
32 #include "net/base/io_buffer.h"
33 #include "net/base/load_flags.h"
34 #include "net/base/mime_util.h"
35 #include "net/base/net_errors.h"
36 #include "net/base/net_util.h"
37 #include "net/filter/filter.h"
38 #include "net/http/http_util.h"
39 #include "net/url_request/url_request_error_job.h"
40 #include "net/url_request/url_request_file_dir_job.h"
41 #include "url/gurl.h"
43 #if defined(OS_WIN)
44 #include "base/win/shortcut.h"
45 #endif
47 namespace net {
49 URLRequestFileJob::FileMetaInfo::FileMetaInfo()
50 : file_size(0),
51 mime_type_result(false),
52 file_exists(false),
53 is_directory(false) {
56 URLRequestFileJob::URLRequestFileJob(
57 URLRequest* request,
58 NetworkDelegate* network_delegate,
59 const base::FilePath& file_path,
60 const scoped_refptr<base::TaskRunner>& file_task_runner)
61 : URLRequestJob(request, network_delegate),
62 file_path_(file_path),
63 stream_(new FileStream(NULL, file_task_runner)),
64 file_task_runner_(file_task_runner),
65 remaining_bytes_(0),
66 weak_ptr_factory_(this) {}
68 void URLRequestFileJob::Start() {
69 FileMetaInfo* meta_info = new FileMetaInfo();
70 file_task_runner_->PostTaskAndReply(
71 FROM_HERE,
72 base::Bind(&URLRequestFileJob::FetchMetaInfo, file_path_,
73 base::Unretained(meta_info)),
74 base::Bind(&URLRequestFileJob::DidFetchMetaInfo,
75 weak_ptr_factory_.GetWeakPtr(),
76 base::Owned(meta_info)));
79 void URLRequestFileJob::Kill() {
80 stream_.reset();
81 weak_ptr_factory_.InvalidateWeakPtrs();
83 URLRequestJob::Kill();
86 bool URLRequestFileJob::ReadRawData(IOBuffer* dest, int dest_size,
87 int *bytes_read) {
88 DCHECK_NE(dest_size, 0);
89 DCHECK(bytes_read);
90 DCHECK_GE(remaining_bytes_, 0);
92 if (remaining_bytes_ < dest_size)
93 dest_size = static_cast<int>(remaining_bytes_);
95 // If we should copy zero bytes because |remaining_bytes_| is zero, short
96 // circuit here.
97 if (!dest_size) {
98 *bytes_read = 0;
99 return true;
102 int rv = stream_->Read(dest, dest_size,
103 base::Bind(&URLRequestFileJob::DidRead,
104 weak_ptr_factory_.GetWeakPtr()));
105 if (rv >= 0) {
106 // Data is immediately available.
107 *bytes_read = rv;
108 remaining_bytes_ -= rv;
109 DCHECK_GE(remaining_bytes_, 0);
110 return true;
113 // Otherwise, a read error occured. We may just need to wait...
114 if (rv == ERR_IO_PENDING) {
115 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
116 } else {
117 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv));
119 return false;
122 bool URLRequestFileJob::IsRedirectResponse(GURL* location,
123 int* http_status_code) {
124 if (meta_info_.is_directory) {
125 // This happens when we discovered the file is a directory, so needs a
126 // slash at the end of the path.
127 std::string new_path = request_->url().path();
128 new_path.push_back('/');
129 GURL::Replacements replacements;
130 replacements.SetPathStr(new_path);
132 *location = request_->url().ReplaceComponents(replacements);
133 *http_status_code = 301; // simulate a permanent redirect
134 return true;
137 #if defined(OS_WIN)
138 // Follow a Windows shortcut.
139 // We just resolve .lnk file, ignore others.
140 if (!LowerCaseEqualsASCII(file_path_.Extension(), ".lnk"))
141 return false;
143 base::FilePath new_path = file_path_;
144 bool resolved;
145 resolved = base::win::ResolveShortcut(new_path, &new_path, NULL);
147 // If shortcut is not resolved succesfully, do not redirect.
148 if (!resolved)
149 return false;
151 *location = FilePathToFileURL(new_path);
152 *http_status_code = 301;
153 return true;
154 #else
155 return false;
156 #endif
159 Filter* URLRequestFileJob::SetupFilter() const {
160 // Bug 9936 - .svgz files needs to be decompressed.
161 return LowerCaseEqualsASCII(file_path_.Extension(), ".svgz")
162 ? Filter::GZipFactory() : NULL;
165 bool URLRequestFileJob::GetMimeType(std::string* mime_type) const {
166 DCHECK(request_);
167 if (meta_info_.mime_type_result) {
168 *mime_type = meta_info_.mime_type;
169 return true;
171 return false;
174 void URLRequestFileJob::SetExtraRequestHeaders(
175 const HttpRequestHeaders& headers) {
176 std::string range_header;
177 if (headers.GetHeader(HttpRequestHeaders::kRange, &range_header)) {
178 // We only care about "Range" header here.
179 std::vector<HttpByteRange> ranges;
180 if (HttpUtil::ParseRangeHeader(range_header, &ranges)) {
181 if (ranges.size() == 1) {
182 byte_range_ = ranges[0];
183 } else {
184 // We don't support multiple range requests in one single URL request,
185 // because we need to do multipart encoding here.
186 // TODO(hclam): decide whether we want to support multiple range
187 // requests.
188 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED,
189 ERR_REQUEST_RANGE_NOT_SATISFIABLE));
195 URLRequestFileJob::~URLRequestFileJob() {
198 void URLRequestFileJob::FetchMetaInfo(const base::FilePath& file_path,
199 FileMetaInfo* meta_info) {
200 base::File::Info file_info;
201 meta_info->file_exists = base::GetFileInfo(file_path, &file_info);
202 if (meta_info->file_exists) {
203 meta_info->file_size = file_info.size;
204 meta_info->is_directory = file_info.is_directory;
206 // On Windows GetMimeTypeFromFile() goes to the registry. Thus it should be
207 // done in WorkerPool.
208 meta_info->mime_type_result = GetMimeTypeFromFile(file_path,
209 &meta_info->mime_type);
212 void URLRequestFileJob::DidFetchMetaInfo(const FileMetaInfo* meta_info) {
213 meta_info_ = *meta_info;
215 // We use URLRequestFileJob to handle files as well as directories without
216 // trailing slash.
217 // If a directory does not exist, we return ERR_FILE_NOT_FOUND. Otherwise,
218 // we will append trailing slash and redirect to FileDirJob.
219 // A special case is "\" on Windows. We should resolve as invalid.
220 // However, Windows resolves "\" to "C:\", thus reports it as existent.
221 // So what happens is we append it with trailing slash and redirect it to
222 // FileDirJob where it is resolved as invalid.
223 if (!meta_info_.file_exists) {
224 DidOpen(ERR_FILE_NOT_FOUND);
225 return;
227 if (meta_info_.is_directory) {
228 DidOpen(OK);
229 return;
232 int flags = base::PLATFORM_FILE_OPEN |
233 base::PLATFORM_FILE_READ |
234 base::PLATFORM_FILE_ASYNC;
235 int rv = stream_->Open(file_path_, flags,
236 base::Bind(&URLRequestFileJob::DidOpen,
237 weak_ptr_factory_.GetWeakPtr()));
238 if (rv != ERR_IO_PENDING)
239 DidOpen(rv);
242 void URLRequestFileJob::DidOpen(int result) {
243 if (result != OK) {
244 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result));
245 return;
248 if (!byte_range_.ComputeBounds(meta_info_.file_size)) {
249 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED,
250 ERR_REQUEST_RANGE_NOT_SATISFIABLE));
251 return;
254 remaining_bytes_ = byte_range_.last_byte_position() -
255 byte_range_.first_byte_position() + 1;
256 DCHECK_GE(remaining_bytes_, 0);
258 if (remaining_bytes_ > 0 && byte_range_.first_byte_position() != 0) {
259 int rv = stream_->Seek(FROM_BEGIN, byte_range_.first_byte_position(),
260 base::Bind(&URLRequestFileJob::DidSeek,
261 weak_ptr_factory_.GetWeakPtr()));
262 if (rv != ERR_IO_PENDING) {
263 // stream_->Seek() failed, so pass an intentionally erroneous value
264 // into DidSeek().
265 DidSeek(-1);
267 } else {
268 // We didn't need to call stream_->Seek() at all, so we pass to DidSeek()
269 // the value that would mean seek success. This way we skip the code
270 // handling seek failure.
271 DidSeek(byte_range_.first_byte_position());
275 void URLRequestFileJob::DidSeek(int64 result) {
276 if (result != byte_range_.first_byte_position()) {
277 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED,
278 ERR_REQUEST_RANGE_NOT_SATISFIABLE));
279 return;
282 set_expected_content_size(remaining_bytes_);
283 NotifyHeadersComplete();
286 void URLRequestFileJob::DidRead(int result) {
287 if (result > 0) {
288 SetStatus(URLRequestStatus()); // Clear the IO_PENDING status
289 } else if (result == 0) {
290 NotifyDone(URLRequestStatus());
291 } else {
292 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result));
295 remaining_bytes_ -= result;
296 DCHECK_GE(remaining_bytes_, 0);
298 NotifyReadComplete(result);
301 } // namespace net