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/location.h" | 9 #include "base/location.h" |
10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
11 #include "base/strings/sys_string_conversions.h" | 11 #include "base/strings/sys_string_conversions.h" |
12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
13 #include "base/thread_task_runner_handle.h" | 13 #include "base/thread_task_runner_handle.h" |
14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
15 #include "net/base/io_buffer.h" | 15 #include "net/base/io_buffer.h" |
| 16 #include "net/base/net_errors.h" |
16 #include "net/base/net_util.h" | 17 #include "net/base/net_util.h" |
17 #include "net/url_request/url_request_status.h" | 18 #include "net/url_request/url_request_status.h" |
18 #include "url/gurl.h" | 19 #include "url/gurl.h" |
19 | 20 |
20 #if defined(OS_POSIX) | 21 #if defined(OS_POSIX) |
21 #include <sys/stat.h> | 22 #include <sys/stat.h> |
22 #endif | 23 #endif |
23 | 24 |
24 namespace net { | 25 namespace net { |
25 | 26 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 canceled_ = true; | 59 canceled_ = true; |
59 | 60 |
60 if (!list_complete_) | 61 if (!list_complete_) |
61 lister_.Cancel(); | 62 lister_.Cancel(); |
62 | 63 |
63 URLRequestJob::Kill(); | 64 URLRequestJob::Kill(); |
64 | 65 |
65 weak_factory_.InvalidateWeakPtrs(); | 66 weak_factory_.InvalidateWeakPtrs(); |
66 } | 67 } |
67 | 68 |
68 int URLRequestFileDirJob::ReadRawData(IOBuffer* buf, int buf_size) { | 69 bool URLRequestFileDirJob::ReadRawData(IOBuffer* buf, |
| 70 int buf_size, |
| 71 int* bytes_read) { |
| 72 DCHECK(bytes_read); |
| 73 *bytes_read = 0; |
| 74 |
69 if (is_done()) | 75 if (is_done()) |
70 return 0; | 76 return true; |
71 | 77 |
72 int bytes_read = 0; | 78 if (FillReadBuffer(buf->data(), buf_size, bytes_read)) |
73 if (FillReadBuffer(buf->data(), buf_size, &bytes_read)) | 79 return true; |
74 return bytes_read; | |
75 | 80 |
76 // We are waiting for more data | 81 // We are waiting for more data |
77 read_pending_ = true; | 82 read_pending_ = true; |
78 read_buffer_ = buf; | 83 read_buffer_ = buf; |
79 read_buffer_length_ = buf_size; | 84 read_buffer_length_ = buf_size; |
80 return ERR_IO_PENDING; | 85 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); |
| 86 return false; |
81 } | 87 } |
82 | 88 |
83 bool URLRequestFileDirJob::GetMimeType(std::string* mime_type) const { | 89 bool URLRequestFileDirJob::GetMimeType(std::string* mime_type) const { |
84 *mime_type = "text/html"; | 90 *mime_type = "text/html"; |
85 return true; | 91 return true; |
86 } | 92 } |
87 | 93 |
88 bool URLRequestFileDirJob::GetCharset(std::string* charset) { | 94 bool URLRequestFileDirJob::GetCharset(std::string* charset) { |
89 // All the filenames are converted to UTF-8 before being added. | 95 // All the filenames are converted to UTF-8 before being added. |
90 *charset = "utf-8"; | 96 *charset = "utf-8"; |
(...skipping 28 matching lines...) Expand all Loading... |
119 const std::string& raw_bytes = filename.value(); | 125 const std::string& raw_bytes = filename.value(); |
120 #endif | 126 #endif |
121 data_.append(GetDirectoryListingEntry( | 127 data_.append(GetDirectoryListingEntry( |
122 data.info.GetName().LossyDisplayName(), | 128 data.info.GetName().LossyDisplayName(), |
123 raw_bytes, | 129 raw_bytes, |
124 data.info.IsDirectory(), | 130 data.info.IsDirectory(), |
125 data.info.GetSize(), | 131 data.info.GetSize(), |
126 data.info.GetLastModifiedTime())); | 132 data.info.GetLastModifiedTime())); |
127 | 133 |
128 // TODO(darin): coalesce more? | 134 // TODO(darin): coalesce more? |
129 CompleteRead(OK); | 135 CompleteRead(); |
130 } | 136 } |
131 | 137 |
132 void URLRequestFileDirJob::OnListDone(int error) { | 138 void URLRequestFileDirJob::OnListDone(int error) { |
133 DCHECK(!canceled_); | 139 DCHECK(!canceled_); |
134 DCHECK_LE(error, OK); | 140 if (error != OK) { |
135 if (error == OK) | 141 read_pending_ = false; |
| 142 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, error)); |
| 143 } else { |
136 list_complete_ = true; | 144 list_complete_ = true; |
137 CompleteRead(static_cast<Error>(error)); | 145 CompleteRead(); |
| 146 } |
138 } | 147 } |
139 | 148 |
140 URLRequestFileDirJob::~URLRequestFileDirJob() {} | 149 URLRequestFileDirJob::~URLRequestFileDirJob() {} |
141 | 150 |
142 void URLRequestFileDirJob::CompleteRead(Error status) { | 151 void URLRequestFileDirJob::CompleteRead() { |
143 DCHECK_LE(status, OK); | 152 if (read_pending_) { |
144 DCHECK_NE(status, ERR_IO_PENDING); | 153 int bytes_read; |
145 | |
146 // Do nothing if there is no read pending. | |
147 if (!read_pending_) | |
148 return; | |
149 | |
150 int result = status; | |
151 if (status == OK) { | |
152 int filled_bytes = 0; | |
153 if (FillReadBuffer(read_buffer_->data(), read_buffer_length_, | 154 if (FillReadBuffer(read_buffer_->data(), read_buffer_length_, |
154 &filled_bytes)) { | 155 &bytes_read)) { |
155 result = filled_bytes; | |
156 // We completed the read, so reset the read buffer. | 156 // We completed the read, so reset the read buffer. |
| 157 read_pending_ = false; |
157 read_buffer_ = NULL; | 158 read_buffer_ = NULL; |
158 read_buffer_length_ = 0; | 159 read_buffer_length_ = 0; |
| 160 |
| 161 SetStatus(URLRequestStatus()); |
| 162 NotifyReadComplete(bytes_read); |
159 } else { | 163 } else { |
160 NOTREACHED(); | 164 NOTREACHED(); |
161 // TODO: Better error code. | 165 // TODO: Better error code. |
162 result = ERR_FAILED; | 166 NotifyDone(URLRequestStatus::FromError(ERR_FAILED)); |
163 } | 167 } |
164 } | 168 } |
165 | |
166 read_pending_ = false; | |
167 ReadRawDataComplete(result); | |
168 } | 169 } |
169 | 170 |
170 bool URLRequestFileDirJob::FillReadBuffer(char* buf, int buf_size, | 171 bool URLRequestFileDirJob::FillReadBuffer(char* buf, int buf_size, |
171 int* bytes_read) { | 172 int* bytes_read) { |
172 DCHECK(bytes_read); | 173 DCHECK(bytes_read); |
173 | 174 |
174 *bytes_read = 0; | 175 *bytes_read = 0; |
175 | 176 |
176 int count = std::min(buf_size, static_cast<int>(data_.size())); | 177 int count = std::min(buf_size, static_cast<int>(data_.size())); |
177 if (count) { | 178 if (count) { |
178 memcpy(buf, &data_[0], count); | 179 memcpy(buf, &data_[0], count); |
179 data_.erase(0, count); | 180 data_.erase(0, count); |
180 *bytes_read = count; | 181 *bytes_read = count; |
181 return true; | 182 return true; |
182 } else if (list_complete_) { | 183 } else if (list_complete_) { |
183 // EOF | 184 // EOF |
184 return true; | 185 return true; |
185 } | 186 } |
186 return false; | 187 return false; |
187 } | 188 } |
188 | 189 |
189 } // namespace net | 190 } // namespace net |
OLD | NEW |