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/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/time.h" |
10 #include "googleurl/src/gurl.h" | 11 #include "googleurl/src/gurl.h" |
11 #include "net/base/net_util.h" | 12 #include "net/base/net_util.h" |
12 #include "net/base/wininet_util.h" | |
13 #include "net/url_request/url_request.h" | 13 #include "net/url_request/url_request.h" |
14 | 14 |
| 15 #if defined(OS_POSIX) |
| 16 #include <sys/stat.h> |
| 17 #endif |
| 18 |
15 using std::string; | 19 using std::string; |
16 using std::wstring; | 20 using std::wstring; |
17 | 21 |
18 using net::WinInetUtil; | |
19 | |
20 URLRequestFileDirJob::URLRequestFileDirJob(URLRequest* request, | 22 URLRequestFileDirJob::URLRequestFileDirJob(URLRequest* request, |
21 const wstring& dir_path) | 23 const wstring& dir_path) |
22 : URLRequestJob(request), | 24 : URLRequestJob(request), |
23 dir_path_(dir_path), | 25 dir_path_(dir_path), |
24 canceled_(false), | 26 canceled_(false), |
25 list_complete_(false), | 27 list_complete_(false), |
26 wrote_header_(false), | 28 wrote_header_(false), |
27 read_pending_(false), | 29 read_pending_(false), |
28 read_buffer_(NULL), | 30 read_buffer_(NULL), |
29 read_buffer_length_(0) { | 31 read_buffer_length_(0) { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 *mime_type = "text/html"; | 92 *mime_type = "text/html"; |
91 return true; | 93 return true; |
92 } | 94 } |
93 | 95 |
94 bool URLRequestFileDirJob::GetCharset(string* charset) { | 96 bool URLRequestFileDirJob::GetCharset(string* charset) { |
95 // All the filenames are converted to UTF-8 before being added. | 97 // All the filenames are converted to UTF-8 before being added. |
96 *charset = "utf-8"; | 98 *charset = "utf-8"; |
97 return true; | 99 return true; |
98 } | 100 } |
99 | 101 |
100 void URLRequestFileDirJob::OnListFile(const WIN32_FIND_DATA& data) { | 102 void URLRequestFileDirJob::OnListFile( |
101 FILETIME local_time; | 103 const file_util::FileEnumerator::FindInfo& data) { |
102 FileTimeToLocalFileTime(&data.ftLastWriteTime, &local_time); | |
103 int64 size = (static_cast<unsigned __int64>(data.nFileSizeHigh) << 32) | | |
104 data.nFileSizeLow; | |
105 | |
106 // We wait to write out the header until we get the first file, so that we | 104 // We wait to write out the header until we get the first file, so that we |
107 // can catch errors from DirectoryLister and show an error page. | 105 // can catch errors from DirectoryLister and show an error page. |
108 if (!wrote_header_) { | 106 if (!wrote_header_) { |
109 data_.append(net::GetDirectoryListingHeader(WideToUTF8(dir_path_))); | 107 data_.append(net::GetDirectoryListingHeader(WideToUTF8(dir_path_))); |
110 wrote_header_ = true; | 108 wrote_header_ = true; |
111 } | 109 } |
112 | 110 |
| 111 #if defined(OS_WIN) |
| 112 FILETIME local_time; |
| 113 ::FileTimeToLocalFileTime(&data.ftLastWriteTime, &local_time); |
| 114 int64 size = (static_cast<unsigned __int64>(data.nFileSizeHigh) << 32) | |
| 115 data.nFileSizeLow; |
| 116 |
113 data_.append(net::GetDirectoryListingEntry( | 117 data_.append(net::GetDirectoryListingEntry( |
114 WideToUTF8(data.cFileName), data.dwFileAttributes, size, &local_time)); | 118 WideToUTF8(data.cFileName), |
| 119 (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? true : false, |
| 120 size, |
| 121 base::Time::FromFileTime(local_time))); |
| 122 |
| 123 #elif defined(OS_POSIX) |
| 124 data_.append(net::GetDirectoryListingEntry( |
| 125 data.filename.c_str(), |
| 126 S_ISDIR(data.stat.st_mode), |
| 127 data.stat.st_size, |
| 128 base::Time::FromTimeT(data.stat.st_mtime))); |
| 129 #endif |
115 | 130 |
116 // TODO(darin): coalesce more? | 131 // TODO(darin): coalesce more? |
117 | |
118 CompleteRead(); | 132 CompleteRead(); |
119 } | 133 } |
120 | 134 |
121 void URLRequestFileDirJob::OnListDone(int error) { | 135 void URLRequestFileDirJob::OnListDone(int error) { |
122 CloseLister(); | 136 CloseLister(); |
123 | 137 |
124 if (error) { | 138 if (error) { |
125 read_pending_ = false; | 139 read_pending_ = false; |
126 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, | 140 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, error)); |
127 WinInetUtil::OSErrorToNetError(error))); | |
128 } else if (canceled_) { | 141 } else if (canceled_) { |
129 read_pending_ = false; | 142 read_pending_ = false; |
130 NotifyCanceled(); | 143 NotifyCanceled(); |
131 } else { | 144 } else { |
132 list_complete_ = true; | 145 list_complete_ = true; |
133 CompleteRead(); | 146 CompleteRead(); |
134 } | 147 } |
135 | 148 |
136 Release(); // The Lister is finished; may delete *this* | 149 Release(); // The Lister is finished; may delete *this* |
137 } | 150 } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 if (FillReadBuffer(read_buffer_, read_buffer_length_, &bytes_read)) { | 182 if (FillReadBuffer(read_buffer_, read_buffer_length_, &bytes_read)) { |
170 // We completed the read, so reset the read buffer. | 183 // We completed the read, so reset the read buffer. |
171 read_pending_ = false; | 184 read_pending_ = false; |
172 read_buffer_ = NULL; | 185 read_buffer_ = NULL; |
173 read_buffer_length_ = 0; | 186 read_buffer_length_ = 0; |
174 | 187 |
175 SetStatus(URLRequestStatus()); | 188 SetStatus(URLRequestStatus()); |
176 NotifyReadComplete(bytes_read); | 189 NotifyReadComplete(bytes_read); |
177 } else { | 190 } else { |
178 NOTREACHED(); | 191 NOTREACHED(); |
179 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, 0)); // TODO: Bette
r error code. | 192 // TODO: Better error code. |
| 193 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, 0)); |
180 } | 194 } |
181 } | 195 } |
182 } | 196 } |
183 | 197 |
OLD | NEW |