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 "base/memory/ref_counted.h" 16 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" 17 #include "mojo/public/cpp/system/handle.h" 18 19 namespace mojo { 20 21 class AssociatedGroupController; 22 23 namespace internal { 24 25 // A container for handles during serialization/deserialization. 26 class SerializedHandleVector { 27 public: 28 SerializedHandleVector(); 29 ~SerializedHandleVector(); 30 size()31 size_t size() const { return handles_.size(); } 32 33 // Adds a handle to the handle list and returns its index for encoding. 34 Handle_Data AddHandle(mojo::Handle handle); 35 36 // Takes a handle from the list of serialized handle data. 37 mojo::Handle TakeHandle(const Handle_Data& encoded_handle); 38 39 // Takes a handle from the list of serialized handle data and returns it in 40 // |*out_handle| as a specific scoped handle type. 41 template <typename T> TakeHandleAs(const Handle_Data & encoded_handle)42 ScopedHandleBase<T> TakeHandleAs(const Handle_Data& encoded_handle) { 43 return MakeScopedHandle(T(TakeHandle(encoded_handle).value())); 44 } 45 46 // Swaps all owned handles out with another Handle vector. 47 void Swap(std::vector<mojo::Handle>* other); 48 49 private: 50 // Handles are owned by this object. 51 std::vector<mojo::Handle> handles_; 52 53 DISALLOW_COPY_AND_ASSIGN(SerializedHandleVector); 54 }; 55 56 // Context information for serialization/deserialization routines. 57 struct SerializationContext { 58 SerializationContext(); 59 explicit SerializationContext( 60 scoped_refptr<AssociatedGroupController> in_group_controller); 61 62 ~SerializationContext(); 63 64 // Used to serialize/deserialize associated interface pointers and requests. 65 scoped_refptr<AssociatedGroupController> group_controller; 66 67 // Opaque context pointers returned by StringTraits::SetUpContext(). 68 std::unique_ptr<std::queue<void*>> custom_contexts; 69 70 // Stashes handles encoded in a message by index. 71 SerializedHandleVector handles; 72 }; 73 74 } // namespace internal 75 } // namespace mojo 76 77 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_SERIALIZATION_CONTEXT_H_ 78