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 10 matching lines...) Expand all Loading... |
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 UNKNOWN_ENUM_VALUE: 'VALIDATION_ERROR_UNKNOWN_ENUM_VALUE', |
28 }; | 28 }; |
29 | 29 |
30 var NULL_MOJO_POINTER = "NULL_MOJO_POINTER"; | 30 var NULL_MOJO_POINTER = "NULL_MOJO_POINTER"; |
31 var gValidationErrorObserver = null; | |
32 | |
33 function reportValidationError(error) { | |
34 if (gValidationErrorObserver) { | |
35 gValidationErrorObserver.setLastError(error); | |
36 } | |
37 } | |
38 | |
39 var ValidationErrorObserverForTesting = (function() { | |
40 function Observer() { | |
41 this.lastError = validationError.NONE; | |
42 this.callback = null; | |
43 } | |
44 | |
45 Observer.prototype.setLastError = function(error) { | |
46 this.lastError = error; | |
47 if (this.callback) { | |
48 this.callback(error); | |
49 } | |
50 }; | |
51 | |
52 Observer.prototype.reset = function(error) { | |
53 this.lastError = validationError.NONE; | |
54 this.callback = null; | |
55 }; | |
56 | |
57 return { | |
58 getInstance: function() { | |
59 if (!gValidationErrorObserver) { | |
60 gValidationErrorObserver = new Observer(); | |
61 } | |
62 return gValidationErrorObserver; | |
63 } | |
64 }; | |
65 })(); | |
66 | |
67 function isTestingMode() { | |
68 return Boolean(gValidationErrorObserver); | |
69 } | |
70 | |
71 function clearTestingMode() { | |
72 gValidationErrorObserver = null; | |
73 } | |
74 | 31 |
75 function isEnumClass(cls) { | 32 function isEnumClass(cls) { |
76 return cls instanceof codec.Enum; | 33 return cls instanceof codec.Enum; |
77 } | 34 } |
78 | 35 |
79 function isStringClass(cls) { | 36 function isStringClass(cls) { |
80 return cls === codec.String || cls === codec.NullableString; | 37 return cls === codec.String || cls === codec.NullableString; |
81 } | 38 } |
82 | 39 |
83 function isHandleClass(cls) { | 40 function isHandleClass(cls) { |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 } | 173 } |
217 | 174 |
218 return validationError.NONE; | 175 return validationError.NONE; |
219 }; | 176 }; |
220 | 177 |
221 Validator.prototype.isFieldInStructVersion = function(offset, fieldVersion) { | 178 Validator.prototype.isFieldInStructVersion = function(offset, fieldVersion) { |
222 var structVersion = this.message.buffer.getUint32(offset + 4); | 179 var structVersion = this.message.buffer.getUint32(offset + 4); |
223 return fieldVersion <= structVersion; | 180 return fieldVersion <= structVersion; |
224 }; | 181 }; |
225 | 182 |
226 // TODO(wangjimmy): Add support for v2 messages. | |
227 Validator.prototype.validateMessageHeader = function() { | 183 Validator.prototype.validateMessageHeader = function() { |
228 | 184 |
229 var err = this.validateStructHeader(0, codec.kMessageHeaderSize); | 185 var err = this.validateStructHeader(0, codec.kMessageHeaderSize); |
230 if (err != validationError.NONE) | 186 if (err != validationError.NONE) |
231 return err; | 187 return err; |
232 | 188 |
233 var numBytes = this.message.getHeaderNumBytes(); | 189 var numBytes = this.message.getHeaderNumBytes(); |
234 var version = this.message.getHeaderVersion(); | 190 var version = this.message.getHeaderVersion(); |
235 | 191 |
236 var validVersionAndNumBytes = | 192 var validVersionAndNumBytes = |
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
545 var err = this.validateEnum(elementOffset, enumClass); | 501 var err = this.validateEnum(elementOffset, enumClass); |
546 if (err != validationError.NONE) | 502 if (err != validationError.NONE) |
547 return err; | 503 return err; |
548 } | 504 } |
549 return validationError.NONE; | 505 return validationError.NONE; |
550 }; | 506 }; |
551 | 507 |
552 var exports = {}; | 508 var exports = {}; |
553 exports.validationError = validationError; | 509 exports.validationError = validationError; |
554 exports.Validator = Validator; | 510 exports.Validator = Validator; |
555 exports.ValidationErrorObserverForTesting = ValidationErrorObserverForTesting; | |
556 exports.reportValidationError = reportValidationError; | |
557 exports.isTestingMode = isTestingMode; | |
558 exports.clearTestingMode = clearTestingMode; | |
559 return exports; | 511 return exports; |
560 }); | 512 }); |
OLD | NEW |