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, ValidationError}; | 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)]; | 21 const MESSAGE_HEADER_VERSIONS: [(u32, u32); 2] = [(0, 16), (1, 24)]; |
22 | 22 |
23 /// A message header object implemented as a Mojom struct. | 23 /// A message header object implemented as a Mojom struct. |
24 pub struct MessageHeader { | 24 pub struct MessageHeader { |
25 pub version: u32, | 25 pub version: u32, |
26 pub name: u32, | 26 pub name: u32, |
27 pub flags: u32, | 27 pub flags: u32, |
28 request_id: u64, | 28 pub request_id: u64, |
29 } | 29 } |
30 | 30 |
31 impl MessageHeader { | 31 impl MessageHeader { |
32 /// Create a new MessageHeader. | 32 /// Create a new MessageHeader. |
33 pub fn new(version: u32, name: u32, flags: u32) -> MessageHeader { | 33 pub fn new(version: u32, name: u32, flags: u32) -> MessageHeader { |
34 MessageHeader { | 34 MessageHeader { |
35 version: version, | 35 version: version, |
36 name: name, | 36 name: name, |
37 flags: flags, | 37 flags: flags, |
38 request_id: 0, | 38 request_id: 0, |
39 } | 39 } |
40 } | 40 } |
41 | |
42 /// Set the request ID. | |
43 pub fn set_request_id(&mut self, id: u64) { | |
44 self.request_id = id; | |
45 } | |
46 } | 41 } |
47 | 42 |
48 impl MojomPointer for MessageHeader { | 43 impl MojomPointer for MessageHeader { |
49 fn header_data(&self) -> DataHeaderValue { | 44 fn header_data(&self) -> DataHeaderValue { |
50 DataHeaderValue::Version(self.version) | 45 DataHeaderValue::Version(self.version) |
51 } | 46 } |
52 | 47 |
53 /// Get the serialized size. | 48 /// Get the serialized size. |
54 /// | 49 /// |
55 /// This value differs based on whether or not | 50 /// This value differs based on whether or not |
56 /// a request_id is necessary. | 51 /// a request_id is necessary. |
57 fn serialized_size(&self, _context: &Context) -> usize { | 52 fn serialized_size(&self, _context: &Context) -> usize { |
58 let mut size = DATA_HEADER_SIZE + 8; | 53 let mut size = DATA_HEADER_SIZE + 8; |
59 if self.flags != MESSAGE_HEADER_NO_FLAG { | 54 if self.flags != MESSAGE_HEADER_NO_FLAG { |
60 size += 8; | 55 size += 8; |
61 } | 56 } |
62 encoding::align_default(size) | 57 encoding::align_default(size) |
63 } | 58 } |
64 | 59 |
65 fn encode_value(self, encoder: &mut Encoder, context: Context) { | 60 fn encode_value(self, encoder: &mut Encoder, context: Context) { |
66 MojomEncodable::encode(self.name, encoder, context.clone()); | 61 MojomEncodable::encode(self.name, encoder, context.clone()); |
67 MojomEncodable::encode(self.flags, encoder, context.clone()); | 62 MojomEncodable::encode(self.flags, encoder, context.clone()); |
68 if self.flags != MESSAGE_HEADER_NO_FLAG { | 63 if self.version > 0 { |
69 MojomEncodable::encode(self.request_id, encoder, context.clone()); | 64 MojomEncodable::encode(self.request_id, encoder, context.clone()); |
70 } | 65 } |
71 } | 66 } |
72 | 67 |
73 fn decode_value(decoder: &mut Decoder, context: Context) -> Result<Self, Val
idationError> { | 68 fn decode_value(decoder: &mut Decoder, context: Context) -> Result<Self, Val
idationError> { |
74 let mut state = decoder.get_mut(&context); | 69 let mut state = decoder.get_mut(&context); |
75 let version = match state.decode_struct_header(&MESSAGE_HEADER_VERSIONS)
{ | 70 let version = match state.decode_struct_header(&MESSAGE_HEADER_VERSIONS)
{ |
76 Ok(header) => header.data(), | 71 Ok(header) => header.data(), |
77 Err(err) => return Err(err), | 72 Err(err) => return Err(err), |
78 }; | 73 }; |
(...skipping 26 matching lines...) Expand all Loading... |
105 } | 100 } |
106 | 101 |
107 impl MojomEncodable for MessageHeader { | 102 impl MojomEncodable for MessageHeader { |
108 impl_encodable_for_pointer!(); | 103 impl_encodable_for_pointer!(); |
109 fn compute_size(&self, context: Context) -> usize { | 104 fn compute_size(&self, context: Context) -> usize { |
110 self.serialized_size(&context) | 105 self.serialized_size(&context) |
111 } | 106 } |
112 } | 107 } |
113 | 108 |
114 impl MojomStruct for MessageHeader {} | 109 impl MojomStruct for MessageHeader {} |
| 110 |
OLD | NEW |