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
3 // found in the LICENSE file.
5 define("mojo/public/js/bindings/validator", [
6 "mojo/public/js/bindings/codec",
9 var validationError = {
10 NONE: 'VALIDATION_ERROR_NONE',
11 MISALIGNED_OBJECT: 'VALIDATION_ERROR_MISALIGNED_OBJECT',
12 ILLEGAL_MEMORY_RANGE: 'VALIDATION_ERROR_ILLEGAL_MEMORY_RANGE',
13 UNEXPECTED_STRUCT_HEADER: 'VALIDATION_ERROR_UNEXPECTED_STRUCT_HEADER',
14 UNEXPECTED_ARRAY_HEADER: 'VALIDATION_ERROR_UNEXPECTED_ARRAY_HEADER',
15 ILLEGAL_HANDLE: 'VALIDATION_ERROR_ILLEGAL_HANDLE',
16 ILLEGAL_POINTER: 'VALIDATION_ERROR_ILLEGAL_POINTER',
17 MESSAGE_HEADER_INVALID_FLAG_COMBINATION:
18 'VALIDATION_ERROR_MESSAGE_HEADER_INVALID_FLAG_COMBINATION',
19 MESSAGE_HEADER_MISSING_REQUEST_ID:
20 'VALIDATION_ERROR_MESSAGE_HEADER_MISSING_REQUEST_ID'
23 function Validator(message) {
24 this.message = message;
28 Object.defineProperty(Validator.prototype, "offsetLimit", {
29 get: function() { return this.message.buffer.byteLength; }
32 // True if we can safely allocate a block of bytes from start to
33 // to start + numBytes.
34 Validator.prototype.isValidRange = function(start, numBytes) {
35 // Only positive JavaScript integers that are less than 2^53
36 // (Number.MAX_SAFE_INTEGER) can be represented exactly.
37 if (start < this.offset || numBytes <= 0 ||
38 !Number.isSafeInteger(start) ||
39 !Number.isSafeInteger(numBytes))
42 var newOffset = start + numBytes;
43 if (!Number.isSafeInteger(newOffset) || newOffset > this.offsetLimit)
49 Validator.prototype.claimRange = function(start, numBytes) {
50 if (this.isValidRange(start, numBytes)) {
51 this.offset = start + numBytes;
57 Validator.prototype.validateStructHeader =
58 function(offset, minNumBytes, minNumFields) {
59 if (!codec.isAligned(offset))
60 return validationError.MISALIGNED_OBJECT;
62 if (!this.isValidRange(offset, codec.kStructHeaderSize))
63 return validationError.ILLEGAL_MEMORY_RANGE;
65 var numBytes = this.message.buffer.getUint32(offset);
66 var numFields = this.message.buffer.getUint32(offset + 4);
68 if (numBytes < minNumBytes || numFields < minNumFields)
69 return validationError.UNEXPECTED_STRUCT_HEADER;
71 if (!this.claimRange(offset, numBytes))
72 return validationError.ILLEGAL_MEMORY_RANGE;
74 return validationError.NONE;
77 Validator.prototype.validateMessageHeader = function() {
78 var numBytes = this.message.getHeaderNumBytes();
79 var numFields = this.message.getHeaderNumFields();
81 var validNumFieldsAndNumBytes =
82 (numFields == 2 && numBytes == codec.kMessageHeaderSize) ||
84 numBytes == codec.kMessageWithRequestIDHeaderSize) ||
86 numBytes >= codec.kMessageWithRequestIDHeaderSize);
87 if (!validNumFieldsAndNumBytes)
88 return validationError.UNEXPECTED_STRUCT_HEADER;
90 var expectsResponse = this.message.expectsResponse();
91 var isResponse = this.message.isResponse();
93 if (numFields == 2 && (expectsResponse || isResponse))
94 return validationError.MESSAGE_HEADER_MISSING_REQUEST_ID;
96 if (isResponse && expectsResponse)
97 return validationError.MESSAGE_HEADER_INVALID_FLAG_COMBINATION;
99 return validationError.NONE;
102 Validator.prototype.validateMessage = function() {
103 var err = this.validateStructHeader(0, codec.kStructHeaderSize, 2);
104 if (err != validationError.NONE)
107 return this.validateMessageHeader();
111 exports.validationError = validationError;
112 exports.Validator = Validator;