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/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 canceled_ = true; | 58 canceled_ = true; |
59 | 59 |
60 if (!list_complete_) | 60 if (!list_complete_) |
61 lister_.Cancel(); | 61 lister_.Cancel(); |
62 | 62 |
63 URLRequestJob::Kill(); | 63 URLRequestJob::Kill(); |
64 | 64 |
65 weak_factory_.InvalidateWeakPtrs(); | 65 weak_factory_.InvalidateWeakPtrs(); |
66 } | 66 } |
67 | 67 |
68 bool URLRequestFileDirJob::ReadRawData(IOBuffer* buf, int buf_size, | 68 bool URLRequestFileDirJob::ReadRawData(IOBuffer* buf, |
| 69 int buf_size, |
69 int* bytes_read) { | 70 int* bytes_read) { |
70 DCHECK(bytes_read); | 71 DCHECK(bytes_read); |
71 *bytes_read = 0; | 72 *bytes_read = 0; |
72 | 73 |
73 if (is_done()) | 74 if (is_done()) |
74 return true; | 75 return true; |
75 | 76 |
76 if (FillReadBuffer(buf->data(), buf_size, bytes_read)) | 77 if (FillReadBuffer(buf->data(), buf_size, bytes_read)) |
77 return true; | 78 return true; |
78 | 79 |
(...skipping 22 matching lines...) Expand all Loading... |
101 // can catch errors from DirectoryLister and show an error page. | 102 // can catch errors from DirectoryLister and show an error page. |
102 if (!wrote_header_) { | 103 if (!wrote_header_) { |
103 #if defined(OS_WIN) | 104 #if defined(OS_WIN) |
104 const base::string16& title = dir_path_.value(); | 105 const base::string16& title = dir_path_.value(); |
105 #elif defined(OS_POSIX) | 106 #elif defined(OS_POSIX) |
106 // TODO(jungshik): Add SysNativeMBToUTF16 to sys_string_conversions. | 107 // TODO(jungshik): Add SysNativeMBToUTF16 to sys_string_conversions. |
107 // On Mac, need to add NFKC->NFC conversion either here or in file_path. | 108 // On Mac, need to add NFKC->NFC conversion either here or in file_path. |
108 // On Linux, the file system encoding is not defined, but we assume that | 109 // On Linux, the file system encoding is not defined, but we assume that |
109 // SysNativeMBToWide takes care of it at least for now. We can try something | 110 // SysNativeMBToWide takes care of it at least for now. We can try something |
110 // more sophisticated if necessary later. | 111 // more sophisticated if necessary later. |
111 const base::string16& title = base::WideToUTF16( | 112 const base::string16& title = |
112 base::SysNativeMBToWide(dir_path_.value())); | 113 base::WideToUTF16(base::SysNativeMBToWide(dir_path_.value())); |
113 #endif | 114 #endif |
114 data_.append(GetDirectoryListingHeader(title)); | 115 data_.append(GetDirectoryListingHeader(title)); |
115 wrote_header_ = true; | 116 wrote_header_ = true; |
116 } | 117 } |
117 | 118 |
118 #if defined(OS_WIN) | 119 #if defined(OS_WIN) |
119 std::string raw_bytes; // Empty on Windows means UTF-8 encoded name. | 120 std::string raw_bytes; // Empty on Windows means UTF-8 encoded name. |
120 #elif defined(OS_POSIX) | 121 #elif defined(OS_POSIX) |
121 // TOOD(jungshik): The same issue as for the directory name. | 122 // TOOD(jungshik): The same issue as for the directory name. |
122 base::FilePath filename = data.info.GetName(); | 123 base::FilePath filename = data.info.GetName(); |
123 const std::string& raw_bytes = filename.value(); | 124 const std::string& raw_bytes = filename.value(); |
124 #endif | 125 #endif |
125 data_.append(GetDirectoryListingEntry( | 126 data_.append(GetDirectoryListingEntry(data.info.GetName().LossyDisplayName(), |
126 data.info.GetName().LossyDisplayName(), | 127 raw_bytes, |
127 raw_bytes, | 128 data.info.IsDirectory(), |
128 data.info.IsDirectory(), | 129 data.info.GetSize(), |
129 data.info.GetSize(), | 130 data.info.GetLastModifiedTime())); |
130 data.info.GetLastModifiedTime())); | |
131 | 131 |
132 // TODO(darin): coalesce more? | 132 // TODO(darin): coalesce more? |
133 CompleteRead(); | 133 CompleteRead(); |
134 } | 134 } |
135 | 135 |
136 void URLRequestFileDirJob::OnListDone(int error) { | 136 void URLRequestFileDirJob::OnListDone(int error) { |
137 DCHECK(!canceled_); | 137 DCHECK(!canceled_); |
138 if (error != OK) { | 138 if (error != OK) { |
139 read_pending_ = false; | 139 read_pending_ = false; |
140 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, error)); | 140 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, error)); |
141 } else { | 141 } else { |
142 list_complete_ = true; | 142 list_complete_ = true; |
143 CompleteRead(); | 143 CompleteRead(); |
144 } | 144 } |
145 } | 145 } |
146 | 146 |
147 URLRequestFileDirJob::~URLRequestFileDirJob() {} | 147 URLRequestFileDirJob::~URLRequestFileDirJob() { |
| 148 } |
148 | 149 |
149 void URLRequestFileDirJob::CompleteRead() { | 150 void URLRequestFileDirJob::CompleteRead() { |
150 if (read_pending_) { | 151 if (read_pending_) { |
151 int bytes_read; | 152 int bytes_read; |
152 if (FillReadBuffer(read_buffer_->data(), read_buffer_length_, | 153 if (FillReadBuffer( |
153 &bytes_read)) { | 154 read_buffer_->data(), read_buffer_length_, &bytes_read)) { |
154 // We completed the read, so reset the read buffer. | 155 // We completed the read, so reset the read buffer. |
155 read_pending_ = false; | 156 read_pending_ = false; |
156 read_buffer_ = NULL; | 157 read_buffer_ = NULL; |
157 read_buffer_length_ = 0; | 158 read_buffer_length_ = 0; |
158 | 159 |
159 SetStatus(URLRequestStatus()); | 160 SetStatus(URLRequestStatus()); |
160 NotifyReadComplete(bytes_read); | 161 NotifyReadComplete(bytes_read); |
161 } else { | 162 } else { |
162 NOTREACHED(); | 163 NOTREACHED(); |
163 // TODO: Better error code. | 164 // TODO: Better error code. |
164 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, 0)); | 165 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, 0)); |
165 } | 166 } |
166 } | 167 } |
167 } | 168 } |
168 | 169 |
169 bool URLRequestFileDirJob::FillReadBuffer(char* buf, int buf_size, | 170 bool URLRequestFileDirJob::FillReadBuffer(char* buf, |
| 171 int buf_size, |
170 int* bytes_read) { | 172 int* bytes_read) { |
171 DCHECK(bytes_read); | 173 DCHECK(bytes_read); |
172 | 174 |
173 *bytes_read = 0; | 175 *bytes_read = 0; |
174 | 176 |
175 int count = std::min(buf_size, static_cast<int>(data_.size())); | 177 int count = std::min(buf_size, static_cast<int>(data_.size())); |
176 if (count) { | 178 if (count) { |
177 memcpy(buf, &data_[0], count); | 179 memcpy(buf, &data_[0], count); |
178 data_.erase(0, count); | 180 data_.erase(0, count); |
179 *bytes_read = count; | 181 *bytes_read = count; |
180 return true; | 182 return true; |
181 } else if (list_complete_) { | 183 } else if (list_complete_) { |
182 // EOF | 184 // EOF |
183 return true; | 185 return true; |
184 } | 186 } |
185 return false; | 187 return false; |
186 } | 188 } |
187 | 189 |
188 } // namespace net | 190 } // namespace net |
OLD | NEW |