• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_SERIALIZATION_CONTEXT_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_SERIALIZATION_CONTEXT_H_
7 
8 #include <stddef.h>
9 
10 #include <memory>
11 #include <vector>
12 
13 #include "base/component_export.h"
14 #include "base/containers/stack_container.h"
15 #include "base/macros.h"
16 #include "mojo/public/cpp/bindings/lib/bindings_internal.h"
17 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h"
18 #include "mojo/public/cpp/system/handle.h"
19 
20 namespace mojo {
21 
22 class Message;
23 
24 namespace internal {
25 
26 // Context information for serialization/deserialization routines.
COMPONENT_EXPORT(MOJO_CPP_BINDINGS_BASE)27 class COMPONENT_EXPORT(MOJO_CPP_BINDINGS_BASE) SerializationContext {
28  public:
29   SerializationContext();
30   ~SerializationContext();
31 
32   // Adds a handle to the handle list and outputs its serialized form in
33   // |*out_data|.
34   void AddHandle(mojo::ScopedHandle handle, Handle_Data* out_data);
35 
36   // Adds an interface info to the handle list and outputs its serialized form
37   // in |*out_data|.
38   void AddInterfaceInfo(mojo::ScopedMessagePipeHandle handle,
39                         uint32_t version,
40                         Interface_Data* out_data);
41 
42   // Adds an associated interface endpoint (for e.g. an
43   // AssociatedInterfaceRequest) to this context and outputs its serialized form
44   // in |*out_data|.
45   void AddAssociatedEndpoint(ScopedInterfaceEndpointHandle handle,
46                              AssociatedEndpointHandle_Data* out_data);
47 
48   // Adds an associated interface info to associated endpoint handle and version
49   // data lists and outputs its serialized form in |*out_data|.
50   void AddAssociatedInterfaceInfo(ScopedInterfaceEndpointHandle handle,
51                                   uint32_t version,
52                                   AssociatedInterface_Data* out_data);
53 
54   const std::vector<mojo::ScopedHandle>* handles() { return &handles_; }
55   std::vector<mojo::ScopedHandle>* mutable_handles() { return &handles_; }
56 
57   const std::vector<ScopedInterfaceEndpointHandle>*
58   associated_endpoint_handles() const {
59     return &associated_endpoint_handles_;
60   }
61   std::vector<ScopedInterfaceEndpointHandle>*
62   mutable_associated_endpoint_handles() {
63     return &associated_endpoint_handles_;
64   }
65 
66   // Takes handles from a received Message object and assumes ownership of them.
67   // Individual handles can be extracted using Take* methods below.
68   void TakeHandlesFromMessage(Message* message);
69 
70   // Takes a handle from the list of serialized handle data.
71   mojo::ScopedHandle TakeHandle(const Handle_Data& encoded_handle);
72 
73   // Takes a handle from the list of serialized handle data and returns it in
74   // |*out_handle| as a specific scoped handle type.
75   template <typename T>
76   ScopedHandleBase<T> TakeHandleAs(const Handle_Data& encoded_handle) {
77     return ScopedHandleBase<T>::From(TakeHandle(encoded_handle));
78   }
79 
80   mojo::ScopedInterfaceEndpointHandle TakeAssociatedEndpointHandle(
81       const AssociatedEndpointHandle_Data& encoded_handle);
82 
83  private:
84   // Handles owned by this object. Used during serialization to hold onto
85   // handles accumulated during pre-serialization, and used during
86   // deserialization to hold onto handles extracted from a message.
87   std::vector<mojo::ScopedHandle> handles_;
88 
89   // Stashes ScopedInterfaceEndpointHandles encoded in a message by index.
90   std::vector<ScopedInterfaceEndpointHandle> associated_endpoint_handles_;
91 
92   DISALLOW_COPY_AND_ASSIGN(SerializationContext);
93 };
94 
95 }  // namespace internal
96 }  // namespace mojo
97 
98 #endif  // MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_SERIALIZATION_CONTEXT_H_
99