| 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 : net::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 | |
| 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 | |
| 78 net::URLRequestJob::Kill(); | |
| 79 | |
| 80 method_factory_.RevokeAll(); | 64 method_factory_.RevokeAll(); |
| 65 URLRequestJob::Kill(); |
| 81 } | 66 } |
| 82 | 67 |
| 83 bool URLRequestFileDirJob::ReadRawData(net::IOBuffer* buf, int buf_size, | 68 bool URLRequestFileDirJob::ReadRawData(net::IOBuffer* buf, int buf_size, |
| 84 int *bytes_read) { | 69 int *bytes_read) { |
| 85 DCHECK(bytes_read); | 70 DCHECK(bytes_read); |
| 86 *bytes_read = 0; | 71 *bytes_read = 0; |
| 87 | 72 |
| 88 if (is_done()) | 73 if (is_done()) |
| 89 return true; | 74 return true; |
| 90 | 75 |
| 91 if (FillReadBuffer(buf->data(), buf_size, bytes_read)) | 76 if (FillReadBuffer(buf->data(), buf_size, bytes_read)) |
| 92 return true; | 77 return true; |
| 93 | 78 |
| 94 // We are waiting for more data | 79 // We are waiting for more data |
| 95 read_pending_ = true; | 80 read_pending_ = true; |
| 96 read_buffer_ = buf; | 81 read_buffer_ = buf; |
| 97 read_buffer_length_ = buf_size; | 82 read_buffer_length_ = buf_size; |
| 98 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); | 83 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); |
| 99 return false; | 84 return false; |
| 100 } | 85 } |
| 101 | 86 |
| 102 bool URLRequestFileDirJob::GetMimeType(string* mime_type) const { | 87 bool URLRequestFileDirJob::GetMimeType(std::string* mime_type) const { |
| 103 *mime_type = "text/html"; | 88 *mime_type = "text/html"; |
| 104 return true; | 89 return true; |
| 105 } | 90 } |
| 106 | 91 |
| 107 bool URLRequestFileDirJob::GetCharset(string* charset) { | 92 bool URLRequestFileDirJob::GetCharset(std::string* charset) { |
| 108 // All the filenames are converted to UTF-8 before being added. | 93 // All the filenames are converted to UTF-8 before being added. |
| 109 *charset = "utf-8"; | 94 *charset = "utf-8"; |
| 110 return true; | 95 return true; |
| 111 } | 96 } |
| 112 | 97 |
| 113 void URLRequestFileDirJob::OnListFile( | 98 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 | 99 // 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. | 100 // can catch errors from DirectoryLister and show an error page. |
| 117 if (!wrote_header_) { | 101 if (!wrote_header_) { |
| 118 #if defined(OS_WIN) | 102 #if defined(OS_WIN) |
| 119 const string16& title = dir_path_.value(); | 103 const string16& title = dir_path_.value(); |
| 120 #elif defined(OS_POSIX) | 104 #elif defined(OS_POSIX) |
| 121 // TODO(jungshik): Add SysNativeMBToUTF16 to sys_string_conversions. | 105 // TODO(jungshik): Add SysNativeMBToUTF16 to sys_string_conversions. |
| 122 // On Mac, need to add NFKC->NFC conversion either here or in file_path. | 106 // 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 | 107 // 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 | 108 // 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), | 135 S_ISDIR(data.info.stat.st_mode), |
| 152 data.info.stat.st_size, | 136 data.info.stat.st_size, |
| 153 base::Time::FromTimeT(data.info.stat.st_mtime))); | 137 base::Time::FromTimeT(data.info.stat.st_mtime))); |
| 154 #endif | 138 #endif |
| 155 | 139 |
| 156 // TODO(darin): coalesce more? | 140 // TODO(darin): coalesce more? |
| 157 CompleteRead(); | 141 CompleteRead(); |
| 158 } | 142 } |
| 159 | 143 |
| 160 void URLRequestFileDirJob::OnListDone(int error) { | 144 void URLRequestFileDirJob::OnListDone(int error) { |
| 161 CloseLister(); | 145 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; | 146 read_pending_ = false; |
| 168 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, error)); | 147 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, error)); |
| 169 } else { | 148 } else { |
| 170 list_complete_ = true; | 149 list_complete_ = true; |
| 171 CompleteRead(); | 150 CompleteRead(); |
| 172 } | 151 } |
| 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 } | 152 } |
| 184 | 153 |
| 185 bool URLRequestFileDirJob::FillReadBuffer(char *buf, int buf_size, | 154 bool URLRequestFileDirJob::FillReadBuffer(char *buf, int buf_size, |
| 186 int *bytes_read) { | 155 int *bytes_read) { |
| 187 DCHECK(bytes_read); | 156 DCHECK(bytes_read); |
| 188 | 157 |
| 189 *bytes_read = 0; | 158 *bytes_read = 0; |
| 190 | 159 |
| 191 int count = std::min(buf_size, static_cast<int>(data_.size())); | 160 int count = std::min(buf_size, static_cast<int>(data_.size())); |
| 192 if (count) { | 161 if (count) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 213 | 182 |
| 214 SetStatus(URLRequestStatus()); | 183 SetStatus(URLRequestStatus()); |
| 215 NotifyReadComplete(bytes_read); | 184 NotifyReadComplete(bytes_read); |
| 216 } else { | 185 } else { |
| 217 NOTREACHED(); | 186 NOTREACHED(); |
| 218 // TODO: Better error code. | 187 // TODO: Better error code. |
| 219 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, 0)); | 188 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, 0)); |
| 220 } | 189 } |
| 221 } | 190 } |
| 222 } | 191 } |
| OLD | NEW |