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 gValidationErrorObserver = this; | |
yzshen1
2017/03/24 21:20:13
This line is redundant (please see line 61).
wangjimmy
2017/03/27 16:51:28
Done.
| |
43 this.callback = undefined; | |
44 } | |
45 | |
46 Observer.prototype.setLastError = function(error) { | |
47 this.lastError = error; | |
48 if (this.callback) { | |
49 this.callback(error); | |
50 } | |
51 }; | |
52 | |
53 Observer.prototype.reset = function(error) { | |
54 this.lastError = validationError.NONE; | |
55 this.callback = undefined; | |
56 }; | |
57 | |
58 return { | |
59 getInstance: function() { | |
60 if (!gValidationErrorObserver) { | |
61 gValidationErrorObserver = new Observer(); | |
62 } | |
63 return gValidationErrorObserver; | |
64 } | |
65 }; | |
66 })(); | |
67 | |
68 function isTestingMode() { | |
69 if (gValidationErrorObserver) { | |
yzshen1
2017/03/24 21:20:13
Could we do "return !!gValidationErrorObserver"?
wangjimmy
2017/03/27 16:51:28
Done. I decided to do "return Boolean(gValidationE
| |
70 return true; | |
71 } | |
72 return false; | |
73 } | |
74 | |
75 function clearTestingMode() { | |
76 gValidationErrorObserver = null; | |
77 } | |
31 | 78 |
32 function isEnumClass(cls) { | 79 function isEnumClass(cls) { |
33 return cls instanceof codec.Enum; | 80 return cls instanceof codec.Enum; |
34 } | 81 } |
35 | 82 |
36 function isStringClass(cls) { | 83 function isStringClass(cls) { |
37 return cls === codec.String || cls === codec.NullableString; | 84 return cls === codec.String || cls === codec.NullableString; |
38 } | 85 } |
39 | 86 |
40 function isHandleClass(cls) { | 87 function isHandleClass(cls) { |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
173 } | 220 } |
174 | 221 |
175 return validationError.NONE; | 222 return validationError.NONE; |
176 }; | 223 }; |
177 | 224 |
178 Validator.prototype.isFieldInStructVersion = function(offset, fieldVersion) { | 225 Validator.prototype.isFieldInStructVersion = function(offset, fieldVersion) { |
179 var structVersion = this.message.buffer.getUint32(offset + 4); | 226 var structVersion = this.message.buffer.getUint32(offset + 4); |
180 return fieldVersion <= structVersion; | 227 return fieldVersion <= structVersion; |
181 }; | 228 }; |
182 | 229 |
230 // TODO(wangjimmy): Add support for v2 messages. | |
183 Validator.prototype.validateMessageHeader = function() { | 231 Validator.prototype.validateMessageHeader = function() { |
184 | 232 |
185 var err = this.validateStructHeader(0, codec.kMessageHeaderSize); | 233 var err = this.validateStructHeader(0, codec.kMessageHeaderSize); |
186 if (err != validationError.NONE) | 234 if (err != validationError.NONE) |
187 return err; | 235 return err; |
188 | 236 |
189 var numBytes = this.message.getHeaderNumBytes(); | 237 var numBytes = this.message.getHeaderNumBytes(); |
190 var version = this.message.getHeaderVersion(); | 238 var version = this.message.getHeaderVersion(); |
191 | 239 |
192 var validVersionAndNumBytes = | 240 var validVersionAndNumBytes = |
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
501 var err = this.validateEnum(elementOffset, enumClass); | 549 var err = this.validateEnum(elementOffset, enumClass); |
502 if (err != validationError.NONE) | 550 if (err != validationError.NONE) |
503 return err; | 551 return err; |
504 } | 552 } |
505 return validationError.NONE; | 553 return validationError.NONE; |
506 }; | 554 }; |
507 | 555 |
508 var exports = {}; | 556 var exports = {}; |
509 exports.validationError = validationError; | 557 exports.validationError = validationError; |
510 exports.Validator = Validator; | 558 exports.Validator = Validator; |
559 exports.ValidationErrorObserverForTesting = ValidationErrorObserverForTesting; | |
560 exports.reportValidationError = reportValidationError; | |
561 exports.isTestingMode = isTestingMode; | |
562 exports.clearTestingMode = clearTestingMode; | |
511 return exports; | 563 return exports; |
512 }); | 564 }); |
OLD | NEW |