OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 | 5 |
6 class DirectoryException { | 6 class DirectoryException { |
7 const DirectoryException(String this.message); | 7 const DirectoryException(String this.message); |
8 final String message; | 8 final String message; |
9 } | 9 } |
10 | 10 |
11 | 11 |
12 class _DirectoryListingIsolate extends Isolate { | 12 class _DirectoryListingIsolate extends Isolate { |
13 | 13 |
14 _DirectoryListingIsolate() : super.heavy(); | 14 _DirectoryListingIsolate() : super.heavy(); |
15 | 15 |
16 void main() { | 16 void main() { |
17 port.receive((message, replyTo) { | 17 port.receive((message, replyTo) { |
18 _list(message['dir'], | 18 _list(message['dir'], |
19 message['id'], | |
20 message['recursive'], | 19 message['recursive'], |
21 message['dirHandler'], | 20 message['dirPort'], |
22 message['fileHandler'], | 21 message['filePort'], |
23 message['doneHandler'], | 22 message['donePort'], |
24 message['dirErrorHandler']); | 23 message['errorPort']); |
| 24 replyTo.send(true); |
25 }); | 25 }); |
26 } | 26 } |
27 | 27 |
28 void _list(String dir, | 28 void _list(String dir, |
29 int id, | |
30 bool recursive, | 29 bool recursive, |
31 SendPort dirHandler, | 30 SendPort dirPort, |
32 SendPort fileHandler, | 31 SendPort filePort, |
33 SendPort doneHandler, | 32 SendPort donePort, |
34 SendPort dirErrorHandler) native "Directory_List"; | 33 SendPort errorPort) native "Directory_List"; |
35 } | 34 } |
36 | 35 |
37 | 36 |
38 class _Directory implements Directory { | 37 class _Directory implements Directory { |
39 | 38 |
40 _Directory.open(String this._dir) { | 39 _Directory(String this._dir); |
41 _id = 0; | |
42 _closed = false; | |
43 _listing = false; | |
44 if (!_open(_dir)) { | |
45 _closed = true; | |
46 throw new DirectoryException("Error: could not open directory"); | |
47 } | |
48 } | |
49 | |
50 bool close() { | |
51 if (_closed) { | |
52 throw new DirectoryException("Error: directory closed"); | |
53 } | |
54 if (_close(_id)) { | |
55 _closePort(_dirHandler); | |
56 _closePort(_fileHandler); | |
57 _closePort(_doneHandler); | |
58 _closePort(_dirErrorHandler); | |
59 _closed = true; | |
60 bool was_listing = _listing; | |
61 _listing = false; | |
62 if (was_listing && _doneHandler !== null) { | |
63 _doneHandler(false); | |
64 } | |
65 return true; | |
66 } | |
67 return false; | |
68 } | |
69 | 40 |
70 void list([bool recursive = false]) { | 41 void list([bool recursive = false]) { |
71 if (_closed) { | |
72 throw new DirectoryException("Error: directory closed"); | |
73 } | |
74 if (_listing) { | |
75 throw new DirectoryException("Error: listing already in progress"); | |
76 } | |
77 _listing = true; | |
78 new _DirectoryListingIsolate().spawn().then((port) { | 42 new _DirectoryListingIsolate().spawn().then((port) { |
| 43 // Build a map of parameters to the directory listing isolate. |
| 44 Map listingParameters = new Map(); |
| 45 listingParameters['dir'] = _dir; |
| 46 listingParameters['recursive'] = recursive; |
| 47 |
| 48 // Setup ports to receive messages from listing. |
79 // TODO(ager): Do not explicitly transform to send ports when | 49 // TODO(ager): Do not explicitly transform to send ports when |
80 // that is done automatically. | 50 // implicit conversions are implemented. |
81 port.send({ 'dir': _dir, | 51 ReceivePort dirPort; |
82 'id': _id, | 52 ReceivePort filePort; |
83 'recursive': recursive, | 53 ReceivePort donePort; |
84 'dirHandler': _dirHandler.toSendPort(), | 54 ReceivePort errorPort; |
85 'fileHandler': _fileHandler.toSendPort(), | 55 if (_dirHandler !== null) { |
86 'doneHandler': _doneHandler.toSendPort(), | 56 dirPort = new ReceivePort(); |
87 'dirErrorHandler': _dirErrorHandler.toSendPort() }); | 57 dirPort.receive((String dir, ignored) { |
| 58 _dirHandler(dir); |
| 59 }); |
| 60 listingParameters['dirPort'] = dirPort.toSendPort(); |
| 61 } |
| 62 if (_fileHandler !== null) { |
| 63 filePort = new ReceivePort(); |
| 64 filePort.receive((String file, ignored) { |
| 65 _fileHandler(file); |
| 66 }); |
| 67 listingParameters['filePort'] = filePort.toSendPort(); |
| 68 } |
| 69 if (_doneHandler !== null) { |
| 70 donePort = new ReceivePort(); |
| 71 donePort.receive((bool completed, ignored) { |
| 72 _doneHandler(completed); |
| 73 }); |
| 74 listingParameters['donePort'] = donePort.toSendPort(); |
| 75 } |
| 76 if (_errorHandler !== null) { |
| 77 errorPort = new ReceivePort(); |
| 78 errorPort.receive((String error, ignored) { |
| 79 _errorHandler(error); |
| 80 }); |
| 81 listingParameters['errorPort'] = errorPort.toSendPort(); |
| 82 } |
| 83 |
| 84 // Close ports when listing is done. |
| 85 ReceivePort closePortsPort = new ReceivePort(); |
| 86 closePortsPort.receive((message, replyTo) { |
| 87 _closePort(dirPort); |
| 88 _closePort(filePort); |
| 89 _closePort(donePort); |
| 90 _closePort(errorPort); |
| 91 _closePort(closePortsPort); |
| 92 }); |
| 93 |
| 94 // Send the listing parameters to the isolate. |
| 95 port.send(listingParameters, closePortsPort.toSendPort()); |
88 }); | 96 }); |
89 } | 97 } |
90 | 98 |
91 // TODO(ager): Implement setting of the handlers as in the process library. | |
92 void setDirHandler(void dirHandler(String dir)) { | 99 void setDirHandler(void dirHandler(String dir)) { |
93 if (_closed) { | 100 _dirHandler = dirHandler; |
94 throw new DirectoryException("Error: directory closed"); | |
95 } | |
96 if (_dirHandler === null) { | |
97 _dirHandler = new ReceivePort(); | |
98 } | |
99 _dirHandler.receive((String dir, ignored) => dirHandler(dir)); | |
100 } | 101 } |
101 | 102 |
102 void setFileHandler(void fileHandler(String file)) { | 103 void setFileHandler(void fileHandler(String file)) { |
103 if (_closed) { | 104 _fileHandler = fileHandler; |
104 throw new DirectoryException("Error: directory closed"); | |
105 } | |
106 if (_fileHandler === null) { | |
107 _fileHandler = new ReceivePort(); | |
108 } | |
109 _fileHandler.receive((String file, ignored) => fileHandler(file)); | |
110 } | 105 } |
111 | 106 |
112 void setDoneHandler(void doneHandler(bool completed)) { | 107 void setDoneHandler(void doneHandler(bool completed)) { |
113 if (_closed) { | 108 _doneHandler = doneHandler; |
114 throw new DirectoryException("Error: directory closed"); | |
115 } | |
116 if (_doneHandler === null) { | |
117 _doneHandler = new ReceivePort(); | |
118 } | |
119 _doneHandler.receive((bool completed, ignored) { | |
120 _listing = false; | |
121 doneHandler(completed); | |
122 }); | |
123 } | 109 } |
124 | 110 |
125 void setDirErrorHandler(void errorHandler(String dir)) { | 111 void setErrorHandler(void errorHandler(String error)) { |
126 if (_closed) { | 112 _errorHandler = errorHandler; |
127 throw new DirectoryException("Error: directory closed"); | |
128 } | |
129 if (_dirErrorHandler === null) { | |
130 _dirErrorHandler = new ReceivePort(); | |
131 } | |
132 _dirErrorHandler.receive((String dir, ignored) { | |
133 errorHandler(dir, completed); | |
134 }); | |
135 } | 113 } |
136 | 114 |
137 // Utility methods. | |
138 void _closePort(ReceivePort port) { | 115 void _closePort(ReceivePort port) { |
139 if (port !== null) { | 116 if (port !== null) { |
140 port.close(); | 117 port.close(); |
141 } | 118 } |
142 } | 119 } |
143 | 120 |
144 // Native code binding. | 121 var _dirHandler; |
145 bool _open(String dir) native "Directory_Open"; | 122 var _fileHandler; |
146 bool _close(int id) native "Directory_Close"; | 123 var _doneHandler; |
147 | 124 var _errorHandler; |
148 ReceivePort _dirHandler; | |
149 ReceivePort _fileHandler; | |
150 ReceivePort _doneHandler; | |
151 ReceivePort _dirErrorHandler; | |
152 | 125 |
153 String _dir; | 126 String _dir; |
154 int _id; | |
155 bool _closed; | |
156 bool _listing; | |
157 } | 127 } |
OLD | NEW |