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 } |
31 | 74 |
32 function isEnumClass(cls) { | 75 function isEnumClass(cls) { |
33 return cls instanceof codec.Enum; | 76 return cls instanceof codec.Enum; |
34 } | 77 } |
35 | 78 |
36 function isStringClass(cls) { | 79 function isStringClass(cls) { |
37 return cls === codec.String || cls === codec.NullableString; | 80 return cls === codec.String || cls === codec.NullableString; |
38 } | 81 } |
39 | 82 |
40 function isHandleClass(cls) { | 83 function isHandleClass(cls) { |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 } | 216 } |
174 | 217 |
175 return validationError.NONE; | 218 return validationError.NONE; |
176 }; | 219 }; |
177 | 220 |
178 Validator.prototype.isFieldInStructVersion = function(offset, fieldVersion) { | 221 Validator.prototype.isFieldInStructVersion = function(offset, fieldVersion) { |
179 var structVersion = this.message.buffer.getUint32(offset + 4); | 222 var structVersion = this.message.buffer.getUint32(offset + 4); |
180 return fieldVersion <= structVersion; | 223 return fieldVersion <= structVersion; |
181 }; | 224 }; |
182 | 225 |
| 226 // TODO(wangjimmy): Add support for v2 messages. |
183 Validator.prototype.validateMessageHeader = function() { | 227 Validator.prototype.validateMessageHeader = function() { |
184 | 228 |
185 var err = this.validateStructHeader(0, codec.kMessageHeaderSize); | 229 var err = this.validateStructHeader(0, codec.kMessageHeaderSize); |
186 if (err != validationError.NONE) | 230 if (err != validationError.NONE) |
187 return err; | 231 return err; |
188 | 232 |
189 var numBytes = this.message.getHeaderNumBytes(); | 233 var numBytes = this.message.getHeaderNumBytes(); |
190 var version = this.message.getHeaderVersion(); | 234 var version = this.message.getHeaderVersion(); |
191 | 235 |
192 var validVersionAndNumBytes = | 236 var validVersionAndNumBytes = |
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
501 var err = this.validateEnum(elementOffset, enumClass); | 545 var err = this.validateEnum(elementOffset, enumClass); |
502 if (err != validationError.NONE) | 546 if (err != validationError.NONE) |
503 return err; | 547 return err; |
504 } | 548 } |
505 return validationError.NONE; | 549 return validationError.NONE; |
506 }; | 550 }; |
507 | 551 |
508 var exports = {}; | 552 var exports = {}; |
509 exports.validationError = validationError; | 553 exports.validationError = validationError; |
510 exports.Validator = Validator; | 554 exports.Validator = Validator; |
| 555 exports.ValidationErrorObserverForTesting = ValidationErrorObserverForTesting; |
| 556 exports.reportValidationError = reportValidationError; |
| 557 exports.isTestingMode = isTestingMode; |
| 558 exports.clearTestingMode = clearTestingMode; |
511 return exports; | 559 return exports; |
512 }); | 560 }); |
OLD | NEW |