OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library vmservice_io; | 5 library vmservice_io; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:collection'; | 8 import 'dart:collection'; |
9 import 'dart:convert'; | 9 import 'dart:convert'; |
10 import 'dart:io'; | 10 import 'dart:io'; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 } | 53 } |
54 } | 54 } |
55 if (_registerSignalHandlerTimer != null) { | 55 if (_registerSignalHandlerTimer != null) { |
56 _registerSignalHandlerTimer.cancel(); | 56 _registerSignalHandlerTimer.cancel(); |
57 _registerSignalHandlerTimer = null; | 57 _registerSignalHandlerTimer = null; |
58 } | 58 } |
59 // Call out to embedder's shutdown callback. | 59 // Call out to embedder's shutdown callback. |
60 _shutdown(); | 60 _shutdown(); |
61 } | 61 } |
62 | 62 |
| 63 Future<Uri> createTempDirCallback(String base) async { |
| 64 Directory temp = await Directory.systemTemp.createTemp(base); |
| 65 return temp.uri; |
| 66 } |
| 67 |
| 68 Future deleteDirCallback(Uri path) async { |
| 69 Directory dir = new Directory.fromUri(path); |
| 70 await dir.delete(recursive: true); |
| 71 } |
| 72 |
| 73 Future writeFileCallback(Uri path, List<int> bytes) async { |
| 74 var file = await new File.fromUri(path); |
| 75 await file.writeAsBytes(bytes); |
| 76 } |
| 77 |
| 78 Future<List<int>> readFileCallback(Uri path) async { |
| 79 var file = await new File.fromUri(path); |
| 80 return await file.readAsBytes(); |
| 81 } |
| 82 |
| 83 Future<List<Map<String,String>>> listFilesCallback(Uri dirPath) async { |
| 84 var dir = new Directory.fromUri(dirPath); |
| 85 var dirPathStr = dirPath.path; |
| 86 var stream = dir.list(recursive: true); |
| 87 var result = []; |
| 88 await for (var fileEntity in stream) { |
| 89 var stat = await fileEntity.stat(); |
| 90 if (stat.type == FileSystemEntityType.FILE && |
| 91 fileEntity.path.startsWith(dirPathStr)) { |
| 92 var map = {}; |
| 93 map['name'] = '/' + fileEntity.path.substring(dirPathStr.length); |
| 94 map['size'] = stat.size; |
| 95 map['modified'] = stat.modified.millisecondsSinceEpoch; |
| 96 result.add(map); |
| 97 } |
| 98 } |
| 99 return result; |
| 100 } |
| 101 |
63 _clearFuture(_) { | 102 _clearFuture(_) { |
64 serverFuture = null; | 103 serverFuture = null; |
65 } | 104 } |
66 | 105 |
67 _onSignal(ProcessSignal signal) { | 106 _onSignal(ProcessSignal signal) { |
68 if (serverFuture != null) { | 107 if (serverFuture != null) { |
69 // Still waiting. | 108 // Still waiting. |
70 return; | 109 return; |
71 } | 110 } |
72 _lazyServerBoot(); | 111 _lazyServerBoot(); |
(...skipping 16 matching lines...) Expand all Loading... |
89 if (_isWindows) { | 128 if (_isWindows) { |
90 // Cannot register for signals on Windows. | 129 // Cannot register for signals on Windows. |
91 return; | 130 return; |
92 } | 131 } |
93 _signalSubscription = _signalWatch(ProcessSignal.SIGQUIT).listen(_onSignal); | 132 _signalSubscription = _signalWatch(ProcessSignal.SIGQUIT).listen(_onSignal); |
94 } | 133 } |
95 | 134 |
96 main() { | 135 main() { |
97 // Set embedder hooks. | 136 // Set embedder hooks. |
98 VMServiceEmbedderHooks.cleanup = cleanupCallback; | 137 VMServiceEmbedderHooks.cleanup = cleanupCallback; |
| 138 VMServiceEmbedderHooks.createTempDir = createTempDirCallback; |
| 139 VMServiceEmbedderHooks.deleteDir = deleteDirCallback; |
| 140 VMServiceEmbedderHooks.writeFile = writeFileCallback; |
| 141 VMServiceEmbedderHooks.readFile = readFileCallback; |
| 142 VMServiceEmbedderHooks.listFiles = listFilesCallback; |
99 // Always instantiate the vmservice object so that the exit message | 143 // Always instantiate the vmservice object so that the exit message |
100 // can be delivered and waiting loaders can be cancelled. | 144 // can be delivered and waiting loaders can be cancelled. |
101 var service = new VMService(); | 145 var service = new VMService(); |
102 if (_autoStart) { | 146 if (_autoStart) { |
103 _lazyServerBoot(); | 147 _lazyServerBoot(); |
104 server.startup(); | 148 server.startup(); |
105 // It's just here to push an event on the event loop so that we invoke the | 149 // It's just here to push an event on the event loop so that we invoke the |
106 // scheduled microtasks. | 150 // scheduled microtasks. |
107 Timer.run(() {}); | 151 Timer.run(() {}); |
108 } | 152 } |
109 scriptLoadPort.handler = _processLoadRequest; | 153 scriptLoadPort.handler = _processLoadRequest; |
110 // Register signal handler after a small delay to avoid stalling main | 154 // Register signal handler after a small delay to avoid stalling main |
111 // isolate startup. | 155 // isolate startup. |
112 _registerSignalHandlerTimer = new Timer(shortDelay, _registerSignalHandler); | 156 _registerSignalHandlerTimer = new Timer(shortDelay, _registerSignalHandler); |
113 return scriptLoadPort; | 157 return scriptLoadPort; |
114 } | 158 } |
115 | 159 |
116 _shutdown() native "VMServiceIO_Shutdown"; | 160 _shutdown() native "VMServiceIO_Shutdown"; |
OLD | NEW |