• 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 #include "mojo/public/cpp/bindings/lib/serialization_context.h"
6 
7 #include <limits>
8 
9 #include "base/logging.h"
10 #include "mojo/public/cpp/bindings/message.h"
11 #include "mojo/public/cpp/system/core.h"
12 
13 namespace mojo {
14 namespace internal {
15 
16 SerializationContext::SerializationContext() = default;
17 
18 SerializationContext::~SerializationContext() = default;
19 
AddHandle(mojo::ScopedHandle handle,Handle_Data * out_data)20 void SerializationContext::AddHandle(mojo::ScopedHandle handle,
21                                      Handle_Data* out_data) {
22   if (!handle.is_valid()) {
23     out_data->value = kEncodedInvalidHandleValue;
24   } else {
25     DCHECK_LT(handles_.size(), std::numeric_limits<uint32_t>::max());
26     out_data->value = static_cast<uint32_t>(handles_.size());
27     handles_.emplace_back(std::move(handle));
28   }
29 }
30 
AddInterfaceInfo(mojo::ScopedMessagePipeHandle handle,uint32_t version,Interface_Data * out_data)31 void SerializationContext::AddInterfaceInfo(
32     mojo::ScopedMessagePipeHandle handle,
33     uint32_t version,
34     Interface_Data* out_data) {
35   AddHandle(ScopedHandle::From(std::move(handle)), &out_data->handle);
36   out_data->version = version;
37 }
38 
AddAssociatedEndpoint(ScopedInterfaceEndpointHandle handle,AssociatedEndpointHandle_Data * out_data)39 void SerializationContext::AddAssociatedEndpoint(
40     ScopedInterfaceEndpointHandle handle,
41     AssociatedEndpointHandle_Data* out_data) {
42   if (!handle.is_valid()) {
43     out_data->value = kEncodedInvalidHandleValue;
44   } else {
45     DCHECK_LT(associated_endpoint_handles_.size(),
46               std::numeric_limits<uint32_t>::max());
47     out_data->value =
48         static_cast<uint32_t>(associated_endpoint_handles_.size());
49     associated_endpoint_handles_.emplace_back(std::move(handle));
50   }
51 }
52 
AddAssociatedInterfaceInfo(ScopedInterfaceEndpointHandle handle,uint32_t version,AssociatedInterface_Data * out_data)53 void SerializationContext::AddAssociatedInterfaceInfo(
54     ScopedInterfaceEndpointHandle handle,
55     uint32_t version,
56     AssociatedInterface_Data* out_data) {
57   AddAssociatedEndpoint(std::move(handle), &out_data->handle);
58   out_data->version = version;
59 }
60 
TakeHandlesFromMessage(Message * message)61 void SerializationContext::TakeHandlesFromMessage(Message* message) {
62   handles_.swap(*message->mutable_handles());
63   associated_endpoint_handles_.swap(
64       *message->mutable_associated_endpoint_handles());
65 }
66 
TakeHandle(const Handle_Data & encoded_handle)67 mojo::ScopedHandle SerializationContext::TakeHandle(
68     const Handle_Data& encoded_handle) {
69   if (!encoded_handle.is_valid())
70     return mojo::ScopedHandle();
71   DCHECK_LT(encoded_handle.value, handles_.size());
72   return std::move(handles_[encoded_handle.value]);
73 }
74 
75 mojo::ScopedInterfaceEndpointHandle
TakeAssociatedEndpointHandle(const AssociatedEndpointHandle_Data & encoded_handle)76 SerializationContext::TakeAssociatedEndpointHandle(
77     const AssociatedEndpointHandle_Data& encoded_handle) {
78   if (!encoded_handle.is_valid())
79     return mojo::ScopedInterfaceEndpointHandle();
80   DCHECK_LT(encoded_handle.value, associated_endpoint_handles_.size());
81   return std::move(associated_endpoint_handles_[encoded_handle.value]);
82 }
83 
84 }  // namespace internal
85 }  // namespace mojo
86