OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 define("mojo/public/js/bindings/validator", [ | |
6 "mojo/public/js/bindings/codec", | |
7 ], function(codec) { | |
8 | |
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' | |
21 }; | |
22 | |
23 function Validator(message, offset) { | |
24 this.message = message; | |
25 this.offset = offset; | |
26 } | |
27 | |
28 Object.defineProperty(Validator.prototype, "offsetLimit", { | |
29 get: function() { return this.message.buffer.byteLength; } | |
30 }); | |
31 | |
32 Validator.prototype.claimMemory = function(numBytes) { | |
33 if (numBytes < 0 || this.offset + numBytes > this.offsetLimit) | |
34 return false; | |
35 this.offset += numBytes; | |
36 return true; | |
37 } | |
38 | |
39 Validator.prototype.validateStructHeader = | |
40 function(offset, minNumBytes, minNumFields) { | |
41 if (!codec.isAligned(offset)) | |
42 return ValidationError.MISALIGNED_OBJECT; | |
43 | |
44 if (offset + codec.kStructHeaderSize > this.offsetLimit) | |
45 return ValidationError.ILLEGAL_MEMORY_RANGE; | |
46 | |
47 var numBytes = this.message.buffer.getUint32(offset); | |
48 var numFields = this.message.buffer.getUint32(offset + 4); | |
49 | |
50 if (numBytes < minNumBytes || numFields < minNumFields) | |
51 return ValidationError.UNEXPECTED_STRUCT_HEADER; | |
52 | |
53 if (!this.claimMemory(numBytes)) | |
54 return ValidationError.ILLEGAL_MEMORY_RANGE; | |
55 | |
56 return ValidationError.NONE; | |
57 } | |
58 | |
59 Validator.prototype.validateMessageHeader = function() { | |
60 var numBytes = this.message.getHeaderNumBytes(); | |
61 var numFields = this.message.getHeaderNumFields(); | |
62 | |
63 var validNumFieldsAndNumBytes = | |
64 (numFields == 2 && numBytes == codec.kMessageHeaderSize) || | |
Matt Perry
2014/07/28 23:59:03
indent += 2
hansmuller
2014/07/29 00:10:42
Done.
| |
65 (numFields == 3 && numBytes == codec.kMessageWithRequestIDHeaderSize); | |
66 if (!validNumFieldsAndNumBytes) | |
67 return ValidationError.UNEXPECTED_STRUCT_HEADER; | |
68 | |
69 var expectsResponse = this.message.expectsResponse(); | |
70 var isResponse = this.message.isResponse(); | |
71 | |
72 if (numFields == 2 && (expectsResponse || isResponse)) | |
73 return ValidationError.MESSAGE_HEADER_MISSING_REQUEST_ID; | |
74 | |
75 if (isResponse && expectsResponse) | |
76 return ValidationError.MESSAGE_HEADER_INVALID_FLAG_COMBINATION; | |
77 | |
78 return ValidationError.NONE; | |
79 } | |
80 | |
81 Validator.prototype.validateMessage = function() { | |
82 var err = this.validateStructHeader(0, codec.kStructHeaderSize, 2); | |
83 if (err != ValidationError.NONE) | |
84 return err; | |
85 | |
86 return this.validateMessageHeader(); | |
87 } | |
88 | |
89 var exports = {}; | |
90 exports.ValidationError = ValidationError; | |
91 exports.Validator = Validator; | |
92 return exports; | |
93 }); | |
OLD | NEW |