| 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', |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 return true; | 102 return true; |
| 103 | 103 |
| 104 if (index < this.handleIndex || index >= this.handleIndexLimit) | 104 if (index < this.handleIndex || index >= this.handleIndexLimit) |
| 105 return false; | 105 return false; |
| 106 | 106 |
| 107 // This is safe because handle indices are uint32. | 107 // This is safe because handle indices are uint32. |
| 108 this.handleIndex = index + 1; | 108 this.handleIndex = index + 1; |
| 109 return true; | 109 return true; |
| 110 }; | 110 }; |
| 111 | 111 |
| 112 Validator.prototype.validateEnum = function(offset, enumClass, nullable) { | 112 Validator.prototype.validateEnum = function(offset, enumClass) { |
| 113 // Note: Assumes that enums are always 32 bits! But this matches | 113 // Note: Assumes that enums are always 32 bits! But this matches |
| 114 // mojom::generate::pack::PackedField::GetSizeForKind, so it should be okay. | 114 // mojom::generate::pack::PackedField::GetSizeForKind, so it should be okay. |
| 115 var value = this.message.buffer.getInt32(offset); | 115 var value = this.message.buffer.getInt32(offset); |
| 116 return enumClass.validate(value); | 116 return enumClass.validate(value); |
| 117 } | 117 } |
| 118 | 118 |
| 119 Validator.prototype.validateHandle = function(offset, nullable) { | 119 Validator.prototype.validateHandle = function(offset, nullable) { |
| 120 var index = this.message.buffer.getUint32(offset); | 120 var index = this.message.buffer.getUint32(offset); |
| 121 | 121 |
| 122 if (index === codec.kEncodedInvalidHandleValue) | 122 if (index === codec.kEncodedInvalidHandleValue) |
| (...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 390 return this.validateArrayElements( | 390 return this.validateArrayElements( |
| 391 elementsOffset, numElements, codec.Uint8, nullable, [0], 0); | 391 elementsOffset, numElements, codec.Uint8, nullable, [0], 0); |
| 392 if (elementType instanceof codec.PointerTo) | 392 if (elementType instanceof codec.PointerTo) |
| 393 return this.validateStructElements( | 393 return this.validateStructElements( |
| 394 elementsOffset, numElements, elementType.cls, nullable); | 394 elementsOffset, numElements, elementType.cls, nullable); |
| 395 if (elementType instanceof codec.ArrayOf) | 395 if (elementType instanceof codec.ArrayOf) |
| 396 return this.validateArrayElements( | 396 return this.validateArrayElements( |
| 397 elementsOffset, numElements, elementType.cls, nullable, | 397 elementsOffset, numElements, elementType.cls, nullable, |
| 398 expectedDimensionSizes, currentDimension + 1); | 398 expectedDimensionSizes, currentDimension + 1); |
| 399 if (isEnumClass(elementType)) | 399 if (isEnumClass(elementType)) |
| 400 return this.validateEnum(elementsOffset, elementType.cls, nullable); | 400 return this.validateEnumElements(elementsOffset, numElements, |
| 401 elementType.cls); |
| 401 | 402 |
| 402 return validationError.NONE; | 403 return validationError.NONE; |
| 403 }; | 404 }; |
| 404 | 405 |
| 405 // Note: the |offset + i * elementSize| computation in the validateFooElements | 406 // Note: the |offset + i * elementSize| computation in the validateFooElements |
| 406 // methods below is "safe" because elementSize <= 8, offset and | 407 // methods below is "safe" because elementSize <= 8, offset and |
| 407 // numElements are uint32, and 0 <= i < numElements. | 408 // numElements are uint32, and 0 <= i < numElements. |
| 408 | 409 |
| 409 Validator.prototype.validateHandleElements = | 410 Validator.prototype.validateHandleElements = |
| 410 function(offset, numElements, nullable) { | 411 function(offset, numElements, nullable) { |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 464 for (var i = 0; i < numElements; i++) { | 465 for (var i = 0; i < numElements; i++) { |
| 465 var elementOffset = offset + i * elementSize; | 466 var elementOffset = offset + i * elementSize; |
| 466 var err = | 467 var err = |
| 467 this.validateStructPointer(elementOffset, structClass, nullable); | 468 this.validateStructPointer(elementOffset, structClass, nullable); |
| 468 if (err != validationError.NONE) | 469 if (err != validationError.NONE) |
| 469 return err; | 470 return err; |
| 470 } | 471 } |
| 471 return validationError.NONE; | 472 return validationError.NONE; |
| 472 }; | 473 }; |
| 473 | 474 |
| 475 Validator.prototype.validateEnumElements = |
| 476 function(offset, numElements, enumClass) { |
| 477 var elementSize = codec.Enum.prototype.encodedSize; |
| 478 for (var i = 0; i < numElements; i++) { |
| 479 var elementOffset = offset + i * elementSize; |
| 480 var err = this.validateEnum(elementOffset, enumClass); |
| 481 if (err != validationError.NONE) |
| 482 return err; |
| 483 } |
| 484 return validationError.NONE; |
| 485 }; |
| 486 |
| 474 var exports = {}; | 487 var exports = {}; |
| 475 exports.validationError = validationError; | 488 exports.validationError = validationError; |
| 476 exports.Validator = Validator; | 489 exports.Validator = Validator; |
| 477 return exports; | 490 return exports; |
| 478 }); | 491 }); |
| OLD | NEW |