| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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 class _ProcessStartStatus { | 5 class _ProcessStartStatus { |
| 6 int _errorCode; // Set to OS error code if process start failed. | 6 int _errorCode; // Set to OS error code if process start failed. |
| 7 String _errorMessage; // Set to OS error message if process start failed. | 7 String _errorMessage; // Set to OS error message if process start failed. |
| 8 } | 8 } |
| 9 | 9 |
| 10 | 10 |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 _arguments, | 159 _arguments, |
| 160 _workingDirectory, | 160 _workingDirectory, |
| 161 _environment, | 161 _environment, |
| 162 _in, | 162 _in, |
| 163 _out, | 163 _out, |
| 164 _err, | 164 _err, |
| 165 _exitHandler, | 165 _exitHandler, |
| 166 status); | 166 status); |
| 167 if (!success) { | 167 if (!success) { |
| 168 close(); | 168 close(); |
| 169 if (_onError !== null) { | 169 _reportError(new ProcessException(status._errorMessage, status._errorCode)
); |
| 170 _onError(new ProcessException(status._errorMessage, status._errorCode)); | 170 return; |
| 171 return; | |
| 172 } | |
| 173 } | 171 } |
| 174 _started = true; | 172 _started = true; |
| 175 | 173 |
| 176 // Make sure to activate socket handlers now that the file | 174 // Make sure to activate socket handlers now that the file |
| 177 // descriptors have been set. | 175 // descriptors have been set. |
| 178 _in._activateHandlers(); | 176 _in._activateHandlers(); |
| 179 _out._activateHandlers(); | 177 _out._activateHandlers(); |
| 180 _err._activateHandlers(); | 178 _err._activateHandlers(); |
| 181 | 179 |
| 182 // Setup an exit handler to handle internal cleanup and possible | 180 // Setup an exit handler to handle internal cleanup and possible |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 | 233 |
| 236 OutputStream get stdin() { | 234 OutputStream get stdin() { |
| 237 if (_closed) { | 235 if (_closed) { |
| 238 throw new ProcessException("Process closed"); | 236 throw new ProcessException("Process closed"); |
| 239 } | 237 } |
| 240 return _out.outputStream; | 238 return _out.outputStream; |
| 241 } | 239 } |
| 242 | 240 |
| 243 void kill() { | 241 void kill() { |
| 244 if (_closed && _pid === null) { | 242 if (_closed && _pid === null) { |
| 245 if (_onError !== null) { | 243 _reportError(new ProcessException("Process closed")); |
| 246 _onError(new ProcessException("Process closed")); | |
| 247 } | |
| 248 return; | 244 return; |
| 249 } | 245 } |
| 250 if (_killed) { | 246 if (_killed) { |
| 251 return; | 247 return; |
| 252 } | 248 } |
| 253 // TODO(ager): Make the actual kill operation asynchronous. | 249 // TODO(ager): Make the actual kill operation asynchronous. |
| 254 if (_kill(_pid)) { | 250 if (_kill(_pid)) { |
| 255 _killed = true; | 251 _killed = true; |
| 256 return; | 252 return; |
| 257 } | 253 } |
| 258 if (_onError !== null) { | 254 _reportError(new ProcessException("Could not kill process")); |
| 259 _onError(new ProcessException("Could not kill process")); | 255 return; |
| 260 return; | |
| 261 } | |
| 262 } | 256 } |
| 263 | 257 |
| 264 void _kill(int pid) native "Process_Kill"; | 258 void _kill(int pid) native "Process_Kill"; |
| 265 | 259 |
| 266 void close() { | 260 void close() { |
| 267 if (_closed) { | 261 if (_closed) { |
| 268 throw new ProcessException("Process closed"); | 262 throw new ProcessException("Process closed"); |
| 269 } | 263 } |
| 270 _in.close(); | 264 _in.close(); |
| 271 _out.close(); | 265 _out.close(); |
| 272 _err.close(); | 266 _err.close(); |
| 273 _exitHandler.close(); | 267 _exitHandler.close(); |
| 274 _closed = true; | 268 _closed = true; |
| 275 } | 269 } |
| 276 | 270 |
| 277 void set onExit(void callback(int exitCode)) { | 271 void set onExit(void callback(int exitCode)) { |
| 278 if (_closed) { | 272 if (_closed) { |
| 279 throw new ProcessException("Process closed"); | 273 throw new ProcessException("Process closed"); |
| 280 } | 274 } |
| 281 if (_killed) { | 275 if (_killed) { |
| 282 throw new ProcessException("Process killed"); | 276 throw new ProcessException("Process killed"); |
| 283 } | 277 } |
| 284 _onExit = callback; | 278 _onExit = callback; |
| 285 } | 279 } |
| 286 | 280 |
| 287 void set onError(void callback(ProcessException exception)) { | 281 void set onError(void callback(e)) { |
| 288 _onError = callback; | 282 _onError = callback; |
| 289 } | 283 } |
| 290 | 284 |
| 291 void set onStart(void callback()) { | 285 void set onStart(void callback()) { |
| 292 _onStart = callback; | 286 _onStart = callback; |
| 293 } | 287 } |
| 294 | 288 |
| 289 void _reportError(e) { |
| 290 if (_onError != null) { |
| 291 _onError(e); |
| 292 } else { |
| 293 throw e; |
| 294 } |
| 295 } |
| 296 |
| 295 String _path; | 297 String _path; |
| 296 ObjectArray<String> _arguments; | 298 ObjectArray<String> _arguments; |
| 297 String _workingDirectory; | 299 String _workingDirectory; |
| 298 List<String> _environment; | 300 List<String> _environment; |
| 299 // Private methods of _Socket are used by _in, _out, and _err. | 301 // Private methods of _Socket are used by _in, _out, and _err. |
| 300 _Socket _in; | 302 _Socket _in; |
| 301 _Socket _out; | 303 _Socket _out; |
| 302 _Socket _err; | 304 _Socket _err; |
| 303 Socket _exitHandler; | 305 Socket _exitHandler; |
| 304 int _pid; | 306 int _pid; |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 402 | 404 |
| 403 void set onStart(void callback()) => _process.onStart = callback; | 405 void set onStart(void callback()) => _process.onStart = callback; |
| 404 | 406 |
| 405 void set onExit(void callback(int exitCode)) { | 407 void set onExit(void callback(int exitCode)) { |
| 406 throw new UnsupportedOperationException( | 408 throw new UnsupportedOperationException( |
| 407 'Cannot set exit handler on process started with ' | 409 'Cannot set exit handler on process started with ' |
| 408 'the run constructor. The exit code will ' | 410 'the run constructor. The exit code will ' |
| 409 'be supplied in the callback on completion.'); | 411 'be supplied in the callback on completion.'); |
| 410 } | 412 } |
| 411 | 413 |
| 412 void set onError(void callback(ProcessException error)) { | 414 void set onError(void callback(e)) { |
| 413 _process.onError = callback; | 415 _process.onError = callback; |
| 414 } | 416 } |
| 415 | 417 |
| 416 void kill() => _process.kill(); | 418 void kill() => _process.kill(); |
| 417 | 419 |
| 418 void close() => _process.close(); | 420 void close() => _process.close(); |
| 419 | 421 |
| 420 Process _process; | 422 Process _process; |
| 421 Function _callback; | 423 Function _callback; |
| 422 StringBuffer _stdoutBuffer; | 424 StringBuffer _stdoutBuffer; |
| 423 StringBuffer _stderrBuffer; | 425 StringBuffer _stderrBuffer; |
| 424 int _exitCode; | 426 int _exitCode; |
| 425 bool _stdoutClosed = false; | 427 bool _stdoutClosed = false; |
| 426 bool _stderrClosed = false; | 428 bool _stderrClosed = false; |
| 427 } | 429 } |
| OLD | NEW |