1 // Copyright 2018 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/system/scope_to_message_pipe.h"
6
7 namespace mojo {
8 namespace internal {
9
10 namespace {
11
OnWatcherSignaled(std::unique_ptr<MessagePipeScoperBase> scoper,MojoResult result,const HandleSignalsState & state)12 void OnWatcherSignaled(std::unique_ptr<MessagePipeScoperBase> scoper,
13 MojoResult result,
14 const HandleSignalsState& state) {
15 DCHECK(scoper);
16 DCHECK_EQ(result, MOJO_RESULT_OK);
17 DCHECK(state.peer_closed());
18
19 // No work to do except for letting |scoper| go out of scope and be destroyed.
20 }
21
22 } // namespace
23
MessagePipeScoperBase(ScopedMessagePipeHandle pipe)24 MessagePipeScoperBase::MessagePipeScoperBase(ScopedMessagePipeHandle pipe)
25 : pipe_(std::move(pipe)),
26 pipe_watcher_(FROM_HERE, SimpleWatcher::ArmingPolicy::AUTOMATIC) {}
27
28 MessagePipeScoperBase::~MessagePipeScoperBase() = default;
29
30 // static
StartWatchingPipe(std::unique_ptr<MessagePipeScoperBase> scoper)31 void MessagePipeScoperBase::StartWatchingPipe(
32 std::unique_ptr<MessagePipeScoperBase> scoper) {
33 auto* unowned_scoper = scoper.get();
34
35 // NOTE: We intentionally use base::Passed here with the owned |scoper|. The
36 // way we use it here, there is no way for the watcher callback to be invoked
37 // more than once. If this expectation is ever violated,
38 // base::RepeatingCallback will CHECK-fail.
39 unowned_scoper->pipe_watcher_.Watch(
40 unowned_scoper->pipe_.get(), MOJO_HANDLE_SIGNAL_PEER_CLOSED,
41 MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
42 base::BindRepeating(&OnWatcherSignaled, base::Passed(&scoper)));
43 }
44
45 } // namespace internal
46 } // namespace mojo
47