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 18 matching lines...) Expand all Loading... | |
29 #include "base/threading/thread_restrictions.h" | 29 #include "base/threading/thread_restrictions.h" |
30 #include "build/build_config.h" | 30 #include "build/build_config.h" |
31 #include "googleurl/src/gurl.h" | 31 #include "googleurl/src/gurl.h" |
32 #include "net/base/io_buffer.h" | 32 #include "net/base/io_buffer.h" |
33 #include "net/base/load_flags.h" | 33 #include "net/base/load_flags.h" |
34 #include "net/base/mime_util.h" | 34 #include "net/base/mime_util.h" |
35 #include "net/base/net_errors.h" | 35 #include "net/base/net_errors.h" |
36 #include "net/base/net_util.h" | 36 #include "net/base/net_util.h" |
37 #include "net/http/http_util.h" | 37 #include "net/http/http_util.h" |
38 #include "net/url_request/url_request.h" | 38 #include "net/url_request/url_request.h" |
39 #include "net/url_request/url_request_context.h" | 39 #include "net/url_request/url_request_context.h" |
mmenke
2012/08/21 15:28:49
This is no longer needed.
shalev
2012/08/22 20:34:10
Done.
| |
40 #include "net/url_request/url_request_error_job.h" | 40 #include "net/url_request/url_request_error_job.h" |
41 #include "net/url_request/url_request_file_dir_job.h" | 41 #include "net/url_request/url_request_file_dir_job.h" |
42 | 42 |
43 namespace net { | 43 namespace net { |
44 | 44 |
45 class URLRequestFileJob::AsyncResolver | 45 class URLRequestFileJob::AsyncResolver |
46 : public base::RefCountedThreadSafe<URLRequestFileJob::AsyncResolver> { | 46 : public base::RefCountedThreadSafe<URLRequestFileJob::AsyncResolver> { |
47 public: | 47 public: |
48 explicit AsyncResolver(URLRequestFileJob* owner) | 48 explicit AsyncResolver(URLRequestFileJob* owner) |
49 : owner_(owner), owner_loop_(MessageLoop::current()) { | 49 : owner_(owner), owner_loop_(MessageLoop::current()) { |
(...skipping 27 matching lines...) Expand all Loading... | |
77 owner_->DidResolve(exists, file_info); | 77 owner_->DidResolve(exists, file_info); |
78 } | 78 } |
79 | 79 |
80 URLRequestFileJob* owner_; | 80 URLRequestFileJob* owner_; |
81 | 81 |
82 base::Lock lock_; | 82 base::Lock lock_; |
83 MessageLoop* owner_loop_; | 83 MessageLoop* owner_loop_; |
84 }; | 84 }; |
85 | 85 |
86 URLRequestFileJob::URLRequestFileJob(URLRequest* request, | 86 URLRequestFileJob::URLRequestFileJob(URLRequest* request, |
87 NetworkDelegate* network_delegate, | |
87 const FilePath& file_path) | 88 const FilePath& file_path) |
88 : URLRequestJob(request, request->context()->network_delegate()), | 89 : URLRequestJob(request, network_delegate), |
89 file_path_(file_path), | 90 file_path_(file_path), |
90 stream_(NULL), | 91 stream_(NULL), |
91 is_directory_(false), | 92 is_directory_(false), |
92 remaining_bytes_(0) { | 93 remaining_bytes_(0) { |
93 } | 94 } |
94 | 95 |
95 // static | 96 // static |
96 URLRequestJob* URLRequestFileJob::Factory(URLRequest* request, | 97 URLRequestJob* URLRequestFileJob::Factory(URLRequest* request, |
98 NetworkDelegate* network_delegate, | |
97 const std::string& scheme) { | 99 const std::string& scheme) { |
98 FilePath file_path; | 100 FilePath file_path; |
99 const bool is_file = FileURLToFilePath(request->url(), &file_path); | 101 const bool is_file = FileURLToFilePath(request->url(), &file_path); |
100 | 102 |
101 // Check file access permissions. | 103 // Check file access permissions. |
102 if (!IsFileAccessAllowed(*request, file_path)) | 104 if (!network_delegate || |
103 return new URLRequestErrorJob(request, ERR_ACCESS_DENIED); | 105 !network_delegate->CanAccessFile(*request, file_path)) { |
104 | 106 return new URLRequestErrorJob(request, network_delegate, ERR_ACCESS_DENIED); |
107 } | |
105 // We need to decide whether to create URLRequestFileJob for file access or | 108 // We need to decide whether to create URLRequestFileJob for file access or |
106 // URLRequestFileDirJob for directory access. To avoid accessing the | 109 // URLRequestFileDirJob for directory access. To avoid accessing the |
107 // filesystem, we only look at the path string here. | 110 // filesystem, we only look at the path string here. |
108 // The code in the URLRequestFileJob::Start() method discovers that a path, | 111 // The code in the URLRequestFileJob::Start() method discovers that a path, |
109 // which doesn't end with a slash, should really be treated as a directory, | 112 // which doesn't end with a slash, should really be treated as a directory, |
110 // and it then redirects to the URLRequestFileDirJob. | 113 // and it then redirects to the URLRequestFileDirJob. |
111 if (is_file && | 114 if (is_file && |
112 file_util::EndsWithSeparator(file_path) && | 115 file_util::EndsWithSeparator(file_path) && |
113 file_path.IsAbsolute()) | 116 file_path.IsAbsolute()) |
114 return new URLRequestFileDirJob(request, file_path); | 117 return new URLRequestFileDirJob(request, network_delegate, file_path); |
115 | 118 |
116 // Use a regular file request job for all non-directories (including invalid | 119 // Use a regular file request job for all non-directories (including invalid |
117 // file names). | 120 // file names). |
118 return new URLRequestFileJob(request, file_path); | 121 return new URLRequestFileJob(request, network_delegate, file_path); |
119 } | 122 } |
120 | 123 |
121 void URLRequestFileJob::Start() { | 124 void URLRequestFileJob::Start() { |
122 DCHECK(!async_resolver_); | 125 DCHECK(!async_resolver_); |
123 async_resolver_ = new AsyncResolver(this); | 126 async_resolver_ = new AsyncResolver(this); |
124 base::WorkerPool::PostTask( | 127 base::WorkerPool::PostTask( |
125 FROM_HERE, | 128 FROM_HERE, |
126 base::Bind(&AsyncResolver::Resolve, async_resolver_.get(), file_path_), | 129 base::Bind(&AsyncResolver::Resolve, async_resolver_.get(), file_path_), |
127 true); | 130 true); |
128 } | 131 } |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
243 // because we need to do multipart encoding here. | 246 // because we need to do multipart encoding here. |
244 // TODO(hclam): decide whether we want to support multiple range | 247 // TODO(hclam): decide whether we want to support multiple range |
245 // requests. | 248 // requests. |
246 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, | 249 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, |
247 ERR_REQUEST_RANGE_NOT_SATISFIABLE)); | 250 ERR_REQUEST_RANGE_NOT_SATISFIABLE)); |
248 } | 251 } |
249 } | 252 } |
250 } | 253 } |
251 } | 254 } |
252 | 255 |
253 // static | |
254 bool URLRequestFileJob::IsFileAccessAllowed(const URLRequest& request, | |
255 const FilePath& path) { | |
256 const NetworkDelegate* delegate = request.context()->network_delegate(); | |
257 if (delegate) | |
258 return delegate->CanAccessFile(request, path); | |
259 return false; | |
260 } | |
261 | |
262 URLRequestFileJob::~URLRequestFileJob() { | 256 URLRequestFileJob::~URLRequestFileJob() { |
263 DCHECK(!async_resolver_); | 257 DCHECK(!async_resolver_); |
264 } | 258 } |
265 | 259 |
266 void URLRequestFileJob::DidResolve( | 260 void URLRequestFileJob::DidResolve( |
267 bool exists, const base::PlatformFileInfo& file_info) { | 261 bool exists, const base::PlatformFileInfo& file_info) { |
268 async_resolver_ = NULL; | 262 async_resolver_ = NULL; |
269 | 263 |
270 // We may have been orphaned... | 264 // We may have been orphaned... |
271 if (!request_) | 265 if (!request_) |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
338 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result)); | 332 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result)); |
339 } | 333 } |
340 | 334 |
341 remaining_bytes_ -= result; | 335 remaining_bytes_ -= result; |
342 DCHECK_GE(remaining_bytes_, 0); | 336 DCHECK_GE(remaining_bytes_, 0); |
343 | 337 |
344 NotifyReadComplete(result); | 338 NotifyReadComplete(result); |
345 } | 339 } |
346 | 340 |
347 } // namespace net | 341 } // namespace net |
OLD | NEW |