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" |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 canceled_ = true; | 59 canceled_ = true; |
60 | 60 |
61 if (!list_complete_) | 61 if (!list_complete_) |
62 lister_.Cancel(); | 62 lister_.Cancel(); |
63 | 63 |
64 URLRequestJob::Kill(); | 64 URLRequestJob::Kill(); |
65 | 65 |
66 weak_factory_.InvalidateWeakPtrs(); | 66 weak_factory_.InvalidateWeakPtrs(); |
67 } | 67 } |
68 | 68 |
69 bool URLRequestFileDirJob::ReadRawData(IOBuffer* buf, int buf_size, | 69 int URLRequestFileDirJob::ReadRawData(IOBuffer* buf, int buf_size) { |
70 int* bytes_read) { | 70 if (is_done()) |
71 DCHECK(bytes_read); | 71 return 0; |
72 *bytes_read = 0; | |
73 | 72 |
74 if (is_done()) | 73 int bytes_read = 0; |
75 return true; | 74 if (FillReadBuffer(buf->data(), buf_size, &bytes_read)) |
76 | 75 return bytes_read; |
77 if (FillReadBuffer(buf->data(), buf_size, bytes_read)) | |
78 return true; | |
79 | 76 |
80 // We are waiting for more data | 77 // We are waiting for more data |
81 read_pending_ = true; | 78 read_pending_ = true; |
82 read_buffer_ = buf; | 79 read_buffer_ = buf; |
83 read_buffer_length_ = buf_size; | 80 read_buffer_length_ = buf_size; |
84 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); | 81 return ERR_IO_PENDING; |
85 return false; | |
86 } | 82 } |
87 | 83 |
88 bool URLRequestFileDirJob::GetMimeType(std::string* mime_type) const { | 84 bool URLRequestFileDirJob::GetMimeType(std::string* mime_type) const { |
89 *mime_type = "text/html"; | 85 *mime_type = "text/html"; |
90 return true; | 86 return true; |
91 } | 87 } |
92 | 88 |
93 bool URLRequestFileDirJob::GetCharset(std::string* charset) { | 89 bool URLRequestFileDirJob::GetCharset(std::string* charset) { |
94 // All the filenames are converted to UTF-8 before being added. | 90 // All the filenames are converted to UTF-8 before being added. |
95 *charset = "utf-8"; | 91 *charset = "utf-8"; |
(...skipping 28 matching lines...) Expand all Loading... |
124 const std::string& raw_bytes = filename.value(); | 120 const std::string& raw_bytes = filename.value(); |
125 #endif | 121 #endif |
126 data_.append(GetDirectoryListingEntry( | 122 data_.append(GetDirectoryListingEntry( |
127 data.info.GetName().LossyDisplayName(), | 123 data.info.GetName().LossyDisplayName(), |
128 raw_bytes, | 124 raw_bytes, |
129 data.info.IsDirectory(), | 125 data.info.IsDirectory(), |
130 data.info.GetSize(), | 126 data.info.GetSize(), |
131 data.info.GetLastModifiedTime())); | 127 data.info.GetLastModifiedTime())); |
132 | 128 |
133 // TODO(darin): coalesce more? | 129 // TODO(darin): coalesce more? |
134 CompleteRead(); | 130 CompleteRead(OK); |
135 } | 131 } |
136 | 132 |
137 void URLRequestFileDirJob::OnListDone(int error) { | 133 void URLRequestFileDirJob::OnListDone(int error) { |
138 DCHECK(!canceled_); | 134 DCHECK(!canceled_); |
139 if (error != OK) { | 135 DCHECK_LE(error, OK); |
140 read_pending_ = false; | 136 if (error == OK) |
141 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, error)); | |
142 } else { | |
143 list_complete_ = true; | 137 list_complete_ = true; |
144 CompleteRead(); | 138 CompleteRead(static_cast<Error>(error)); |
145 } | |
146 } | 139 } |
147 | 140 |
148 URLRequestFileDirJob::~URLRequestFileDirJob() {} | 141 URLRequestFileDirJob::~URLRequestFileDirJob() {} |
149 | 142 |
150 void URLRequestFileDirJob::CompleteRead() { | 143 void URLRequestFileDirJob::CompleteRead(Error status) { |
151 if (read_pending_) { | 144 DCHECK_LE(status, OK); |
152 int bytes_read; | 145 DCHECK_NE(status, ERR_IO_PENDING); |
| 146 |
| 147 // Do nothing if there is no read pending. |
| 148 if (!read_pending_) |
| 149 return; |
| 150 |
| 151 int result = status; |
| 152 if (status == OK) { |
| 153 int filled_bytes = 0; |
153 if (FillReadBuffer(read_buffer_->data(), read_buffer_length_, | 154 if (FillReadBuffer(read_buffer_->data(), read_buffer_length_, |
154 &bytes_read)) { | 155 &filled_bytes)) { |
| 156 result = filled_bytes; |
155 // We completed the read, so reset the read buffer. | 157 // We completed the read, so reset the read buffer. |
156 read_pending_ = false; | |
157 read_buffer_ = NULL; | 158 read_buffer_ = NULL; |
158 read_buffer_length_ = 0; | 159 read_buffer_length_ = 0; |
159 | |
160 SetStatus(URLRequestStatus()); | |
161 NotifyReadComplete(bytes_read); | |
162 } else { | 160 } else { |
163 NOTREACHED(); | 161 NOTREACHED(); |
164 // TODO: Better error code. | 162 // TODO: Better error code. |
165 NotifyDone(URLRequestStatus::FromError(ERR_FAILED)); | 163 result = ERR_FAILED; |
166 } | 164 } |
167 } | 165 } |
| 166 |
| 167 read_pending_ = false; |
| 168 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 |