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", [ | |
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.
| |
6 "mojo/public/js/bindings/codec", | |
7 ], function(codec) { | |
8 | |
9 var ValidationError = (function() { | |
10 var errorEnum = {}; | |
11 function defineError(name) { | |
12 errorEnum[name] = "VALIDATION_ERROR_" + name; | |
13 } | |
abarth-chromium
2014/07/22 03:51:00
Normally we'd just make this an anonymous function
| |
14 ["NONE", | |
abarth-chromium
2014/07/22 03:51:00
Blank line after [
| |
15 "MISALIGNED_OBJECT", | |
16 "ILLEGAL_MEMORY_RANGE", | |
17 "UNEXPECTED_STRUCT_HEADER", | |
18 "UNEXPECTED_ARRAY_HEADER", | |
19 "ILLEGAL_HANDLE", | |
20 "ILLEGAL_POINTER", | |
21 "MESSAGE_HEADER_INVALID_FLAG_COMBINATION", | |
22 "MESSAGE_HEADER_MISSING_REQUEST_ID" | |
abarth-chromium
2014/07/22 03:51:00
Missing , at the end of this line.
| |
23 ].forEach(defineError); | |
24 return errorEnum; | |
25 })(); | |
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
| |
26 | |
27 function validateMessageHeader(message) { | |
28 if (message.byteLength() < codec.kMessageHeaderSize) | |
29 return ValidationError.ILLEGAL_MEMORY_RANGE; | |
30 | |
31 var numBytes = message.getHeaderNumBytes(); | |
32 var numFields = message.getHeaderNumFields(); | |
33 | |
34 if (message.byteLength() < numBytes) | |
35 return ValidationError.ILLEGAL_MEMORY_RANGE; | |
36 | |
37 if (!((numFields == 2 && numBytes == codec.kMessageHeaderSize) || | |
38 (numFields == 3 && | |
39 numBytes == codec.kMessageWithRequestIDHeaderSize))) | |
40 return ValidationError.UNEXPECTED_STRUCT_HEADER; | |
41 | |
42 var flags = message.getHeaderFlags(); | |
43 var expectsResponse = flags & codec.kMessageExpectsResponse; | |
44 var isResponse = flags & codec.kMessageIsResponse; | |
45 | |
46 if (numFields == 2 && (expectsResponse || isResponse)) | |
47 return ValidationError.MESSAGE_HEADER_MISSING_REQUEST_ID; | |
48 | |
49 if (isResponse && expectsResponse) | |
50 return ValidationError.MESSAGE_HEADER_INVALID_FLAG_COMBINATION; | |
51 | |
52 return ValidationError.NONE; | |
53 } | |
54 | |
55 var exports = {}; | |
56 exports.ValidationError = ValidationError; | |
57 exports.validateMessageHeader = validateMessageHeader; | |
58 return exports; | |
59 }); | |
OLD | NEW |