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/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"
28 using net::NetworkDelegate
;
29 using net::URLRequest
;
30 using net::URLRequestJob
;
31 using net::URLRequestStatus
;
35 FileSystemDirURLRequestJob::FileSystemDirURLRequestJob(
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
),
46 FileSystemDirURLRequestJob::~FileSystemDirURLRequestJob() {
49 bool FileSystemDirURLRequestJob::ReadRawData(net::IOBuffer
* dest
, int dest_size
,
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()));
58 memcpy(dest
->data(), data_
.data(), count
);
59 data_
.erase(0, count
);
65 void FileSystemDirURLRequestJob::Start() {
66 base::MessageLoop::current()->PostTask(
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";
82 bool FileSystemDirURLRequestJob::GetCharset(std::string
* charset
) {
87 void FileSystemDirURLRequestJob::StartAsync() {
90 url_
= file_system_context_
->CrackURL(request_
->url());
91 if (!url_
.is_valid()) {
92 file_system_context_
->AttemptAutoMountForURLRequest(
95 base::Bind(&FileSystemDirURLRequestJob::DidAttemptAutoMount
,
96 weak_factory_
.GetWeakPtr()));
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
>(),
108 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED
,
109 net::ERR_FILE_NOT_FOUND
));
112 file_system_context_
->operation_runner()->ReadDirectory(
114 base::Bind(&FileSystemDirURLRequestJob::DidReadDirectory
, this));
117 void FileSystemDirURLRequestJob::DidAttemptAutoMount(base::File::Error result
) {
119 file_system_context_
->CrackURL(request_
->url()).is_valid()) {
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
,
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
));
143 base::FilePath relative_path
= url_
.path();
144 #if defined(OS_POSIX)
146 base::FilePath(FILE_PATH_LITERAL("/") + relative_path
.value());
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
));
161 set_expected_content_size(data_
.size());
162 NotifyHeadersComplete();
166 } // namespace storage