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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
// Copyright 2014 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 "chrome/browser/chromeos/drive/fileapi/fileapi_worker.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/task_runner_util.h"
#include "base/thread_task_runner_handle.h"
#include "base/threading/sequenced_worker_pool.h"
#include "chrome/browser/chromeos/drive/drive.pb.h"
#include "chrome/browser/chromeos/drive/file_errors.h"
#include "chrome/browser/chromeos/drive/file_system_interface.h"
#include "chrome/browser/chromeos/drive/file_system_util.h"
#include "chrome/browser/chromeos/drive/resource_entry_conversion.h"
#include "content/public/browser/browser_thread.h"
#include "storage/browser/fileapi/file_system_url.h"
#include "storage/common/fileapi/directory_entry.h"
using content::BrowserThread;
namespace drive {
namespace fileapi_internal {
namespace {
// The summary of opening mode is:
// - File::FLAG_OPEN: Open the existing file. Fail if not exists.
// - File::FLAG_CREATE: Create the file if not exists. Fail if exists.
// - File::FLAG_OPEN_ALWAYS: Open the existing file. Create a new file
// if not exists.
// - File::FLAG_CREATE_ALWAYS: Create a new file if not exists. If exists
// open it with truncate.
// - File::FLAG_OPEN_TRUNCATE: Open the existing file with truncate.
// Fail if not exists.
OpenMode GetOpenMode(int file_flag) {
if (file_flag & (base::File::FLAG_OPEN | base::File::FLAG_OPEN_TRUNCATED))
return OPEN_FILE;
if (file_flag & base::File::FLAG_CREATE)
return CREATE_FILE;
DCHECK(file_flag &
(base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_CREATE_ALWAYS));
return OPEN_OR_CREATE_FILE;
}
// Runs |callback| with the File::Error converted from |error|.
void RunStatusCallbackByFileError(const StatusCallback& callback,
FileError error) {
callback.Run(FileErrorToBaseFileError(error));
}
// Runs |callback| with arguments converted from |error| and |entry|.
void RunGetFileInfoCallback(const GetFileInfoCallback& callback,
FileError error,
scoped_ptr<ResourceEntry> entry) {
if (error != FILE_ERROR_OK) {
callback.Run(FileErrorToBaseFileError(error), base::File::Info());
return;
}
DCHECK(entry);
base::File::Info file_info;
ConvertResourceEntryToFileInfo(*entry, &file_info);
callback.Run(base::File::FILE_OK, file_info);
}
// Runs |callback| with entries.
void RunReadDirectoryCallbackWithEntries(
const ReadDirectoryCallback& callback,
scoped_ptr<ResourceEntryVector> resource_entries) {
DCHECK(resource_entries);
std::vector<storage::DirectoryEntry> entries;
// Convert drive files to File API's directory entry.
entries.reserve(resource_entries->size());
for (size_t i = 0; i < resource_entries->size(); ++i) {
const ResourceEntry& resource_entry = (*resource_entries)[i];
storage::DirectoryEntry entry;
entry.name = resource_entry.base_name();
const PlatformFileInfoProto& file_info = resource_entry.file_info();
entry.is_directory = file_info.is_directory();
entry.size = file_info.size();
entry.last_modified_time =
base::Time::FromInternalValue(file_info.last_modified());
entries.push_back(entry);
}
callback.Run(base::File::FILE_OK, entries, true /*has_more*/);
}
// Runs |callback| with |error|.
void RunReadDirectoryCallbackOnCompletion(const ReadDirectoryCallback& callback,
FileError error) {
callback.Run(FileErrorToBaseFileError(error),
std::vector<storage::DirectoryEntry>(),
false /*has_more*/);
}
// Runs |callback| with arguments based on |error|, |local_path| and |entry|.
void RunCreateSnapshotFileCallback(const CreateSnapshotFileCallback& callback,
FileError error,
const base::FilePath& local_path,
scoped_ptr<ResourceEntry> entry) {
if (error != FILE_ERROR_OK) {
callback.Run(FileErrorToBaseFileError(error),
base::File::Info(),
base::FilePath(),
storage::ScopedFile::ScopeOutPolicy());
return;
}
DCHECK(entry);
// When reading file, last modified time specified in file info will be
// compared to the last modified time of the local version of the drive file.
// Since those two values don't generally match (last modification time on the
// drive server vs. last modification time of the local, downloaded file), so
// we have to opt out from this check. We do this by unsetting last_modified
// value in the file info passed to the CreateSnapshot caller.
base::File::Info file_info;
ConvertResourceEntryToFileInfo(*entry, &file_info);
file_info.last_modified = base::Time();
// If the file is a hosted document, a temporary JSON file is created to
// represent the document. The JSON file is not cached and its lifetime
// is managed by ShareableFileReference.
storage::ScopedFile::ScopeOutPolicy scope_out_policy =
entry->file_specific_info().is_hosted_document()
? storage::ScopedFile::DELETE_ON_SCOPE_OUT
: storage::ScopedFile::DONT_DELETE_ON_SCOPE_OUT;
callback.Run(base::File::FILE_OK, file_info, local_path, scope_out_policy);
}
// Runs |callback| with arguments converted from |error| and |local_path|.
void RunCreateWritableSnapshotFileCallback(
const CreateWritableSnapshotFileCallback& callback,
FileError error,
const base::FilePath& local_path,
const base::Closure& close_callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
callback.Run(FileErrorToBaseFileError(error), local_path, close_callback);
}
// Runs |callback| with |file|.
void RunOpenFileCallback(const OpenFileCallback& callback,
const base::Closure& close_callback,
base::File file) {
callback.Run(file.Pass(), close_callback);
}
base::File OpenFile(const base::FilePath& path, int flags) {
return base::File(path, flags);
}
// Part of OpenFile(). Called after FileSystem::OpenFile().
void OpenFileAfterFileSystemOpenFile(int file_flags,
const OpenFileCallback& callback,
FileError error,
const base::FilePath& local_path,
const base::Closure& close_callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (error != FILE_ERROR_OK) {
callback.Run(base::File(FileErrorToBaseFileError(error)), base::Closure());
return;
}
// Here, the file should be at |local_path|, but there may be timing issue.
// Because the file is managed by Drive file system, so, in order to avoid
// unexpected file creation, CREATE, OPEN_ALWAYS and CREATE_ALWAYS are
// translated into OPEN or OPEN_TRUNCATED, here. Keep OPEN and OPEN_TRUNCATED
// as is.
if (file_flags & (base::File::FLAG_CREATE |
base::File::FLAG_OPEN_ALWAYS)) {
file_flags &= ~(base::File::FLAG_CREATE |
base::File::FLAG_OPEN_ALWAYS);
file_flags |= base::File::FLAG_OPEN;
} else if (file_flags & base::File::FLAG_CREATE_ALWAYS) {
file_flags &= ~base::File::FLAG_CREATE_ALWAYS;
file_flags |= base::File::FLAG_OPEN_TRUNCATED;
}
// Cache file prepared for modification is available. Open it locally.
bool posted = base::PostTaskAndReplyWithResult(
BrowserThread::GetBlockingPool(), FROM_HERE,
base::Bind(&OpenFile, local_path, file_flags),
base::Bind(&RunOpenFileCallback, callback, close_callback));
DCHECK(posted);
}
} // namespace
FileSystemInterface* GetFileSystemFromUrl(const storage::FileSystemURL& url) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
Profile* profile = util::ExtractProfileFromPath(url.path());
return profile ? util::GetFileSystemByProfile(profile) : NULL;
}
void RunFileSystemCallback(
const FileSystemGetter& file_system_getter,
const base::Callback<void(FileSystemInterface*)>& callback,
const base::Closure& on_error_callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
FileSystemInterface* file_system = file_system_getter.Run();
if (!file_system) {
if (!on_error_callback.is_null())
on_error_callback.Run();
return;
}
callback.Run(file_system);
}
void GetFileInfo(const base::FilePath& file_path,
const GetFileInfoCallback& callback,
FileSystemInterface* file_system) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
file_system->GetResourceEntry(
file_path,
base::Bind(&RunGetFileInfoCallback, callback));
}
void Copy(const base::FilePath& src_file_path,
const base::FilePath& dest_file_path,
bool preserve_last_modified,
const StatusCallback& callback,
FileSystemInterface* file_system) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
file_system->Copy(src_file_path, dest_file_path, preserve_last_modified,
base::Bind(&RunStatusCallbackByFileError, callback));
}
void Move(const base::FilePath& src_file_path,
const base::FilePath& dest_file_path,
const StatusCallback& callback,
FileSystemInterface* file_system) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
file_system->Move(src_file_path, dest_file_path,
base::Bind(&RunStatusCallbackByFileError, callback));
}
void CopyInForeignFile(const base::FilePath& src_foreign_file_path,
const base::FilePath& dest_file_path,
const StatusCallback& callback,
FileSystemInterface* file_system) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
file_system->TransferFileFromLocalToRemote(
src_foreign_file_path, dest_file_path,
base::Bind(&RunStatusCallbackByFileError, callback));
}
void ReadDirectory(const base::FilePath& file_path,
const ReadDirectoryCallback& callback,
FileSystemInterface* file_system) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
file_system->ReadDirectory(
file_path,
base::Bind(&RunReadDirectoryCallbackWithEntries, callback),
base::Bind(&RunReadDirectoryCallbackOnCompletion, callback));
}
void Remove(const base::FilePath& file_path,
bool is_recursive,
const StatusCallback& callback,
FileSystemInterface* file_system) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
file_system->Remove(file_path, is_recursive,
base::Bind(&RunStatusCallbackByFileError, callback));
}
void CreateDirectory(const base::FilePath& file_path,
bool is_exclusive,
bool is_recursive,
const StatusCallback& callback,
FileSystemInterface* file_system) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
file_system->CreateDirectory(
file_path, is_exclusive, is_recursive,
base::Bind(&RunStatusCallbackByFileError, callback));
}
void CreateFile(const base::FilePath& file_path,
bool is_exclusive,
const StatusCallback& callback,
FileSystemInterface* file_system) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
file_system->CreateFile(file_path, is_exclusive,
std::string(), // no mime type; guess from file_path
base::Bind(&RunStatusCallbackByFileError, callback));
}
void Truncate(const base::FilePath& file_path,
int64 length,
const StatusCallback& callback,
FileSystemInterface* file_system) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
file_system->TruncateFile(
file_path, length,
base::Bind(&RunStatusCallbackByFileError, callback));
}
void CreateSnapshotFile(const base::FilePath& file_path,
const CreateSnapshotFileCallback& callback,
FileSystemInterface* file_system) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
file_system->GetFile(file_path,
base::Bind(&RunCreateSnapshotFileCallback, callback));
}
void CreateWritableSnapshotFile(
const base::FilePath& file_path,
const CreateWritableSnapshotFileCallback& callback,
FileSystemInterface* file_system) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
file_system->OpenFile(
file_path,
OPEN_FILE,
std::string(), // no mime type; we never create a new file here.
base::Bind(&RunCreateWritableSnapshotFileCallback, callback));
}
void OpenFile(const base::FilePath& file_path,
int file_flags,
const OpenFileCallback& callback,
FileSystemInterface* file_system) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
// Returns an error if any unsupported flag is found.
if (file_flags & ~(base::File::FLAG_OPEN |
base::File::FLAG_CREATE |
base::File::FLAG_OPEN_ALWAYS |
base::File::FLAG_CREATE_ALWAYS |
base::File::FLAG_OPEN_TRUNCATED |
base::File::FLAG_READ |
base::File::FLAG_WRITE |
base::File::FLAG_WRITE_ATTRIBUTES |
base::File::FLAG_APPEND)) {
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
base::Bind(callback,
Passed(base::File(base::File::FILE_ERROR_FAILED)),
base::Closure()));
return;
}
file_system->OpenFile(
file_path, GetOpenMode(file_flags),
std::string(), // no mime type; guess from file_path
base::Bind(&OpenFileAfterFileSystemOpenFile, file_flags, callback));
}
void TouchFile(const base::FilePath& file_path,
const base::Time& last_access_time,
const base::Time& last_modified_time,
const StatusCallback& callback,
FileSystemInterface* file_system) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
file_system->TouchFile(file_path, last_access_time, last_modified_time,
base::Bind(&RunStatusCallbackByFileError, callback));
}
} // namespace fileapi_internal
} // namespace drive
|