Vectorize website settings icons in omnibox
[chromium-blink-merge.git] / storage / browser / fileapi / file_system_dir_url_request_job.cc
blob35b6c1d8d63d9644c7c6861a35be3e366d79f440
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 "storage/browser/fileapi/file_system_dir_url_request_job.h"
7 #include <algorithm>
9 #include "base/bind.h"
10 #include "base/compiler_specific.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/strings/sys_string_conversions.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/time/time.h"
15 #include "build/build_config.h"
16 #include "net/base/io_buffer.h"
17 #include "net/base/net_errors.h"
18 #include "net/base/net_util.h"
19 #include "net/url_request/url_request.h"
20 #include "storage/browser/fileapi/file_system_context.h"
21 #include "storage/browser/fileapi/file_system_operation_runner.h"
22 #include "storage/common/fileapi/directory_entry.h"
23 #include "storage/common/fileapi/file_system_util.h"
24 #include "url/gurl.h"
26 using net::NetworkDelegate;
27 using net::URLRequest;
28 using net::URLRequestJob;
29 using net::URLRequestStatus;
31 namespace storage {
33 FileSystemDirURLRequestJob::FileSystemDirURLRequestJob(
34 URLRequest* request,
35 NetworkDelegate* network_delegate,
36 const std::string& storage_domain,
37 FileSystemContext* file_system_context)
38 : URLRequestJob(request, network_delegate),
39 storage_domain_(storage_domain),
40 file_system_context_(file_system_context),
41 weak_factory_(this) {
44 FileSystemDirURLRequestJob::~FileSystemDirURLRequestJob() {
47 bool FileSystemDirURLRequestJob::ReadRawData(net::IOBuffer* dest, int dest_size,
48 int *bytes_read) {
49 int count = std::min(dest_size, static_cast<int>(data_.size()));
50 if (count > 0) {
51 memcpy(dest->data(), data_.data(), count);
52 data_.erase(0, count);
54 *bytes_read = count;
55 return true;
58 void FileSystemDirURLRequestJob::Start() {
59 base::MessageLoop::current()->PostTask(
60 FROM_HERE,
61 base::Bind(&FileSystemDirURLRequestJob::StartAsync,
62 weak_factory_.GetWeakPtr()));
65 void FileSystemDirURLRequestJob::Kill() {
66 URLRequestJob::Kill();
67 weak_factory_.InvalidateWeakPtrs();
70 bool FileSystemDirURLRequestJob::GetMimeType(std::string* mime_type) const {
71 *mime_type = "text/html";
72 return true;
75 bool FileSystemDirURLRequestJob::GetCharset(std::string* charset) {
76 *charset = "utf-8";
77 return true;
80 void FileSystemDirURLRequestJob::StartAsync() {
81 if (!request_)
82 return;
83 url_ = file_system_context_->CrackURL(request_->url());
84 if (!url_.is_valid()) {
85 file_system_context_->AttemptAutoMountForURLRequest(
86 request_,
87 storage_domain_,
88 base::Bind(&FileSystemDirURLRequestJob::DidAttemptAutoMount,
89 weak_factory_.GetWeakPtr()));
90 return;
92 if (!file_system_context_->CanServeURLRequest(url_)) {
93 // In incognito mode the API is not usable and there should be no data.
94 if (url_.is_valid() && VirtualPath::IsRootPath(url_.virtual_path())) {
95 // Return an empty directory if the filesystem root is queried.
96 DidReadDirectory(base::File::FILE_OK,
97 std::vector<DirectoryEntry>(),
98 false);
99 return;
101 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED,
102 net::ERR_FILE_NOT_FOUND));
103 return;
105 file_system_context_->operation_runner()->ReadDirectory(
106 url_,
107 base::Bind(&FileSystemDirURLRequestJob::DidReadDirectory, this));
110 void FileSystemDirURLRequestJob::DidAttemptAutoMount(base::File::Error result) {
111 if (result >= 0 &&
112 file_system_context_->CrackURL(request_->url()).is_valid()) {
113 StartAsync();
114 } else {
115 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED,
116 net::ERR_FILE_NOT_FOUND));
120 void FileSystemDirURLRequestJob::DidReadDirectory(
121 base::File::Error result,
122 const std::vector<DirectoryEntry>& entries,
123 bool has_more) {
124 if (result != base::File::FILE_OK) {
125 int rv = net::ERR_FILE_NOT_FOUND;
126 if (result == base::File::FILE_ERROR_INVALID_URL)
127 rv = net::ERR_INVALID_URL;
128 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv));
129 return;
132 if (!request_)
133 return;
135 if (data_.empty()) {
136 base::FilePath relative_path = url_.path();
137 #if defined(OS_POSIX)
138 relative_path =
139 base::FilePath(FILE_PATH_LITERAL("/") + relative_path.value());
140 #endif
141 const base::string16& title = relative_path.LossyDisplayName();
142 data_.append(net::GetDirectoryListingHeader(title));
145 typedef std::vector<DirectoryEntry>::const_iterator EntryIterator;
146 for (EntryIterator it = entries.begin(); it != entries.end(); ++it) {
147 const base::string16& name = base::FilePath(it->name).LossyDisplayName();
148 data_.append(net::GetDirectoryListingEntry(
149 name, std::string(), it->is_directory, it->size,
150 it->last_modified_time));
153 if (!has_more) {
154 set_expected_content_size(data_.size());
155 NotifyHeadersComplete();
159 } // namespace storage