1 // Copyright 2015 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/scoped_interface_endpoint_handle.h"
6
7 #include "base/logging.h"
8 #include "mojo/public/cpp/bindings/associated_group_controller.h"
9
10 namespace mojo {
11
ScopedInterfaceEndpointHandle()12 ScopedInterfaceEndpointHandle::ScopedInterfaceEndpointHandle()
13 : ScopedInterfaceEndpointHandle(kInvalidInterfaceId, true, nullptr) {}
14
ScopedInterfaceEndpointHandle(ScopedInterfaceEndpointHandle && other)15 ScopedInterfaceEndpointHandle::ScopedInterfaceEndpointHandle(
16 ScopedInterfaceEndpointHandle&& other)
17 : id_(other.id_), is_local_(other.is_local_) {
18 group_controller_.swap(other.group_controller_);
19 other.id_ = kInvalidInterfaceId;
20 }
21
~ScopedInterfaceEndpointHandle()22 ScopedInterfaceEndpointHandle::~ScopedInterfaceEndpointHandle() {
23 reset();
24 }
25
operator =(ScopedInterfaceEndpointHandle && other)26 ScopedInterfaceEndpointHandle& ScopedInterfaceEndpointHandle::operator=(
27 ScopedInterfaceEndpointHandle&& other) {
28 reset();
29 swap(other);
30
31 return *this;
32 }
33
reset()34 void ScopedInterfaceEndpointHandle::reset() {
35 if (!IsValidInterfaceId(id_))
36 return;
37
38 group_controller_->CloseEndpointHandle(id_, is_local_);
39
40 id_ = kInvalidInterfaceId;
41 is_local_ = true;
42 group_controller_ = nullptr;
43 }
44
swap(ScopedInterfaceEndpointHandle & other)45 void ScopedInterfaceEndpointHandle::swap(ScopedInterfaceEndpointHandle& other) {
46 using std::swap;
47 swap(other.id_, id_);
48 swap(other.is_local_, is_local_);
49 swap(other.group_controller_, group_controller_);
50 }
51
release()52 InterfaceId ScopedInterfaceEndpointHandle::release() {
53 InterfaceId result = id_;
54
55 id_ = kInvalidInterfaceId;
56 is_local_ = true;
57 group_controller_ = nullptr;
58
59 return result;
60 }
61
ScopedInterfaceEndpointHandle(InterfaceId id,bool is_local,scoped_refptr<AssociatedGroupController> group_controller)62 ScopedInterfaceEndpointHandle::ScopedInterfaceEndpointHandle(
63 InterfaceId id,
64 bool is_local,
65 scoped_refptr<AssociatedGroupController> group_controller)
66 : id_(id),
67 is_local_(is_local),
68 group_controller_(std::move(group_controller)) {
69 DCHECK(!IsValidInterfaceId(id) || group_controller_);
70 }
71
72 } // namespace mojo
73