• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package org.chromium.mojo.bindings;
6 
7 import org.chromium.mojo.system.Core;
8 
9 /**
10  * Base class for all mojo structs.
11  */
12 public abstract class Struct {
13     /**
14      * The base size of the encoded struct.
15      */
16     private final int mEncodedBaseSize;
17 
18     /**
19      * The version of the struct.
20      */
21     private final int mVersion;
22 
23     /**
24      * Constructor.
25      */
Struct(int encodedBaseSize, int version)26     protected Struct(int encodedBaseSize, int version) {
27         mEncodedBaseSize = encodedBaseSize;
28         mVersion = version;
29     }
30 
31     /**
32      * Returns the version of the struct. It is the max version of the struct in the mojom if it has
33      * been created locally, and the version of the received struct if it has been deserialized.
34      */
getVersion()35     public int getVersion() {
36         return mVersion;
37     }
38 
39     /**
40      * Returns the serialization of the struct. This method can close Handles.
41      *
42      * @param core the |Core| implementation used to generate handles. Only used if the data
43      *            structure being encoded contains interfaces, can be |null| otherwise.
44      */
serialize(Core core)45     public Message serialize(Core core) {
46         Encoder encoder = new Encoder(core, mEncodedBaseSize);
47         encode(encoder);
48         return encoder.getMessage();
49     }
50 
51     /**
52      * Returns the serialization of the struct prepended with the given header.
53      *
54      * @param header the header to prepend to the returned message.
55      * @param core the |Core| implementation used to generate handles. Only used if the |Struct|
56      *            being encoded contains interfaces, can be |null| otherwise.
57      */
serializeWithHeader(Core core, MessageHeader header)58     public ServiceMessage serializeWithHeader(Core core, MessageHeader header) {
59         Encoder encoder = new Encoder(core, mEncodedBaseSize + header.getSize());
60         header.encode(encoder);
61         encode(encoder);
62         return new ServiceMessage(encoder.getMessage(), header);
63     }
64 
65     /**
66      * Use the given encoder to serialize this data structure.
67      */
encode(Encoder encoder)68     protected abstract void encode(Encoder encoder);
69 }
70