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
|
// Copyright 2013 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/file_manager/fileapi_util.h"
#include "base/files/file.h"
#include "base/files/file_path.h"
#include "chrome/browser/chromeos/drive/file_system_util.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/site_instance.h"
#include "content/public/browser/storage_partition.h"
#include "extensions/browser/extension_system.h"
#include "net/base/escape.h"
#include "url/gurl.h"
#include "webkit/browser/fileapi/file_system_context.h"
#include "webkit/browser/fileapi/open_file_system_mode.h"
#include "webkit/common/fileapi/file_system_util.h"
using content::BrowserThread;
namespace file_manager {
namespace util {
namespace {
GURL ConvertRelativeFilePathToFileSystemUrl(const base::FilePath& relative_path,
const std::string& extension_id) {
GURL base_url = fileapi::GetFileSystemRootURI(
extensions::Extension::GetBaseURLFromExtensionId(extension_id),
fileapi::kFileSystemTypeExternal);
return GURL(base_url.spec() +
net::EscapeUrlEncodedData(relative_path.AsUTF8Unsafe(),
false)); // Space to %20 instead of +.
}
// Creates an ErrorDefinition with an error set to |error|.
EntryDefinition CreateEntryDefinitionWithError(base::File::Error error) {
EntryDefinition result;
result.error = error;
return result;
}
// Helper class for performing conversions from file definitions to entry
// definitions. It is possible to do it without a class, but the code would be
// crazy and super tricky.
//
// This class copies the input |file_definition_list|,
// so there is no need to worry about validity of passed |file_definition_list|
// reference. Also, it automatically deletes itself after converting finished,
// or if shutdown is invoked during ResolveURL(). Must be called on UI thread.
class FileDefinitionListConverter {
public:
FileDefinitionListConverter(Profile* profile,
const std::string& extension_id,
const FileDefinitionList& file_definition_list,
const EntryDefinitionListCallback& callback);
~FileDefinitionListConverter() {}
private:
// Converts the element under the iterator to an entry. First, converts
// the virtual path to an URL, and calls OnResolvedURL(). In case of error
// calls OnIteratorConverted with an error entry definition.
void ConvertNextIterator(scoped_ptr<FileDefinitionListConverter> self_deleter,
FileDefinitionList::const_iterator iterator);
// Creates an entry definition from the URL as well as the file definition.
// Then, calls OnIteratorConverted with the created entry definition.
void OnResolvedURL(scoped_ptr<FileDefinitionListConverter> self_deleter,
FileDefinitionList::const_iterator iterator,
const GURL& root_url,
const std::string& name,
base::File::Error error);
// Called when the iterator is converted. Adds the |entry_definition| to
// |results_| and calls ConvertNextIterator() for the next element.
void OnIteratorConverted(scoped_ptr<FileDefinitionListConverter> self_deleter,
FileDefinitionList::const_iterator iterator,
const EntryDefinition& entry_definition);
scoped_refptr<fileapi::FileSystemContext> file_system_context_;
const std::string extension_id_;
const FileDefinitionList file_definition_list_;
const EntryDefinitionListCallback callback_;
scoped_ptr<EntryDefinitionList> result_;
};
FileDefinitionListConverter::FileDefinitionListConverter(
Profile* profile,
const std::string& extension_id,
const FileDefinitionList& file_definition_list,
const EntryDefinitionListCallback& callback)
: extension_id_(extension_id),
file_definition_list_(file_definition_list),
callback_(callback),
result_(new EntryDefinitionList) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
// May be NULL during unit_tests.
ExtensionService* service =
extensions::ExtensionSystem::Get(profile)->extension_service();
if (service) {
// File browser APIs are meant to be used only from extension context, so
// the extension's site is the one in whose file system context the virtual
// path should be found.
GURL site = service->GetSiteForExtensionId(extension_id_);
file_system_context_ =
content::BrowserContext::GetStoragePartitionForSite(
profile, site)->GetFileSystemContext();
}
// Deletes the converter, once the scoped pointer gets out of scope. It is
// either, if the conversion is finished, or ResolveURL() is terminated, and
// the callback not called because of shutdown.
scoped_ptr<FileDefinitionListConverter> self_deleter(this);
ConvertNextIterator(self_deleter.Pass(), file_definition_list_.begin());
}
void FileDefinitionListConverter::ConvertNextIterator(
scoped_ptr<FileDefinitionListConverter> self_deleter,
FileDefinitionList::const_iterator iterator) {
if (iterator == file_definition_list_.end()) {
// The converter object will be destroyed since |self_deleter| gets out of
// scope.
callback_.Run(result_.Pass());
return;
}
if (!file_system_context_.get()) {
OnIteratorConverted(self_deleter.Pass(),
iterator,
CreateEntryDefinitionWithError(
base::File::FILE_ERROR_INVALID_OPERATION));
return;
}
fileapi::ExternalFileSystemBackend* backend =
file_system_context_->external_backend();
if (!backend) {
OnIteratorConverted(self_deleter.Pass(),
iterator,
CreateEntryDefinitionWithError(
base::File::FILE_ERROR_INVALID_OPERATION));
return;
}
fileapi::FileSystemURL url = file_system_context_->CreateCrackedFileSystemURL(
extensions::Extension::GetBaseURLFromExtensionId(extension_id_),
fileapi::kFileSystemTypeExternal,
iterator->virtual_path);
DCHECK(url.is_valid());
// The converter object will be deleted if the callback is not called because
// of shutdown during ResolveURL().
//
// TODO(mtomasz, nhiroki): Call FileSystemContext::ResolveURL() directly,
// after removing redundant thread restrictions in there.
backend->ResolveURL(
url,
// Not sandboxed file systems are not creatable via ResolveURL().
fileapi::OPEN_FILE_SYSTEM_FAIL_IF_NONEXISTENT,
base::Bind(&FileDefinitionListConverter::OnResolvedURL,
base::Unretained(this),
base::Passed(&self_deleter),
iterator));
}
void FileDefinitionListConverter::OnResolvedURL(
scoped_ptr<FileDefinitionListConverter> self_deleter,
FileDefinitionList::const_iterator iterator,
const GURL& root_url,
const std::string& name,
base::File::Error error) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
EntryDefinition entry_definition;
entry_definition.file_system_root_url = root_url.spec();
entry_definition.file_system_name = name;
entry_definition.is_directory = iterator->is_directory;
entry_definition.error = error;
// Construct a target Entry.fullPath value from the virtual path and the
// root URL. Eg. Downloads/A/b.txt -> A/b.txt.
const base::FilePath root_virtual_path =
file_system_context_->CrackURL(root_url).virtual_path();
DCHECK(root_virtual_path == iterator->virtual_path ||
root_virtual_path.IsParent(iterator->virtual_path));
base::FilePath full_path;
root_virtual_path.AppendRelativePath(iterator->virtual_path, &full_path);
entry_definition.full_path = full_path;
OnIteratorConverted(self_deleter.Pass(), iterator, entry_definition);
}
void FileDefinitionListConverter::OnIteratorConverted(
scoped_ptr<FileDefinitionListConverter> self_deleter,
FileDefinitionList::const_iterator iterator,
const EntryDefinition& entry_definition) {
result_->push_back(entry_definition);
ConvertNextIterator(self_deleter.Pass(), ++iterator);
}
// Helper function to return the converted definition entry directly, without
// the redundant container.
void OnConvertFileDefinitionDone(
const EntryDefinitionCallback& callback,
scoped_ptr<EntryDefinitionList> entry_definition_list) {
DCHECK_EQ(1u, entry_definition_list->size());
callback.Run(entry_definition_list->at(0));
}
} // namespace
EntryDefinition::EntryDefinition() {
}
EntryDefinition::~EntryDefinition() {
}
fileapi::FileSystemContext* GetFileSystemContextForExtensionId(
Profile* profile,
const std::string& extension_id) {
GURL site = extensions::ExtensionSystem::Get(profile)->
extension_service()->GetSiteForExtensionId(extension_id);
return content::BrowserContext::GetStoragePartitionForSite(profile, site)->
GetFileSystemContext();
}
fileapi::FileSystemContext* GetFileSystemContextForRenderViewHost(
Profile* profile,
content::RenderViewHost* render_view_host) {
content::SiteInstance* site_instance = render_view_host->GetSiteInstance();
return content::BrowserContext::GetStoragePartition(profile, site_instance)->
GetFileSystemContext();
}
base::FilePath ConvertDrivePathToRelativeFileSystemPath(
Profile* profile,
const std::string& extension_id,
const base::FilePath& drive_path) {
// "/special/drive-xxx"
base::FilePath path = drive::util::GetDriveMountPointPath(profile);
// appended with (|drive_path| - "drive").
drive::util::GetDriveGrandRootPath().AppendRelativePath(drive_path, &path);
base::FilePath relative_path;
ConvertAbsoluteFilePathToRelativeFileSystemPath(profile,
extension_id,
path,
&relative_path);
return relative_path;
}
GURL ConvertDrivePathToFileSystemUrl(Profile* profile,
const base::FilePath& drive_path,
const std::string& extension_id) {
const base::FilePath relative_path =
ConvertDrivePathToRelativeFileSystemPath(profile, extension_id,
drive_path);
if (relative_path.empty())
return GURL();
return ConvertRelativeFilePathToFileSystemUrl(relative_path, extension_id);
}
bool ConvertAbsoluteFilePathToFileSystemUrl(Profile* profile,
const base::FilePath& absolute_path,
const std::string& extension_id,
GURL* url) {
base::FilePath relative_path;
if (!ConvertAbsoluteFilePathToRelativeFileSystemPath(profile,
extension_id,
absolute_path,
&relative_path)) {
return false;
}
*url = ConvertRelativeFilePathToFileSystemUrl(relative_path, extension_id);
return true;
}
bool ConvertAbsoluteFilePathToRelativeFileSystemPath(
Profile* profile,
const std::string& extension_id,
const base::FilePath& absolute_path,
base::FilePath* virtual_path) {
ExtensionService* service =
extensions::ExtensionSystem::Get(profile)->extension_service();
// May be NULL during unit_tests.
if (!service)
return false;
// File browser APIs are meant to be used only from extension context, so the
// extension's site is the one in whose file system context the virtual path
// should be found.
GURL site = service->GetSiteForExtensionId(extension_id);
fileapi::ExternalFileSystemBackend* backend =
content::BrowserContext::GetStoragePartitionForSite(profile, site)->
GetFileSystemContext()->external_backend();
if (!backend)
return false;
// Find if this file path is managed by the external backend.
if (!backend->GetVirtualPath(absolute_path, virtual_path))
return false;
return true;
}
void ConvertFileDefinitionListToEntryDefinitionList(
Profile* profile,
const std::string& extension_id,
const FileDefinitionList& file_definition_list,
const EntryDefinitionListCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
// The converter object destroys itself.
new FileDefinitionListConverter(
profile, extension_id, file_definition_list, callback);
}
void ConvertFileDefinitionToEntryDefinition(
Profile* profile,
const std::string& extension_id,
const FileDefinition& file_definition,
const EntryDefinitionCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
FileDefinitionList file_definition_list;
file_definition_list.push_back(file_definition);
ConvertFileDefinitionListToEntryDefinitionList(
profile,
extension_id,
file_definition_list,
base::Bind(&OnConvertFileDefinitionDone, callback));
}
} // namespace util
} // namespace file_manager
|