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

Side by Side Diff: mojo/public/rust/src/bindings/message.rs

Issue 2250183003: Make the fuchsia mojo/public repo the source of truth. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 4 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/rust/src/bindings/macros.rs ('k') | mojo/public/rust/src/bindings/mod.rs » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 use bindings::decoding::{Decoder, ValidationError};
6 use bindings::encoding;
7 use bindings::encoding::{Context, DATA_HEADER_SIZE, DataHeaderValue, Encoder};
8 use bindings::mojom::{MojomEncodable, MojomPointer, MojomStruct};
9
10 /// A flag for the message header indicating that no flag has been set.
11 pub const MESSAGE_HEADER_NO_FLAG: u32 = 0;
12
13 /// A flag for the message header indicating that this message expects
14 /// a response.
15 pub const MESSAGE_HEADER_EXPECT_RESPONSE: u32 = 1;
16
17 /// A flag for the message header indicating that this message is
18 /// a response.
19 pub const MESSAGE_HEADER_IS_RESPONSE: u32 = 2;
20
21 const MESSAGE_HEADER_VERSIONS: [(u32, u32); 2] = [(0, 16), (1, 24)];
22
23 /// A message header object implemented as a Mojom struct.
24 pub struct MessageHeader {
25 pub version: u32,
26 pub name: u32,
27 pub flags: u32,
28 pub request_id: u64,
29 }
30
31 impl MessageHeader {
32 /// Create a new MessageHeader.
33 pub fn new(version: u32, name: u32, flags: u32) -> MessageHeader {
34 MessageHeader {
35 version: version,
36 name: name,
37 flags: flags,
38 request_id: 0,
39 }
40 }
41 }
42
43 impl MojomPointer for MessageHeader {
44 fn header_data(&self) -> DataHeaderValue {
45 DataHeaderValue::Version(self.version)
46 }
47
48 /// Get the serialized size.
49 ///
50 /// This value differs based on whether or not
51 /// a request_id is necessary.
52 fn serialized_size(&self, _context: &Context) -> usize {
53 let mut size = DATA_HEADER_SIZE + 8;
54 if self.flags != MESSAGE_HEADER_NO_FLAG {
55 size += 8;
56 }
57 encoding::align_default(size)
58 }
59
60 fn encode_value(self, encoder: &mut Encoder, context: Context) {
61 MojomEncodable::encode(self.name, encoder, context.clone());
62 MojomEncodable::encode(self.flags, encoder, context.clone());
63 if self.version > 0 {
64 MojomEncodable::encode(self.request_id, encoder, context.clone());
65 }
66 }
67
68 fn decode_value(decoder: &mut Decoder, context: Context) -> Result<Self, Val idationError> {
69 let mut state = decoder.get_mut(&context);
70 let version = match state.decode_struct_header(&MESSAGE_HEADER_VERSIONS) {
71 Ok(header) => header.data(),
72 Err(err) => return Err(err),
73 };
74 let name = state.decode::<u32>();
75 let flags = state.decode::<u32>();
76 if flags > MESSAGE_HEADER_IS_RESPONSE {
77 return Err(ValidationError::MessageHeaderInvalidFlags);
78 }
79 if version == 0 {
80 if flags == MESSAGE_HEADER_IS_RESPONSE || flags == MESSAGE_HEADER_EX PECT_RESPONSE {
81 return Err(ValidationError::MessageHeaderMissingRequestId);
82 }
83 Ok(MessageHeader {
84 version: version,
85 name: name,
86 flags: flags,
87 request_id: 0,
88 })
89 } else if version == 1 {
90 Ok(MessageHeader {
91 version: version,
92 name: name,
93 flags: flags,
94 request_id: state.decode::<u64>(),
95 })
96 } else {
97 return Err(ValidationError::UnexpectedStructHeader);
98 }
99 }
100 }
101
102 impl MojomEncodable for MessageHeader {
103 impl_encodable_for_pointer!();
104 fn compute_size(&self, context: Context) -> usize {
105 self.serialized_size(&context)
106 }
107 }
108
109 impl MojomStruct for MessageHeader {}
110
OLDNEW
« no previous file with comments | « mojo/public/rust/src/bindings/macros.rs ('k') | mojo/public/rust/src/bindings/mod.rs » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698