OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library file_system_browser; |
| 6 |
| 7 import 'dart:html'; |
| 8 import 'file_system.dart'; |
| 9 import 'package:js/js.dart' as js; |
| 10 |
| 11 |
| 12 /** |
| 13 * File system implementation indirectly using the Chrome Extension Api's to |
| 14 * proxy arbitrary urls. See extension/background.js for the code that does |
| 15 * the actual proxying. |
| 16 */ |
| 17 class BrowserFileSystem implements FileSystem { |
| 18 |
| 19 /** |
| 20 * Chrome extension port used to communicate back to the source page that |
| 21 * will consume these proxied urls. |
| 22 */ |
| 23 js.Proxy sourcePagePort; |
| 24 |
| 25 final _filesToProxy = <String>{}; |
| 26 |
| 27 BrowserFileSystem(this.sourcePagePort); |
| 28 |
| 29 Future flush() { |
| 30 // TODO(jacobr): this should really only return the future when the |
| 31 // urls are fully proxied. |
| 32 js.scoped(() { |
| 33 var requests = []; |
| 34 _filesToProxy.forEach((k,v) { |
| 35 requests.add( js.map({'url': k, 'content': v})); |
| 36 }); |
| 37 _filesToProxy.clear(); |
| 38 js.context.proxyUrls(sourcePagePort, js.array(requests)); |
| 39 }); |
| 40 |
| 41 return new Future.immediate(null); |
| 42 } |
| 43 |
| 44 void writeString(String path, String text) { |
| 45 _filesToProxy[path] = text; |
| 46 } |
| 47 |
| 48 Future<String> readAll(String filename) { |
| 49 var completer = new Completer<String>(); |
| 50 new HttpRequest.get(filename, onSuccess(HttpRequest request) { |
| 51 completer.complete(request.responseText); |
| 52 }); |
| 53 return completer.future; |
| 54 } |
| 55 |
| 56 void createDirectory(String path, [bool recursive = false]) { |
| 57 // No need to actually create directories on the web. |
| 58 } |
| 59 |
| 60 void removeDirectory(String path, [bool recursive = false]) { |
| 61 throw 'removeDirectory() is not implemented by BrowserFileSystem yet.'; |
| 62 } |
| 63 } |
OLD | NEW |