forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_entry_binding_util.js
190 lines (176 loc) · 7.93 KB
/
file_entry_binding_util.js
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
// 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.
var fileSystemNatives = requireNative('file_system_natives');
var GetIsolatedFileSystem = fileSystemNatives.GetIsolatedFileSystem;
var GetModuleSystem = requireNative('v8_context').GetModuleSystem;
// TODO(sammc): Don't require extension. See http://crbug.com/235689.
var GetExtensionViews = requireNative('runtime').GetExtensionViews;
var safeCallbackApply = require('uncaught_exception_handler').safeCallbackApply;
var jsLastError = bindingUtil ? undefined : require('lastError');
function runCallbackWithLastError(name, message, stack, callback) {
if (bindingUtil)
bindingUtil.runCallbackWithLastError(message, callback);
else
jsLastError.run(name, message, stack, callback);
}
var WINDOW = {};
try {
WINDOW = window;
} catch (e) {
// Running in SW context.
// TODO(lazyboy): Synchronous access to background page is not possible from
// service worker context. Decide what we should do in this case for the class
// of APIs that require access to background page or window object
}
// For a given |apiName|, generates object with two elements that are used
// in file system relayed APIs:
// * 'bindFileEntryCallback' function that provides mapping between JS objects
// into actual FileEntry|DirectoryEntry objects.
// * 'entryIdManager' object that implements methods for keeping the tracks of
// previously saved file entries.
function getFileBindingsForApi(apiName) {
// Fallback to using the current window if no background page is running.
var views = GetExtensionViews(-1, -1, 'BACKGROUND');
// GetExtensionViews() can return null if called from a context without an
// associated extension.
var backgroundPage = views && views[0] ? views[0] : WINDOW;
var backgroundPageModuleSystem = GetModuleSystem(backgroundPage);
// All windows use the bindFileEntryCallback from the background page so their
// FileEntry objects have the background page's context as their own. This
// allows them to be used from other windows (including the background page)
// after the original window is closed.
if (WINDOW == backgroundPage) {
var bindFileEntryCallback = function(functionName, apiFunctions) {
apiFunctions.setCustomCallback(functionName,
function(name, request, callback, response) {
if (callback) {
if (!response) {
callback();
return;
}
var entries = [];
var hasError = false;
var getEntryError = function(fileError) {
if (!hasError) {
hasError = true;
runCallbackWithLastError(
apiName + '.' + functionName,
'Error getting fileEntry, code: ' + fileError.code,
request.stack,
callback);
}
}
// Loop through the response entries and asynchronously get the
// FileEntry for each. We use hasError to ensure that only the first
// error is reported. Note that an error can occur either during the
// loop or in the asynchronous error callback to getFile.
$Array.forEach(response.entries, function(entry) {
if (hasError)
return;
var fileSystemId = entry.fileSystemId;
var baseName = entry.baseName;
var id = entry.id;
var fs = GetIsolatedFileSystem(fileSystemId);
try {
var getEntryCallback = function(fileEntry) {
if (hasError)
return;
entryIdManager.registerEntry(id, fileEntry);
entries.push(fileEntry);
// Once all entries are ready, pass them to the callback. In the
// event of an error, this condition will never be satisfied so
// the callback will not be called with any entries.
if (entries.length == response.entries.length) {
if (response.multiple) {
safeCallbackApply(apiName + '.' + functionName, request,
callback, [entries]);
} else {
safeCallbackApply(
apiName + '.' + functionName, request, callback,
[entries[0]]);
}
}
}
// TODO(koz): fs.root.getFile() makes a trip to the browser
// process, but it might be possible avoid that by calling
// WebDOMFileSystem::createV8Entry().
if (entry.isDirectory) {
fs.root.getDirectory(baseName, {}, getEntryCallback,
getEntryError);
} else {
fs.root.getFile(baseName, {}, getEntryCallback, getEntryError);
}
} catch (e) {
if (!hasError) {
hasError = true;
runCallbackWithLastError(apiName + '.' + functionName,
'Error getting fileEntry: ' + e.stack,
request.stack, callback);
}
}
});
}
});
};
var entryIdManager = require('entryIdManager');
} else {
// Force the fileSystem API to be loaded in the background page. Using
// backgroundPageModuleSystem.require('fileSystem') is insufficient as
// requireNative is only allowed while lazily loading an API.
backgroundPage.chrome.fileSystem;
var bindFileEntryCallback =
backgroundPageModuleSystem.require('fileEntryBindingUtil')
.getFileBindingsForApi(apiName).bindFileEntryCallback;
var entryIdManager = backgroundPageModuleSystem.require('entryIdManager');
}
return {bindFileEntryCallback: bindFileEntryCallback,
entryIdManager: entryIdManager};
}
function getBindDirectoryEntryCallback() {
// Get the background page if one exists. Otherwise, default to the current
// window.
var views = GetExtensionViews(-1, -1, 'BACKGROUND');
// GetExtensionViews() can return null if called from a context without an
// associated extension.
var backgroundPage = views && views[0] ? views[0] : WINDOW;
// For packaged apps, all windows use the bindFileEntryCallback from the
// background page so their FileEntry objects have the background page's
// context as their own. This allows them to be used from other windows
// (including the background page) after the original window is closed.
if (WINDOW == backgroundPage) {
return function(name, request, callback, response) {
if (callback) {
if (!response) {
callback();
return;
}
var fileSystemId = response.fileSystemId;
var baseName = response.baseName;
var fs = GetIsolatedFileSystem(fileSystemId);
try {
fs.root.getDirectory(baseName, {}, callback, function(fileError) {
runCallbackWithLastError(
'runtime.' + functionName,
'Error getting Entry, code: ' + fileError.code,
request.stack, callback);
});
} catch (e) {
runCallbackWithLastError('runtime.' + functionName,
'Error: ' + e.stack,
request.stack, callback);
}
}
}
} else {
var backgroundPageModuleSystem = GetModuleSystem(backgroundPage);
// Force the runtime API to be loaded in the background page. Using
// backgroundPageModuleSystem.require('runtime') is insufficient as
// requireNative is only allowed while lazily loading an API.
backgroundPage.chrome.runtime;
return backgroundPageModuleSystem.require('fileEntryBindingUtil')
.getBindDirectoryEntryCallback();
}
}
exports.$set('getFileBindingsForApi', getFileBindingsForApi);
exports.$set('getBindDirectoryEntryCallback', getBindDirectoryEntryCallback);