OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium OS Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 'use strict'; | |
6 | |
7 /** | |
8 * Defines the protocol used to communicate between JS and NaCL. | |
9 * This should be consistent with cpp/request.h. | |
10 * @namespace | |
11 */ | |
12 unpacker.request = { | |
13 /** | |
14 * Defines request ids. Every key should be unique and the same as the keys | |
15 * on the NaCL side. | |
16 * @enum {string} | |
17 */ | |
18 Key: { | |
19 // Mandatory keys for all unpacking operations. | |
20 OPERATION: 'operation', // Should be a unpacker.request.Operation. | |
21 FILE_SYSTEM_ID: 'file_system_id', // Should be a string. | |
22 REQUEST_ID: 'request_id', // Should be a string. | |
23 | |
24 // Optional keys unique to unpacking operations. | |
25 METADATA: 'metadata', // Should be a dictionary. | |
26 ARCHIVE_SIZE: 'archive_size', // Should be a string as only int is | |
27 // supported by pp::Var on C++. | |
28 INDEX: 'index', // Should be a string. Same reason as ARCHIVE_SIZE. | |
29 ENCODING: 'encoding', // Should be a string. | |
30 OPEN_REQUEST_ID: 'open_request_id', // Should be a string, just like | |
31 // REQUEST_ID. | |
32 READ_FILE_DATA: 'read_file_data', // Should be an ArrayBuffer. | |
33 HAS_MORE_DATA: 'has_more_data', // Should be a boolean. | |
34 PASSPHRASE: 'passphrase', // Should be a string. | |
35 | |
36 // Mandatory keys for all packing operations. | |
37 COMPRESSOR_ID: 'compressor_id', // Should be an int. | |
38 | |
39 // Optional keys unique to packing operations. | |
40 ENTRY_ID: 'entry_id', // Should be an int. | |
41 PATHNAME: 'pathname', // should be a string. | |
42 FILE_SIZE: 'file_size', // should be a string. Same reason | |
43 // as ARCHIVE_SIZE. | |
44 IS_DIRECTORY: 'is_directory', // should be a boolean. | |
45 MODIFICATION_TIME: 'modification_time', // should be a string. | |
46 // (mm/dd/yy h:m:s) | |
47 HAS_ERROR: 'has_error', // Should be a boolean Sent from JS | |
48 // to NaCL. | |
49 | |
50 // Optional keys used for both packing and unpacking operations. | |
51 ERROR: 'error', // Should be a string. | |
52 CHUNK_BUFFER: 'chunk_buffer', // Should be an ArrayBuffer. | |
53 OFFSET: 'offset', // Should be a string. Same reason as ARCHIVE_SIZE. | |
54 LENGTH: 'length', // Should be a string. Same reason as ARCHIVE_SIZE. | |
55 SRC_FILE: 'src_file', // Should be a string. | |
56 SRC_LINE: 'src_line', // Should be a int. | |
57 SRC_FUNC: 'src_func', // Should be a string. | |
58 MESSAGE: 'message', // Should be a string. | |
59 }, | |
60 | |
61 /** | |
62 * Defines request operations. These operation should be the same as the | |
63 * operations on the NaCL side. FILE_SYSTEM_ID and REQUEST_ID are mandatory | |
64 * for all unpack requests, while COMPRESSOR_ID is required for all pack | |
65 * requests. All the values of unpacking operations must be smaller than any | |
66 * packing operation (except errors). | |
67 * @enum {number} | |
68 */ | |
69 Operation: { | |
70 READ_METADATA: 0, | |
71 READ_METADATA_DONE: 1, | |
72 READ_CHUNK: 2, | |
73 READ_CHUNK_DONE: 3, | |
74 READ_CHUNK_ERROR: 4, | |
75 READ_PASSPHRASE: 5, | |
76 READ_PASSPHRASE_DONE: 6, | |
77 READ_PASSPHRASE_ERROR: 7, | |
78 CLOSE_VOLUME: 8, | |
79 OPEN_FILE: 9, | |
80 OPEN_FILE_DONE: 10, | |
81 CLOSE_FILE: 11, | |
82 CLOSE_FILE_DONE: 12, | |
83 READ_FILE: 13, | |
84 READ_FILE_DONE: 14, | |
85 CONSOLE_LOG: 15, | |
86 CONSOLE_DEBUG: 16, | |
87 CREATE_ARCHIVE: 17, | |
88 CREATE_ARCHIVE_DONE: 18, | |
89 ADD_TO_ARCHIVE: 19, | |
90 ADD_TO_ARCHIVE_DONE: 20, | |
91 READ_FILE_CHUNK: 21, | |
92 READ_FILE_CHUNK_DONE: 22, | |
93 WRITE_CHUNK: 23, | |
94 WRITE_CHUNK_DONE: 24, | |
95 CLOSE_ARCHIVE: 25, | |
96 CLOSE_ARCHIVE_DONE: 26, | |
97 FILE_SYSTEM_ERROR: -1, | |
98 COMPRESSOR_ERROR: -2 | |
99 }, | |
100 | |
101 /** | |
102 * Operations greater than or equal to this value are for packing. | |
103 * @const {number} | |
104 */ | |
105 MINIMUM_PACK_REQUEST_VALUE: 17, | |
106 | |
107 /** | |
108 * Return true if the given operation is related to packing. | |
109 * @param {!unpacker.request.Operation} operation | |
110 * @return {boolean} | |
111 */ | |
112 isPackRequest: function(operation) { | |
113 return unpacker.request.MINIMUM_PACK_REQUEST_VALUE <= operation || | |
114 operation == unpacker.request.Operation.COMPRESSOR_ERROR; | |
115 }, | |
116 | |
117 /** | |
118 * Creates a basic request with mandatory fields. | |
119 * @param {!unpacker.request.Operation} operation | |
120 * @param {!unpacker.types.FileSystemId} fileSystemId | |
121 * @param {!unpacker.types.RequestId} requestId The request id. Should be | |
122 * unique only per file system. | |
123 * @private | |
124 * @return {!Object} A new request with mandatory fields. | |
125 */ | |
126 createBasic_: function(operation, fileSystemId, requestId) { | |
127 var basicRequest = {}; | |
128 basicRequest[unpacker.request.Key.OPERATION] = operation; | |
129 basicRequest[unpacker.request.Key.FILE_SYSTEM_ID] = fileSystemId; | |
130 basicRequest[unpacker.request.Key.REQUEST_ID] = requestId.toString(); | |
131 return basicRequest; | |
132 }, | |
133 | |
134 /** | |
135 * Creates a read metadata request. | |
136 * @param {!unpacker.types.FileSystemId} fileSystemId | |
137 * @param {!unpacker.types.RequestId} requestId | |
138 * @param {string} encoding Default encoding for the archive. | |
139 * @param {number} archiveSize The size of the archive for fileSystemId. | |
140 * @return {!Object} A read metadata request. | |
141 */ | |
142 createReadMetadataRequest: function(fileSystemId, requestId, encoding, | |
143 archiveSize) { | |
144 var readMetadataRequest = unpacker.request.createBasic_( | |
145 unpacker.request.Operation.READ_METADATA, fileSystemId, requestId); | |
146 readMetadataRequest[unpacker.request.Key.ENCODING] = encoding; | |
147 readMetadataRequest[unpacker.request.Key.ARCHIVE_SIZE] = | |
148 archiveSize.toString(); | |
149 return readMetadataRequest; | |
150 }, | |
151 | |
152 /** | |
153 * Creates a read chunk done response. This is a response to a READ_CHUNK | |
154 * request from NaCl. | |
155 * @param {!unpacker.types.FileSystemId} fileSystemId | |
156 * @param {!unpacker.types.RequestId} requestId | |
157 * @param {!ArrayBuffer} buffer A buffer containing the data that was read. | |
158 * @param {number} readOffset The offset from where buffer starts. This is | |
159 * required for distinguishing multiple read chunk requests done in | |
160 * parallel for different offsets. | |
161 * @return {!Object} A read chunk done response. | |
162 */ | |
163 createReadChunkDoneResponse: function(fileSystemId, requestId, buffer, | |
164 readOffset) { | |
165 var response = unpacker.request.createBasic_( | |
166 unpacker.request.Operation.READ_CHUNK_DONE, fileSystemId, requestId); | |
167 response[unpacker.request.Key.CHUNK_BUFFER] = buffer; | |
168 response[unpacker.request.Key.OFFSET] = readOffset.toString(); | |
169 return response; | |
170 }, | |
171 | |
172 /** | |
173 * Creates a read chunk error response. This is a response to a READ_CHUNK | |
174 * request from NaCl in case of any errors in order for NaCl to cleanup | |
175 * resources. | |
176 * @param {!unpacker.types.FileSystemId} fileSystemId | |
177 * @param {!unpacker.types.RequestId} requestId | |
178 * @return {!Object} A read chunk error response. | |
179 */ | |
180 createReadChunkErrorResponse: function(fileSystemId, requestId) { | |
181 return unpacker.request.createBasic_( | |
182 unpacker.request.Operation.READ_CHUNK_ERROR, fileSystemId, requestId); | |
183 }, | |
184 | |
185 /** | |
186 * Creates a read passphrase done response. This is a response to a | |
187 * READ_PASSPHRASE request from NaCl. | |
188 * @param {!unpacker.types.FileSystemId} fileSystemId | |
189 * @param {!unpacker.types.RequestId} requestId | |
190 * @param {string} passphrase The passphrase. | |
191 * @return {!Object} A read passphrase done response. | |
192 */ | |
193 createReadPassphraseDoneResponse: function(fileSystemId, requestId, | |
194 passphrase) { | |
195 var response = unpacker.request.createBasic_( | |
196 unpacker.request.Operation.READ_PASSPHRASE_DONE, fileSystemId, | |
197 requestId); | |
198 response[unpacker.request.Key.PASSPHRASE] = passphrase; | |
199 return response; | |
200 }, | |
201 | |
202 /** | |
203 * Creates a read passphrase error response. This is a response to a | |
204 * READ_PASSPHRASE request from NaCl in case of any errors in order for NaCl | |
205 * to cleanup resources. | |
206 * @param {!unpacker.types.FileSystemId} fileSystemId | |
207 * @param {!unpacker.types.RequestId} requestId | |
208 * @return {!Object} A read passphrase error response. | |
209 */ | |
210 createReadPassphraseErrorResponse: function(fileSystemId, requestId) { | |
211 return unpacker.request.createBasic_( | |
212 unpacker.request.Operation.READ_PASSPHRASE_ERROR, fileSystemId, | |
213 requestId); | |
214 }, | |
215 | |
216 /** | |
217 * Creates a request to close a volume related to a fileSystemId. | |
218 * Can be called after any request. | |
219 * @param {!unpacker.types.FileSystemId} fileSystemId | |
220 * @return {!Object} A close volume request. | |
221 */ | |
222 createCloseVolumeRequest: function(fileSystemId) { | |
223 return unpacker.request.createBasic_( | |
224 unpacker.request.Operation.CLOSE_VOLUME, fileSystemId, -1); | |
225 }, | |
226 | |
227 /** | |
228 * Creates an open file request. | |
229 * @param {!unpacker.types.FileSystemId} fileSystemId | |
230 * @param {number} index The index of the file in the header list. | |
231 * @param {string} encoding Default encoding for the archive. | |
232 * @param {number} archiveSize The size of the volume's archive. | |
233 * @return {!Object} An open file request. | |
234 */ | |
235 createOpenFileRequest: function(fileSystemId, requestId, index, encoding, | |
236 archiveSize) { | |
237 var openFileRequest = unpacker.request.createBasic_( | |
238 unpacker.request.Operation.OPEN_FILE, fileSystemId, requestId); | |
239 openFileRequest[unpacker.request.Key.INDEX] = index.toString(); | |
240 openFileRequest[unpacker.request.Key.ENCODING] = encoding; | |
241 openFileRequest[unpacker.request.Key.ARCHIVE_SIZE] = archiveSize.toString(); | |
242 return openFileRequest; | |
243 }, | |
244 | |
245 /** | |
246 * Creates a close file request. | |
247 * @param {!unpacker.types.FileSystemId} fileSystemId | |
248 * @param {!unpacker.types.RequestId} requestId | |
249 * @param {!unpacker.types.RequestId} openRequestId | |
250 * @return {!Object} A close file request. | |
251 */ | |
252 createCloseFileRequest: function(fileSystemId, requestId, openRequestId) { | |
253 var closeFileRequest = unpacker.request.createBasic_( | |
254 unpacker.request.Operation.CLOSE_FILE, fileSystemId, requestId); | |
255 closeFileRequest[unpacker.request.Key.OPEN_REQUEST_ID] = | |
256 openRequestId.toString(); | |
257 return closeFileRequest; | |
258 }, | |
259 | |
260 /** | |
261 * Creates a read file request. | |
262 * @param {!unpacker.types.FileSystemId} fileSystemId | |
263 * @param {!unpacker.types.RequestId} requestId | |
264 * @param {!unpacker.types.RequestId} openRequestId | |
265 * @param {number} offset The offset from where read is done. | |
266 * @param {number} length The number of bytes required. | |
267 * @return {!Object} A read file request. | |
268 */ | |
269 createReadFileRequest: function(fileSystemId, requestId, openRequestId, | |
270 offset, length) { | |
271 var readFileRequest = unpacker.request.createBasic_( | |
272 unpacker.request.Operation.READ_FILE, fileSystemId, requestId); | |
273 readFileRequest[unpacker.request.Key.OPEN_REQUEST_ID] = | |
274 openRequestId.toString(); | |
275 readFileRequest[unpacker.request.Key.OFFSET] = offset.toString(); | |
276 readFileRequest[unpacker.request.Key.LENGTH] = length.toString(); | |
277 return readFileRequest; | |
278 }, | |
279 | |
280 /** | |
281 * Creates a create archive request for compressor. | |
282 * @param {!unpacker.types.CompressorId} compressorId | |
283 * @return {!Object} A create archive request. | |
284 */ | |
285 createCreateArchiveRequest: function(compressorId) { | |
286 var request = {}; | |
287 request[unpacker.request.Key.OPERATION] = | |
288 unpacker.request.Operation.CREATE_ARCHIVE; | |
289 request[unpacker.request.Key.COMPRESSOR_ID] = compressorId; | |
290 return request; | |
291 }, | |
292 | |
293 /** | |
294 * Creates an add to archive request for compressor. | |
295 * @param {!unpacker.types.CompressorId} compressorId | |
296 * @param {!unpacker.types.EntryId} entryId | |
297 * @param {string} pathname The relative path of the entry. | |
298 * @param {number} fileSize The size of the entry. | |
299 * @param {string} modificationTime The modification time of the entry. | |
300 * @param {boolean} isDirectory Whether the entry is a directory or not. | |
301 * @return {!Object} An add to archive request. | |
302 */ | |
303 createAddToArchiveRequest: function(compressorId, entryId, pathname, | |
304 fileSize, modificationTime, isDirectory) { | |
305 var request = {}; | |
306 request[unpacker.request.Key.OPERATION] = | |
307 unpacker.request.Operation.ADD_TO_ARCHIVE; | |
308 request[unpacker.request.Key.COMPRESSOR_ID] = compressorId; | |
309 request[unpacker.request.Key.ENTRY_ID] = entryId; | |
310 request[unpacker.request.Key.PATHNAME] = pathname.toString(); | |
311 request[unpacker.request.Key.FILE_SIZE] = fileSize.toString(); | |
312 request[unpacker.request.Key.MODIFICATION_TIME] = | |
313 modificationTime.toString(); | |
314 request[unpacker.request.Key.IS_DIRECTORY] = isDirectory; | |
315 return request; | |
316 }, | |
317 | |
318 /** | |
319 * Creates a read file chunk response for compressor. | |
320 * @param {!unpacker.types.CompressorId} compressorId | |
321 * @param {number} length The number of bytes read from the entry. | |
322 * @param {!ArrayBuffer} buffer A buffer containing the data that was read. | |
323 * @return {!Object} A read file chunk done response. | |
324 */ | |
325 createReadFileChunkDoneResponse: function(compressorId, length, buffer) { | |
326 var response = {}; | |
327 response[unpacker.request.Key.OPERATION] = | |
328 unpacker.request.Operation.READ_FILE_CHUNK_DONE; | |
329 response[unpacker.request.Key.COMPRESSOR_ID] = compressorId; | |
330 response[unpacker.request.Key.LENGTH] = length.toString(); | |
331 response[unpacker.request.Key.CHUNK_BUFFER] = buffer; | |
332 return response; | |
333 }, | |
334 | |
335 /** | |
336 * Creates a write chunk done response for compressor. | |
337 * @param {!unpacker.types.CompressorId} compressorId | |
338 * @param {number} length The number of bytes written onto the archive file. | |
339 * @return {!Object} A write chunk done response. | |
340 */ | |
341 createWriteChunkDoneResponse: function(compressorId, length) { | |
342 var response = {}; | |
343 response[unpacker.request.Key.OPERATION] = | |
344 unpacker.request.Operation.WRITE_CHUNK_DONE; | |
345 response[unpacker.request.Key.COMPRESSOR_ID] = compressorId; | |
346 response[unpacker.request.Key.LENGTH] = length.toString(); | |
347 return response; | |
348 }, | |
349 | |
350 /** | |
351 * Creates a close archive request for compressor. | |
352 * @param {!unpacker.types.CompressorId} compressorId | |
353 * @param {boolean} hasError True if some error occurred. | |
354 * @return {!Object} A close archive request. | |
355 */ | |
356 createCloseArchiveRequest: function(compressorId, hasError) { | |
357 var request = {}; | |
358 request[unpacker.request.Key.OPERATION] = | |
359 unpacker.request.Operation.CLOSE_ARCHIVE; | |
360 request[unpacker.request.Key.COMPRESSOR_ID] = compressorId; | |
361 request[unpacker.request.Key.HAS_ERROR] = hasError; | |
362 return request; | |
363 } | |
364 }; | |
OLD | NEW |