| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/sys_string_conversions.h" | 10 #include "base/sys_string_conversions.h" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "base/time.h" | 12 #include "base/time.h" |
| 13 #include "googleurl/src/gurl.h" | 13 #include "googleurl/src/gurl.h" |
| 14 #include "net/base/io_buffer.h" | 14 #include "net/base/io_buffer.h" |
| 15 #include "net/base/net_errors.h" |
| 15 #include "net/base/net_util.h" | 16 #include "net/base/net_util.h" |
| 16 #include "net/url_request/url_request.h" | 17 #include "net/url_request/url_request.h" |
| 17 | 18 |
| 18 #if defined(OS_POSIX) | 19 #if defined(OS_POSIX) |
| 19 #include <sys/stat.h> | 20 #include <sys/stat.h> |
| 20 #endif | 21 #endif |
| 21 | 22 |
| 22 using std::string; | |
| 23 | |
| 24 URLRequestFileDirJob::URLRequestFileDirJob(net::URLRequest* request, | 23 URLRequestFileDirJob::URLRequestFileDirJob(net::URLRequest* request, |
| 25 const FilePath& dir_path) | 24 const FilePath& dir_path) |
| 26 : URLRequestJob(request), | 25 : URLRequestJob(request), |
| 26 ALLOW_THIS_IN_INITIALIZER_LIST(lister_(dir_path, this)), |
| 27 dir_path_(dir_path), | 27 dir_path_(dir_path), |
| 28 canceled_(false), | 28 canceled_(false), |
| 29 list_complete_(false), | 29 list_complete_(false), |
| 30 wrote_header_(false), | 30 wrote_header_(false), |
| 31 read_pending_(false), | 31 read_pending_(false), |
| 32 read_buffer_length_(0), | 32 read_buffer_length_(0), |
| 33 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | 33 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { |
| 34 } | 34 } |
| 35 | 35 |
| 36 URLRequestFileDirJob::~URLRequestFileDirJob() { | 36 URLRequestFileDirJob::~URLRequestFileDirJob() { |
| 37 DCHECK(read_pending_ == false); | 37 DCHECK(!read_pending_); |
| 38 DCHECK(lister_ == NULL); | |
| 39 } | 38 } |
| 40 | 39 |
| 41 void URLRequestFileDirJob::Start() { | 40 void URLRequestFileDirJob::Start() { |
| 42 // Start reading asynchronously so that all error reporting and data | 41 // Start reading asynchronously so that all error reporting and data |
| 43 // callbacks happen as they would for network requests. | 42 // callbacks happen as they would for network requests. |
| 44 MessageLoop::current()->PostTask( | 43 MessageLoop::current()->PostTask( |
| 45 FROM_HERE, | 44 FROM_HERE, |
| 46 method_factory_.NewRunnableMethod( | 45 method_factory_.NewRunnableMethod( |
| 47 &URLRequestFileDirJob::StartAsync)); | 46 &URLRequestFileDirJob::StartAsync)); |
| 48 } | 47 } |
| 49 | 48 |
| 50 void URLRequestFileDirJob::StartAsync() { | 49 void URLRequestFileDirJob::StartAsync() { |
| 51 DCHECK(!lister_); | 50 lister_.Start(); |
| 52 | |
| 53 // TODO(willchan): This is stupid. We should tell |lister_| not to call us | |
| 54 // back. Fix this stupidity. | |
| 55 | |
| 56 // AddRef so that *this* cannot be destroyed while the lister_ | |
| 57 // is trying to feed us data. | |
| 58 | |
| 59 AddRef(); | |
| 60 lister_ = new net::DirectoryLister(dir_path_, this); | |
| 61 lister_->Start(); | |
| 62 | 51 |
| 63 NotifyHeadersComplete(); | 52 NotifyHeadersComplete(); |
| 64 } | 53 } |
| 65 | 54 |
| 66 void URLRequestFileDirJob::Kill() { | 55 void URLRequestFileDirJob::Kill() { |
| 67 if (canceled_) | 56 if (!is_done() && !list_complete_) { |
| 68 return; | 57 lister_.Cancel(); |
| 58 read_pending_ = false; |
| 59 list_complete_ = true; |
| 60 } |
| 69 | 61 |
| 70 canceled_ = true; | 62 DCHECK(!read_pending_); |
| 71 | 63 DCHECK(list_complete_); |
| 72 // Don't call CloseLister or dispatch an error to the net::URLRequest because | 64 method_factory_.RevokeAll(); |
| 73 // we want OnListDone to be called to also write the error to the output | |
| 74 // stream. OnListDone will notify the net::URLRequest at this time. | |
| 75 if (lister_) | |
| 76 lister_->Cancel(); | |
| 77 | 65 |
| 78 URLRequestJob::Kill(); | 66 URLRequestJob::Kill(); |
| 79 | |
| 80 method_factory_.RevokeAll(); | |
| 81 } | 67 } |
| 82 | 68 |
| 83 bool URLRequestFileDirJob::ReadRawData(net::IOBuffer* buf, int buf_size, | 69 bool URLRequestFileDirJob::ReadRawData(net::IOBuffer* buf, int buf_size, |
| 84 int *bytes_read) { | 70 int *bytes_read) { |
| 85 DCHECK(bytes_read); | 71 DCHECK(bytes_read); |
| 86 *bytes_read = 0; | 72 *bytes_read = 0; |
| 87 | 73 |
| 88 if (is_done()) | 74 if (is_done()) |
| 89 return true; | 75 return true; |
| 90 | 76 |
| 91 if (FillReadBuffer(buf->data(), buf_size, bytes_read)) | 77 if (FillReadBuffer(buf->data(), buf_size, bytes_read)) |
| 92 return true; | 78 return true; |
| 93 | 79 |
| 94 // We are waiting for more data | 80 // We are waiting for more data |
| 95 read_pending_ = true; | 81 read_pending_ = true; |
| 96 read_buffer_ = buf; | 82 read_buffer_ = buf; |
| 97 read_buffer_length_ = buf_size; | 83 read_buffer_length_ = buf_size; |
| 98 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); | 84 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); |
| 99 return false; | 85 return false; |
| 100 } | 86 } |
| 101 | 87 |
| 102 bool URLRequestFileDirJob::GetMimeType(string* mime_type) const { | 88 bool URLRequestFileDirJob::GetMimeType(std::string* mime_type) const { |
| 103 *mime_type = "text/html"; | 89 *mime_type = "text/html"; |
| 104 return true; | 90 return true; |
| 105 } | 91 } |
| 106 | 92 |
| 107 bool URLRequestFileDirJob::GetCharset(string* charset) { | 93 bool URLRequestFileDirJob::GetCharset(std::string* charset) { |
| 108 // All the filenames are converted to UTF-8 before being added. | 94 // All the filenames are converted to UTF-8 before being added. |
| 109 *charset = "utf-8"; | 95 *charset = "utf-8"; |
| 110 return true; | 96 return true; |
| 111 } | 97 } |
| 112 | 98 |
| 113 void URLRequestFileDirJob::OnListFile( | 99 void URLRequestFileDirJob::OnListFile(const net::DirectoryLister::Data& data) { |
| 114 const net::DirectoryLister::DirectoryListerData& data) { | |
| 115 // We wait to write out the header until we get the first file, so that we | 100 // We wait to write out the header until we get the first file, so that we |
| 116 // can catch errors from DirectoryLister and show an error page. | 101 // can catch errors from DirectoryLister and show an error page. |
| 117 if (!wrote_header_) { | 102 if (!wrote_header_) { |
| 118 #if defined(OS_WIN) | 103 #if defined(OS_WIN) |
| 119 const string16& title = dir_path_.value(); | 104 const string16& title = dir_path_.value(); |
| 120 #elif defined(OS_POSIX) | 105 #elif defined(OS_POSIX) |
| 121 // TODO(jungshik): Add SysNativeMBToUTF16 to sys_string_conversions. | 106 // TODO(jungshik): Add SysNativeMBToUTF16 to sys_string_conversions. |
| 122 // On Mac, need to add NFKC->NFC conversion either here or in file_path. | 107 // On Mac, need to add NFKC->NFC conversion either here or in file_path. |
| 123 // On Linux, the file system encoding is not defined, but we assume that | 108 // On Linux, the file system encoding is not defined, but we assume that |
| 124 // SysNativeMBToWide takes care of it at least for now. We can try something | 109 // SysNativeMBToWide takes care of it at least for now. We can try something |
| (...skipping 26 matching lines...) Expand all Loading... |
| 151 S_ISDIR(data.info.stat.st_mode), | 136 S_ISDIR(data.info.stat.st_mode), |
| 152 data.info.stat.st_size, | 137 data.info.stat.st_size, |
| 153 base::Time::FromTimeT(data.info.stat.st_mtime))); | 138 base::Time::FromTimeT(data.info.stat.st_mtime))); |
| 154 #endif | 139 #endif |
| 155 | 140 |
| 156 // TODO(darin): coalesce more? | 141 // TODO(darin): coalesce more? |
| 157 CompleteRead(); | 142 CompleteRead(); |
| 158 } | 143 } |
| 159 | 144 |
| 160 void URLRequestFileDirJob::OnListDone(int error) { | 145 void URLRequestFileDirJob::OnListDone(int error) { |
| 161 CloseLister(); | 146 if (error != net::OK) { |
| 162 | |
| 163 if (canceled_) { | |
| 164 read_pending_ = false; | |
| 165 // No need for NotifyCanceled() since canceled_ is set inside Kill(). | |
| 166 } else if (error) { | |
| 167 read_pending_ = false; | 147 read_pending_ = false; |
| 168 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, error)); | 148 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, error)); |
| 169 } else { | 149 } else { |
| 170 list_complete_ = true; | 150 list_complete_ = true; |
| 171 CompleteRead(); | 151 CompleteRead(); |
| 172 } | 152 } |
| 173 | |
| 174 Release(); // The Lister is finished; may delete *this* | |
| 175 } | |
| 176 | |
| 177 void URLRequestFileDirJob::CloseLister() { | |
| 178 if (lister_) { | |
| 179 lister_->Cancel(); | |
| 180 lister_->set_delegate(NULL); | |
| 181 lister_ = NULL; | |
| 182 } | |
| 183 } | 153 } |
| 184 | 154 |
| 185 bool URLRequestFileDirJob::FillReadBuffer(char *buf, int buf_size, | 155 bool URLRequestFileDirJob::FillReadBuffer(char *buf, int buf_size, |
| 186 int *bytes_read) { | 156 int *bytes_read) { |
| 187 DCHECK(bytes_read); | 157 DCHECK(bytes_read); |
| 188 | 158 |
| 189 *bytes_read = 0; | 159 *bytes_read = 0; |
| 190 | 160 |
| 191 int count = std::min(buf_size, static_cast<int>(data_.size())); | 161 int count = std::min(buf_size, static_cast<int>(data_.size())); |
| 192 if (count) { | 162 if (count) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 213 | 183 |
| 214 SetStatus(URLRequestStatus()); | 184 SetStatus(URLRequestStatus()); |
| 215 NotifyReadComplete(bytes_read); | 185 NotifyReadComplete(bytes_read); |
| 216 } else { | 186 } else { |
| 217 NOTREACHED(); | 187 NOTREACHED(); |
| 218 // TODO: Better error code. | 188 // TODO: Better error code. |
| 219 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, 0)); | 189 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, 0)); |
| 220 } | 190 } |
| 221 } | 191 } |
| 222 } | 192 } |
| OLD | NEW |