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/associated_group_controller.h"
11#include "mojo/public/cpp/system/core.h"
12
13namespace mojo {
14namespace internal {
15
16SerializedHandleVector::SerializedHandleVector() {}
17
18SerializedHandleVector::~SerializedHandleVector() {
19  for (auto handle : handles_) {
20    if (handle.is_valid()) {
21      MojoResult rv = MojoClose(handle.value());
22      DCHECK_EQ(rv, MOJO_RESULT_OK);
23    }
24  }
25}
26
27Handle_Data SerializedHandleVector::AddHandle(mojo::Handle handle) {
28  Handle_Data data;
29  if (!handle.is_valid()) {
30    data.value = kEncodedInvalidHandleValue;
31  } else {
32    DCHECK_LT(handles_.size(), std::numeric_limits<uint32_t>::max());
33    data.value = static_cast<uint32_t>(handles_.size());
34    handles_.push_back(handle);
35  }
36  return data;
37}
38
39mojo::Handle SerializedHandleVector::TakeHandle(
40    const Handle_Data& encoded_handle) {
41  if (!encoded_handle.is_valid())
42    return mojo::Handle();
43  DCHECK_LT(encoded_handle.value, handles_.size());
44  return FetchAndReset(&handles_[encoded_handle.value]);
45}
46
47void SerializedHandleVector::Swap(std::vector<mojo::Handle>* other) {
48  handles_.swap(*other);
49}
50
51SerializationContext::SerializationContext() {}
52
53SerializationContext::SerializationContext(
54    scoped_refptr<AssociatedGroupController> in_group_controller)
55    : group_controller(std::move(in_group_controller)) {}
56
57SerializationContext::~SerializationContext() {
58  DCHECK(!custom_contexts || custom_contexts->empty());
59}
60
61}  // namespace internal
62}  // namespace mojo
63