| 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 24 matching lines...) Expand all Loading... |
| 35 | 35 |
| 36 function isStringClass(cls) { | 36 function isStringClass(cls) { |
| 37 return cls === codec.String || cls === codec.NullableString; | 37 return cls === codec.String || cls === codec.NullableString; |
| 38 } | 38 } |
| 39 | 39 |
| 40 function isHandleClass(cls) { | 40 function isHandleClass(cls) { |
| 41 return cls === codec.Handle || cls === codec.NullableHandle; | 41 return cls === codec.Handle || cls === codec.NullableHandle; |
| 42 } | 42 } |
| 43 | 43 |
| 44 function isInterfaceClass(cls) { | 44 function isInterfaceClass(cls) { |
| 45 return cls === codec.Interface || cls === codec.NullableInterface; | 45 return cls instanceof codec.Interface; |
| 46 } |
| 47 |
| 48 function isInterfaceRequestClass(cls) { |
| 49 return cls === codec.InterfaceRequest || |
| 50 cls === codec.NullableInterfaceRequest; |
| 46 } | 51 } |
| 47 | 52 |
| 48 function isNullable(type) { | 53 function isNullable(type) { |
| 49 return type === codec.NullableString || type === codec.NullableHandle || | 54 return type === codec.NullableString || type === codec.NullableHandle || |
| 50 type === codec.NullableInterface || | 55 type === codec.NullableInterface || |
| 56 type === codec.NullableInterfaceRequest || |
| 51 type instanceof codec.NullableArrayOf || | 57 type instanceof codec.NullableArrayOf || |
| 52 type instanceof codec.NullablePointerTo; | 58 type instanceof codec.NullablePointerTo; |
| 53 } | 59 } |
| 54 | 60 |
| 55 function Validator(message) { | 61 function Validator(message) { |
| 56 this.message = message; | 62 this.message = message; |
| 57 this.offset = 0; | 63 this.offset = 0; |
| 58 this.handleIndex = 0; | 64 this.handleIndex = 0; |
| 59 } | 65 } |
| 60 | 66 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 | 118 |
| 113 Validator.prototype.validateHandle = function(offset, nullable) { | 119 Validator.prototype.validateHandle = function(offset, nullable) { |
| 114 var index = this.message.buffer.getUint32(offset); | 120 var index = this.message.buffer.getUint32(offset); |
| 115 | 121 |
| 116 if (index === codec.kEncodedInvalidHandleValue) | 122 if (index === codec.kEncodedInvalidHandleValue) |
| 117 return nullable ? | 123 return nullable ? |
| 118 validationError.NONE : validationError.UNEXPECTED_INVALID_HANDLE; | 124 validationError.NONE : validationError.UNEXPECTED_INVALID_HANDLE; |
| 119 | 125 |
| 120 if (!this.claimHandle(index)) | 126 if (!this.claimHandle(index)) |
| 121 return validationError.ILLEGAL_HANDLE; | 127 return validationError.ILLEGAL_HANDLE; |
| 128 |
| 122 return validationError.NONE; | 129 return validationError.NONE; |
| 123 }; | 130 }; |
| 124 | 131 |
| 125 Validator.prototype.validateInterface = function(offset, nullable) { | 132 Validator.prototype.validateInterface = function(offset, nullable) { |
| 126 return this.validateHandle(offset, nullable); | 133 return this.validateHandle(offset, nullable); |
| 127 }; | 134 }; |
| 128 | 135 |
| 136 Validator.prototype.validateInterfaceRequest = function(offset, nullable) { |
| 137 return this.validateHandle(offset, nullable); |
| 138 }; |
| 139 |
| 129 Validator.prototype.validateStructHeader = function(offset, minNumBytes) { | 140 Validator.prototype.validateStructHeader = function(offset, minNumBytes) { |
| 130 if (!codec.isAligned(offset)) | 141 if (!codec.isAligned(offset)) |
| 131 return validationError.MISALIGNED_OBJECT; | 142 return validationError.MISALIGNED_OBJECT; |
| 132 | 143 |
| 133 if (!this.isValidRange(offset, codec.kStructHeaderSize)) | 144 if (!this.isValidRange(offset, codec.kStructHeaderSize)) |
| 134 return validationError.ILLEGAL_MEMORY_RANGE; | 145 return validationError.ILLEGAL_MEMORY_RANGE; |
| 135 | 146 |
| 136 var numBytes = this.message.buffer.getUint32(offset); | 147 var numBytes = this.message.buffer.getUint32(offset); |
| 137 | 148 |
| 138 if (numBytes < minNumBytes) | 149 if (numBytes < minNumBytes) |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 // Validate the array's elements if they are pointers or handles. | 376 // Validate the array's elements if they are pointers or handles. |
| 366 | 377 |
| 367 var elementsOffset = offset + codec.kArrayHeaderSize; | 378 var elementsOffset = offset + codec.kArrayHeaderSize; |
| 368 var nullable = isNullable(elementType); | 379 var nullable = isNullable(elementType); |
| 369 | 380 |
| 370 if (isHandleClass(elementType)) | 381 if (isHandleClass(elementType)) |
| 371 return this.validateHandleElements(elementsOffset, numElements, nullable); | 382 return this.validateHandleElements(elementsOffset, numElements, nullable); |
| 372 if (isInterfaceClass(elementType)) | 383 if (isInterfaceClass(elementType)) |
| 373 return this.validateInterfaceElements( | 384 return this.validateInterfaceElements( |
| 374 elementsOffset, numElements, nullable); | 385 elementsOffset, numElements, nullable); |
| 386 if (isInterfaceRequestClass(elementType)) |
| 387 return this.validateInterfaceRequestElements( |
| 388 elementsOffset, numElements, nullable); |
| 375 if (isStringClass(elementType)) | 389 if (isStringClass(elementType)) |
| 376 return this.validateArrayElements( | 390 return this.validateArrayElements( |
| 377 elementsOffset, numElements, codec.Uint8, nullable, [0], 0); | 391 elementsOffset, numElements, codec.Uint8, nullable, [0], 0); |
| 378 if (elementType instanceof codec.PointerTo) | 392 if (elementType instanceof codec.PointerTo) |
| 379 return this.validateStructElements( | 393 return this.validateStructElements( |
| 380 elementsOffset, numElements, elementType.cls, nullable); | 394 elementsOffset, numElements, elementType.cls, nullable); |
| 381 if (elementType instanceof codec.ArrayOf) | 395 if (elementType instanceof codec.ArrayOf) |
| 382 return this.validateArrayElements( | 396 return this.validateArrayElements( |
| 383 elementsOffset, numElements, elementType.cls, nullable, | 397 elementsOffset, numElements, elementType.cls, nullable, |
| 384 expectedDimensionSizes, currentDimension + 1); | 398 expectedDimensionSizes, currentDimension + 1); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 399 var elementOffset = offset + i * elementSize; | 413 var elementOffset = offset + i * elementSize; |
| 400 var err = this.validateHandle(elementOffset, nullable); | 414 var err = this.validateHandle(elementOffset, nullable); |
| 401 if (err != validationError.NONE) | 415 if (err != validationError.NONE) |
| 402 return err; | 416 return err; |
| 403 } | 417 } |
| 404 return validationError.NONE; | 418 return validationError.NONE; |
| 405 }; | 419 }; |
| 406 | 420 |
| 407 Validator.prototype.validateInterfaceElements = | 421 Validator.prototype.validateInterfaceElements = |
| 408 function(offset, numElements, nullable) { | 422 function(offset, numElements, nullable) { |
| 409 var elementSize = codec.Interface.encodedSize; | 423 var elementSize = codec.Interface.prototype.encodedSize; |
| 410 for (var i = 0; i < numElements; i++) { | 424 for (var i = 0; i < numElements; i++) { |
| 411 var elementOffset = offset + i * elementSize; | 425 var elementOffset = offset + i * elementSize; |
| 412 var err = this.validateInterface(elementOffset, nullable); | 426 var err = this.validateInterface(elementOffset, nullable); |
| 413 if (err != validationError.NONE) | 427 if (err != validationError.NONE) |
| 414 return err; | 428 return err; |
| 415 } | 429 } |
| 416 return validationError.NONE; | 430 return validationError.NONE; |
| 417 }; | 431 }; |
| 418 | 432 |
| 433 Validator.prototype.validateInterfaceRequestElements = |
| 434 function(offset, numElements, nullable) { |
| 435 var elementSize = codec.InterfaceRequest.encodedSize; |
| 436 for (var i = 0; i < numElements; i++) { |
| 437 var elementOffset = offset + i * elementSize; |
| 438 var err = this.validateInterfaceRequest(elementOffset, nullable); |
| 439 if (err != validationError.NONE) |
| 440 return err; |
| 441 } |
| 442 return validationError.NONE; |
| 443 }; |
| 444 |
| 419 // The elementClass parameter is the element type of the element arrays. | 445 // The elementClass parameter is the element type of the element arrays. |
| 420 Validator.prototype.validateArrayElements = | 446 Validator.prototype.validateArrayElements = |
| 421 function(offset, numElements, elementClass, nullable, | 447 function(offset, numElements, elementClass, nullable, |
| 422 expectedDimensionSizes, currentDimension) { | 448 expectedDimensionSizes, currentDimension) { |
| 423 var elementSize = codec.PointerTo.prototype.encodedSize; | 449 var elementSize = codec.PointerTo.prototype.encodedSize; |
| 424 for (var i = 0; i < numElements; i++) { | 450 for (var i = 0; i < numElements; i++) { |
| 425 var elementOffset = offset + i * elementSize; | 451 var elementOffset = offset + i * elementSize; |
| 426 var err = this.validateArrayPointer( | 452 var err = this.validateArrayPointer( |
| 427 elementOffset, elementClass.encodedSize, elementClass, nullable, | 453 elementOffset, elementClass.encodedSize, elementClass, nullable, |
| 428 expectedDimensionSizes, currentDimension); | 454 expectedDimensionSizes, currentDimension); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 443 return err; | 469 return err; |
| 444 } | 470 } |
| 445 return validationError.NONE; | 471 return validationError.NONE; |
| 446 }; | 472 }; |
| 447 | 473 |
| 448 var exports = {}; | 474 var exports = {}; |
| 449 exports.validationError = validationError; | 475 exports.validationError = validationError; |
| 450 exports.Validator = Validator; | 476 exports.Validator = Validator; |
| 451 return exports; | 477 return exports; |
| 452 }); | 478 }); |
| OLD | NEW |