Home | History | Annotate | Download | only in lib
      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 <queue>
     12 #include <vector>
     13 
     14 #include "base/macros.h"
     15 #include "mojo/public/cpp/bindings/bindings_export.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 namespace internal {
     22 
     23 // A container for handles during serialization/deserialization.
     24 class MOJO_CPP_BINDINGS_EXPORT SerializedHandleVector {
     25  public:
     26   SerializedHandleVector();
     27   ~SerializedHandleVector();
     28 
     29   size_t size() const { return handles_.size(); }
     30 
     31   // Adds a handle to the handle list and returns its index for encoding.
     32   Handle_Data AddHandle(mojo::Handle handle);
     33 
     34   // Takes a handle from the list of serialized handle data.
     35   mojo::Handle TakeHandle(const Handle_Data& encoded_handle);
     36 
     37   // Takes a handle from the list of serialized handle data and returns it in
     38   // |*out_handle| as a specific scoped handle type.
     39   template <typename T>
     40   ScopedHandleBase<T> TakeHandleAs(const Handle_Data& encoded_handle) {
     41     return MakeScopedHandle(T(TakeHandle(encoded_handle).value()));
     42   }
     43 
     44   // Swaps all owned handles out with another Handle vector.
     45   void Swap(std::vector<mojo::Handle>* other);
     46 
     47  private:
     48   // Handles are owned by this object.
     49   std::vector<mojo::Handle> handles_;
     50 
     51   DISALLOW_COPY_AND_ASSIGN(SerializedHandleVector);
     52 };
     53 
     54 // Context information for serialization/deserialization routines.
     55 struct MOJO_CPP_BINDINGS_EXPORT SerializationContext {
     56   SerializationContext();
     57 
     58   ~SerializationContext();
     59 
     60   // Opaque context pointers returned by StringTraits::SetUpContext().
     61   std::unique_ptr<std::queue<void*>> custom_contexts;
     62 
     63   // Stashes handles encoded in a message by index.
     64   SerializedHandleVector handles;
     65 
     66   // The number of ScopedInterfaceEndpointHandles that need to be serialized.
     67   // It is calculated by PrepareToSerialize().
     68   uint32_t associated_endpoint_count = 0;
     69 
     70   // Stashes ScopedInterfaceEndpointHandles encoded in a message by index.
     71   std::vector<ScopedInterfaceEndpointHandle> associated_endpoint_handles;
     72 };
     73 
     74 }  // namespace internal
     75 }  // namespace mojo
     76 
     77 #endif  // MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_SERIALIZATION_CONTEXT_H_
     78