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"
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/browser/fileapi/file_system_url.h"
23 #include "storage/common/fileapi/directory_entry.h"
24 #include "storage/common/fileapi/file_system_util.h"
27 using net::NetworkDelegate
;
28 using net::URLRequest
;
29 using net::URLRequestJob
;
30 using net::URLRequestStatus
;
34 FileSystemDirURLRequestJob::FileSystemDirURLRequestJob(
36 NetworkDelegate
* network_delegate
,
37 const std::string
& storage_domain
,
38 FileSystemContext
* file_system_context
)
39 : URLRequestJob(request
, network_delegate
),
40 storage_domain_(storage_domain
),
41 file_system_context_(file_system_context
),
45 FileSystemDirURLRequestJob::~FileSystemDirURLRequestJob() {
48 bool FileSystemDirURLRequestJob::ReadRawData(net::IOBuffer
* dest
, int dest_size
,
50 int count
= std::min(dest_size
, static_cast<int>(data_
.size()));
52 memcpy(dest
->data(), data_
.data(), count
);
53 data_
.erase(0, count
);
59 void FileSystemDirURLRequestJob::Start() {
60 base::MessageLoop::current()->PostTask(
62 base::Bind(&FileSystemDirURLRequestJob::StartAsync
,
63 weak_factory_
.GetWeakPtr()));
66 void FileSystemDirURLRequestJob::Kill() {
67 URLRequestJob::Kill();
68 weak_factory_
.InvalidateWeakPtrs();
71 bool FileSystemDirURLRequestJob::GetMimeType(std::string
* mime_type
) const {
72 *mime_type
= "text/html";
76 bool FileSystemDirURLRequestJob::GetCharset(std::string
* charset
) {
81 void FileSystemDirURLRequestJob::StartAsync() {
84 url_
= file_system_context_
->CrackURL(request_
->url());
85 if (!url_
.is_valid()) {
86 file_system_context_
->AttemptAutoMountForURLRequest(
89 base::Bind(&FileSystemDirURLRequestJob::DidAttemptAutoMount
,
90 weak_factory_
.GetWeakPtr()));
93 if (!file_system_context_
->CanServeURLRequest(url_
)) {
94 // In incognito mode the API is not usable and there should be no data.
95 if (url_
.is_valid() && VirtualPath::IsRootPath(url_
.virtual_path())) {
96 // Return an empty directory if the filesystem root is queried.
97 DidReadDirectory(base::File::FILE_OK
,
98 std::vector
<DirectoryEntry
>(),
102 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED
,
103 net::ERR_FILE_NOT_FOUND
));
106 file_system_context_
->operation_runner()->ReadDirectory(
108 base::Bind(&FileSystemDirURLRequestJob::DidReadDirectory
, this));
111 void FileSystemDirURLRequestJob::DidAttemptAutoMount(base::File::Error result
) {
113 file_system_context_
->CrackURL(request_
->url()).is_valid()) {
116 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED
,
117 net::ERR_FILE_NOT_FOUND
));
121 void FileSystemDirURLRequestJob::DidReadDirectory(
122 base::File::Error result
,
123 const std::vector
<DirectoryEntry
>& entries
,
125 if (result
!= base::File::FILE_OK
) {
126 int rv
= net::ERR_FILE_NOT_FOUND
;
127 if (result
== base::File::FILE_ERROR_INVALID_URL
)
128 rv
= net::ERR_INVALID_URL
;
129 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED
, rv
));
137 base::FilePath relative_path
= url_
.path();
138 #if defined(OS_POSIX)
140 base::FilePath(FILE_PATH_LITERAL("/") + relative_path
.value());
142 const base::string16
& title
= relative_path
.LossyDisplayName();
143 data_
.append(net::GetDirectoryListingHeader(title
));
146 typedef std::vector
<DirectoryEntry
>::const_iterator EntryIterator
;
147 for (EntryIterator it
= entries
.begin(); it
!= entries
.end(); ++it
) {
148 const base::string16
& name
= base::FilePath(it
->name
).LossyDisplayName();
149 data_
.append(net::GetDirectoryListingEntry(
150 name
, std::string(), it
->is_directory
, it
->size
,
151 it
->last_modified_time
));
155 set_expected_content_size(data_
.size());
156 NotifyHeadersComplete();
160 } // namespace storage