Misc clean up in browser_shutdown.cc.
[chromium-blink-merge.git] / net / url_request / url_request_file_dir_job.cc
blob64597d569e78bc752fce398d983ac6685d635718
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"
7 #include "base/bind.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"
19 #include "url/gurl.h"
21 #if defined(OS_POSIX)
22 #include <sys/stat.h>
23 #endif
25 namespace net {
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),
32 dir_path_(dir_path),
33 canceled_(false),
34 list_complete_(false),
35 wrote_header_(false),
36 read_pending_(false),
37 read_buffer_length_(0),
38 weak_factory_(this) {
41 void URLRequestFileDirJob::StartAsync() {
42 lister_.Start();
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() {
56 if (canceled_)
57 return;
59 canceled_ = true;
61 if (!list_complete_)
62 lister_.Cancel();
64 URLRequestJob::Kill();
66 weak_factory_.InvalidateWeakPtrs();
69 bool URLRequestFileDirJob::ReadRawData(IOBuffer* buf, int buf_size,
70 int* bytes_read) {
71 DCHECK(bytes_read);
72 *bytes_read = 0;
74 if (is_done())
75 return true;
77 if (FillReadBuffer(buf->data(), buf_size, bytes_read))
78 return true;
80 // We are waiting for more data
81 read_pending_ = true;
82 read_buffer_ = buf;
83 read_buffer_length_ = buf_size;
84 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
85 return false;
88 bool URLRequestFileDirJob::GetMimeType(std::string* mime_type) const {
89 *mime_type = "text/html";
90 return true;
93 bool URLRequestFileDirJob::GetCharset(std::string* charset) {
94 // All the filenames are converted to UTF-8 before being added.
95 *charset = "utf-8";
96 return true;
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_) {
104 #if defined(OS_WIN)
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()));
114 #endif
115 data_.append(GetDirectoryListingHeader(title));
116 wrote_header_ = true;
119 #if defined(OS_WIN)
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();
125 #endif
126 data_.append(GetDirectoryListingEntry(
127 data.info.GetName().LossyDisplayName(),
128 raw_bytes,
129 data.info.IsDirectory(),
130 data.info.GetSize(),
131 data.info.GetLastModifiedTime()));
133 // TODO(darin): coalesce more?
134 CompleteRead();
137 void URLRequestFileDirJob::OnListDone(int error) {
138 DCHECK(!canceled_);
139 if (error != OK) {
140 read_pending_ = false;
141 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, error));
142 } else {
143 list_complete_ = true;
144 CompleteRead();
148 URLRequestFileDirJob::~URLRequestFileDirJob() {}
150 void URLRequestFileDirJob::CompleteRead() {
151 if (read_pending_) {
152 int bytes_read;
153 if (FillReadBuffer(read_buffer_->data(), read_buffer_length_,
154 &bytes_read)) {
155 // We completed the read, so reset the read buffer.
156 read_pending_ = false;
157 read_buffer_ = NULL;
158 read_buffer_length_ = 0;
160 SetStatus(URLRequestStatus());
161 NotifyReadComplete(bytes_read);
162 } else {
163 NOTREACHED();
164 // TODO: Better error code.
165 NotifyDone(URLRequestStatus::FromError(ERR_FAILED));
170 bool URLRequestFileDirJob::FillReadBuffer(char* buf, int buf_size,
171 int* bytes_read) {
172 DCHECK(bytes_read);
174 *bytes_read = 0;
176 int count = std::min(buf_size, static_cast<int>(data_.size()));
177 if (count) {
178 memcpy(buf, &data_[0], count);
179 data_.erase(0, count);
180 *bytes_read = count;
181 return true;
182 } else if (list_complete_) {
183 // EOF
184 return true;
186 return false;
189 } // namespace net