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:_mojo/application.dart'; | 5 import 'dart:_mojo/application.dart'; |
6 import 'dart:_mojo/bindings.dart'; | 6 import 'dart:_mojo/bindings.dart'; |
7 import 'dart:_mojo/core.dart'; | 7 import 'dart:_mojo/core.dart'; |
8 import 'dart:_mojo/mojo/network_error.mojom.dart'; | 8 import 'dart:_mojo/mojo/network_error.mojom.dart'; |
9 import 'dart:_mojo_services/mojo/host_resolver.mojom.dart'; | 9 import 'dart:_mojo_services/mojo/host_resolver.mojom.dart'; |
10 import 'dart:_mojo_services/mojo/net_address.mojom.dart'; | 10 import 'dart:_mojo_services/mojo/net_address.mojom.dart'; |
11 import 'dart:_mojo_services/mojo/network_service.mojom.dart'; | 11 import 'dart:_mojo_services/mojo/network_service.mojom.dart'; |
12 import 'dart:_mojo_services/mojo/tcp_bound_socket.mojom.dart'; | 12 import 'dart:_mojo_services/mojo/tcp_bound_socket.mojom.dart'; |
13 import 'dart:_mojo_services/mojo/tcp_connected_socket.mojom.dart'; | 13 import 'dart:_mojo_services/mojo/tcp_connected_socket.mojom.dart'; |
14 import 'dart:_mojo_services/mojo/tcp_server_socket.mojom.dart'; | 14 import 'dart:_mojo_services/mojo/tcp_server_socket.mojom.dart'; |
15 import 'dart:_mojo_services/mojo/files/file.mojom.dart'; | 15 import 'dart:_mojo_services/mojo/files/file.mojom.dart'; |
16 import 'dart:_mojo_services/mojo/files/files.mojom.dart'; | 16 import 'dart:_mojo_services/mojo/files/files.mojom.dart'; |
17 import 'dart:_mojo_services/mojo/files/directory.mojom.dart'; | 17 import 'dart:_mojo_services/mojo/files/directory.mojom.dart'; |
18 import 'dart:_mojo_services/mojo/files/ioctl.mojom.dart'; | 18 import 'dart:_mojo_services/mojo/files/ioctl.mojom.dart'; |
19 import 'dart:_mojo_services/mojo/files/types.mojom.dart'; | 19 import 'dart:_mojo_services/mojo/files/types.mojom.dart' as types; |
| 20 |
| 21 // When developing, set fileSystemDeveloper to true and the file system will |
| 22 // persist under ~/MojoAppPersistentCaches/. |
| 23 const bool fileSystemDeveloper = false; |
| 24 const String fileSystemName = |
| 25 fileSystemDeveloper ? 'app_persistent_cache' : null; |
| 26 |
| 27 // System temp path relative to the root directory. |
| 28 const String systemTempPath = 'tmp'; |
20 | 29 |
21 // | 30 // |
22 // Mojo objects and helper functions used by the 'dart:io' library. | 31 // Mojo objects and helper functions used by the 'dart:io' library. |
23 // | 32 // |
24 int _networkServiceHandle; | 33 int _networkServiceHandle; |
25 int _filesServiceHandle; | 34 int _filesServiceHandle; |
26 NetworkServiceProxy _networkService; | 35 NetworkServiceProxy _networkService; |
27 HostResolverProxy _hostResolver; | 36 HostResolverProxy _hostResolver; |
28 FilesProxy _files; | 37 FilesProxy _files; |
| 38 DirectoryProxy _rootDirectory; |
| 39 DirectoryProxy _systemTempDirectory; |
29 | 40 |
30 void _initialize(int networkServiceHandle, int filesServiceHandle) { | 41 void _initialize(int networkServiceHandle, int filesServiceHandle) { |
31 if (networkServiceHandle != MojoHandle.INVALID) { | 42 if (networkServiceHandle != MojoHandle.INVALID) { |
32 _networkServiceHandle = networkServiceHandle; | 43 _networkServiceHandle = networkServiceHandle; |
33 } | 44 } |
34 if (filesServiceHandle != MojoHandle.INVALID) { | 45 if (filesServiceHandle != MojoHandle.INVALID) { |
35 _filesServiceHandle = filesServiceHandle; | 46 _filesServiceHandle = filesServiceHandle; |
36 } | 47 } |
37 } | 48 } |
38 | 49 |
(...skipping 21 matching lines...) Expand all Loading... |
60 _networkService = null; | 71 _networkService = null; |
61 } | 72 } |
62 if (_hostResolver != null) { | 73 if (_hostResolver != null) { |
63 _hostResolver.close(immediate: true); | 74 _hostResolver.close(immediate: true); |
64 _hostResolver = null; | 75 _hostResolver = null; |
65 } | 76 } |
66 if (_files != null) { | 77 if (_files != null) { |
67 _files.close(immediate: true); | 78 _files.close(immediate: true); |
68 _files = null; | 79 _files = null; |
69 } | 80 } |
| 81 if (_rootDirectory != null) { |
| 82 _rootDirectory.close(immediate: true); |
| 83 _rootDirectory = null; |
| 84 } |
| 85 if (_systemTempDirectory != null) { |
| 86 _systemTempDirectory.close(immediate: true); |
| 87 _systemTempDirectory = null; |
| 88 } |
70 } | 89 } |
71 | 90 |
72 /// Get the singleton NetworkServiceProxy. | 91 /// Get the singleton NetworkServiceProxy. |
73 NetworkServiceProxy _getNetworkService() { | 92 NetworkServiceProxy _getNetworkService() { |
74 if (_networkService != null) { | 93 if (_networkService != null) { |
75 return _networkService; | 94 return _networkService; |
76 } | 95 } |
77 _networkService = new NetworkServiceProxy.fromHandle( | 96 _networkService = new NetworkServiceProxy.fromHandle( |
78 new MojoHandle(_networkServiceHandle).pass()); | 97 new MojoHandle(_networkServiceHandle).pass()); |
79 _networkServiceHandle = null; | 98 _networkServiceHandle = null; |
(...skipping 21 matching lines...) Expand all Loading... |
101 FilesProxy _getFiles() { | 120 FilesProxy _getFiles() { |
102 if (_files != null) { | 121 if (_files != null) { |
103 return _files; | 122 return _files; |
104 } | 123 } |
105 _files = new FilesProxy.fromHandle( | 124 _files = new FilesProxy.fromHandle( |
106 new MojoHandle(_filesServiceHandle).pass()); | 125 new MojoHandle(_filesServiceHandle).pass()); |
107 _filesServiceHandle = null; | 126 _filesServiceHandle = null; |
108 return _files; | 127 return _files; |
109 } | 128 } |
110 | 129 |
| 130 /// Get the singleton DirectoryProxy for the root directory. |
| 131 Future<DirectoryProxy> _getRootDirectory() async { |
| 132 if (_rootDirectory != null) { |
| 133 return _rootDirectory; |
| 134 } |
| 135 FilesProxy files = _getFiles(); |
| 136 assert(files != null); |
| 137 _rootDirectory = new DirectoryProxy.unbound(); |
| 138 var response = |
| 139 await files.ptr.openFileSystem(fileSystemName, _rootDirectory); |
| 140 // Remove the root directory's handle from the open set because it is not |
| 141 // under application control and does not affect isolate shutdown. |
| 142 _rootDirectory.impl.endpoint.handle.pass(); |
| 143 |
| 144 // Ensure system temporary directory exists before returning the root |
| 145 // directory. |
| 146 await _getSystemTempDirectory(); |
| 147 return _rootDirectory; |
| 148 } |
| 149 |
| 150 /// Get the singleton DirectoryProxy for the system temp directory. |
| 151 Future<DirectoryProxy> _getSystemTempDirectory() async { |
| 152 if (_systemTempDirectory != null) { |
| 153 return _systempTempDirectory; |
| 154 } |
| 155 DirectoryProxy rootDirectory = await _getRootDirectory(); |
| 156 int flags = types.kOpenFlagRead | |
| 157 types.kOpenFlagWrite | |
| 158 types.kOpenFlagCreate; |
| 159 _systemTempDirectory = new DirectoryProxy.unbound(); |
| 160 var response = |
| 161 await rootDirectory.ptr.openDirectory(systemTempPath, |
| 162 _systemTempDirectory, |
| 163 flags); |
| 164 assert(response.error == types.Error.ok); |
| 165 // Remove the system temp directory's handle from the open set because it |
| 166 // is not under application control and does not affect isolate shutdown. |
| 167 _systemTempDirectory.impl.endpoint.handle.pass(); |
| 168 return _systemTempDirectory; |
| 169 } |
| 170 |
111 /// Static utility methods for converting between 'dart:io' and | 171 /// Static utility methods for converting between 'dart:io' and |
112 /// 'mojo:network_service' structs. | 172 /// 'mojo:network_service' structs. |
113 class _NetworkServiceCodec { | 173 class _NetworkServiceCodec { |
114 /// Returns a string representation of an ip address encoded in [address]. | 174 /// Returns a string representation of an ip address encoded in [address]. |
115 /// Supports both IPv4 and IPv6. | 175 /// Supports both IPv4 and IPv6. |
116 static String _addressToString(List<int> address) { | 176 static String _addressToString(List<int> address) { |
117 String r = ''; | 177 String r = ''; |
118 if (address == null) { | 178 if (address == null) { |
119 return r; | 179 return r; |
120 } | 180 } |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 } | 289 } |
230 | 290 |
231 static _throwOnError(NetworkError error) { | 291 static _throwOnError(NetworkError error) { |
232 if (_okay(error)) { | 292 if (_okay(error)) { |
233 return; | 293 return; |
234 } | 294 } |
235 throw new OSError(error.description, error.code); | 295 throw new OSError(error.description, error.code); |
236 } | 296 } |
237 } | 297 } |
238 | 298 |
OLD | NEW |