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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
// Copyright (c) 2010 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/glue/plugins/pepper_file_ref.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "ppapi/c/pp_errors.h"
#include "webkit/glue/plugins/pepper_common.h"
#include "webkit/glue/plugins/pepper_directory_reader.h"
#include "webkit/glue/plugins/pepper_file_callbacks.h"
#include "webkit/glue/plugins/pepper_file_system.h"
#include "webkit/glue/plugins/pepper_plugin_delegate.h"
#include "webkit/glue/plugins/pepper_plugin_instance.h"
#include "webkit/glue/plugins/pepper_plugin_module.h"
#include "webkit/glue/plugins/pepper_var.h"
namespace pepper {
namespace {
bool IsValidLocalPath(const std::string& path) {
// The path must start with '/'
if (path.empty() || path[0] != '/')
return false;
// The path must contain valid UTF-8 characters.
if (!IsStringUTF8(path))
return false;
return true;
}
void TrimTrailingSlash(std::string* path) {
// If this path ends with a slash, then normalize it away unless path is the
// root path.
if (path->size() > 1 && path->at(path->size() - 1) == '/')
path->erase(path->size() - 1, 1);
}
PP_Resource Create(PP_Resource file_system_id, const char* path) {
scoped_refptr<FileSystem> file_system(
Resource::GetAs<FileSystem>(file_system_id));
if (!file_system)
return 0;
if (!file_system->instance())
return 0;
std::string validated_path(path);
if (!IsValidLocalPath(validated_path))
return 0;
TrimTrailingSlash(&validated_path);
FileRef* file_ref = new FileRef(file_system->instance()->module(),
file_system,
validated_path);
return file_ref->GetReference();
}
PP_Bool IsFileRef(PP_Resource resource) {
return BoolToPPBool(!!Resource::GetAs<FileRef>(resource));
}
PP_FileSystemType_Dev GetFileSystemType(PP_Resource file_ref_id) {
scoped_refptr<FileRef> file_ref(Resource::GetAs<FileRef>(file_ref_id));
if (!file_ref)
return PP_FILESYSTEMTYPE_EXTERNAL;
return file_ref->GetFileSystemType();
}
PP_Var GetName(PP_Resource file_ref_id) {
scoped_refptr<FileRef> file_ref(Resource::GetAs<FileRef>(file_ref_id));
if (!file_ref)
return PP_MakeUndefined();
return StringVar::StringToPPVar(file_ref->module(), file_ref->GetName());
}
PP_Var GetPath(PP_Resource file_ref_id) {
scoped_refptr<FileRef> file_ref(Resource::GetAs<FileRef>(file_ref_id));
if (!file_ref)
return PP_MakeUndefined();
if (file_ref->GetFileSystemType() == PP_FILESYSTEMTYPE_EXTERNAL)
return PP_MakeUndefined();
return StringVar::StringToPPVar(file_ref->module(), file_ref->GetPath());
}
PP_Resource GetParent(PP_Resource file_ref_id) {
scoped_refptr<FileRef> file_ref(Resource::GetAs<FileRef>(file_ref_id));
if (!file_ref)
return 0;
if (file_ref->GetFileSystemType() == PP_FILESYSTEMTYPE_EXTERNAL)
return 0;
scoped_refptr<FileRef> parent_ref(file_ref->GetParent());
if (!parent_ref)
return 0;
return parent_ref->GetReference();
}
int32_t MakeDirectory(PP_Resource directory_ref_id,
PP_Bool make_ancestors,
PP_CompletionCallback callback) {
scoped_refptr<FileRef> directory_ref(
Resource::GetAs<FileRef>(directory_ref_id));
if (!directory_ref)
return PP_ERROR_BADRESOURCE;
scoped_refptr<FileSystem> file_system = directory_ref->GetFileSystem();
if (!file_system || !file_system->opened() ||
(file_system->type() == PP_FILESYSTEMTYPE_EXTERNAL))
return PP_ERROR_NOACCESS;
PluginInstance* instance = file_system->instance();
if (!instance->delegate()->MakeDirectory(
directory_ref->GetSystemPath(), PPBoolToBool(make_ancestors),
new FileCallbacks(instance->module()->AsWeakPtr(),
callback, NULL, NULL, NULL)))
return PP_ERROR_FAILED;
return PP_ERROR_WOULDBLOCK;
}
int32_t Query(PP_Resource file_ref_id,
PP_FileInfo_Dev* info,
PP_CompletionCallback callback) {
scoped_refptr<FileRef> file_ref(Resource::GetAs<FileRef>(file_ref_id));
if (!file_ref)
return PP_ERROR_BADRESOURCE;
scoped_refptr<FileSystem> file_system = file_ref->GetFileSystem();
if (!file_system || !file_system->opened() ||
(file_system->type() == PP_FILESYSTEMTYPE_EXTERNAL))
return PP_ERROR_NOACCESS;
PluginInstance* instance = file_system->instance();
if (!instance->delegate()->Query(
file_ref->GetSystemPath(),
new FileCallbacks(instance->module()->AsWeakPtr(),
callback, info, file_system, NULL)))
return PP_ERROR_FAILED;
return PP_ERROR_WOULDBLOCK;
}
int32_t Touch(PP_Resource file_ref_id,
PP_Time last_access_time,
PP_Time last_modified_time,
PP_CompletionCallback callback) {
scoped_refptr<FileRef> file_ref(Resource::GetAs<FileRef>(file_ref_id));
if (!file_ref)
return PP_ERROR_BADRESOURCE;
scoped_refptr<FileSystem> file_system = file_ref->GetFileSystem();
if (!file_system || !file_system->opened() ||
(file_system->type() == PP_FILESYSTEMTYPE_EXTERNAL))
return PP_ERROR_NOACCESS;
PluginInstance* instance = file_system->instance();
if (!instance->delegate()->Touch(
file_ref->GetSystemPath(), base::Time::FromDoubleT(last_access_time),
base::Time::FromDoubleT(last_modified_time),
new FileCallbacks(instance->module()->AsWeakPtr(),
callback, NULL, NULL, NULL)))
return PP_ERROR_FAILED;
return PP_ERROR_WOULDBLOCK;
}
int32_t Delete(PP_Resource file_ref_id,
PP_CompletionCallback callback) {
scoped_refptr<FileRef> file_ref(Resource::GetAs<FileRef>(file_ref_id));
if (!file_ref)
return PP_ERROR_BADRESOURCE;
scoped_refptr<FileSystem> file_system = file_ref->GetFileSystem();
if (!file_system || !file_system->opened() ||
(file_system->type() == PP_FILESYSTEMTYPE_EXTERNAL))
return PP_ERROR_NOACCESS;
PluginInstance* instance = file_system->instance();
if (!instance->delegate()->Delete(
file_ref->GetSystemPath(),
new FileCallbacks(instance->module()->AsWeakPtr(),
callback, NULL, NULL, NULL)))
return PP_ERROR_FAILED;
return PP_ERROR_WOULDBLOCK;
}
int32_t Rename(PP_Resource file_ref_id,
PP_Resource new_file_ref_id,
PP_CompletionCallback callback) {
scoped_refptr<FileRef> file_ref(Resource::GetAs<FileRef>(file_ref_id));
if (!file_ref)
return PP_ERROR_BADRESOURCE;
scoped_refptr<FileRef> new_file_ref(
Resource::GetAs<FileRef>(new_file_ref_id));
if (!new_file_ref)
return PP_ERROR_BADRESOURCE;
scoped_refptr<FileSystem> file_system = file_ref->GetFileSystem();
if (!file_system || !file_system->opened() ||
(file_system != new_file_ref->GetFileSystem()) ||
(file_system->type() == PP_FILESYSTEMTYPE_EXTERNAL))
return PP_ERROR_NOACCESS;
PluginInstance* instance = file_system->instance();
if (!instance->delegate()->Rename(
file_ref->GetSystemPath(), new_file_ref->GetSystemPath(),
new FileCallbacks(instance->module()->AsWeakPtr(),
callback, NULL, NULL, NULL)))
return PP_ERROR_FAILED;
return PP_ERROR_WOULDBLOCK;
}
const PPB_FileRef_Dev ppb_fileref = {
&Create,
&IsFileRef,
&GetFileSystemType,
&GetName,
&GetPath,
&GetParent,
&MakeDirectory,
&Query,
&Touch,
&Delete,
&Rename
};
} // namespace
FileRef::FileRef()
: Resource(NULL),
file_system_(NULL) {
}
FileRef::FileRef(PluginModule* module,
scoped_refptr<FileSystem> file_system,
const std::string& validated_path)
: Resource(module),
file_system_(file_system),
virtual_path_(validated_path) {
}
FileRef::FileRef(PluginModule* module,
const FilePath& external_file_path)
: Resource(module),
file_system_(NULL),
system_path_(external_file_path) {
}
FileRef::~FileRef() {
}
// static
const PPB_FileRef_Dev* FileRef::GetInterface() {
return &ppb_fileref;
}
std::string FileRef::GetName() const {
if (GetFileSystemType() == PP_FILESYSTEMTYPE_EXTERNAL) {
FilePath::StringType path = system_path_.value();
size_t pos = path.rfind(FilePath::kSeparators[0]);
DCHECK(pos != FilePath::StringType::npos);
#if defined(OS_WIN)
return WideToUTF8(path.substr(pos + 1));
#elif defined(OS_POSIX)
return path.substr(pos + 1);
#else
#error "Unsupported platform."
#endif
}
if (virtual_path_.size() == 1 && virtual_path_[0] == '/')
return virtual_path_;
// There should always be a leading slash at least!
size_t pos = virtual_path_.rfind('/');
DCHECK(pos != std::string::npos);
return virtual_path_.substr(pos + 1);
}
scoped_refptr<FileRef> FileRef::GetParent() {
if (GetFileSystemType() == PP_FILESYSTEMTYPE_EXTERNAL)
return new FileRef();
// There should always be a leading slash at least!
size_t pos = virtual_path_.rfind('/');
DCHECK(pos != std::string::npos);
// If the path is "/foo", then we want to include the slash.
if (pos == 0)
pos++;
std::string parent_path = virtual_path_.substr(0, pos);
FileRef* parent_ref = new FileRef(module(), file_system_, parent_path);
return parent_ref;
}
scoped_refptr<FileSystem> FileRef::GetFileSystem() const {
return file_system_;
}
PP_FileSystemType_Dev FileRef::GetFileSystemType() const {
if (!file_system_)
return PP_FILESYSTEMTYPE_EXTERNAL;
return file_system_->type();
}
std::string FileRef::GetPath() const {
return virtual_path_;
}
FilePath FileRef::GetSystemPath() const {
if (GetFileSystemType() == PP_FILESYSTEMTYPE_EXTERNAL)
return system_path_;
// Since |virtual_path_| starts with a '/', it is considered an absolute path
// on POSIX systems. We need to remove the '/' before calling Append() or we
// will run into a DCHECK.
FilePath virtual_file_path(
#if defined(OS_WIN)
UTF8ToWide(virtual_path_.substr(1))
#elif defined(OS_POSIX)
virtual_path_.substr(1)
#else
#error "Unsupported platform."
#endif
);
return file_system_->root_path().Append(virtual_file_path);
}
} // namespace pepper
|