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/location.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/strings/sys_string_conversions.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "base/time/time.h"
15 #include "net/base/io_buffer.h"
16 #include "net/base/net_errors.h"
17 #include "net/base/net_util.h"
18 #include "net/url_request/url_request_status.h"
27 URLRequestFileDirJob::URLRequestFileDirJob(URLRequest
* request
,
28 NetworkDelegate
* network_delegate
,
29 const base::FilePath
& dir_path
)
30 : URLRequestJob(request
, network_delegate
),
31 lister_(dir_path
, this),
34 list_complete_(false),
37 read_buffer_length_(0),
41 void URLRequestFileDirJob::StartAsync() {
44 NotifyHeadersComplete();
47 void URLRequestFileDirJob::Start() {
48 // Start reading asynchronously so that all error reporting and data
49 // callbacks happen as they would for network requests.
50 base::ThreadTaskRunnerHandle::Get()->PostTask(
51 FROM_HERE
, base::Bind(&URLRequestFileDirJob::StartAsync
,
52 weak_factory_
.GetWeakPtr()));
55 void URLRequestFileDirJob::Kill() {
64 URLRequestJob::Kill();
66 weak_factory_
.InvalidateWeakPtrs();
69 bool URLRequestFileDirJob::ReadRawData(IOBuffer
* buf
, int buf_size
,
77 if (FillReadBuffer(buf
->data(), buf_size
, bytes_read
))
80 // We are waiting for more data
83 read_buffer_length_
= buf_size
;
84 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING
, 0));
88 bool URLRequestFileDirJob::GetMimeType(std::string
* mime_type
) const {
89 *mime_type
= "text/html";
93 bool URLRequestFileDirJob::GetCharset(std::string
* charset
) {
94 // All the filenames are converted to UTF-8 before being added.
99 void URLRequestFileDirJob::OnListFile(
100 const DirectoryLister::DirectoryListerData
& data
) {
101 // We wait to write out the header until we get the first file, so that we
102 // can catch errors from DirectoryLister and show an error page.
103 if (!wrote_header_
) {
105 const base::string16
& title
= dir_path_
.value();
106 #elif defined(OS_POSIX)
107 // TODO(jungshik): Add SysNativeMBToUTF16 to sys_string_conversions.
108 // On Mac, need to add NFKC->NFC conversion either here or in file_path.
109 // On Linux, the file system encoding is not defined, but we assume that
110 // SysNativeMBToWide takes care of it at least for now. We can try something
111 // more sophisticated if necessary later.
112 const base::string16
& title
= base::WideToUTF16(
113 base::SysNativeMBToWide(dir_path_
.value()));
115 data_
.append(GetDirectoryListingHeader(title
));
116 wrote_header_
= true;
120 std::string raw_bytes
; // Empty on Windows means UTF-8 encoded name.
121 #elif defined(OS_POSIX)
122 // TOOD(jungshik): The same issue as for the directory name.
123 base::FilePath filename
= data
.info
.GetName();
124 const std::string
& raw_bytes
= filename
.value();
126 data_
.append(GetDirectoryListingEntry(
127 data
.info
.GetName().LossyDisplayName(),
129 data
.info
.IsDirectory(),
131 data
.info
.GetLastModifiedTime()));
133 // TODO(darin): coalesce more?
137 void URLRequestFileDirJob::OnListDone(int error
) {
140 read_pending_
= false;
141 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED
, error
));
143 list_complete_
= true;
148 URLRequestFileDirJob::~URLRequestFileDirJob() {}
150 void URLRequestFileDirJob::CompleteRead() {
153 if (FillReadBuffer(read_buffer_
->data(), read_buffer_length_
,
155 // We completed the read, so reset the read buffer.
156 read_pending_
= false;
158 read_buffer_length_
= 0;
160 SetStatus(URLRequestStatus());
161 NotifyReadComplete(bytes_read
);
164 // TODO: Better error code.
165 NotifyDone(URLRequestStatus::FromError(ERR_FAILED
));
170 bool URLRequestFileDirJob::FillReadBuffer(char* buf
, int buf_size
,
176 int count
= std::min(buf_size
, static_cast<int>(data_
.size()));
178 memcpy(buf
, &data_
[0], count
);
179 data_
.erase(0, count
);
182 } else if (list_complete_
) {