Index: mojo/public/js/bindings/validator.js |
diff --git a/mojo/public/js/bindings/validator.js b/mojo/public/js/bindings/validator.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..19aee8fd50c41bfd2da93a25d639dae806f7299f |
--- /dev/null |
+++ b/mojo/public/js/bindings/validator.js |
@@ -0,0 +1,59 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+define("mojo/public/js/bindings/validator", [ |
abarth-chromium
2014/07/22 03:51:00
No need to name the module. The module loader wil
hansmuller
2014/07/22 15:49:07
OK.
|
+ "mojo/public/js/bindings/codec", |
+ ], function(codec) { |
+ |
+ var ValidationError = (function() { |
+ var errorEnum = {}; |
+ function defineError(name) { |
+ errorEnum[name] = "VALIDATION_ERROR_" + name; |
+ } |
abarth-chromium
2014/07/22 03:51:00
Normally we'd just make this an anonymous function
|
+ ["NONE", |
abarth-chromium
2014/07/22 03:51:00
Blank line after [
|
+ "MISALIGNED_OBJECT", |
+ "ILLEGAL_MEMORY_RANGE", |
+ "UNEXPECTED_STRUCT_HEADER", |
+ "UNEXPECTED_ARRAY_HEADER", |
+ "ILLEGAL_HANDLE", |
+ "ILLEGAL_POINTER", |
+ "MESSAGE_HEADER_INVALID_FLAG_COMBINATION", |
+ "MESSAGE_HEADER_MISSING_REQUEST_ID" |
abarth-chromium
2014/07/22 03:51:00
Missing , at the end of this line.
|
+ ].forEach(defineError); |
+ return errorEnum; |
+ })(); |
abarth-chromium
2014/07/22 03:51:00
Why not just write out the code? It seems unneces
hansmuller
2014/07/22 15:49:07
Sorry, I got carried away. I'll write out the cons
|
+ |
+ function validateMessageHeader(message) { |
+ if (message.byteLength() < codec.kMessageHeaderSize) |
+ return ValidationError.ILLEGAL_MEMORY_RANGE; |
+ |
+ var numBytes = message.getHeaderNumBytes(); |
+ var numFields = message.getHeaderNumFields(); |
+ |
+ if (message.byteLength() < numBytes) |
+ return ValidationError.ILLEGAL_MEMORY_RANGE; |
+ |
+ if (!((numFields == 2 && numBytes == codec.kMessageHeaderSize) || |
+ (numFields == 3 && |
+ numBytes == codec.kMessageWithRequestIDHeaderSize))) |
+ return ValidationError.UNEXPECTED_STRUCT_HEADER; |
+ |
+ var flags = message.getHeaderFlags(); |
+ var expectsResponse = flags & codec.kMessageExpectsResponse; |
+ var isResponse = flags & codec.kMessageIsResponse; |
+ |
+ if (numFields == 2 && (expectsResponse || isResponse)) |
+ return ValidationError.MESSAGE_HEADER_MISSING_REQUEST_ID; |
+ |
+ if (isResponse && expectsResponse) |
+ return ValidationError.MESSAGE_HEADER_INVALID_FLAG_COMBINATION; |
+ |
+ return ValidationError.NONE; |
+ } |
+ |
+ var exports = {}; |
+ exports.ValidationError = ValidationError; |
+ exports.validateMessageHeader = validateMessageHeader; |
+ return exports; |
+}); |