| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 use bindings::decoding::Decoder; | 5 use bindings::decoding::{Decoder, ValidationError}; |
| 6 use bindings::encoding; | 6 use bindings::encoding; |
| 7 use bindings::encoding::{Context, DATA_HEADER_SIZE, DataHeaderValue, Encoder}; | 7 use bindings::encoding::{Context, DATA_HEADER_SIZE, DataHeaderValue, Encoder}; |
| 8 use bindings::mojom::{MojomEncodable, MojomPointer, MojomStruct}; | 8 use bindings::mojom::{MojomEncodable, MojomPointer, MojomStruct}; |
| 9 | 9 |
| 10 /// A flag for the message header indicating that no flag has been set. | 10 /// A flag for the message header indicating that no flag has been set. |
| 11 pub const MESSAGE_HEADER_NO_FLAG: u32 = 0; | 11 pub const MESSAGE_HEADER_NO_FLAG: u32 = 0; |
| 12 | 12 |
| 13 /// A flag for the message header indicating that this message expects | 13 /// A flag for the message header indicating that this message expects |
| 14 /// a response. | 14 /// a response. |
| 15 pub const MESSAGE_HEADER_EXPECT_RESPONSE: u32 = 1; | 15 pub const MESSAGE_HEADER_EXPECT_RESPONSE: u32 = 1; |
| 16 | 16 |
| 17 /// A flag for the message header indicating that this message is | 17 /// A flag for the message header indicating that this message is |
| 18 /// a response. | 18 /// a response. |
| 19 pub const MESSAGE_HEADER_IS_RESPONSE: u32 = 2; | 19 pub const MESSAGE_HEADER_IS_RESPONSE: u32 = 2; |
| 20 | 20 |
| 21 const MESSAGE_HEADER_VERSIONS: [(u32, u32); 2] = [(0, 16), (1, 24)]; |
| 22 |
| 21 /// A message header object implemented as a Mojom struct. | 23 /// A message header object implemented as a Mojom struct. |
| 22 pub struct MessageHeader { | 24 pub struct MessageHeader { |
| 23 pub version: u32, | 25 pub version: u32, |
| 24 pub name: u32, | 26 pub name: u32, |
| 25 pub flags: u32, | 27 pub flags: u32, |
| 26 request_id: u64, | 28 request_id: u64, |
| 27 } | 29 } |
| 28 | 30 |
| 29 impl MessageHeader { | 31 impl MessageHeader { |
| 30 /// Create a new MessageHeader. | 32 /// Create a new MessageHeader. |
| (...skipping 30 matching lines...) Expand all Loading... |
| 61 } | 63 } |
| 62 | 64 |
| 63 fn encode_value(self, encoder: &mut Encoder, context: Context) { | 65 fn encode_value(self, encoder: &mut Encoder, context: Context) { |
| 64 MojomEncodable::encode(self.name, encoder, context.clone()); | 66 MojomEncodable::encode(self.name, encoder, context.clone()); |
| 65 MojomEncodable::encode(self.flags, encoder, context.clone()); | 67 MojomEncodable::encode(self.flags, encoder, context.clone()); |
| 66 if self.flags != MESSAGE_HEADER_NO_FLAG { | 68 if self.flags != MESSAGE_HEADER_NO_FLAG { |
| 67 MojomEncodable::encode(self.request_id, encoder, context.clone()); | 69 MojomEncodable::encode(self.request_id, encoder, context.clone()); |
| 68 } | 70 } |
| 69 } | 71 } |
| 70 | 72 |
| 71 fn decode_value(decoder: &mut Decoder, context: Context) -> Self { | 73 fn decode_value(decoder: &mut Decoder, context: Context) -> Result<Self, Val
idationError> { |
| 72 let mut state = decoder.get_mut(&context); | 74 let mut state = decoder.get_mut(&context); |
| 73 // TODO(mknyszek): Verify bytes and T::embed_size match | 75 let version = match state.decode_struct_header(&MESSAGE_HEADER_VERSIONS)
{ |
| 74 let bytes = state.decode::<u32>(); | 76 Ok(header) => header.data(), |
| 75 let version = state.decode::<u32>(); | 77 Err(err) => return Err(err), |
| 76 // TODO(mknyszek): Don't trust bytes blindly | 78 }; |
| 77 let name = state.decode::<u32>(); | 79 let name = state.decode::<u32>(); |
| 78 let flags = state.decode::<u32>(); | 80 let flags = state.decode::<u32>(); |
| 79 if (bytes as usize) == DATA_HEADER_SIZE + 8 { | 81 if flags > MESSAGE_HEADER_IS_RESPONSE { |
| 80 MessageHeader { | 82 return Err(ValidationError::MessageHeaderInvalidFlags); |
| 83 } |
| 84 if version == 0 { |
| 85 if flags == MESSAGE_HEADER_IS_RESPONSE || flags == MESSAGE_HEADER_EX
PECT_RESPONSE { |
| 86 return Err(ValidationError::MessageHeaderMissingRequestId); |
| 87 } |
| 88 Ok(MessageHeader { |
| 81 version: version, | 89 version: version, |
| 82 name: name, | 90 name: name, |
| 83 flags: flags, | 91 flags: flags, |
| 84 request_id: 0, | 92 request_id: 0, |
| 85 } | 93 }) |
| 86 } else if (bytes as usize) == DATA_HEADER_SIZE + 16 { | 94 } else if version == 1 { |
| 87 MessageHeader { | 95 Ok(MessageHeader { |
| 88 version: version, | 96 version: version, |
| 89 name: name, | 97 name: name, |
| 90 flags: flags, | 98 flags: flags, |
| 91 request_id: state.decode::<u64>(), | 99 request_id: state.decode::<u64>(), |
| 92 } | 100 }) |
| 93 } else { | 101 } else { |
| 94 panic!("Invalid message header size found: `{}`", bytes); | 102 return Err(ValidationError::UnexpectedStructHeader); |
| 95 } | 103 } |
| 96 } | 104 } |
| 97 } | 105 } |
| 98 | 106 |
| 99 impl MojomEncodable for MessageHeader { | 107 impl MojomEncodable for MessageHeader { |
| 100 impl_encodable_for_pointer!(); | 108 impl_encodable_for_pointer!(); |
| 101 fn compute_size(&self, context: Context) -> usize { | 109 fn compute_size(&self, context: Context) -> usize { |
| 102 self.serialized_size(&context) | 110 self.serialized_size(&context) |
| 103 } | 111 } |
| 104 } | 112 } |
| 105 | 113 |
| 106 impl MojomStruct for MessageHeader {} | 114 impl MojomStruct for MessageHeader {} |
| 107 | |
| OLD | NEW |