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

Side by Side Diff: runtime/bin/vmservice/loader.dart

Issue 1154173006: - Avoid using a streamed receive port for single messages. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Removed unused methods. Created 5 years, 6 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
« runtime/bin/builtin.dart ('K') | « runtime/bin/builtin.dart ('k') | 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) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, 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 part of vmservice_io; 5 part of vmservice_io;
6 6
7 var _httpClient; 7 var _httpClient;
8 8
9 void _loadHttp(sendPort, uri) { 9 // Send a response to the requesting isolate.
10 void _sendResponse(SendPort sp, int id, dynamic data) {
11 assert((data is List<int>) || (data is String));
12 var msg = new List(2);
13 msg[0] = id;
14 msg[1] = data;
15 sp.send(msg);
16 }
17
18 void _loadHttp(SendPort sp, int id, Uri uri) {
10 if (_httpClient == null) { 19 if (_httpClient == null) {
11 _httpClient = new HttpClient()..maxConnectionsPerHost = 6; 20 _httpClient = new HttpClient()..maxConnectionsPerHost = 6;
12 } 21 }
13 _httpClient.getUrl(uri) 22 _httpClient.getUrl(uri)
14 .then((HttpClientRequest request) => request.close()) 23 .then((HttpClientRequest request) => request.close())
15 .then((HttpClientResponse response) { 24 .then((HttpClientResponse response) {
16 var builder = new BytesBuilder(copy: false); 25 var builder = new BytesBuilder(copy: false);
17 response.listen( 26 response.listen(
18 builder.add, 27 builder.add,
19 onDone: () { 28 onDone: () {
20 if (response.statusCode != 200) { 29 if (response.statusCode != 200) {
21 var msg = 'Failure getting $uri: ' 30 var msg = "Failure getting $uri:\n"
22 '${response.statusCode} ${response.reasonPhrase}'; 31 " ${response.statusCode} ${response.reasonPhrase}";
23 sendPort.send(msg); 32 _sendResponse(sp, id, msg);
24 } else { 33 } else {
25 sendPort.send(builder.takeBytes()); 34 _sendResponse(sp, id, builder.takeBytes());
26 } 35 }
27 }, 36 },
28 onError: (e) { 37 onError: (e) {
29 sendPort.send(e.toString()); 38 _sendResponse(sp, d, e.toString());
30 }); 39 });
31 }) 40 })
32 .catchError((e) { 41 .catchError((e) {
33 sendPort.send(e.toString()); 42 _sendResponse(sp, id, e.toString());
34 }); 43 });
35 // It's just here to push an event on the event loop so that we invoke the 44 // It's just here to push an event on the event loop so that we invoke the
36 // scheduled microtasks. 45 // scheduled microtasks.
37 Timer.run(() {}); 46 Timer.run(() {});
38 } 47 }
39 48
40 void _loadFile(sendPort, path) { 49 void _loadFile(SendPort sp, int id, Uri uri) {
50 var path = uri.toFilePath();
41 var sourceFile = new File(path); 51 var sourceFile = new File(path);
42 sourceFile.readAsBytes().then((data) { 52 sourceFile.readAsBytes().then((data) {
43 sendPort.send(data); 53 _sendResponse(sp, id, data);
44 }, 54 },
45 onError: (e) { 55 onError: (e) {
46 sendPort.send(e.toString()); 56 var err = "Error loading $uri:\n $e";
57 _sendResponse(sp, id, err);
47 }); 58 });
48 } 59 }
49 60
50 var dataUriRegex = new RegExp( 61 var dataUriRegex = new RegExp(
51 r"data:([\w-]+/[\w-]+)?(;charset=([\w-]+))?(;base64)?,(.*)"); 62 r"data:([\w-]+/[\w-]+)?(;charset=([\w-]+))?(;base64)?,(.*)");
52 63
53 void _loadDataUri(sendPort, uri) { 64 void _loadDataUri(SendPort sp, int id, Uri uri) {
54 try { 65 try {
55 var match = dataUriRegex.firstMatch(uri.toString()); 66 var match = dataUriRegex.firstMatch(uri.toString());
56 if (match == null) throw "Malformed data uri"; 67 if (match == null) throw "Malformed data uri";
57 68
58 var mimeType = match.group(1); 69 var mimeType = match.group(1);
59 var encoding = match.group(3); 70 var encoding = match.group(3);
60 var maybeBase64 = match.group(4); 71 var maybeBase64 = match.group(4);
61 var encodedData = match.group(5); 72 var encodedData = match.group(5);
62 73
63 if (mimeType != "application/dart") { 74 if (mimeType != "application/dart") {
64 throw "MIME-type must be application/dart"; 75 throw "MIME-type must be application/dart";
65 } 76 }
66 if (encoding != "utf-8") { 77 if (encoding != "utf-8") {
67 // Default is ASCII. The C++ portion of the embedder assumes UTF-8. 78 // Default is ASCII. The C++ portion of the embedder assumes UTF-8.
68 throw "Only utf-8 encoding is supported"; 79 throw "Only utf-8 encoding is supported";
69 } 80 }
70 if (maybeBase64 != null) { 81 if (maybeBase64 != null) {
71 throw "Only percent encoding is supported"; 82 throw "Only percent encoding is supported";
72 } 83 }
73 84
74 var data = UTF8.encode(Uri.decodeComponent(encodedData)); 85 var data = UTF8.encode(Uri.decodeComponent(encodedData));
75 sendPort.send(data); 86 _sendResponse(sp, id, data);
76 } catch (e) { 87 } catch (e) {
77 sendPort.send("Invalid data uri ($uri) $e"); 88 _sendResponse(sp, id, "Invalid data uri ($uri):\n $e");
78 } 89 }
79 } 90 }
80 91
81 _processLoadRequest(request) { 92 _processLoadRequest(request) {
82 var sp = request[0]; 93 SendPort sp = request[0];
83 var uri = Uri.parse(request[1]); 94 int id = request[1];
95 String resource = request[2];
96 var uri = Uri.parse(request[2]);
84 if (uri.scheme == 'file') { 97 if (uri.scheme == 'file') {
85 _loadFile(sp, uri.toFilePath()); 98 _loadFile(sp, id, uri);
86 } else if ((uri.scheme == 'http') || (uri.scheme == 'https')) { 99 } else if ((uri.scheme == 'http') || (uri.scheme == 'https')) {
87 _loadHttp(sp, uri); 100 _loadHttp(sp, id, uri);
88 } else if ((uri.scheme == 'data')) { 101 } else if ((uri.scheme == 'data')) {
89 _loadDataUri(sp, uri); 102 _loadDataUri(sp, id, uri);
90 } else { 103 } else {
91 sp.send('Unknown scheme (${uri.scheme}) for $uri'); 104 sp.send('Unknown scheme (${uri.scheme}) for $uri');
92 } 105 }
93 } 106 }
OLDNEW
« runtime/bin/builtin.dart ('K') | « runtime/bin/builtin.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698