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 = true; | |
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 = await files.ptr.openFileSystem('app_persistent_cache', _rootDir ectory); | |
zra
2015/12/18 22:51:46
long line
Cutch
2015/12/18 23:05:20
Done.
| |
139 // Remove the root directory's handle from the open set because it is not | |
140 // under application control and does not affect isolate shutdown. | |
141 _rootDirectory.impl.endpoint.handle.pass(); | |
142 | |
143 // Ensure system temporary directory exists before returning the root | |
144 // directory. | |
145 await _getSystemTempDirectory(); | |
146 return _rootDirectory; | |
147 } | |
148 | |
149 /// Get the singleton DirectoryProxy for the system temp directory. | |
150 Future<DirectoryProxy> _getSystemTempDirectory() async { | |
151 if (_systemTempDirectory != null) { | |
152 return _systempTempDirectory; | |
153 } | |
154 DirectoryProxy rootDirectory = await _getRootDirectory(); | |
155 int flags = types.kOpenFlagRead | | |
156 types.kOpenFlagWrite | | |
157 types.kOpenFlagCreate; | |
158 _systemTempDirectory = new DirectoryProxy.unbound(); | |
159 var response = | |
160 await rootDirectory.ptr.openDirectory(systemTempPath, | |
161 _systemTempDirectory, | |
162 flags); | |
163 assert(response.error == types.Error.ok); | |
164 // Remove the system temp directory's handle from the open set because it | |
165 // is not under application control and does not affect isolate shutdown. | |
166 _systemTempDirectory.impl.endpoint.handle.pass(); | |
167 return _systemTempDirectory; | |
168 } | |
169 | |
111 /// Static utility methods for converting between 'dart:io' and | 170 /// Static utility methods for converting between 'dart:io' and |
112 /// 'mojo:network_service' structs. | 171 /// 'mojo:network_service' structs. |
113 class _NetworkServiceCodec { | 172 class _NetworkServiceCodec { |
114 /// Returns a string representation of an ip address encoded in [address]. | 173 /// Returns a string representation of an ip address encoded in [address]. |
115 /// Supports both IPv4 and IPv6. | 174 /// Supports both IPv4 and IPv6. |
116 static String _addressToString(List<int> address) { | 175 static String _addressToString(List<int> address) { |
117 String r = ''; | 176 String r = ''; |
118 if (address == null) { | 177 if (address == null) { |
119 return r; | 178 return r; |
120 } | 179 } |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
229 } | 288 } |
230 | 289 |
231 static _throwOnError(NetworkError error) { | 290 static _throwOnError(NetworkError error) { |
232 if (_okay(error)) { | 291 if (_okay(error)) { |
233 return; | 292 return; |
234 } | 293 } |
235 throw new OSError(error.description, error.code); | 294 throw new OSError(error.description, error.code); |
236 } | 295 } |
237 } | 296 } |
238 | 297 |
OLD | NEW |