Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(318)

Side by Side Diff: ipc/mojo/ipc_mojo_bootstrap.cc

Issue 2069803003: Fold //ipc/mojo into //ipc. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@mojo-ipc-deps
Patch Set: Try fix mac tests. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 "ipc/mojo/ipc_mojo_bootstrap.h"
6
7 #include <stdint.h>
8 #include <utility>
9
10 #include "base/logging.h"
11 #include "base/macros.h"
12 #include "base/memory/ptr_util.h"
13 #include "base/process/process_handle.h"
14 #include "build/build_config.h"
15 #include "ipc/ipc_message_utils.h"
16 #include "ipc/ipc_platform_file.h"
17 #include "mojo/public/cpp/bindings/binding.h"
18
19 namespace IPC {
20
21 namespace {
22
23 // MojoBootstrap for the server process. You should create the instance
24 // using MojoBootstrap::Create().
25 class MojoServerBootstrap : public MojoBootstrap {
26 public:
27 MojoServerBootstrap();
28
29 private:
30 // MojoBootstrap implementation.
31 void Connect() override;
32
33 void OnInitDone(int32_t peer_pid);
34
35 mojom::BootstrapPtr bootstrap_;
36 IPC::mojom::ChannelAssociatedPtrInfo send_channel_;
37 IPC::mojom::ChannelAssociatedRequest receive_channel_request_;
38
39 DISALLOW_COPY_AND_ASSIGN(MojoServerBootstrap);
40 };
41
42 MojoServerBootstrap::MojoServerBootstrap() = default;
43
44 void MojoServerBootstrap::Connect() {
45 DCHECK_EQ(state(), STATE_INITIALIZED);
46
47 bootstrap_.Bind(mojom::BootstrapPtrInfo(TakeHandle(), 0));
48 bootstrap_.set_connection_error_handler(
49 base::Bind(&MojoServerBootstrap::Fail, base::Unretained(this)));
50
51 IPC::mojom::ChannelAssociatedRequest send_channel_request;
52 IPC::mojom::ChannelAssociatedPtrInfo receive_channel;
53
54 bootstrap_.associated_group()->CreateAssociatedInterface(
55 mojo::AssociatedGroup::WILL_PASS_REQUEST, &send_channel_,
56 &send_channel_request);
57 bootstrap_.associated_group()->CreateAssociatedInterface(
58 mojo::AssociatedGroup::WILL_PASS_PTR, &receive_channel,
59 &receive_channel_request_);
60
61 bootstrap_->Init(
62 std::move(send_channel_request), std::move(receive_channel),
63 GetSelfPID(),
64 base::Bind(&MojoServerBootstrap::OnInitDone, base::Unretained(this)));
65
66 set_state(STATE_WAITING_ACK);
67 }
68
69 void MojoServerBootstrap::OnInitDone(int32_t peer_pid) {
70 if (state() != STATE_WAITING_ACK) {
71 set_state(STATE_ERROR);
72 LOG(ERROR) << "Got inconsistent message from client.";
73 return;
74 }
75
76 set_state(STATE_READY);
77 bootstrap_.set_connection_error_handler(mojo::Closure());
78 delegate()->OnPipesAvailable(std::move(send_channel_),
79 std::move(receive_channel_request_), peer_pid);
80 }
81
82 // MojoBootstrap for client processes. You should create the instance
83 // using MojoBootstrap::Create().
84 class MojoClientBootstrap : public MojoBootstrap, public mojom::Bootstrap {
85 public:
86 MojoClientBootstrap();
87
88 private:
89 // MojoBootstrap implementation.
90 void Connect() override;
91
92 // mojom::Bootstrap implementation.
93 void Init(mojom::ChannelAssociatedRequest receive_channel,
94 mojom::ChannelAssociatedPtrInfo send_channel,
95 int32_t peer_pid,
96 const mojo::Callback<void(int32_t)>& callback) override;
97
98 mojo::Binding<mojom::Bootstrap> binding_;
99
100 DISALLOW_COPY_AND_ASSIGN(MojoClientBootstrap);
101 };
102
103 MojoClientBootstrap::MojoClientBootstrap() : binding_(this) {}
104
105 void MojoClientBootstrap::Connect() {
106 binding_.Bind(TakeHandle());
107 binding_.set_connection_error_handler(
108 base::Bind(&MojoClientBootstrap::Fail, base::Unretained(this)));
109 }
110
111 void MojoClientBootstrap::Init(mojom::ChannelAssociatedRequest receive_channel,
112 mojom::ChannelAssociatedPtrInfo send_channel,
113 int32_t peer_pid,
114 const mojo::Callback<void(int32_t)>& callback) {
115 callback.Run(GetSelfPID());
116 set_state(STATE_READY);
117 binding_.set_connection_error_handler(mojo::Closure());
118 delegate()->OnPipesAvailable(std::move(send_channel),
119 std::move(receive_channel), peer_pid);
120 }
121
122 } // namespace
123
124 // MojoBootstrap
125
126 // static
127 std::unique_ptr<MojoBootstrap> MojoBootstrap::Create(
128 mojo::ScopedMessagePipeHandle handle,
129 Channel::Mode mode,
130 Delegate* delegate) {
131 CHECK(mode == Channel::MODE_CLIENT || mode == Channel::MODE_SERVER);
132 std::unique_ptr<MojoBootstrap> self =
133 mode == Channel::MODE_CLIENT
134 ? std::unique_ptr<MojoBootstrap>(new MojoClientBootstrap)
135 : std::unique_ptr<MojoBootstrap>(new MojoServerBootstrap);
136
137 self->Init(std::move(handle), delegate);
138 return self;
139 }
140
141 MojoBootstrap::MojoBootstrap() : delegate_(NULL), state_(STATE_INITIALIZED) {
142 }
143
144 MojoBootstrap::~MojoBootstrap() {}
145
146 void MojoBootstrap::Init(mojo::ScopedMessagePipeHandle handle,
147 Delegate* delegate) {
148 handle_ = std::move(handle);
149 delegate_ = delegate;
150 }
151
152 base::ProcessId MojoBootstrap::GetSelfPID() const {
153 #if defined(OS_LINUX)
154 if (int global_pid = Channel::GetGlobalPid())
155 return global_pid;
156 #endif // OS_LINUX
157 return base::GetCurrentProcId();
158 }
159
160 void MojoBootstrap::Fail() {
161 set_state(STATE_ERROR);
162 delegate()->OnBootstrapError();
163 }
164
165 bool MojoBootstrap::HasFailed() const {
166 return state() == STATE_ERROR;
167 }
168
169 mojo::ScopedMessagePipeHandle MojoBootstrap::TakeHandle() {
170 return std::move(handle_);
171 }
172
173 } // namespace IPC
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698