1 // Copyright 2014 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/core/platform_handle_dispatcher.h"
6
7 #include "base/synchronization/lock.h"
8
9 namespace mojo {
10 namespace core {
11
12 // static
Create(PlatformHandle platform_handle)13 scoped_refptr<PlatformHandleDispatcher> PlatformHandleDispatcher::Create(
14 PlatformHandle platform_handle) {
15 return new PlatformHandleDispatcher(std::move(platform_handle));
16 }
17
TakePlatformHandle()18 PlatformHandle PlatformHandleDispatcher::TakePlatformHandle() {
19 return std::move(platform_handle_);
20 }
21
GetType() const22 Dispatcher::Type PlatformHandleDispatcher::GetType() const {
23 return Type::PLATFORM_HANDLE;
24 }
25
Close()26 MojoResult PlatformHandleDispatcher::Close() {
27 base::AutoLock lock(lock_);
28 if (is_closed_ || in_transit_)
29 return MOJO_RESULT_INVALID_ARGUMENT;
30 is_closed_ = true;
31 platform_handle_.reset();
32 return MOJO_RESULT_OK;
33 }
34
StartSerialize(uint32_t * num_bytes,uint32_t * num_ports,uint32_t * num_handles)35 void PlatformHandleDispatcher::StartSerialize(uint32_t* num_bytes,
36 uint32_t* num_ports,
37 uint32_t* num_handles) {
38 *num_bytes = 0;
39 *num_ports = 0;
40 *num_handles = 1;
41 }
42
EndSerialize(void * destination,ports::PortName * ports,PlatformHandle * handles)43 bool PlatformHandleDispatcher::EndSerialize(void* destination,
44 ports::PortName* ports,
45 PlatformHandle* handles) {
46 base::AutoLock lock(lock_);
47 if (is_closed_)
48 return false;
49 handles[0] = std::move(platform_handle_);
50 return true;
51 }
52
BeginTransit()53 bool PlatformHandleDispatcher::BeginTransit() {
54 base::AutoLock lock(lock_);
55 if (in_transit_)
56 return false;
57 in_transit_ = !is_closed_;
58 return in_transit_;
59 }
60
CompleteTransitAndClose()61 void PlatformHandleDispatcher::CompleteTransitAndClose() {
62 base::AutoLock lock(lock_);
63 in_transit_ = false;
64 is_closed_ = true;
65 }
66
CancelTransit()67 void PlatformHandleDispatcher::CancelTransit() {
68 base::AutoLock lock(lock_);
69 in_transit_ = false;
70 }
71
72 // static
Deserialize(const void * bytes,size_t num_bytes,const ports::PortName * ports,size_t num_ports,PlatformHandle * handles,size_t num_handles)73 scoped_refptr<PlatformHandleDispatcher> PlatformHandleDispatcher::Deserialize(
74 const void* bytes,
75 size_t num_bytes,
76 const ports::PortName* ports,
77 size_t num_ports,
78 PlatformHandle* handles,
79 size_t num_handles) {
80 if (num_bytes || num_ports || num_handles != 1)
81 return nullptr;
82
83 return PlatformHandleDispatcher::Create(std::move(handles[0]));
84 }
85
PlatformHandleDispatcher(PlatformHandle platform_handle)86 PlatformHandleDispatcher::PlatformHandleDispatcher(
87 PlatformHandle platform_handle)
88 : platform_handle_(std::move(platform_handle)) {}
89
~PlatformHandleDispatcher()90 PlatformHandleDispatcher::~PlatformHandleDispatcher() {
91 DCHECK(is_closed_ && !in_transit_);
92 DCHECK(!platform_handle_.is_valid());
93 }
94
95 } // namespace core
96 } // namespace mojo
97