1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "webkit/fileapi/file_system_dir_url_request_job.h"
#include <algorithm>
#include "base/compiler_specific.h"
#include "base/file_util_proxy.h"
#include "base/message_loop.h"
#include "base/platform_file.h"
#include "base/sys_string_conversions.h"
#include "base/time.h"
#include "base/utf_string_conversions.h"
#include "build/build_config.h"
#include "googleurl/src/gurl.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/base/net_util.h"
#include "net/url_request/url_request.h"
#include "webkit/fileapi/file_system_path_manager.h"
#include "webkit/fileapi/file_system_util.h"
using net::URLRequest;
using net::URLRequestJob;
using net::URLRequestStatus;
namespace fileapi {
FileSystemDirURLRequestJob::FileSystemDirURLRequestJob(
URLRequest* request, FileSystemPathManager* path_manager,
scoped_refptr<base::MessageLoopProxy> file_thread_proxy)
: URLRequestJob(request),
path_manager_(path_manager),
ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)),
ALLOW_THIS_IN_INITIALIZER_LIST(callback_factory_(this)),
file_thread_proxy_(file_thread_proxy) {
}
FileSystemDirURLRequestJob::~FileSystemDirURLRequestJob() {
}
void FileSystemDirURLRequestJob::Start() {
MessageLoop::current()->PostTask(FROM_HERE,
method_factory_.NewRunnableMethod(
&FileSystemDirURLRequestJob::StartAsync));
}
void FileSystemDirURLRequestJob::Kill() {
URLRequestJob::Kill();
callback_factory_.RevokeAll();
}
bool FileSystemDirURLRequestJob::ReadRawData(net::IOBuffer* dest, int dest_size,
int *bytes_read) {
int count = std::min(dest_size, static_cast<int>(data_.size()));
if (count > 0) {
memcpy(dest->data(), data_.data(), count);
data_.erase(0, count);
}
*bytes_read = count;
return true;
}
bool FileSystemDirURLRequestJob::GetMimeType(std::string* mime_type) const {
*mime_type = "text/html";
return true;
}
bool FileSystemDirURLRequestJob::GetCharset(std::string* charset) {
*charset = "utf-8";
return true;
}
void FileSystemDirURLRequestJob::StartAsync() {
GURL origin_url;
FileSystemType type;
if (!CrackFileSystemURL(request_->url(), &origin_url, &type,
&relative_dir_path_)) {
NotifyFailed(net::ERR_INVALID_URL);
return;
}
path_manager_->GetFileSystemRootPath(
origin_url, type, false, // create
callback_factory_.NewCallback(
&FileSystemDirURLRequestJob::DidGetRootPath));
}
void FileSystemDirURLRequestJob::DidGetRootPath(bool success,
const FilePath& root_path,
const std::string& name) {
if (!success) {
NotifyFailed(net::ERR_FILE_NOT_FOUND);
return;
}
absolute_dir_path_ = root_path.Append(relative_dir_path_);
// We assume it's a directory if we've gotten here: either the path
// ends with '/', or FileSystemDirURLRequestJob already statted it and
// found it to be a directory.
base::FileUtilProxy::ReadDirectory(file_thread_proxy_, absolute_dir_path_,
callback_factory_.NewCallback(
&FileSystemDirURLRequestJob::DidReadDirectory));
}
void FileSystemDirURLRequestJob::DidReadDirectory(
base::PlatformFileError error_code,
const std::vector<base::FileUtilProxy::Entry>& entries) {
if (error_code != base::PLATFORM_FILE_OK) {
NotifyFailed(error_code);
return;
}
#if defined(OS_WIN)
const string16& title = relative_dir_path_.value();
#elif defined(OS_POSIX)
const string16& title = WideToUTF16(
base::SysNativeMBToWide(relative_dir_path_.value()));
#endif
data_.append(net::GetDirectoryListingHeader(ASCIIToUTF16("/") + title));
typedef std::vector<base::FileUtilProxy::Entry>::const_iterator EntryIterator;
for (EntryIterator it = entries.begin(); it != entries.end(); ++it) {
#if defined(OS_WIN)
const string16& name = it->name;
#elif defined(OS_POSIX)
const string16& name =
WideToUTF16(base::SysNativeMBToWide(it->name));
#endif
// TODO(adamk): Add file size?
data_.append(net::GetDirectoryListingEntry(
name, std::string(), it->is_directory, 0, base::Time()));
}
set_expected_content_size(data_.size());
NotifyHeadersComplete();
}
void FileSystemDirURLRequestJob::NotifyFailed(int rv) {
NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv));
}
} // namespace fileapi
|