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