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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 Future<Uri> createTempDirCallback(String base) async { | 64 Future<Uri> createTempDirCallback(String base) async { |
65 Directory temp = await Directory.systemTemp.createTemp(base); | 65 Directory temp = await Directory.systemTemp.createTemp(base); |
66 return temp.uri; | 66 return temp.uri; |
67 } | 67 } |
68 | 68 |
69 Future deleteDirCallback(Uri path) async { | 69 Future deleteDirCallback(Uri path) async { |
70 Directory dir = new Directory.fromUri(path); | 70 Directory dir = new Directory.fromUri(path); |
71 await dir.delete(recursive: true); | 71 await dir.delete(recursive: true); |
72 } | 72 } |
73 | 73 |
| 74 class PendingWrite { |
| 75 PendingWrite(this.uri, this.bytes); |
| 76 final Completer completer = new Completer(); |
| 77 final Uri uri; |
| 78 final List<int> bytes; |
| 79 |
| 80 Future write() async { |
| 81 var file = new File.fromUri(uri); |
| 82 var parent_directory = file.parent; |
| 83 await parent_directory.create(recursive: true); |
| 84 var result = await file.writeAsBytes(bytes); |
| 85 completer.complete(null); |
| 86 WriteLimiter._writeCompleted(); |
| 87 } |
| 88 } |
| 89 |
| 90 class WriteLimiter { |
| 91 static final List<PendingWrite> pendingWrites = new List<PendingWrite>(); |
| 92 |
| 93 // non-rooted Android devices have a very low limit for the number of |
| 94 // open files. Artificially cap ourselves to 16. |
| 95 static const kMaxOpenWrites = 16; |
| 96 static int openWrites = 0; |
| 97 |
| 98 static Future scheduleWrite(Uri path, List<int> bytes) async { |
| 99 // Create a new pending write. |
| 100 PendingWrite pw = new PendingWrite(path, bytes); |
| 101 pendingWrites.add(pw); |
| 102 _maybeWriteFiles(); |
| 103 return pw.completer.future; |
| 104 } |
| 105 |
| 106 static _maybeWriteFiles() { |
| 107 while (openWrites < kMaxOpenWrites) { |
| 108 if (pendingWrites.length > 0) { |
| 109 PendingWrite pw = pendingWrites.removeLast(); |
| 110 pw.write(); |
| 111 openWrites++; |
| 112 } else { |
| 113 break; |
| 114 } |
| 115 } |
| 116 } |
| 117 |
| 118 static _writeCompleted() { |
| 119 openWrites--; |
| 120 assert(openWrites >= 0); |
| 121 _maybeWriteFiles(); |
| 122 } |
| 123 } |
| 124 |
74 Future writeFileCallback(Uri path, List<int> bytes) async { | 125 Future writeFileCallback(Uri path, List<int> bytes) async { |
75 var file = new File.fromUri(path); | 126 return WriteLimiter.scheduleWrite(path, bytes); |
76 var parent_directory = file.parent; | |
77 await parent_directory.create(recursive: true); | |
78 await file.writeAsBytes(bytes); | |
79 } | 127 } |
80 | 128 |
81 Future<List<int>> readFileCallback(Uri path) async { | 129 Future<List<int>> readFileCallback(Uri path) async { |
82 var file = new File.fromUri(path); | 130 var file = new File.fromUri(path); |
83 return await file.readAsBytes(); | 131 return await file.readAsBytes(); |
84 } | 132 } |
85 | 133 |
86 Future<List<Map<String,String>>> listFilesCallback(Uri dirPath) async { | 134 Future<List<Map<String,String>>> listFilesCallback(Uri dirPath) async { |
87 var dir = new Directory.fromUri(dirPath); | 135 var dir = new Directory.fromUri(dirPath); |
88 var dirPathStr = dirPath.path; | 136 var dirPathStr = dirPath.path; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 Timer.run(() {}); | 203 Timer.run(() {}); |
156 } | 204 } |
157 scriptLoadPort.handler = _processLoadRequest; | 205 scriptLoadPort.handler = _processLoadRequest; |
158 // Register signal handler after a small delay to avoid stalling main | 206 // Register signal handler after a small delay to avoid stalling main |
159 // isolate startup. | 207 // isolate startup. |
160 _registerSignalHandlerTimer = new Timer(shortDelay, _registerSignalHandler); | 208 _registerSignalHandlerTimer = new Timer(shortDelay, _registerSignalHandler); |
161 return scriptLoadPort; | 209 return scriptLoadPort; |
162 } | 210 } |
163 | 211 |
164 _shutdown() native "VMServiceIO_Shutdown"; | 212 _shutdown() native "VMServiceIO_Shutdown"; |
OLD | NEW |