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
3 // found in the LICENSE file.
5 #include "net/url_request/url_request_file_dir_job.h"
8 #include "base/compiler_specific.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/sys_string_conversions.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/time/time.h"
13 #include "net/base/io_buffer.h"
14 #include "net/base/net_errors.h"
15 #include "net/base/net_util.h"
16 #include "net/url_request/url_request_status.h"
25 URLRequestFileDirJob::URLRequestFileDirJob(URLRequest* request,
26 NetworkDelegate* network_delegate,
27 const base::FilePath& dir_path)
28 : URLRequestJob(request, network_delegate),
29 lister_(dir_path, this),
32 list_complete_(false),
35 read_buffer_length_(0),
39 void URLRequestFileDirJob::StartAsync() {
42 NotifyHeadersComplete();
45 void URLRequestFileDirJob::Start() {
46 // Start reading asynchronously so that all error reporting and data
47 // callbacks happen as they would for network requests.
48 base::MessageLoop::current()->PostTask(
50 base::Bind(&URLRequestFileDirJob::StartAsync,
51 weak_factory_.GetWeakPtr()));
54 void URLRequestFileDirJob::Kill() {
63 URLRequestJob::Kill();
65 weak_factory_.InvalidateWeakPtrs();
68 bool URLRequestFileDirJob::ReadRawData(IOBuffer* buf, int buf_size,
76 if (FillReadBuffer(buf->data(), buf_size, bytes_read))
79 // We are waiting for more data
82 read_buffer_length_ = buf_size;
83 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
87 bool URLRequestFileDirJob::GetMimeType(std::string* mime_type) const {
88 *mime_type = "text/html";
92 bool URLRequestFileDirJob::GetCharset(std::string* charset) {
93 // All the filenames are converted to UTF-8 before being added.
98 void URLRequestFileDirJob::OnListFile(
99 const DirectoryLister::DirectoryListerData& data) {
100 // We wait to write out the header until we get the first file, so that we
101 // can catch errors from DirectoryLister and show an error page.
102 if (!wrote_header_) {
104 const base::string16& title = dir_path_.value();
105 #elif defined(OS_POSIX)
106 // 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 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 // more sophisticated if necessary later.
111 const base::string16& title = WideToUTF16(
112 base::SysNativeMBToWide(dir_path_.value()));
114 data_.append(GetDirectoryListingHeader(title));
115 wrote_header_ = true;
119 std::string raw_bytes; // Empty on Windows means UTF-8 encoded name.
120 #elif defined(OS_POSIX)
121 // TOOD(jungshik): The same issue as for the directory name.
122 base::FilePath filename = data.info.GetName();
123 const std::string& raw_bytes = filename.value();
125 data_.append(GetDirectoryListingEntry(
126 data.info.GetName().LossyDisplayName(),
128 data.info.IsDirectory(),
130 data.info.GetLastModifiedTime()));
132 // TODO(darin): coalesce more?
136 void URLRequestFileDirJob::OnListDone(int error) {
139 read_pending_ = false;
140 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, error));
142 list_complete_ = true;
147 URLRequestFileDirJob::~URLRequestFileDirJob() {}
149 void URLRequestFileDirJob::CompleteRead() {
152 if (FillReadBuffer(read_buffer_->data(), read_buffer_length_,
154 // We completed the read, so reset the read buffer.
155 read_pending_ = false;
157 read_buffer_length_ = 0;
159 SetStatus(URLRequestStatus());
160 NotifyReadComplete(bytes_read);
163 // TODO: Better error code.
164 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, 0));
169 bool URLRequestFileDirJob::FillReadBuffer(char* buf, int buf_size,
175 int count = std::min(buf_size, static_cast<int>(data_.size()));
177 memcpy(buf, &data_[0], count);
178 data_.erase(0, count);
181 } else if (list_complete_) {