forked from adobe/brackets
-
Notifications
You must be signed in to change notification settings - Fork 279
/
RemoteFiler.js
86 lines (74 loc) · 2.71 KB
/
RemoteFiler.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
/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global define */
define(function (require, exports, module) {
"use strict";
/**
* Management of the remote connection to a Filer FileSystem running in
* the hosting window holding this iframe. This code needs to get started
* early, so that fs operations run quickly on startup.
*/
/**
* NOTE: For reading and writing UTF8 and Binary files, it is best/easiest
* to use the filesystem/impls/filer/FilerUtils module, since it does
* things at the right level of abstraction:
* - readFileAsUTF8()
* - readFileAsBinary()
* - writeFileAsUTF8()
* - writeFileASBinary()
*/
var fnQueue = require("filesystem/impls/filer/lib/queue");
var ChannelUtils = require("bramble/ChannelUtils");
var UUID = ChannelUtils.UUID;
var allowArrayBufferTransfer;
var port;
// Remote filesystem callbacks
var callbackQueue = {};
function remoteFSCallbackHandler(e) {
var data = e.data;
var callbackItem = callbackQueue[data.callback];
if(!callbackItem.persist) {
delete callbackQueue[data.callback];
}
callbackItem.callback.apply(null, data.result);
}
function receiveMessagePort(e) {
var data = e.data;
try {
data = JSON.parse(data);
data = data || {};
} catch(err) {
data = {};
}
if (data.type === "bramble:filer") {
window.removeEventListener("message", receiveMessagePort, false);
port = e.ports[0];
port.start();
ChannelUtils.checkArrayBufferTransfer(port, function(err, isAllowed) {
allowArrayBufferTransfer = isAllowed;
port.addEventListener("message", remoteFSCallbackHandler, false);
fnQueue.ready();
});
}
}
function init() {
// Request the that remote FS be setup
window.addEventListener("message", receiveMessagePort, false);
window.parent.postMessage(JSON.stringify({type: "bramble:filer"}), "*");
}
function proxyCall(fn, options, callback) {
var id = UUID.generate();
callbackQueue[id] = {
callback: callback,
persist: options.persist
};
fnQueue.exec(function() {
var transferable;
if (allowArrayBufferTransfer && options.transfer) {
transferable = [options.transfer];
}
port.postMessage({method: fn, shell: options.shell, callback: id, args: options.args}, transferable);
});
}
exports.init = init;
exports.proxyCall = proxyCall;
});