We started redesigning GpuMemoryBuffer interface to handle multiple buffers [0].
[chromium-blink-merge.git] / storage / browser / fileapi / file_system_dir_url_request_job.cc
blob07b8f8d170f1d8c10b5921c6aa7802d4be19569a
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/profiler/scoped_tracker.h"
13 #include "base/strings/sys_string_conversions.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/time/time.h"
16 #include "build/build_config.h"
17 #include "net/base/io_buffer.h"
18 #include "net/base/net_errors.h"
19 #include "net/base/net_util.h"
20 #include "net/url_request/url_request.h"
21 #include "storage/browser/fileapi/file_system_context.h"
22 #include "storage/browser/fileapi/file_system_operation_runner.h"
23 #include "storage/browser/fileapi/file_system_url.h"
24 #include "storage/common/fileapi/directory_entry.h"
25 #include "storage/common/fileapi/file_system_util.h"
26 #include "url/gurl.h"
28 using net::NetworkDelegate;
29 using net::URLRequest;
30 using net::URLRequestJob;
31 using net::URLRequestStatus;
33 namespace storage {
35 FileSystemDirURLRequestJob::FileSystemDirURLRequestJob(
36 URLRequest* request,
37 NetworkDelegate* network_delegate,
38 const std::string& storage_domain,
39 FileSystemContext* file_system_context)
40 : URLRequestJob(request, network_delegate),
41 storage_domain_(storage_domain),
42 file_system_context_(file_system_context),
43 weak_factory_(this) {
46 FileSystemDirURLRequestJob::~FileSystemDirURLRequestJob() {
49 bool FileSystemDirURLRequestJob::ReadRawData(net::IOBuffer* dest, int dest_size,
50 int *bytes_read) {
51 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
52 tracked_objects::ScopedTracker tracking_profile(
53 FROM_HERE_WITH_EXPLICIT_FUNCTION(
54 "423948 FileSystemDirURLRequestJob::ReadRawData"));
56 int count = std::min(dest_size, static_cast<int>(data_.size()));
57 if (count > 0) {
58 memcpy(dest->data(), data_.data(), count);
59 data_.erase(0, count);
61 *bytes_read = count;
62 return true;
65 void FileSystemDirURLRequestJob::Start() {
66 base::MessageLoop::current()->PostTask(
67 FROM_HERE,
68 base::Bind(&FileSystemDirURLRequestJob::StartAsync,
69 weak_factory_.GetWeakPtr()));
72 void FileSystemDirURLRequestJob::Kill() {
73 URLRequestJob::Kill();
74 weak_factory_.InvalidateWeakPtrs();
77 bool FileSystemDirURLRequestJob::GetMimeType(std::string* mime_type) const {
78 *mime_type = "text/html";
79 return true;
82 bool FileSystemDirURLRequestJob::GetCharset(std::string* charset) {
83 *charset = "utf-8";
84 return true;
87 void FileSystemDirURLRequestJob::StartAsync() {
88 if (!request_)
89 return;
90 url_ = file_system_context_->CrackURL(request_->url());
91 if (!url_.is_valid()) {
92 file_system_context_->AttemptAutoMountForURLRequest(
93 request_,
94 storage_domain_,
95 base::Bind(&FileSystemDirURLRequestJob::DidAttemptAutoMount,
96 weak_factory_.GetWeakPtr()));
97 return;
99 if (!file_system_context_->CanServeURLRequest(url_)) {
100 // In incognito mode the API is not usable and there should be no data.
101 if (url_.is_valid() && VirtualPath::IsRootPath(url_.virtual_path())) {
102 // Return an empty directory if the filesystem root is queried.
103 DidReadDirectory(base::File::FILE_OK,
104 std::vector<DirectoryEntry>(),
105 false);
106 return;
108 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED,
109 net::ERR_FILE_NOT_FOUND));
110 return;
112 file_system_context_->operation_runner()->ReadDirectory(
113 url_,
114 base::Bind(&FileSystemDirURLRequestJob::DidReadDirectory, this));
117 void FileSystemDirURLRequestJob::DidAttemptAutoMount(base::File::Error result) {
118 if (result >= 0 &&
119 file_system_context_->CrackURL(request_->url()).is_valid()) {
120 StartAsync();
121 } else {
122 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED,
123 net::ERR_FILE_NOT_FOUND));
127 void FileSystemDirURLRequestJob::DidReadDirectory(
128 base::File::Error result,
129 const std::vector<DirectoryEntry>& entries,
130 bool has_more) {
131 if (result != base::File::FILE_OK) {
132 int rv = net::ERR_FILE_NOT_FOUND;
133 if (result == base::File::FILE_ERROR_INVALID_URL)
134 rv = net::ERR_INVALID_URL;
135 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv));
136 return;
139 if (!request_)
140 return;
142 if (data_.empty()) {
143 base::FilePath relative_path = url_.path();
144 #if defined(OS_POSIX)
145 relative_path =
146 base::FilePath(FILE_PATH_LITERAL("/") + relative_path.value());
147 #endif
148 const base::string16& title = relative_path.LossyDisplayName();
149 data_.append(net::GetDirectoryListingHeader(title));
152 typedef std::vector<DirectoryEntry>::const_iterator EntryIterator;
153 for (EntryIterator it = entries.begin(); it != entries.end(); ++it) {
154 const base::string16& name = base::FilePath(it->name).LossyDisplayName();
155 data_.append(net::GetDirectoryListingEntry(
156 name, std::string(), it->is_directory, it->size,
157 it->last_modified_time));
160 if (!has_more) {
161 set_expected_content_size(data_.size());
162 NotifyHeadersComplete();
166 } // namespace storage