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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
// 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/tools/test_shell/simple_file_system.h"
#include "base/file_path.h"
#include "base/memory/scoped_callback_factory.h"
#include "base/message_loop.h"
#include "base/message_loop_proxy.h"
#include "base/time.h"
#include "base/utf_string_conversions.h"
#include "googleurl/src/gurl.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFileInfo.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFileSystemCallbacks.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFileSystemEntry.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebVector.h"
#include "webkit/fileapi/file_system_callback_dispatcher.h"
#include "webkit/fileapi/file_system_context.h"
#include "webkit/fileapi/file_system_file_util.h"
#include "webkit/fileapi/file_system_operation.h"
#include "webkit/fileapi/file_system_path_manager.h"
#include "webkit/fileapi/file_system_types.h"
#include "webkit/glue/webkit_glue.h"
#include "webkit/tools/test_shell/simple_file_writer.h"
using base::WeakPtr;
using WebKit::WebFileInfo;
using WebKit::WebFileSystem;
using WebKit::WebFileSystemCallbacks;
using WebKit::WebFileSystemEntry;
using WebKit::WebFileWriter;
using WebKit::WebFileWriterClient;
using WebKit::WebFrame;
using WebKit::WebSecurityOrigin;
using WebKit::WebString;
using WebKit::WebURL;
using WebKit::WebVector;
using fileapi::FileSystemCallbackDispatcher;
using fileapi::FileSystemContext;
using fileapi::FileSystemFileUtil;
using fileapi::FileSystemOperation;
namespace {
class SimpleFileSystemCallbackDispatcher
: public FileSystemCallbackDispatcher {
public:
SimpleFileSystemCallbackDispatcher(
const WeakPtr<SimpleFileSystem>& file_system,
WebFileSystemCallbacks* callbacks)
: file_system_(file_system),
callbacks_(callbacks) {
}
~SimpleFileSystemCallbackDispatcher() {
}
virtual void DidSucceed() {
DCHECK(file_system_);
callbacks_->didSucceed();
}
virtual void DidReadMetadata(const base::PlatformFileInfo& info,
const FilePath& platform_path) {
DCHECK(file_system_);
WebFileInfo web_file_info;
web_file_info.length = info.size;
web_file_info.modificationTime = info.last_modified.ToDoubleT();
web_file_info.type = info.is_directory ?
WebFileInfo::TypeDirectory : WebFileInfo::TypeFile;
web_file_info.platformPath =
webkit_glue::FilePathToWebString(platform_path);
callbacks_->didReadMetadata(web_file_info);
}
virtual void DidReadDirectory(
const std::vector<base::FileUtilProxy::Entry>& entries,
bool has_more) {
DCHECK(file_system_);
std::vector<WebFileSystemEntry> web_entries_vector;
for (std::vector<base::FileUtilProxy::Entry>::const_iterator it =
entries.begin(); it != entries.end(); ++it) {
WebFileSystemEntry entry;
entry.name = webkit_glue::FilePathStringToWebString(it->name);
entry.isDirectory = it->is_directory;
web_entries_vector.push_back(entry);
}
WebVector<WebKit::WebFileSystemEntry> web_entries =
web_entries_vector;
callbacks_->didReadDirectory(web_entries, has_more);
}
virtual void DidOpenFileSystem(
const std::string& name, const GURL& root) {
DCHECK(file_system_);
if (!root.is_valid())
callbacks_->didFail(WebKit::WebFileErrorSecurity);
else
// Temporary hack to ease a 4-phase Chromium/WebKit commit.
#ifdef WEBFILESYSTEMCALLBACKS_USE_URL_NOT_STRING
callbacks_->didOpenFileSystem(WebString::fromUTF8(name), root);
#else
callbacks_->didOpenFileSystem(
WebString::fromUTF8(name), WebString::fromUTF8(root.spec()));
#endif
}
virtual void DidFail(base::PlatformFileError error_code) {
DCHECK(file_system_);
callbacks_->didFail(
webkit_glue::PlatformFileErrorToWebFileError(error_code));
}
virtual void DidWrite(int64, bool) {
NOTREACHED();
}
private:
WeakPtr<SimpleFileSystem> file_system_;
WebFileSystemCallbacks* callbacks_;
};
} // namespace
SimpleFileSystem::SimpleFileSystem() {
if (file_system_dir_.CreateUniqueTempDir()) {
file_system_context_ = new FileSystemContext(
base::MessageLoopProxy::CreateForCurrentThread(),
base::MessageLoopProxy::CreateForCurrentThread(),
NULL /* special storage policy */,
NULL /* quota manager */,
file_system_dir_.path(),
false /* incognito */,
true /* allow_file_access */,
true /* unlimited_quota */,
NULL);
} else {
LOG(WARNING) << "Failed to create a temp dir for the filesystem."
"FileSystem feature will be disabled.";
}
}
SimpleFileSystem::~SimpleFileSystem() {
}
void SimpleFileSystem::OpenFileSystem(
WebFrame* frame, WebFileSystem::Type web_filesystem_type,
long long, bool create,
WebFileSystemCallbacks* callbacks) {
if (!frame || !file_system_context_.get()) {
// The FileSystem temp directory was not initialized successfully.
callbacks->didFail(WebKit::WebFileErrorSecurity);
return;
}
fileapi::FileSystemType type;
if (web_filesystem_type == WebFileSystem::TypeTemporary)
type = fileapi::kFileSystemTypeTemporary;
else if (web_filesystem_type == WebFileSystem::TypePersistent)
type = fileapi::kFileSystemTypePersistent;
else if (web_filesystem_type == WebFileSystem::TypeExternal)
type = fileapi::kFileSystemTypeExternal;
else {
// Unknown type filesystem is requested.
callbacks->didFail(WebKit::WebFileErrorSecurity);
return;
}
GURL origin_url(frame->document().securityOrigin().toString());
GetNewOperation(callbacks)->OpenFileSystem(origin_url, type, create);
}
void SimpleFileSystem::move(const WebString& src_path,
const WebString& dest_path,
WebFileSystemCallbacks* callbacks) {
move(GURL(src_path), GURL(dest_path), callbacks);
}
void SimpleFileSystem::copy(const WebString& src_path,
const WebString& dest_path,
WebFileSystemCallbacks* callbacks) {
copy(GURL(src_path), GURL(dest_path), callbacks);
}
void SimpleFileSystem::remove(const WebString& path,
WebFileSystemCallbacks* callbacks) {
remove(GURL(path), callbacks);
}
void SimpleFileSystem::removeRecursively(const WebString& path,
WebFileSystemCallbacks* callbacks) {
removeRecursively(GURL(path), callbacks);
}
void SimpleFileSystem::readMetadata(const WebString& path,
WebFileSystemCallbacks* callbacks) {
readMetadata(GURL(path), callbacks);
}
void SimpleFileSystem::createFile(const WebString& path,
bool exclusive,
WebFileSystemCallbacks* callbacks) {
createFile(GURL(path), exclusive, callbacks);
}
void SimpleFileSystem::createDirectory(const WebString& path,
bool exclusive,
WebFileSystemCallbacks* callbacks) {
createDirectory(GURL(path), exclusive, callbacks);
}
void SimpleFileSystem::fileExists(const WebString& path,
WebFileSystemCallbacks* callbacks) {
fileExists(GURL(path), callbacks);
}
void SimpleFileSystem::directoryExists(const WebString& path,
WebFileSystemCallbacks* callbacks) {
directoryExists(GURL(path), callbacks);
}
void SimpleFileSystem::readDirectory(const WebString& path,
WebFileSystemCallbacks* callbacks) {
readDirectory(GURL(path), callbacks);
}
WebKit::WebFileWriter* SimpleFileSystem::createFileWriter(
const WebString& path, WebKit::WebFileWriterClient* client) {
return createFileWriter(GURL(path), client);
}
void SimpleFileSystem::move(
const WebURL& src_path,
const WebURL& dest_path, WebFileSystemCallbacks* callbacks) {
GetNewOperation(callbacks)->Move(GURL(src_path), GURL(dest_path));
}
void SimpleFileSystem::copy(
const WebURL& src_path, const WebURL& dest_path,
WebFileSystemCallbacks* callbacks) {
GetNewOperation(callbacks)->Copy(GURL(src_path), GURL(dest_path));
}
void SimpleFileSystem::remove(
const WebURL& path, WebFileSystemCallbacks* callbacks) {
GetNewOperation(callbacks)->Remove(path, false /* recursive */);
}
void SimpleFileSystem::removeRecursively(
const WebURL& path, WebFileSystemCallbacks* callbacks) {
GetNewOperation(callbacks)->Remove(path, true /* recursive */);
}
void SimpleFileSystem::readMetadata(
const WebURL& path, WebFileSystemCallbacks* callbacks) {
GetNewOperation(callbacks)->GetMetadata(path);
}
void SimpleFileSystem::createFile(
const WebURL& path, bool exclusive, WebFileSystemCallbacks* callbacks) {
GetNewOperation(callbacks)->CreateFile(path, exclusive);
}
void SimpleFileSystem::createDirectory(
const WebURL& path, bool exclusive, WebFileSystemCallbacks* callbacks) {
GetNewOperation(callbacks)->CreateDirectory(path, exclusive, false);
}
void SimpleFileSystem::fileExists(
const WebURL& path, WebFileSystemCallbacks* callbacks) {
GetNewOperation(callbacks)->FileExists(path);
}
void SimpleFileSystem::directoryExists(
const WebURL& path, WebFileSystemCallbacks* callbacks) {
GetNewOperation(callbacks)->DirectoryExists(path);
}
void SimpleFileSystem::readDirectory(
const WebURL& path, WebFileSystemCallbacks* callbacks) {
GetNewOperation(callbacks)->ReadDirectory(path);
}
WebFileWriter* SimpleFileSystem::createFileWriter(
const WebURL& path, WebFileWriterClient* client) {
return new SimpleFileWriter(path, client, file_system_context_.get());
}
FileSystemOperation* SimpleFileSystem::GetNewOperation(
WebFileSystemCallbacks* callbacks) {
SimpleFileSystemCallbackDispatcher* dispatcher =
new SimpleFileSystemCallbackDispatcher(AsWeakPtr(), callbacks);
FileSystemOperation* operation = new FileSystemOperation(
dispatcher, base::MessageLoopProxy::CreateForCurrentThread(),
file_system_context_.get(), NULL);
return operation;
}
|