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

Side by Side Diff: sdk/lib/vmservice/asset.dart

Issue 1411853003: Service isolate requests Observatory assets from embedder (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years, 1 month 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
« no previous file with comments | « runtime/vm/service.cc ('k') | sdk/lib/vmservice/vmservice.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 part of dart._vmservice;
6
7 class Asset {
8 final String name;
9 final Uint8List data;
10
11 Asset(this.name, this.data);
12
13 String get mimeType {
14 var extensionStart = name.lastIndexOf('.');
15 var extension = name.substring(extensionStart+1);
16 switch (extension) {
17 case 'html':
18 return 'text/html; charset=UTF-8';
19 case 'dart':
20 return 'application/dart; charset=UTF-8';
21 case 'js':
22 return 'application/javascript; charset=UTF-8';
23 case 'css':
24 return 'text/css; charset=UTF-8';
25 case 'gif':
26 return 'image/gif';
27 case 'png':
28 return 'image/png';
29 case 'jpg':
30 return 'image/jpeg';
31 case 'jpeg':
32 return 'image/jpeg';
33 case 'svg':
34 return 'image/svg+xml';
35 default:
36 return 'text/plain';
37 }
38 }
39
40 /// Call to request assets from the embedder.
41 static Map<String, Asset> request() {
42 Map<String, Asset> assets = new Map<String, Asset>();
43 Uint8List tarBytes = _requestAssets();
44 if (tarBytes == null) {
45 return assets;
46 }
47 _TarArchive archive = new _TarArchive(tarBytes);
48 while (archive.hasNext()) {
49 Asset asset = archive.next();
50 if (asset == null) {
51 // Skip over special files.
52 continue;
53 }
54 assets[asset.name] = asset;
55 }
56 return assets;
57 }
58
59 String toString() => '$name ($mimeType)';
60 }
61
62
63 class _ByteStream {
64 final Uint8List bytes;
65 final int offset;
66 int get length => bytes.length - offset;
67 int _cursor = 0;
68
69 _ByteStream(this.bytes, [this.offset = 0]);
70
71 void reset() {
72 _cursor = 0;
73 }
74
75 int peekByte([int index = 0]) => bytes[offset + _cursor + index];
76
77 int readByte() {
78 int r = peekByte();
79 _advance(1);
80 return r;
81 }
82
83 void skip(int bytes) => _advance(bytes);
84
85 void seekToNextBlock(int blockSize) {
86 int remainder = blockSize - (_cursor % blockSize);
87 _advance(remainder);
88 }
89
90 void _advance(int bytes) {
91 _cursor += bytes;
92 if (_cursor > length) {
93 _cursor = length;
94 }
95 }
96
97 int get remaining => length - _cursor;
98 bool get hasMore => remaining > 0;
99 int get cursor => _cursor;
100 void set cursor(int cursor) {
101 _cursor = cursor;
102 if (_cursor > length) {
103 _cursor = length;
104 }
105 }
106 }
107
108 class _TarArchive {
109 static const List<int> tarMagic = const [ 0x75, 0x73, 0x74, 0x61, 0x72, 0 ];
110 static const List<int> tarVersion = const [ 0x30, 0x30 ];
111 static const int tarHeaderSize = 512;
112 static const int tarHeaderFilenameSize = 100;
113 static const int tarHeaderFilenameOffset = 0;
114 static const int tarHeaderSizeSize = 12;
115 static const int tarHeaderSizeOffset = 124;
116 static const int tarHeaderTypeSize = 1;
117 static const int tarHeaderTypeOffset = 156;
118 static const int tarHeaderFileType = 0x30;
119
120 static String _readCString(_ByteStream bs, int length) {
121 StringBuffer sb = new StringBuffer();
122 int count = 0;
123 while (bs.hasMore && count < length) {
124 if (bs.peekByte() == 0) {
125 // Null character.
126 break;
127 }
128 sb.writeCharCode(bs.readByte());
129 count++;
130 }
131 return sb.toString();
132 }
133
134 static String _readFilename(_ByteStream bs) {
135 String filename = _readCString(bs, tarHeaderFilenameSize);
136 if (filename.startsWith('/')) {
137 return filename;
138 }
139 return '/' + filename;
140 }
141
142 static Uint8List _readContents(_ByteStream bs, int size) {
143 Uint8List result = new Uint8List(size);
144 int i = 0;
145 while (bs.hasMore && i < size) {
146 result[i] = bs.readByte();
147 i++;
148 }
149 bs.seekToNextBlock(tarHeaderSize);
150 return result;
151 }
152
153 static void _skipContents(_ByteStream bs, int size) {
154 bs.skip(size);
155 bs.seekToNextBlock(tarHeaderSize);
156 }
157
158 static int _readSize(_ByteStream bs) {
159 String octalSize = _readCString(bs, tarHeaderSizeSize);
160 return int.parse(octalSize,
161 radix: 8,
162 onError: (_) => 0);
163 }
164
165 static int _readType(_ByteStream bs) {
166 return bs.readByte();
167 }
168
169 static bool _endOfArchive(_ByteStream bs) {
170 if (bs.remaining < (tarHeaderSize * 2)) {
171 return true;
172 }
173 for (int i = 0; i < (tarHeaderSize * 2); i++) {
174 if (bs.peekByte(i) != 0) {
175 return false;
176 }
177 }
178 return true;
179 }
180
181 final _ByteStream _bs;
182
183 _TarArchive(Uint8List bytes)
184 : _bs = new _ByteStream(bytes);
185
186 bool hasNext() {
187 return !_endOfArchive(_bs);
188 }
189
190 Asset next() {
191 int startOfBlock = _bs.cursor;
192 String filename = _readFilename(_bs);
193 _bs.cursor = startOfBlock + tarHeaderSizeOffset;
194 int size = _readSize(_bs);
195 _bs.cursor = startOfBlock + tarHeaderTypeOffset;
196 int type = _readType(_bs);
197 _bs.seekToNextBlock(tarHeaderSize);
198 if (type != tarHeaderFileType) {
199 _skipContents(_bs, size);
200 return null;
201 }
202 Uint8List bytes = _readContents(_bs, size);
203 return new Asset(filename, bytes);
204 }
205 }
OLDNEW
« no previous file with comments | « runtime/vm/service.cc ('k') | sdk/lib/vmservice/vmservice.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698