Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1253)

Side by Side Diff: runtime/bin/builtin.dart

Issue 539473002: Reuse HttpClient in the built-in resource loader. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix merge copy. Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 library builtin; 5 library builtin;
6 import 'dart:io'; 6 import 'dart:io';
7 import 'dart:async'; 7 import 'dart:async';
8 import 'dart:convert'; 8 import 'dart:convert';
9 // import 'root_library'; happens here from C Code 9 // import 'root_library'; happens here from C Code
10 10
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 150
151 const _DART_EXT = 'dart-ext:'; 151 const _DART_EXT = 'dart-ext:';
152 152
153 String _resolveUri(String base, String userString) { 153 String _resolveUri(String base, String userString) {
154 _logResolution('# Resolving: $userString from $base'); 154 _logResolution('# Resolving: $userString from $base');
155 var baseUri = Uri.parse(base); 155 var baseUri = Uri.parse(base);
156 if (userString.startsWith(_DART_EXT)) { 156 if (userString.startsWith(_DART_EXT)) {
157 var uri = userString.substring(_DART_EXT.length); 157 var uri = userString.substring(_DART_EXT.length);
158 return '$_DART_EXT${baseUri.resolve(uri)}'; 158 return '$_DART_EXT${baseUri.resolve(uri)}';
159 } else { 159 } else {
160 return '${baseUri.resolve(userString)}'; 160 return baseUri.resolve(userString).toString();
161 } 161 }
162 } 162 }
163 163
164 164
165 // Returns either a file path or a URI starting with http:, as a String. 165 // Returns either a file path or a URI starting with http:, as a String.
166 String _filePathFromUri(String userUri) { 166 String _filePathFromUri(String userUri) {
167 var uri = Uri.parse(userUri); 167 var uri = Uri.parse(userUri);
168 _logResolution('# Getting file path from: $uri'); 168 _logResolution('# Getting file path from: $uri');
169 169
170 var path; 170 var path;
(...skipping 27 matching lines...) Expand all
198 } 198 }
199 199
200 var packageRoot = _packageRoot == null ? 200 var packageRoot = _packageRoot == null ?
201 _entryPointScript.resolve('packages/') : 201 _entryPointScript.resolve('packages/') :
202 _packageRoot; 202 _packageRoot;
203 return _filePathFromUri(packageRoot.resolve(uri.path).toString()); 203 return _filePathFromUri(packageRoot.resolve(uri.path).toString());
204 } 204 }
205 205
206 206
207 int _numOutstandingLoadRequests = 0; 207 int _numOutstandingLoadRequests = 0;
208 208 var _httpClient;
209 209
210 void _httpGet(Uri uri, String libraryUri, loadCallback(List<int> data)) { 210 void _httpGet(Uri uri, String libraryUri, loadCallback(List<int> data)) {
211 var httpClient = new HttpClient(); 211 if (_httpClient == null) {
212 try { 212 _httpClient = new HttpClient()..maxConnectionsPerHost = 6;
213 httpClient.getUrl(uri)
214 .then((HttpClientRequest request) {
215 request.persistentConnection = false;
216 return request.close();
217 })
218 .then((HttpClientResponse response) {
219 // Only create a ByteBuilder if multiple chunks are received.
220 var builder = new BytesBuilder(copy: false);
221 response.listen(
222 builder.add,
223 onDone: () {
224 if (response.statusCode != 200) {
225 var msg = 'Failure getting $uri: '
226 '${response.statusCode} ${response.reasonPhrase}';
227 _asyncLoadError(uri.toString(), libraryUri, msg);
228 }
229
230 List<int> data = builder.takeBytes();
231 httpClient.close();
232 loadCallback(data);
233 },
234 onError: (error) {
235 _asyncLoadError(uri.toString(), libraryUri, error);
236 });
237 })
238 .catchError((error) {
239 _asyncLoadError(uri.toString(), libraryUri, error);
240 });
241 } catch (error) {
242 _asyncLoadError(uri.toString(), libraryUri, error);
243 } 213 }
214 _httpClient.getUrl(uri)
215 .then((HttpClientRequest request) => request.close())
216 .then((HttpClientResponse response) {
217 // Only create a ByteBuilder if multiple chunks are received.
Ivan Posva 2014/09/03 15:37:38 I am reading this comment as a TODO, correct?
Anders Johnsen 2014/09/04 08:26:28 Removing, the bytebuffer does this for us.
218 var builder = new BytesBuilder(copy: false);
219 response.listen(
220 builder.add,
221 onDone: () {
222 if (response.statusCode != 200) {
223 var msg = 'Failure getting $uri: '
224 '${response.statusCode} ${response.reasonPhrase}';
225 _asyncLoadError(uri.toString(), libraryUri, msg);
226 }
227 loadCallback(builder.takeBytes());
228 },
229 onError: (error) {
230 _asyncLoadError(uri.toString(), libraryUri, error);
231 });
232 })
233 .catchError((error) {
234 _asyncLoadError(uri.toString(), libraryUri, error);
235 });
244 // TODO(floitsch): remove this line. It's just here to push an event on the 236 // TODO(floitsch): remove this line. It's just here to push an event on the
245 // event loop so that we invoke the scheduled microtasks. Also remove the 237 // event loop so that we invoke the scheduled microtasks. Also remove the
246 // import of dart:async when this line is not needed anymore. 238 // import of dart:async when this line is not needed anymore.
247 Timer.run(() {}); 239 Timer.run(() {});
248 } 240 }
249 241
250 242
251 void _signalDoneLoading() native "Builtin_DoneLoading"; 243 void _signalDoneLoading() native "Builtin_DoneLoading";
252 244
253 void _loadScriptCallback(int tag, String uri, String libraryUri, List<int> data) 245 void _loadScriptCallback(int tag, String uri, String libraryUri, List<int> data)
254 native "Builtin_LoadScript"; 246 native "Builtin_LoadScript";
255 247
256 void _loadScript(int tag, String uri, String libraryUri, List<int> data) { 248 void _loadScript(int tag, String uri, String libraryUri, List<int> data) {
257 // TODO: Currently a compilation error while loading the script is 249 // TODO: Currently a compilation error while loading the script is
258 // fatal for the isolate. _loadScriptCallback() does not return and 250 // fatal for the isolate. _loadScriptCallback() does not return and
259 // the _numOutstandingLoadRequests counter remains out of sync. 251 // the _numOutstandingLoadRequests counter remains out of sync.
260 _loadScriptCallback(tag, uri, libraryUri, data); 252 _loadScriptCallback(tag, uri, libraryUri, data);
261 assert(_numOutstandingLoadRequests > 0); 253 assert(_numOutstandingLoadRequests > 0);
262 _numOutstandingLoadRequests--; 254 _numOutstandingLoadRequests--;
263 _logResolution("native Builtin_LoadScript($uri) completed, " 255 _logResolution("native Builtin_LoadScript($uri) completed, "
264 "${_numOutstandingLoadRequests} requests remaining"); 256 "${_numOutstandingLoadRequests} requests remaining");
265 if (_numOutstandingLoadRequests == 0) { 257 if (_numOutstandingLoadRequests == 0) {
266 _signalDoneLoading(); 258 _signalDoneLoading();
259 if (_httpClient != null) {
Ivan Posva 2014/09/03 15:37:38 Can you move this code in a helper function which
Anders Johnsen 2014/09/04 08:26:28 Good catch.
260 _httpClient.close();
261 _httpClient = null;
262 }
267 } 263 }
268 } 264 }
269 265
270 266
271 void _asyncLoadErrorCallback(uri, libraryUri, error) 267 void _asyncLoadErrorCallback(uri, libraryUri, error)
272 native "Builtin_AsyncLoadError"; 268 native "Builtin_AsyncLoadError";
273 269
274 void _asyncLoadError(uri, libraryUri, error) { 270 void _asyncLoadError(uri, libraryUri, error) {
275 assert(_numOutstandingLoadRequests > 0); 271 assert(_numOutstandingLoadRequests > 0);
276 _logResolution("_asyncLoadError($uri), error: $error"); 272 _logResolution("_asyncLoadError($uri), error: $error");
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
348 } else if (Platform.isWindows) { 344 } else if (Platform.isWindows) {
349 filename = '$name.dll'; 345 filename = '$name.dll';
350 } else { 346 } else {
351 _logResolution( 347 _logResolution(
352 'Native extensions not supported on ${Platform.operatingSystem}'); 348 'Native extensions not supported on ${Platform.operatingSystem}');
353 throw 'Native extensions not supported on ${Platform.operatingSystem}'; 349 throw 'Native extensions not supported on ${Platform.operatingSystem}';
354 } 350 }
355 351
356 return [path, filename, name]; 352 return [path, filename, name];
357 } 353 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698