Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(639)

Side by Side Diff: mojo/public/js/codec.js

Issue 2796253002: Associated Message Validation (Closed)
Patch Set: Fix formatting and add missing check_err() Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/public/js/associated_bindings.js ('k') | mojo/public/js/constants.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/codec", [ 5 define("mojo/public/js/codec", [
6 "mojo/public/js/buffer", 6 "mojo/public/js/buffer",
7 "mojo/public/js/interface_types", 7 "mojo/public/js/interface_types",
8 "mojo/public/js/lib/interface_endpoint_handle",
8 "mojo/public/js/unicode", 9 "mojo/public/js/unicode",
9 ], function(buffer, types, unicode) { 10 ], function(buffer, types, interfaceEndpointHandle, unicode) {
10 11
11 var kErrorUnsigned = "Passing negative value to unsigned"; 12 var kErrorUnsigned = "Passing negative value to unsigned";
12 var kErrorArray = "Passing non Array for array type"; 13 var kErrorArray = "Passing non Array for array type";
13 var kErrorString = "Passing non String for string type"; 14 var kErrorString = "Passing non String for string type";
14 var kErrorMap = "Passing non Map for map type"; 15 var kErrorMap = "Passing non Map for map type";
16 var InterfaceEndpointHandle = interfaceEndpointHandle.InterfaceEndpointHandle;
15 17
16 // Memory ------------------------------------------------------------------- 18 // Memory -------------------------------------------------------------------
17 19
18 var kAlignment = 8; 20 var kAlignment = 8;
19 21
20 function align(size) { 22 function align(size) {
21 return size + (kAlignment - (size % kAlignment)) % kAlignment; 23 return size + (kAlignment - (size % kAlignment)) % kAlignment;
22 } 24 }
23 25
24 function isAligned(offset) { 26 function isAligned(offset) {
25 return offset >= 0 && (offset % kAlignment) === 0; 27 return offset >= 0 && (offset % kAlignment) === 0;
26 } 28 }
27 29
28 // Constants ---------------------------------------------------------------- 30 // Constants ----------------------------------------------------------------
29 31
30 var kArrayHeaderSize = 8; 32 var kArrayHeaderSize = 8;
31 var kStructHeaderSize = 8; 33 var kStructHeaderSize = 8;
32 var kMessageHeaderSize = 24; 34 var kMessageHeaderSize = 24;
33 var kMessageWithRequestIDHeaderSize = 32; 35 var kMessageWithRequestIDHeaderSize = 32;
36 var kMessageV2HeaderSize = 48;
34 var kMapStructPayloadSize = 16; 37 var kMapStructPayloadSize = 16;
35 38
36 var kStructHeaderNumBytesOffset = 0; 39 var kStructHeaderNumBytesOffset = 0;
37 var kStructHeaderVersionOffset = 4; 40 var kStructHeaderVersionOffset = 4;
38 41
39 var kEncodedInvalidHandleValue = 0xFFFFFFFF; 42 var kEncodedInvalidHandleValue = 0xFFFFFFFF;
40 43
41 // Decoder ------------------------------------------------------------------ 44 // Decoder ------------------------------------------------------------------
42 45
43 function Decoder(buffer, handles, base) { 46 function Decoder(buffer, handles, base) {
(...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after
450 }; 453 };
451 454
452 Message.prototype.getFlags = function() { 455 Message.prototype.getFlags = function() {
453 return this.buffer.getUint32(kMessageFlagsOffset); 456 return this.buffer.getUint32(kMessageFlagsOffset);
454 }; 457 };
455 458
456 Message.prototype.getInterfaceId = function() { 459 Message.prototype.getInterfaceId = function() {
457 return this.buffer.getUint32(kMessageInterfaceIdOffset); 460 return this.buffer.getUint32(kMessageInterfaceIdOffset);
458 }; 461 };
459 462
463 Message.prototype.getPayloadInterfaceIds = function() {
464 if (this.getHeaderVersion() < 2) {
465 throw new Error(
466 "Version of message does not support payload interface ids.");
467 }
468
469 var decoder = new Decoder(this.buffer, this.handles,
470 kMessageV2HeaderSize-8);
Ken Rockot(use gerrit already) 2017/04/10 22:20:17 nit: Perhaps this should be its own "constant" lik
wangjimmy 2017/04/11 00:21:24 var messageV2PayloadInterfaceIdsPointer = kMessage
471 var payloadInterfaceIds = decoder.decodeArrayPointer(Uint32);
472 return payloadInterfaceIds;
473 };
474
460 Message.prototype.isResponse = function() { 475 Message.prototype.isResponse = function() {
461 return (this.getFlags() & kMessageIsResponse) != 0; 476 return (this.getFlags() & kMessageIsResponse) != 0;
462 }; 477 };
463 478
464 Message.prototype.expectsResponse = function() { 479 Message.prototype.expectsResponse = function() {
465 return (this.getFlags() & kMessageExpectsResponse) != 0; 480 return (this.getFlags() & kMessageExpectsResponse) != 0;
466 }; 481 };
467 482
468 Message.prototype.setRequestID = function(requestID) { 483 Message.prototype.setRequestID = function(requestID) {
469 // TODO(darin): Verify that space was reserved for this field! 484 // TODO(darin): Verify that space was reserved for this field!
(...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after
831 encoder.encodeHandle(interfacePtrInfo.handle); 846 encoder.encodeHandle(interfacePtrInfo.handle);
832 encoder.writeUint32(interfacePtrInfo.version); 847 encoder.writeUint32(interfacePtrInfo.version);
833 }; 848 };
834 849
835 function NullableInterface(cls) { 850 function NullableInterface(cls) {
836 Interface.call(this, cls); 851 Interface.call(this, cls);
837 } 852 }
838 853
839 NullableInterface.prototype = Object.create(Interface.prototype); 854 NullableInterface.prototype = Object.create(Interface.prototype);
840 855
856 function AssociatedInterfacePtrInfo() {
857 }
858
859 AssociatedInterfacePtrInfo.prototype.encodedSize = 8;
860
861 AssociatedInterfacePtrInfo.decode = function(decoder) {
862 return new types.AssociatedInterfacePtrInfo(
863 new InterfaceEndpointHandle(decoder.decodeHandle()),
864 decoder.readUint32());
865 };
866
867 AssociatedInterfacePtrInfo.encode = function(encoder, val) {
868 var associatedinterfacePtrInfo =
869 val ? val : new types.AssociatedInterfacePtrInfo(null, 0);
870 encoder.encodeHandle(
871 associatedinterfacePtrInfo.interfaceEndpointHandle.id());
872 encoder.writeUint32(associatedinterfacePtrInfo.version);
873 };
874
875 function NullableAssociatedInterfacePtrInfo() {
876 }
877
878 NullableAssociatedInterfacePtrInfo.encodedSize =
879 AssociatedInterfacePtrInfo.encodedSize;
880
881 NullableAssociatedInterfacePtrInfo.decode =
882 AssociatedInterfacePtrInfo.decode;
883
884 NullableAssociatedInterfacePtrInfo.encode =
885 AssociatedInterfacePtrInfo.encode;
886
841 function InterfaceRequest() { 887 function InterfaceRequest() {
842 } 888 }
843 889
844 InterfaceRequest.encodedSize = 4; 890 InterfaceRequest.encodedSize = 4;
845 891
846 InterfaceRequest.decode = function(decoder) { 892 InterfaceRequest.decode = function(decoder) {
847 return new types.InterfaceRequest(decoder.decodeHandle()); 893 return new types.InterfaceRequest(decoder.decodeHandle());
848 }; 894 };
849 895
850 InterfaceRequest.encode = function(encoder, val) { 896 InterfaceRequest.encode = function(encoder, val) {
851 encoder.encodeHandle(val ? val.handle : null); 897 encoder.encodeHandle(val ? val.handle : null);
852 }; 898 };
853 899
854 function NullableInterfaceRequest() { 900 function NullableInterfaceRequest() {
855 } 901 }
856 902
857 NullableInterfaceRequest.encodedSize = InterfaceRequest.encodedSize; 903 NullableInterfaceRequest.encodedSize = InterfaceRequest.encodedSize;
858 904
859 NullableInterfaceRequest.decode = InterfaceRequest.decode; 905 NullableInterfaceRequest.decode = InterfaceRequest.decode;
860 906
861 NullableInterfaceRequest.encode = InterfaceRequest.encode; 907 NullableInterfaceRequest.encode = InterfaceRequest.encode;
862 908
909 function AssociatedInterfaceRequest() {
910 }
911
912 AssociatedInterfaceRequest.encodedSize = 4;
913
914 AssociatedInterfaceRequest.decode = function(decoder) {
915 return new types.AssociatedInterfaceRequest(new InterfaceEndpointHandle(
916 decoder.decodeHandle()));
917 };
918
919 AssociatedInterfaceRequest.encode = function(encoder, val) {
920 encoder.encodeHandle(val ? val.interfaceEndpointHandle.id() : null);
921 };
922
923 function NullableAssociatedInterfaceRequest() {
924 }
925
926 NullableAssociatedInterfaceRequest.encodedSize =
927 AssociatedInterfaceRequest.encodedSize;
928
929 NullableAssociatedInterfaceRequest.decode =
930 AssociatedInterfaceRequest.decode;
931
932 NullableAssociatedInterfaceRequest.encode =
933 AssociatedInterfaceRequest.encode;
934
863 function MapOf(keyClass, valueClass) { 935 function MapOf(keyClass, valueClass) {
864 this.keyClass = keyClass; 936 this.keyClass = keyClass;
865 this.valueClass = valueClass; 937 this.valueClass = valueClass;
866 } 938 }
867 939
868 MapOf.prototype.encodedSize = 8; 940 MapOf.prototype.encodedSize = 8;
869 941
870 MapOf.prototype.decode = function(decoder) { 942 MapOf.prototype.decode = function(decoder) {
871 return decoder.decodeMapPointer(this.keyClass, this.valueClass); 943 return decoder.decodeMapPointer(this.keyClass, this.valueClass);
872 }; 944 };
(...skipping 14 matching lines...) Expand all
887 exports.Message = Message; 959 exports.Message = Message;
888 exports.MessageBuilder = MessageBuilder; 960 exports.MessageBuilder = MessageBuilder;
889 exports.MessageWithRequestIDBuilder = MessageWithRequestIDBuilder; 961 exports.MessageWithRequestIDBuilder = MessageWithRequestIDBuilder;
890 exports.MessageReader = MessageReader; 962 exports.MessageReader = MessageReader;
891 exports.kArrayHeaderSize = kArrayHeaderSize; 963 exports.kArrayHeaderSize = kArrayHeaderSize;
892 exports.kMapStructPayloadSize = kMapStructPayloadSize; 964 exports.kMapStructPayloadSize = kMapStructPayloadSize;
893 exports.kStructHeaderSize = kStructHeaderSize; 965 exports.kStructHeaderSize = kStructHeaderSize;
894 exports.kEncodedInvalidHandleValue = kEncodedInvalidHandleValue; 966 exports.kEncodedInvalidHandleValue = kEncodedInvalidHandleValue;
895 exports.kMessageHeaderSize = kMessageHeaderSize; 967 exports.kMessageHeaderSize = kMessageHeaderSize;
896 exports.kMessageWithRequestIDHeaderSize = kMessageWithRequestIDHeaderSize; 968 exports.kMessageWithRequestIDHeaderSize = kMessageWithRequestIDHeaderSize;
969 exports.kMessageV2HeaderSize = kMessageV2HeaderSize;
897 exports.kMessageExpectsResponse = kMessageExpectsResponse; 970 exports.kMessageExpectsResponse = kMessageExpectsResponse;
898 exports.kMessageIsResponse = kMessageIsResponse; 971 exports.kMessageIsResponse = kMessageIsResponse;
899 exports.Int8 = Int8; 972 exports.Int8 = Int8;
900 exports.Uint8 = Uint8; 973 exports.Uint8 = Uint8;
901 exports.Int16 = Int16; 974 exports.Int16 = Int16;
902 exports.Uint16 = Uint16; 975 exports.Uint16 = Uint16;
903 exports.Int32 = Int32; 976 exports.Int32 = Int32;
904 exports.Uint32 = Uint32; 977 exports.Uint32 = Uint32;
905 exports.Int64 = Int64; 978 exports.Int64 = Int64;
906 exports.Uint64 = Uint64; 979 exports.Uint64 = Uint64;
907 exports.Float = Float; 980 exports.Float = Float;
908 exports.Double = Double; 981 exports.Double = Double;
909 exports.String = String; 982 exports.String = String;
910 exports.Enum = Enum; 983 exports.Enum = Enum;
911 exports.NullableString = NullableString; 984 exports.NullableString = NullableString;
912 exports.PointerTo = PointerTo; 985 exports.PointerTo = PointerTo;
913 exports.NullablePointerTo = NullablePointerTo; 986 exports.NullablePointerTo = NullablePointerTo;
914 exports.ArrayOf = ArrayOf; 987 exports.ArrayOf = ArrayOf;
915 exports.NullableArrayOf = NullableArrayOf; 988 exports.NullableArrayOf = NullableArrayOf;
916 exports.PackedBool = PackedBool; 989 exports.PackedBool = PackedBool;
917 exports.Handle = Handle; 990 exports.Handle = Handle;
918 exports.NullableHandle = NullableHandle; 991 exports.NullableHandle = NullableHandle;
919 exports.Interface = Interface; 992 exports.Interface = Interface;
920 exports.NullableInterface = NullableInterface; 993 exports.NullableInterface = NullableInterface;
921 exports.InterfaceRequest = InterfaceRequest; 994 exports.InterfaceRequest = InterfaceRequest;
922 exports.NullableInterfaceRequest = NullableInterfaceRequest; 995 exports.NullableInterfaceRequest = NullableInterfaceRequest;
996 exports.AssociatedInterfacePtrInfo = AssociatedInterfacePtrInfo;
997 exports.NullableAssociatedInterfacePtrInfo =
998 NullableAssociatedInterfacePtrInfo;
999 exports.AssociatedInterfaceRequest = AssociatedInterfaceRequest;
1000 exports.NullableAssociatedInterfaceRequest =
1001 NullableAssociatedInterfaceRequest;
923 exports.MapOf = MapOf; 1002 exports.MapOf = MapOf;
924 exports.NullableMapOf = NullableMapOf; 1003 exports.NullableMapOf = NullableMapOf;
925 return exports; 1004 return exports;
926 }); 1005 });
OLDNEW
« no previous file with comments | « mojo/public/js/associated_bindings.js ('k') | mojo/public/js/constants.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698