Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 import 'dart:nativewrappers'; | 5 import 'dart:nativewrappers'; |
| 6 | 6 |
| 7 import 'dart:_mojo/application.dart'; | 7 import 'dart:_mojo/application.dart'; |
| 8 import 'dart:_mojo/bindings.dart'; | 8 import 'dart:_mojo/bindings.dart'; |
| 9 import 'dart:_mojo/core.dart'; | 9 import 'dart:_mojo/core.dart'; |
| 10 import 'dart:_mojo/mojo/network_error.mojom.dart'; | 10 import 'dart:_mojo/mojo/network_error.mojom.dart'; |
| 11 import 'dart:_mojo_services/mojo/host_resolver.mojom.dart'; | 11 import 'dart:_mojo_services/mojo/host_resolver.mojom.dart'; |
| 12 import 'dart:_mojo_services/mojo/net_address.mojom.dart'; | 12 import 'dart:_mojo_services/mojo/net_address.mojom.dart'; |
| 13 import 'dart:_mojo_services/mojo/network_service.mojom.dart'; | 13 import 'dart:_mojo_services/mojo/network_service.mojom.dart'; |
| 14 import 'dart:_mojo_services/mojo/tcp_bound_socket.mojom.dart'; | 14 import 'dart:_mojo_services/mojo/tcp_bound_socket.mojom.dart'; |
| 15 import 'dart:_mojo_services/mojo/tcp_connected_socket.mojom.dart'; | 15 import 'dart:_mojo_services/mojo/tcp_connected_socket.mojom.dart'; |
| 16 import 'dart:_mojo_services/mojo/tcp_server_socket.mojom.dart'; | 16 import 'dart:_mojo_services/mojo/tcp_server_socket.mojom.dart'; |
| 17 import 'dart:_mojo_services/mojo/files/file.mojom.dart'; | 17 import 'dart:_mojo_services/mojo/files/file.mojom.dart'; |
| 18 import 'dart:_mojo_services/mojo/files/files.mojom.dart'; | 18 import 'dart:_mojo_services/mojo/files/files.mojom.dart'; |
| 19 import 'dart:_mojo_services/mojo/files/directory.mojom.dart'; | 19 import 'dart:_mojo_services/mojo/files/directory.mojom.dart'; |
| 20 import 'dart:_mojo_services/mojo/files/ioctl.mojom.dart'; | 20 import 'dart:_mojo_services/mojo/files/ioctl.mojom.dart'; |
| 21 import 'dart:_mojo_services/mojo/files/types.mojom.dart' as types; | 21 import 'dart:_mojo_services/mojo/files/types.mojom.dart' as types; |
| 22 | 22 |
| 23 class _NetworkServiceProxy extends FuturizedProxy<NetworkServiceProxy> { | |
| 24 Map<Symbol, Function> _mojoMethods; | |
| 25 NetworkServiceProxy _nsProxy; | |
| 26 | |
| 27 _NetworkServiceProxy(NetworkServiceProxy p) : super(p) { | |
| 28 _mojoMethods = <Symbol, Function>{ | |
| 29 #createTcpBoundSocket: p.createTcpBoundSocket, | |
| 30 #createTcpConnectedSocket: p.createTcpConnectedSocket, | |
| 31 #createHttpServer: p.createHttpServer, | |
| 32 }; | |
| 33 _nsProxy = p; | |
| 34 } | |
| 35 Map<Symbol, Function> get mojoMethods => _mojoMethods; | |
| 36 | |
| 37 Function get createUrlLoader => _nsProxy.createUrlLoader; | |
| 38 Function get getCookieStore => _nsProxy.getCookieStore; | |
| 39 Function get createWebSocket => _nsProxy.createWebSocket; | |
| 40 Function get createUdpSocket => _nsProxy.createUdpSocket; | |
| 41 Function get registerUrlLoaderInterceptor => | |
| 42 _nsProxy.registerUrlLoaderInterceptor; | |
| 43 Function get createHostResolver => _nsProxy.createHostResolver; | |
| 44 | |
| 45 static final Map<Symbol, Function> _mojoResponses = { | |
| 46 #createTcpBoundSocket: | |
| 47 new NetworkServiceCreateTcpBoundSocketResponseParams#init, | |
| 48 #createTcpConnectedSocket: | |
| 49 new NetworkServiceCreateTcpConnectedSocketResponseParams#init, | |
| 50 #createHttpServer: | |
| 51 new NetworkServiceCreateHttpServerResponseParams#init, | |
| 52 }; | |
| 53 Map<Symbol, Function> get mojoResponses => _mojoResponses; | |
| 54 } | |
| 55 | |
| 56 class _HostResolverProxy extends FuturizedProxy<HostResolverProxy> { | |
| 57 Map<Symbol, Function> _mojoMethods; | |
| 58 | |
| 59 _HostResolverProxy(HostResolverProxy proxy) : super(proxy) { | |
| 60 _mojoMethods = <Symbol, Function>{ | |
| 61 #getHostAddresses: proxy.getHostAddresses, | |
| 62 }; | |
| 63 } | |
| 64 Map<Symbol, Function> get mojoMethods => _mojoMethods; | |
| 65 | |
| 66 _HostResolverProxy.unbound() : this(new HostResolverProxy.unbound()); | |
| 67 | |
| 68 static final Map<Symbol, Function> _mojoResponses = { | |
| 69 #getHostAddresses: new HostResolverGetHostAddressesResponseParams#init, | |
| 70 }; | |
| 71 Map<Symbol, Function> get mojoResponses => _mojoResponses; | |
| 72 } | |
| 73 | |
| 74 class _TcpBoundSocketProxy extends FuturizedProxy<TcpBoundSocketProxy> { | |
| 75 Map<Symbol, Function> _mojoMethods; | |
| 76 | |
| 77 _TcpBoundSocketProxy(TcpBoundSocketProxy proxy) : super(proxy) { | |
| 78 _mojoMethods = <Symbol, Function>{ | |
| 79 #startListening: proxy.startListening, | |
| 80 #connect: proxy.connect, | |
| 81 }; | |
| 82 } | |
| 83 Map<Symbol, Function> get mojoMethods => _mojoMethods; | |
| 84 | |
| 85 _TcpBoundSocketProxy.unbound() : this(new TcpBoundSocketProxy.unbound()); | |
|
Cutch
2016/06/07 17:16:54
I haven't seen this syntax before.
": this" === "
zra
2016/06/07 22:11:50
Yah. You can redirect a constructor to another con
| |
| 86 | |
| 87 static final Map<Symbol, Function> _mojoResponses = { | |
| 88 #startListening: new TcpBoundSocketStartListeningResponseParams#init, | |
| 89 #connect: new TcpBoundSocketConnectResponseParams#init, | |
| 90 }; | |
| 91 Map<Symbol, Function> get mojoResponses => _mojoResponses; | |
| 92 } | |
| 93 | |
| 94 class _TcpServerSocketProxy extends FuturizedProxy<TcpServerSocketProxy> { | |
| 95 Map<Symbol, Function> _mojoMethods; | |
| 96 | |
| 97 _TcpServerSocketProxy(TcpServerSocketProxy proxy) : super(proxy) { | |
| 98 _mojoMethods = <Symbol, Function>{ | |
| 99 #accept: proxy.accept, | |
| 100 }; | |
| 101 } | |
| 102 Map<Symbol, Function> get mojoMethods => _mojoMethods; | |
| 103 | |
| 104 _TcpServerSocketProxy.unbound() : this(new TcpServerSocketProxy.unbound()); | |
| 105 | |
| 106 static final Map<Symbol, Function> _mojoResponses = { | |
| 107 #accept: new TcpServerSocketAcceptResponseParams#init, | |
| 108 }; | |
| 109 Map<Symbol, Function> get mojoResponses => _mojoResponses; | |
| 110 } | |
| 111 | |
| 112 class _FilesProxy extends FuturizedProxy<FilesProxy> { | |
| 113 Map<Symbol, Function> _mojoMethods; | |
| 114 | |
| 115 _FilesProxy(FilesProxy proxy) : super(proxy) { | |
| 116 _mojoMethods = <Symbol, Function>{ | |
| 117 #openFileSystem: proxy.openFileSystem, | |
| 118 }; | |
| 119 } | |
| 120 Map<Symbol, Function> get mojoMethods => _mojoMethods; | |
| 121 | |
| 122 static final Map<Symbol, Function> _mojoResponses = { | |
| 123 #openFileSystem: new FilesOpenFileSystemResponseParams#init, | |
| 124 }; | |
| 125 Map<Symbol, Function> get mojoResponses => _mojoResponses; | |
| 126 } | |
| 127 | |
| 128 class _FileProxy extends FuturizedProxy<FileProxy> { | |
| 129 Map<Symbol, Function> _mojoMethods; | |
| 130 | |
| 131 _FileProxy(FileProxy proxy) : super(proxy) { | |
| 132 _mojoMethods = <Symbol, Function>{ | |
| 133 #close_: proxy.close_, | |
| 134 #read: proxy.read, | |
|
Cutch
2016/06/07 17:16:54
I worry about going through this abstraction for s
zra
2016/06/07 22:11:50
It probably depends on the IO latency whether a fe
| |
| 135 #write: proxy.write, | |
| 136 #readToStream: proxy.readToStream, | |
| 137 #writeFromStream: proxy.writeFromStream, | |
| 138 #tell: proxy.tell, | |
| 139 #seek: proxy.seek, | |
| 140 #stat: proxy.stat, | |
| 141 #truncate: proxy.truncate, | |
| 142 #touch: proxy.touch, | |
| 143 #dup: proxy.dup, | |
| 144 #repoen: proxy.reopen, | |
| 145 #asBuffer: proxy.asBuffer, | |
| 146 #ioctl: proxy.ioctl, | |
| 147 }; | |
| 148 } | |
| 149 Map<Symbol, Function> get mojoMethods => _mojoMethods; | |
| 150 | |
| 151 _FileProxy.unbound() : this(new FileProxy.unbound()); | |
| 152 | |
| 153 static final Map<Symbol, Function> _mojoResponses = { | |
| 154 #close_: new FileCloseResponseParams#init, | |
| 155 #read: new FileReadResponseParams#init, | |
| 156 #write: new FileWriteResponseParams#init, | |
| 157 #readToStream: new FileReadToStreamResponseParams#init, | |
| 158 #writeFromStream: new FileWriteFromStreamResponseParams#init, | |
| 159 #tell: new FileTellResponseParams#init, | |
| 160 #seek: new FileSeekResponseParams#init, | |
| 161 #stat: new FileStatResponseParams#init, | |
| 162 #truncate: new FileTruncateResponseParams#init, | |
| 163 #touch: new FileTouchResponseParams#init, | |
| 164 #dup: new FileDupResponseParams#init, | |
| 165 #repoen: new FileReopenResponseParams#init, | |
| 166 #asBuffer: new FileAsBufferResponseParams#init, | |
| 167 #ioctl: new FileIoctlResponseParams#init, | |
| 168 }; | |
| 169 Map<Symbol, Function> get mojoResponses => _mojoResponses; | |
| 170 } | |
| 171 | |
| 172 class _DirectoryProxy extends FuturizedProxy<DirectoryProxy> { | |
| 173 Map<Symbol, Function> _mojoMethods; | |
| 174 | |
| 175 _DirectoryProxy(DirectoryProxy proxy) : super(proxy) { | |
| 176 _mojoMethods = <Symbol, Function>{ | |
| 177 #read: proxy.read, | |
| 178 #stat: proxy.stat, | |
| 179 #touch: proxy.touch, | |
| 180 #openFile: proxy.openFile, | |
| 181 #openDirectory: proxy.openDirectory, | |
| 182 #rename: proxy.rename, | |
| 183 #delete: proxy.delete, | |
| 184 }; | |
| 185 } | |
| 186 Map<Symbol, Function> get mojoMethods => _mojoMethods; | |
| 187 | |
| 188 _DirectoryProxy.unbound() : this(new DirectoryProxy.unbound()); | |
| 189 | |
| 190 static final Map<Symbol, Function> _mojoResponses = { | |
| 191 #read: new DirectoryReadResponseParams#init, | |
| 192 #stat: new DirectoryStatResponseParams#init, | |
| 193 #touch: new DirectoryTouchResponseParams#init, | |
| 194 #openFile: new DirectoryOpenFileResponseParams#init, | |
| 195 #openDirectory: new DirectoryOpenDirectoryResponseParams#init, | |
| 196 #rename: new DirectoryRenameResponseParams#init, | |
| 197 #delete: new DirectoryDeleteResponseParams#init, | |
| 198 }; | |
| 199 Map<Symbol, Function> get mojoResponses => _mojoResponses; | |
| 200 } | |
| 201 | |
| 23 // When developing, set fileSystemDeveloper to true and the file system will | 202 // When developing, set fileSystemDeveloper to true and the file system will |
| 24 // persist under ~/MojoAppPersistentCaches/. | 203 // persist under ~/MojoAppPersistentCaches/. |
| 25 const bool fileSystemDeveloper = false; | 204 const bool fileSystemDeveloper = false; |
| 26 const String fileSystemName = | 205 const String fileSystemName = |
| 27 fileSystemDeveloper ? 'app_persistent_cache' : null; | 206 fileSystemDeveloper ? 'app_persistent_cache' : null; |
| 28 | 207 |
| 29 // System temp path relative to the root directory. | 208 // System temp path relative to the root directory. |
| 30 const String systemTempPath = 'tmp'; | 209 const String systemTempPath = 'tmp'; |
| 31 | 210 |
| 32 // | 211 // |
| 33 // Mojo objects and helper functions used by the 'dart:io' library. | 212 // Mojo objects and helper functions used by the 'dart:io' library. |
| 34 // | 213 // |
| 35 int _networkServiceHandle; | 214 int _networkServiceHandle; |
| 36 int _filesServiceHandle; | 215 int _filesServiceHandle; |
| 37 NetworkServiceProxy _networkService; | 216 _NetworkServiceProxy _networkService; |
| 38 HostResolverProxy _hostResolver; | 217 _HostResolverProxy _hostResolver; |
| 39 FilesProxy _files; | 218 _FilesProxy _files; |
| 40 DirectoryProxy _rootDirectory; | 219 _DirectoryProxy _rootDirectory; |
| 41 DirectoryProxy _systemTempDirectory; | 220 _DirectoryProxy _systemTempDirectory; |
| 42 | 221 |
| 43 void _initialize( | 222 void _initialize( |
| 44 int networkServiceHandle, int filesServiceHandle, String scriptPath) { | 223 int networkServiceHandle, int filesServiceHandle, String scriptPath) { |
| 45 if (networkServiceHandle != MojoHandle.INVALID) { | 224 if (networkServiceHandle != MojoHandle.INVALID) { |
| 46 _networkServiceHandle = networkServiceHandle; | 225 _networkServiceHandle = networkServiceHandle; |
| 47 } | 226 } |
| 48 if (filesServiceHandle != MojoHandle.INVALID) { | 227 if (filesServiceHandle != MojoHandle.INVALID) { |
| 49 _filesServiceHandle = filesServiceHandle; | 228 _filesServiceHandle = filesServiceHandle; |
| 50 } | 229 } |
| 51 // TODO(floitsch): do this lazily once _Platform.script is a getter. | 230 // TODO(floitsch): do this lazily once _Platform.script is a getter. |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 87 _rootDirectory.close(immediate: true); | 266 _rootDirectory.close(immediate: true); |
| 88 _rootDirectory = null; | 267 _rootDirectory = null; |
| 89 } | 268 } |
| 90 if (_systemTempDirectory != null) { | 269 if (_systemTempDirectory != null) { |
| 91 _systemTempDirectory.close(immediate: true); | 270 _systemTempDirectory.close(immediate: true); |
| 92 _systemTempDirectory = null; | 271 _systemTempDirectory = null; |
| 93 } | 272 } |
| 94 } | 273 } |
| 95 | 274 |
| 96 /// Get the singleton NetworkServiceProxy. | 275 /// Get the singleton NetworkServiceProxy. |
| 97 NetworkServiceProxy _getNetworkService() { | 276 _NetworkServiceProxy _getNetworkService() { |
| 98 if (_networkService != null) { | 277 if (_networkService != null) { |
| 99 return _networkService; | 278 return _networkService; |
| 100 } | 279 } |
| 101 _networkService = new NetworkServiceProxy.fromHandle( | 280 var networkService = new NetworkServiceProxy.fromHandle( |
| 102 new MojoHandle(_networkServiceHandle).pass()); | 281 new MojoHandle(_networkServiceHandle).pass()); |
| 282 _networkService = new _NetworkServiceProxy(networkService); | |
| 103 _networkServiceHandle = null; | 283 _networkServiceHandle = null; |
| 104 return _networkService; | 284 return _networkService; |
| 105 } | 285 } |
| 106 | 286 |
| 107 /// Get the singleton HostResolverProxy. | 287 /// Get the singleton HostResolverProxy. |
| 108 HostResolverProxy _getHostResolver() { | 288 _HostResolverProxy _getHostResolver() { |
| 109 if (_hostResolver != null) { | 289 if (_hostResolver != null) { |
| 110 return _hostResolver; | 290 return _hostResolver; |
| 111 } | 291 } |
| 112 var networkService = _getNetworkService(); | 292 _NetworkServiceProxy networkService = _getNetworkService(); |
| 113 if (networkService == null) { | 293 if (networkService == null) { |
| 114 return null; | 294 return null; |
| 115 } | 295 } |
| 116 _hostResolver = new HostResolverProxy.unbound(); | 296 _hostResolver = new _HostResolverProxy.unbound(); |
| 117 networkService.createHostResolver(_hostResolver); | 297 networkService.createHostResolver(_hostResolver.proxy); |
| 118 // Remove the host resolver's handle from the open set because it is not | 298 // Remove the host resolver's handle from the open set because it is not |
| 119 // under application control and does not affect isolate shutdown. | 299 // under application control and does not affect isolate shutdown. |
| 120 _hostResolver.ctrl.endpoint.handle.pass(); | 300 _hostResolver.proxy.ctrl.endpoint.handle.pass(); |
| 121 return _hostResolver; | 301 return _hostResolver; |
| 122 } | 302 } |
| 123 | 303 |
| 124 /// Get the singleton FilesProxy. | 304 /// Get the singleton FilesProxy. |
| 125 FilesProxy _getFiles() { | 305 _FilesProxy _getFiles() { |
| 126 if (_files != null) { | 306 if (_files != null) { |
| 127 return _files; | 307 return _files; |
| 128 } | 308 } |
| 129 _files = new FilesProxy.fromHandle( | 309 var files = new FilesProxy.fromHandle( |
| 130 new MojoHandle(_filesServiceHandle).pass()); | 310 new MojoHandle(_filesServiceHandle).pass()); |
| 311 _files = new _FilesProxy(files); | |
| 131 _filesServiceHandle = null; | 312 _filesServiceHandle = null; |
| 132 return _files; | 313 return _files; |
| 133 } | 314 } |
| 134 | 315 |
| 135 /// Get the singleton DirectoryProxy for the root directory. | 316 /// Get the singleton DirectoryProxy for the root directory. |
| 136 Future<DirectoryProxy> _getRootDirectory() async { | 317 Future<_DirectoryProxy> _getRootDirectory() async { |
| 137 if (_rootDirectory != null) { | 318 if (_rootDirectory != null) { |
| 138 return _rootDirectory; | 319 return _rootDirectory; |
| 139 } | 320 } |
| 140 FilesProxy files = _getFiles(); | 321 _FilesProxy files = _getFiles(); |
| 141 assert(files != null); | 322 assert(files != null); |
| 142 _rootDirectory = new DirectoryProxy.unbound(); | 323 _rootDirectory = new _DirectoryProxy.unbound(); |
| 143 var response = | 324 var response = |
| 144 await files.openFileSystem(fileSystemName, _rootDirectory); | 325 await files.openFileSystem(fileSystemName, _rootDirectory.proxy); |
| 145 // Remove the root directory's handle from the open set because it is not | 326 // Remove the root directory's handle from the open set because it is not |
| 146 // under application control and does not affect isolate shutdown. | 327 // under application control and does not affect isolate shutdown. |
| 147 _rootDirectory.ctrl.endpoint.handle.pass(); | 328 _rootDirectory.proxy.ctrl.endpoint.handle.pass(); |
| 148 | 329 |
| 149 // Ensure system temporary directory exists before returning the root | 330 // Ensure system temporary directory exists before returning the root |
| 150 // directory. | 331 // directory. |
| 151 await _getSystemTempDirectory(); | 332 await _getSystemTempDirectory(); |
| 152 return _rootDirectory; | 333 return _rootDirectory; |
| 153 } | 334 } |
| 154 | 335 |
| 155 /// Get the singleton DirectoryProxy for the system temp directory. | 336 /// Get the singleton DirectoryProxy for the system temp directory. |
| 156 Future<DirectoryProxy> _getSystemTempDirectory() async { | 337 Future<_DirectoryProxy> _getSystemTempDirectory() async { |
| 157 if (_systemTempDirectory != null) { | 338 if (_systemTempDirectory != null) { |
| 158 return _systempTempDirectory; | 339 return _systempTempDirectory; |
| 159 } | 340 } |
| 160 DirectoryProxy rootDirectory = await _getRootDirectory(); | 341 _DirectoryProxy rootDirectory = await _getRootDirectory(); |
| 161 int flags = types.kOpenFlagRead | | 342 int flags = types.kOpenFlagRead | |
| 162 types.kOpenFlagWrite | | 343 types.kOpenFlagWrite | |
| 163 types.kOpenFlagCreate; | 344 types.kOpenFlagCreate; |
| 164 _systemTempDirectory = new DirectoryProxy.unbound(); | 345 _systemTempDirectory = new _DirectoryProxy.unbound(); |
| 165 var response = | 346 var response = |
| 166 await rootDirectory.openDirectory(systemTempPath, | 347 await rootDirectory.openDirectory(systemTempPath, |
| 167 _systemTempDirectory, | 348 _systemTempDirectory.proxy, |
| 168 flags); | 349 flags); |
| 169 assert(response.error == types.Error.ok); | 350 assert(response.error == types.Error.ok); |
| 170 // Remove the system temp directory's handle from the open set because it | 351 // Remove the system temp directory's handle from the open set because it |
| 171 // is not under application control and does not affect isolate shutdown. | 352 // is not under application control and does not affect isolate shutdown. |
| 172 _systemTempDirectory.ctrl.endpoint.handle.pass(); | 353 _systemTempDirectory.proxy.ctrl.endpoint.handle.pass(); |
| 173 return _systemTempDirectory; | 354 return _systemTempDirectory; |
| 174 } | 355 } |
| 175 | 356 |
| 176 /// Static utility methods for converting between 'dart:io' and | 357 /// Static utility methods for converting between 'dart:io' and |
| 177 /// 'mojo:network_service' structs. | 358 /// 'mojo:network_service' structs. |
| 178 class _NetworkServiceCodec { | 359 class _NetworkServiceCodec { |
| 179 /// Returns a string representation of an ip address encoded in [address]. | 360 /// Returns a string representation of an ip address encoded in [address]. |
| 180 /// Supports both IPv4 and IPv6. | 361 /// Supports both IPv4 and IPv6. |
| 181 static String _addressToString(List<int> address) { | 362 static String _addressToString(List<int> address) { |
| 182 String r = ''; | 363 String r = ''; |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 294 } | 475 } |
| 295 | 476 |
| 296 static _throwOnError(NetworkError error) { | 477 static _throwOnError(NetworkError error) { |
| 297 if (_okay(error)) { | 478 if (_okay(error)) { |
| 298 return; | 479 return; |
| 299 } | 480 } |
| 300 throw new OSError(error.description, error.code); | 481 throw new OSError(error.description, error.code); |
| 301 } | 482 } |
| 302 } | 483 } |
| 303 | 484 |
| OLD | NEW |