| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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_util.h" | 15 #include "net/base/net_util.h" |
| 16 #include "net/url_request/url_request.h" | 16 #include "net/url_request/url_request.h" |
| 17 | 17 |
| 18 #if defined(OS_POSIX) | 18 #if defined(OS_POSIX) |
| 19 #include <sys/stat.h> | 19 #include <sys/stat.h> |
| 20 #endif | 20 #endif |
| 21 | 21 |
| 22 using std::string; | 22 namespace net { |
| 23 | 23 |
| 24 URLRequestFileDirJob::URLRequestFileDirJob(net::URLRequest* request, | 24 URLRequestFileDirJob::URLRequestFileDirJob(URLRequest* request, |
| 25 const FilePath& dir_path) | 25 const FilePath& dir_path) |
| 26 : net::URLRequestJob(request), | 26 : URLRequestJob(request), |
| 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() { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 50 void URLRequestFileDirJob::StartAsync() { | 50 void URLRequestFileDirJob::StartAsync() { |
| 51 DCHECK(!lister_); | 51 DCHECK(!lister_); |
| 52 | 52 |
| 53 // TODO(willchan): This is stupid. We should tell |lister_| not to call us | 53 // TODO(willchan): This is stupid. We should tell |lister_| not to call us |
| 54 // back. Fix this stupidity. | 54 // back. Fix this stupidity. |
| 55 | 55 |
| 56 // AddRef so that *this* cannot be destroyed while the lister_ | 56 // AddRef so that *this* cannot be destroyed while the lister_ |
| 57 // is trying to feed us data. | 57 // is trying to feed us data. |
| 58 | 58 |
| 59 AddRef(); | 59 AddRef(); |
| 60 lister_ = new net::DirectoryLister(dir_path_, this); | 60 lister_ = new DirectoryLister(dir_path_, this); |
| 61 lister_->Start(); | 61 lister_->Start(); |
| 62 | 62 |
| 63 NotifyHeadersComplete(); | 63 NotifyHeadersComplete(); |
| 64 } | 64 } |
| 65 | 65 |
| 66 void URLRequestFileDirJob::Kill() { | 66 void URLRequestFileDirJob::Kill() { |
| 67 if (canceled_) | 67 if (canceled_) |
| 68 return; | 68 return; |
| 69 | 69 |
| 70 canceled_ = true; | 70 canceled_ = true; |
| 71 | 71 |
| 72 // Don't call CloseLister or dispatch an error to the net::URLRequest because | 72 // Don't call CloseLister or dispatch an error to the URLRequest because |
| 73 // we want OnListDone to be called to also write the error to the output | 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. | 74 // stream. OnListDone will notify the URLRequest at this time. |
| 75 if (lister_) | 75 if (lister_) |
| 76 lister_->Cancel(); | 76 lister_->Cancel(); |
| 77 | 77 |
| 78 net::URLRequestJob::Kill(); | 78 URLRequestJob::Kill(); |
| 79 | 79 |
| 80 method_factory_.RevokeAll(); | 80 method_factory_.RevokeAll(); |
| 81 } | 81 } |
| 82 | 82 |
| 83 bool URLRequestFileDirJob::ReadRawData(net::IOBuffer* buf, int buf_size, | 83 bool URLRequestFileDirJob::ReadRawData(IOBuffer* buf, int buf_size, |
| 84 int *bytes_read) { | 84 int *bytes_read) { |
| 85 DCHECK(bytes_read); | 85 DCHECK(bytes_read); |
| 86 *bytes_read = 0; | 86 *bytes_read = 0; |
| 87 | 87 |
| 88 if (is_done()) | 88 if (is_done()) |
| 89 return true; | 89 return true; |
| 90 | 90 |
| 91 if (FillReadBuffer(buf->data(), buf_size, bytes_read)) | 91 if (FillReadBuffer(buf->data(), buf_size, bytes_read)) |
| 92 return true; | 92 return true; |
| 93 | 93 |
| 94 // We are waiting for more data | 94 // We are waiting for more data |
| 95 read_pending_ = true; | 95 read_pending_ = true; |
| 96 read_buffer_ = buf; | 96 read_buffer_ = buf; |
| 97 read_buffer_length_ = buf_size; | 97 read_buffer_length_ = buf_size; |
| 98 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); | 98 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); |
| 99 return false; | 99 return false; |
| 100 } | 100 } |
| 101 | 101 |
| 102 bool URLRequestFileDirJob::GetMimeType(string* mime_type) const { | 102 bool URLRequestFileDirJob::GetMimeType(std::string* mime_type) const { |
| 103 *mime_type = "text/html"; | 103 *mime_type = "text/html"; |
| 104 return true; | 104 return true; |
| 105 } | 105 } |
| 106 | 106 |
| 107 bool URLRequestFileDirJob::GetCharset(string* charset) { | 107 bool URLRequestFileDirJob::GetCharset(std::string* charset) { |
| 108 // All the filenames are converted to UTF-8 before being added. | 108 // All the filenames are converted to UTF-8 before being added. |
| 109 *charset = "utf-8"; | 109 *charset = "utf-8"; |
| 110 return true; | 110 return true; |
| 111 } | 111 } |
| 112 | 112 |
| 113 void URLRequestFileDirJob::OnListFile( | 113 void URLRequestFileDirJob::OnListFile( |
| 114 const net::DirectoryLister::DirectoryListerData& data) { | 114 const DirectoryLister::DirectoryListerData& data) { |
| 115 // We wait to write out the header until we get the first file, so that we | 115 // 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. | 116 // can catch errors from DirectoryLister and show an error page. |
| 117 if (!wrote_header_) { | 117 if (!wrote_header_) { |
| 118 #if defined(OS_WIN) | 118 #if defined(OS_WIN) |
| 119 const string16& title = dir_path_.value(); | 119 const string16& title = dir_path_.value(); |
| 120 #elif defined(OS_POSIX) | 120 #elif defined(OS_POSIX) |
| 121 // TODO(jungshik): Add SysNativeMBToUTF16 to sys_string_conversions. | 121 // TODO(jungshik): Add SysNativeMBToUTF16 to sys_string_conversions. |
| 122 // On Mac, need to add NFKC->NFC conversion either here or in file_path. | 122 // 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 | 123 // 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 | 124 // SysNativeMBToWide takes care of it at least for now. We can try something |
| 125 // more sophisticated if necessary later. | 125 // more sophisticated if necessary later. |
| 126 const string16& title = WideToUTF16( | 126 const string16& title = WideToUTF16( |
| 127 base::SysNativeMBToWide(dir_path_.value())); | 127 base::SysNativeMBToWide(dir_path_.value())); |
| 128 #endif | 128 #endif |
| 129 data_.append(net::GetDirectoryListingHeader(title)); | 129 data_.append(GetDirectoryListingHeader(title)); |
| 130 wrote_header_ = true; | 130 wrote_header_ = true; |
| 131 } | 131 } |
| 132 | 132 |
| 133 #if defined(OS_WIN) | 133 #if defined(OS_WIN) |
| 134 int64 size = (static_cast<unsigned __int64>(data.info.nFileSizeHigh) << 32) | | 134 int64 size = (static_cast<unsigned __int64>(data.info.nFileSizeHigh) << 32) | |
| 135 data.info.nFileSizeLow; | 135 data.info.nFileSizeLow; |
| 136 | 136 |
| 137 // Note that we should not convert ftLastWriteTime to the local time because | 137 // Note that we should not convert ftLastWriteTime to the local time because |
| 138 // ICU's datetime formatting APIs expect time in UTC and take into account | 138 // ICU's datetime formatting APIs expect time in UTC and take into account |
| 139 // the timezone before formatting. | 139 // the timezone before formatting. |
| 140 data_.append(net::GetDirectoryListingEntry( | 140 data_.append(GetDirectoryListingEntry( |
| 141 data.info.cFileName, std::string(), | 141 data.info.cFileName, std::string(), |
| 142 (data.info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? true : false, | 142 (data.info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? true : false, |
| 143 size, | 143 size, |
| 144 base::Time::FromFileTime(data.info.ftLastWriteTime))); | 144 base::Time::FromFileTime(data.info.ftLastWriteTime))); |
| 145 | 145 |
| 146 #elif defined(OS_POSIX) | 146 #elif defined(OS_POSIX) |
| 147 // TOOD(jungshik): The same issue as for the directory name. | 147 // TOOD(jungshik): The same issue as for the directory name. |
| 148 data_.append(net::GetDirectoryListingEntry( | 148 data_.append(GetDirectoryListingEntry( |
| 149 WideToUTF16(base::SysNativeMBToWide(data.info.filename)), | 149 WideToUTF16(base::SysNativeMBToWide(data.info.filename)), |
| 150 data.info.filename, | 150 data.info.filename, |
| 151 S_ISDIR(data.info.stat.st_mode), | 151 S_ISDIR(data.info.stat.st_mode), |
| 152 data.info.stat.st_size, | 152 data.info.stat.st_size, |
| 153 base::Time::FromTimeT(data.info.stat.st_mtime))); | 153 base::Time::FromTimeT(data.info.stat.st_mtime))); |
| 154 #endif | 154 #endif |
| 155 | 155 |
| 156 // TODO(darin): coalesce more? | 156 // TODO(darin): coalesce more? |
| 157 CompleteRead(); | 157 CompleteRead(); |
| 158 } | 158 } |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 | 213 |
| 214 SetStatus(URLRequestStatus()); | 214 SetStatus(URLRequestStatus()); |
| 215 NotifyReadComplete(bytes_read); | 215 NotifyReadComplete(bytes_read); |
| 216 } else { | 216 } else { |
| 217 NOTREACHED(); | 217 NOTREACHED(); |
| 218 // TODO: Better error code. | 218 // TODO: Better error code. |
| 219 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, 0)); | 219 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, 0)); |
| 220 } | 220 } |
| 221 } | 221 } |
| 222 } | 222 } |
| 223 |
| 224 } // namespace net |
| OLD | NEW |