OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 define("mojo/public/js/validator", [ | 5 define("mojo/public/js/validator", [ |
6 "mojo/public/js/codec", | 6 "mojo/public/js/codec", |
7 ], function(codec) { | 7 ], function(codec) { |
8 | 8 |
9 var validationError = { | 9 var validationError = { |
10 NONE: 'VALIDATION_ERROR_NONE', | 10 NONE: 'VALIDATION_ERROR_NONE', |
11 MISALIGNED_OBJECT: 'VALIDATION_ERROR_MISALIGNED_OBJECT', | 11 MISALIGNED_OBJECT: 'VALIDATION_ERROR_MISALIGNED_OBJECT', |
12 ILLEGAL_MEMORY_RANGE: 'VALIDATION_ERROR_ILLEGAL_MEMORY_RANGE', | 12 ILLEGAL_MEMORY_RANGE: 'VALIDATION_ERROR_ILLEGAL_MEMORY_RANGE', |
13 UNEXPECTED_STRUCT_HEADER: 'VALIDATION_ERROR_UNEXPECTED_STRUCT_HEADER', | 13 UNEXPECTED_STRUCT_HEADER: 'VALIDATION_ERROR_UNEXPECTED_STRUCT_HEADER', |
14 UNEXPECTED_ARRAY_HEADER: 'VALIDATION_ERROR_UNEXPECTED_ARRAY_HEADER', | 14 UNEXPECTED_ARRAY_HEADER: 'VALIDATION_ERROR_UNEXPECTED_ARRAY_HEADER', |
15 ILLEGAL_HANDLE: 'VALIDATION_ERROR_ILLEGAL_HANDLE', | 15 ILLEGAL_HANDLE: 'VALIDATION_ERROR_ILLEGAL_HANDLE', |
16 UNEXPECTED_INVALID_HANDLE: 'VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE', | 16 UNEXPECTED_INVALID_HANDLE: 'VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE', |
17 ILLEGAL_POINTER: 'VALIDATION_ERROR_ILLEGAL_POINTER', | 17 ILLEGAL_POINTER: 'VALIDATION_ERROR_ILLEGAL_POINTER', |
18 UNEXPECTED_NULL_POINTER: 'VALIDATION_ERROR_UNEXPECTED_NULL_POINTER', | 18 UNEXPECTED_NULL_POINTER: 'VALIDATION_ERROR_UNEXPECTED_NULL_POINTER', |
19 MESSAGE_HEADER_INVALID_FLAGS: | 19 MESSAGE_HEADER_INVALID_FLAGS: |
20 'VALIDATION_ERROR_MESSAGE_HEADER_INVALID_FLAGS', | 20 'VALIDATION_ERROR_MESSAGE_HEADER_INVALID_FLAGS', |
21 MESSAGE_HEADER_MISSING_REQUEST_ID: | 21 MESSAGE_HEADER_MISSING_REQUEST_ID: |
22 'VALIDATION_ERROR_MESSAGE_HEADER_MISSING_REQUEST_ID', | 22 'VALIDATION_ERROR_MESSAGE_HEADER_MISSING_REQUEST_ID', |
23 DIFFERENT_SIZED_ARRAYS_IN_MAP: | 23 DIFFERENT_SIZED_ARRAYS_IN_MAP: |
24 'VALIDATION_ERROR_DIFFERENT_SIZED_ARRAYS_IN_MAP', | 24 'VALIDATION_ERROR_DIFFERENT_SIZED_ARRAYS_IN_MAP', |
25 INVALID_UNION_SIZE: 'VALIDATION_ERROR_INVALID_UNION_SIZE', | 25 INVALID_UNION_SIZE: 'VALIDATION_ERROR_INVALID_UNION_SIZE', |
26 UNEXPECTED_NULL_UNION: 'VALIDATION_ERROR_UNEXPECTED_NULL_UNION', | 26 UNEXPECTED_NULL_UNION: 'VALIDATION_ERROR_UNEXPECTED_NULL_UNION', |
| 27 UNKNOWN_ENUM_VALUE: 'VALIDATION_ERROR_UNKNOWN_ENUM_VALUE', |
27 }; | 28 }; |
28 | 29 |
29 var NULL_MOJO_POINTER = "NULL_MOJO_POINTER"; | 30 var NULL_MOJO_POINTER = "NULL_MOJO_POINTER"; |
30 | 31 |
| 32 function isEnumClass(cls) { |
| 33 return cls instanceof codec.Enum; |
| 34 } |
| 35 |
31 function isStringClass(cls) { | 36 function isStringClass(cls) { |
32 return cls === codec.String || cls === codec.NullableString; | 37 return cls === codec.String || cls === codec.NullableString; |
33 } | 38 } |
34 | 39 |
35 function isHandleClass(cls) { | 40 function isHandleClass(cls) { |
36 return cls === codec.Handle || cls === codec.NullableHandle; | 41 return cls === codec.Handle || cls === codec.NullableHandle; |
37 } | 42 } |
38 | 43 |
39 function isInterfaceClass(cls) { | 44 function isInterfaceClass(cls) { |
40 return cls === codec.Interface || cls === codec.NullableInterface; | 45 return cls === codec.Interface || cls === codec.NullableInterface; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 return true; | 96 return true; |
92 | 97 |
93 if (index < this.handleIndex || index >= this.handleIndexLimit) | 98 if (index < this.handleIndex || index >= this.handleIndexLimit) |
94 return false; | 99 return false; |
95 | 100 |
96 // This is safe because handle indices are uint32. | 101 // This is safe because handle indices are uint32. |
97 this.handleIndex = index + 1; | 102 this.handleIndex = index + 1; |
98 return true; | 103 return true; |
99 } | 104 } |
100 | 105 |
| 106 Validator.prototype.validateEnum = function(offset, enumClass, nullable) { |
| 107 // Note: Assumes that enums are always 32 bits! But this matches |
| 108 // mojom::generate::pack::PackedField::GetSizeForKind, so it should be okay. |
| 109 var value = this.message.buffer.getInt32(offset); |
| 110 return enumClass.validate(value); |
| 111 } |
| 112 |
101 Validator.prototype.validateHandle = function(offset, nullable) { | 113 Validator.prototype.validateHandle = function(offset, nullable) { |
102 var index = this.message.buffer.getUint32(offset); | 114 var index = this.message.buffer.getUint32(offset); |
103 | 115 |
104 if (index === codec.kEncodedInvalidHandleValue) | 116 if (index === codec.kEncodedInvalidHandleValue) |
105 return nullable ? | 117 return nullable ? |
106 validationError.NONE : validationError.UNEXPECTED_INVALID_HANDLE; | 118 validationError.NONE : validationError.UNEXPECTED_INVALID_HANDLE; |
107 | 119 |
108 if (!this.claimHandle(index)) | 120 if (!this.claimHandle(index)) |
109 return validationError.ILLEGAL_HANDLE; | 121 return validationError.ILLEGAL_HANDLE; |
110 return validationError.NONE; | 122 return validationError.NONE; |
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 if (isStringClass(elementType)) | 352 if (isStringClass(elementType)) |
341 return this.validateArrayElements( | 353 return this.validateArrayElements( |
342 elementsOffset, numElements, codec.Uint8, nullable, [0], 0); | 354 elementsOffset, numElements, codec.Uint8, nullable, [0], 0); |
343 if (elementType instanceof codec.PointerTo) | 355 if (elementType instanceof codec.PointerTo) |
344 return this.validateStructElements( | 356 return this.validateStructElements( |
345 elementsOffset, numElements, elementType.cls, nullable); | 357 elementsOffset, numElements, elementType.cls, nullable); |
346 if (elementType instanceof codec.ArrayOf) | 358 if (elementType instanceof codec.ArrayOf) |
347 return this.validateArrayElements( | 359 return this.validateArrayElements( |
348 elementsOffset, numElements, elementType.cls, nullable, | 360 elementsOffset, numElements, elementType.cls, nullable, |
349 expectedDimensionSizes, currentDimension + 1); | 361 expectedDimensionSizes, currentDimension + 1); |
| 362 if (isEnumClass(elementType)) |
| 363 return this.validateEnum(elementsOffset, elementType.cls, nullable); |
350 | 364 |
351 return validationError.NONE; | 365 return validationError.NONE; |
352 } | 366 } |
353 | 367 |
354 // Note: the |offset + i * elementSize| computation in the validateFooElements | 368 // Note: the |offset + i * elementSize| computation in the validateFooElements |
355 // methods below is "safe" because elementSize <= 8, offset and | 369 // methods below is "safe" because elementSize <= 8, offset and |
356 // numElements are uint32, and 0 <= i < numElements. | 370 // numElements are uint32, and 0 <= i < numElements. |
357 | 371 |
358 Validator.prototype.validateHandleElements = | 372 Validator.prototype.validateHandleElements = |
359 function(offset, numElements, nullable) { | 373 function(offset, numElements, nullable) { |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
406 return err; | 420 return err; |
407 } | 421 } |
408 return validationError.NONE; | 422 return validationError.NONE; |
409 } | 423 } |
410 | 424 |
411 var exports = {}; | 425 var exports = {}; |
412 exports.validationError = validationError; | 426 exports.validationError = validationError; |
413 exports.Validator = Validator; | 427 exports.Validator = Validator; |
414 return exports; | 428 return exports; |
415 }); | 429 }); |
OLD | NEW |