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 "net/url_request/url_request_file_dir_job.h" | 5 #include "net/url_request/url_request_file_dir_job.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/files/file_util.h" |
9 #include "base/location.h" | 10 #include "base/location.h" |
10 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
11 #include "base/strings/sys_string_conversions.h" | 12 #include "base/strings/sys_string_conversions.h" |
12 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "base/task_scheduler/post_task.h" |
13 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
14 #include "base/time/time.h" | 16 #include "base/time/time.h" |
15 #include "net/base/directory_listing.h" | 17 #include "net/base/directory_listing.h" |
16 #include "net/base/io_buffer.h" | 18 #include "net/base/io_buffer.h" |
17 #include "net/url_request/url_request_status.h" | 19 #include "net/url_request/url_request_status.h" |
18 #include "url/gurl.h" | 20 #include "url/gurl.h" |
19 | 21 |
20 #if defined(OS_POSIX) | 22 #if defined(OS_POSIX) |
21 #include <sys/stat.h> | 23 #include <sys/stat.h> |
22 #endif | 24 #endif |
23 | 25 |
24 namespace net { | 26 namespace net { |
25 | 27 |
26 URLRequestFileDirJob::URLRequestFileDirJob(URLRequest* request, | 28 URLRequestFileDirJob::URLRequestFileDirJob(URLRequest* request, |
27 NetworkDelegate* network_delegate, | 29 NetworkDelegate* network_delegate, |
28 const base::FilePath& dir_path) | 30 const base::FilePath& dir_path) |
29 : URLRequestJob(request, network_delegate), | 31 : URLRequestJob(request, network_delegate), |
30 lister_(dir_path, this), | 32 lister_(dir_path, this), |
31 dir_path_(dir_path), | 33 dir_path_(dir_path), |
32 canceled_(false), | 34 canceled_(false), |
33 list_complete_(false), | 35 list_complete_(false), |
34 wrote_header_(false), | 36 wrote_header_(false), |
35 read_pending_(false), | 37 read_pending_(false), |
36 read_buffer_length_(0), | 38 read_buffer_length_(0), |
37 weak_factory_(this) {} | 39 weak_factory_(this) {} |
38 | 40 |
39 void URLRequestFileDirJob::StartAsync() { | 41 void URLRequestFileDirJob::StartAsync() { |
40 lister_.Start(); | 42 base::PostTaskWithTraitsAndReplyWithResult( |
41 NotifyHeadersComplete(); | 43 FROM_HERE, |
| 44 {base::MayBlock(), base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}, |
| 45 base::Bind(&base::MakeAbsoluteFilePath, dir_path_), |
| 46 base::Bind(&URLRequestFileDirJob::DidMakeAbsolutePath, |
| 47 weak_factory_.GetWeakPtr())); |
42 } | 48 } |
43 | 49 |
44 void URLRequestFileDirJob::Start() { | 50 void URLRequestFileDirJob::Start() { |
45 // Start reading asynchronously so that all error reporting and data | 51 // Start reading asynchronously so that all error reporting and data |
46 // callbacks happen as they would for network requests. | 52 // callbacks happen as they would for network requests. |
47 base::ThreadTaskRunnerHandle::Get()->PostTask( | 53 base::ThreadTaskRunnerHandle::Get()->PostTask( |
48 FROM_HERE, base::Bind(&URLRequestFileDirJob::StartAsync, | 54 FROM_HERE, base::Bind(&URLRequestFileDirJob::StartAsync, |
49 weak_factory_.GetWeakPtr())); | 55 weak_factory_.GetWeakPtr())); |
50 } | 56 } |
51 | 57 |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 DCHECK(!canceled_); | 135 DCHECK(!canceled_); |
130 DCHECK_LE(error, OK); | 136 DCHECK_LE(error, OK); |
131 | 137 |
132 list_complete_ = true; | 138 list_complete_ = true; |
133 list_complete_result_ = static_cast<Error>(error); | 139 list_complete_result_ = static_cast<Error>(error); |
134 CompleteRead(list_complete_result_); | 140 CompleteRead(list_complete_result_); |
135 } | 141 } |
136 | 142 |
137 URLRequestFileDirJob::~URLRequestFileDirJob() {} | 143 URLRequestFileDirJob::~URLRequestFileDirJob() {} |
138 | 144 |
| 145 void URLRequestFileDirJob::DidMakeAbsolutePath( |
| 146 const base::FilePath& absolute_path) { |
| 147 if (network_delegate() && !network_delegate()->CanAccessFile( |
| 148 *request(), dir_path_, absolute_path)) { |
| 149 NotifyStartError(URLRequestStatus::FromError(ERR_ACCESS_DENIED)); |
| 150 return; |
| 151 } |
| 152 |
| 153 lister_.Start(); |
| 154 NotifyHeadersComplete(); |
| 155 } |
| 156 |
139 void URLRequestFileDirJob::CompleteRead(Error error) { | 157 void URLRequestFileDirJob::CompleteRead(Error error) { |
140 DCHECK_LE(error, OK); | 158 DCHECK_LE(error, OK); |
141 DCHECK_NE(error, ERR_IO_PENDING); | 159 DCHECK_NE(error, ERR_IO_PENDING); |
142 | 160 |
143 // Do nothing if there is no read pending. | 161 // Do nothing if there is no read pending. |
144 if (!read_pending_) | 162 if (!read_pending_) |
145 return; | 163 return; |
146 | 164 |
147 int result = error; | 165 int result = error; |
148 if (error == OK) { | 166 if (error == OK) { |
(...skipping 20 matching lines...) Expand all Loading... |
169 data_.erase(0, count); | 187 data_.erase(0, count); |
170 return count; | 188 return count; |
171 } else if (list_complete_) { | 189 } else if (list_complete_) { |
172 // EOF | 190 // EOF |
173 return list_complete_result_; | 191 return list_complete_result_; |
174 } | 192 } |
175 return ERR_IO_PENDING; | 193 return ERR_IO_PENDING; |
176 } | 194 } |
177 | 195 |
178 } // namespace net | 196 } // namespace net |
OLD | NEW |