This repository has been archived by the owner on Apr 24, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
4,999 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Copyright (c) 2014, Google Inc. Please see the AUTHORS file for details. | ||
// All rights reserved. Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
library spark.wamfs; | ||
|
||
import 'dart:async'; | ||
import 'dart:js' as js; | ||
import 'dart:typed_data'; | ||
|
||
class WAMFS { | ||
|
||
// Javascript object to wrap. | ||
js.JsObject _jsWAMFS; | ||
|
||
WAMFS() { | ||
_jsWAMFS = new js.JsObject(js.context['WAMFS'], []); | ||
} | ||
|
||
Future connect(String extensionID, String mountPath) { | ||
Completer completer = new Completer(); | ||
Function callback = () { | ||
completer.complete(); | ||
}; | ||
Function errorHandler = (error) { | ||
completer.completeError(error); | ||
}; | ||
_jsWAMFS.callMethod('connect', [extensionID, mountPath, callback, errorHandler]); | ||
return completer.future; | ||
} | ||
|
||
Future copyFile(String source, String destination) { | ||
Completer completer = new Completer(); | ||
Function callback = () { | ||
completer.complete(); | ||
}; | ||
Function errorHandler = (error) { | ||
completer.completeError(error); | ||
}; | ||
_jsWAMFS.callMethod('copyFile', [source, destination, callback, | ||
errorHandler]); | ||
return completer.future; | ||
} | ||
|
||
Future<Uint8List> readFile(String filename) { | ||
Completer completer = new Completer(); | ||
Function callback = (data) { | ||
completer.complete(data); | ||
}; | ||
Function errorHandler = (error) { | ||
completer.completeError(error); | ||
}; | ||
_jsWAMFS.callMethod('readFile', [callback, errorHandler]); | ||
return completer.future; | ||
} | ||
|
||
Future writeDataToFile(String filename, Uint8List content) { | ||
Completer completer = new Completer(); | ||
Function callback = () { | ||
completer.complete(); | ||
}; | ||
Function errorHandler = (error) { | ||
completer.completeError(error); | ||
}; | ||
_jsWAMFS.callMethod('writeDataToFile', [filename, content, callback, | ||
errorHandler]); | ||
return completer.future; | ||
} | ||
|
||
Future writeStringToFile(String filename, String content) { | ||
Completer completer = new Completer(); | ||
Function callback = () { | ||
completer.complete(); | ||
}; | ||
Function errorHandler = (error) { | ||
completer.completeError(error); | ||
}; | ||
_jsWAMFS.callMethod('writeStringToFile', [filename, content, callback, | ||
errorHandler]); | ||
return completer.future; | ||
} | ||
|
||
Future executeCommand(String executablePath, List<String> parameters, | ||
void printStdout(String string), void printStderr(String string)) { | ||
Completer completer = new Completer(); | ||
Function callback = () { | ||
completer.complete(); | ||
}; | ||
Function errorHandler = (error) { | ||
completer.completeError(error); | ||
}; | ||
_jsWAMFS.callMethod('executeCommand', [executablePath, | ||
new js.JsObject.jsify(parameters), printStdout, printStderr, callback, | ||
errorHandler]); | ||
return completer.future; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
'use strict'; | ||
|
||
var WAMFS = function() { | ||
// Our in-memory wam-ready file system. | ||
this.jsfs = new wam.jsfs.FileSystem(); | ||
} | ||
|
||
WAMFS.prototype.connect = function(extensionID, mountPath, onSuccess, onError) { | ||
// Reflect our DOM file system into the wam file system. | ||
this.jsfs.makeEntry('/domfs', new wam.jsfs.dom.FileSystem(), | ||
function() { | ||
this._mount(extensionID, mountPath, onSuccess, onError) | ||
}.bind(this), | ||
function(e) { | ||
onError(e); | ||
}); | ||
}; | ||
|
||
WAMFS.prototype._mount = function(id, path, onSuccess, onError) { | ||
// wam remote file system object. | ||
var rfs = null; | ||
|
||
// Called when the remote file system becomes ready. | ||
var onFileSystemReady = function() { | ||
// Now that it's ready we can remove our initial listeners. These | ||
// were only intended to cover the initial ready or close-with-error | ||
// conditions. A longer term onClose listener should be installed to | ||
// check for unexpected loss of the file system. | ||
rfs.onReady.removeListener(onFileSystemReady); | ||
rfs.onClose.removeListener(onFileSystemClose); | ||
|
||
// Link the remote file system into our root file system under the given | ||
// path. | ||
this.jsfs.makeEntry(path, rfs, function() { | ||
onSuccess(); | ||
}, function(value) { | ||
transport.disconnect(); | ||
onError(value); | ||
}); | ||
}.bind(this); | ||
|
||
// Called when the remote file system gets closed. | ||
var onFileSystemClose = function(value) { | ||
rfs.onReady.removeListener(onFileSystemReady); | ||
rfs.onClose.removeListener(onFileSystemClose); | ||
|
||
onError(value); | ||
}; | ||
|
||
// Connection failed at the transport level. The target app probably isn't | ||
// installed or isn't willing to acknowledge us. | ||
var onTransportClose = function(reason, value) { | ||
transport.readyBinding.onClose.removeListener(onTransportClose); | ||
transport.readyBinding.onReady.removeListener(onTransportReady); | ||
|
||
onError(wam.mkerr('wam.FileSystem.Error.RuntimeError', | ||
['Transport connection failed.'])); | ||
return; | ||
}; | ||
|
||
// The target app accepted our connection request, now we've got to negotiate | ||
// a wam channel. | ||
var onTransportReady = function(reason, value) { | ||
transport.readyBinding.onClose.removeListener(onTransportClose); | ||
transport.readyBinding.onReady.removeListener(onTransportReady); | ||
|
||
var channel = new wam.Channel(transport, 'crx:' + id); | ||
// Uncomment the next line for verbose logging. | ||
// channel.verbose = wam.Channel.verbosity.ALL; | ||
rfs = new wam.jsfs.RemoteFileSystem(channel); | ||
rfs.onReady.addListener(onFileSystemReady); | ||
rfs.onClose.addListener(onFileSystemClose); | ||
}; | ||
|
||
// Create a new chrome.runtime.connect based transport. | ||
var transport = new wam.transport.ChromePort(); | ||
|
||
// Register our close and ready handlers. | ||
transport.readyBinding.onClose.addListener(onTransportClose); | ||
transport.readyBinding.onReady.addListener(onTransportReady); | ||
|
||
// Connect to the target extension/app id. | ||
transport.connect(id); | ||
}; | ||
|
||
WAMFS.prototype.copyFile = function(source, destination, onSuccess, onError) { | ||
var executeContext = this.jsfs.defaultBinding.createExecuteContext(); | ||
executeContext.copyFile(source, destination, | ||
function() { | ||
onSuccess(); | ||
}, function(e) { | ||
onError(e); | ||
}); | ||
}; | ||
|
||
WAMFS.prototype.readFile = function(filename, onSuccess, onError) { | ||
var executeContext = this.jsfs.defaultBinding.createExecuteContext(); | ||
executeContext.readFile(filename, | ||
function() { | ||
onSuccess(); | ||
}, function(e) { | ||
onError(e); | ||
}); | ||
}; | ||
|
||
WAMFS.prototype.writeDataToFile = function(filename, content, onSuccess, onError) { | ||
this.jsfs.defaultBinding.writeFile(filename, | ||
{mode: {create: true}}, | ||
{dataType: 'arraybuffer', data: content}, | ||
function() { | ||
onSuccess(); | ||
}, function(e) { | ||
onError(); | ||
}); | ||
}; | ||
|
||
WAMFS.prototype.writeStringToFile = function(filename, stringContent, onSuccess, onError) { | ||
this.jsfs.defaultBinding.writeFile(filename, | ||
{mode: {create: true}}, | ||
{dataType: 'utf8-string', data: stringContent}, | ||
function() { | ||
onSuccess(); | ||
}, function(e) { | ||
onError(e); | ||
}); | ||
}; | ||
|
||
WAMFS.prototype.executeCommand = function(executablePath, parameters, | ||
printStdout, printStderr, onSuccess, onError) { | ||
var executeContext = this.jsfs.defaultBinding.createExecuteContext(); | ||
executeContext.onStdOut.addListener(function(l) { | ||
printStdout(l); | ||
}); | ||
executeContext.onStdErr.addListener(function(l) { | ||
printStderr(l); | ||
}); | ||
executeContext.onClose.addListener(function() { | ||
onSuccess(); | ||
}); | ||
executeContext.execute(executablePath, parameters); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.