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 // For loading files, we make use of overlapped i/o to ensure that reading from | 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 | 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 | 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 | 8 // of threads, but it seems better to leverage the operating system's ability |
9 // to do background file reads for us. | 9 // to do background file reads for us. |
10 // | 10 // |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 URLRequestFileJob::URLRequestFileJob(URLRequest* request, | 55 URLRequestFileJob::URLRequestFileJob(URLRequest* request, |
56 NetworkDelegate* network_delegate, | 56 NetworkDelegate* network_delegate, |
57 const base::FilePath& file_path) | 57 const base::FilePath& file_path) |
58 : URLRequestJob(request, network_delegate), | 58 : URLRequestJob(request, network_delegate), |
59 file_path_(file_path), | 59 file_path_(file_path), |
60 stream_(new FileStream(NULL)), | 60 stream_(new FileStream(NULL)), |
61 remaining_bytes_(0), | 61 remaining_bytes_(0), |
62 weak_ptr_factory_(this) { | 62 weak_ptr_factory_(this) { |
63 } | 63 } |
64 | 64 |
65 // static | |
66 URLRequestJob* URLRequestFileJob::Factory(URLRequest* request, | |
67 NetworkDelegate* network_delegate, | |
68 const std::string& scheme) { | |
69 base::FilePath file_path; | |
70 const bool is_file = FileURLToFilePath(request->url(), &file_path); | |
71 | |
72 // Check file access permissions. | |
73 if (!network_delegate || | |
74 !network_delegate->CanAccessFile(*request, file_path)) { | |
75 return new URLRequestErrorJob(request, network_delegate, ERR_ACCESS_DENIED); | |
76 } | |
77 // We need to decide whether to create URLRequestFileJob for file access or | |
78 // URLRequestFileDirJob for directory access. To avoid accessing the | |
79 // filesystem, we only look at the path string here. | |
80 // The code in the URLRequestFileJob::Start() method discovers that a path, | |
81 // which doesn't end with a slash, should really be treated as a directory, | |
82 // and it then redirects to the URLRequestFileDirJob. | |
83 if (is_file && | |
84 file_path.EndsWithSeparator() && | |
85 file_path.IsAbsolute()) | |
86 return new URLRequestFileDirJob(request, network_delegate, file_path); | |
87 | |
88 // Use a regular file request job for all non-directories (including invalid | |
89 // file names). | |
90 return new URLRequestFileJob(request, network_delegate, file_path); | |
91 } | |
92 | |
93 void URLRequestFileJob::Start() { | 65 void URLRequestFileJob::Start() { |
94 FileMetaInfo* meta_info = new FileMetaInfo(); | 66 FileMetaInfo* meta_info = new FileMetaInfo(); |
95 base::WorkerPool::PostTaskAndReply( | 67 base::WorkerPool::PostTaskAndReply( |
96 FROM_HERE, | 68 FROM_HERE, |
97 base::Bind(&URLRequestFileJob::FetchMetaInfo, file_path_, | 69 base::Bind(&URLRequestFileJob::FetchMetaInfo, file_path_, |
98 base::Unretained(meta_info)), | 70 base::Unretained(meta_info)), |
99 base::Bind(&URLRequestFileJob::DidFetchMetaInfo, | 71 base::Bind(&URLRequestFileJob::DidFetchMetaInfo, |
100 weak_ptr_factory_.GetWeakPtr(), | 72 weak_ptr_factory_.GetWeakPtr(), |
101 base::Owned(meta_info)), | 73 base::Owned(meta_info)), |
102 true); | 74 true); |
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result)); | 290 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result)); |
319 } | 291 } |
320 | 292 |
321 remaining_bytes_ -= result; | 293 remaining_bytes_ -= result; |
322 DCHECK_GE(remaining_bytes_, 0); | 294 DCHECK_GE(remaining_bytes_, 0); |
323 | 295 |
324 NotifyReadComplete(result); | 296 NotifyReadComplete(result); |
325 } | 297 } |
326 | 298 |
327 } // namespace net | 299 } // namespace net |
OLD | NEW |