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