1// Copyright 2017 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/edk/embedder/pending_process_connection.h"
6
7#include "mojo/edk/embedder/embedder.h"
8#include "mojo/edk/embedder/embedder_internal.h"
9#include "mojo/edk/system/core.h"
10
11namespace mojo {
12namespace edk {
13
14PendingProcessConnection::PendingProcessConnection()
15    : process_token_(GenerateRandomToken()) {
16  DCHECK(internal::g_core);
17}
18
19PendingProcessConnection::~PendingProcessConnection() {
20  if (has_message_pipes_ && !connected_) {
21    DCHECK(internal::g_core);
22    internal::g_core->ChildLaunchFailed(process_token_);
23  }
24}
25
26ScopedMessagePipeHandle PendingProcessConnection::CreateMessagePipe(
27    std::string* token) {
28  has_message_pipes_ = true;
29  DCHECK(internal::g_core);
30  *token = GenerateRandomToken();
31  return internal::g_core->CreateParentMessagePipe(*token, process_token_);
32}
33
34void PendingProcessConnection::Connect(
35    base::ProcessHandle process,
36    ConnectionParams connection_params,
37    const ProcessErrorCallback& error_callback) {
38  // It's now safe to avoid cleanup in the destructor, as the lifetime of any
39  // associated resources is effectively bound to the |channel| passed to
40  // AddChild() below.
41  DCHECK(!connected_);
42  connected_ = true;
43
44  DCHECK(internal::g_core);
45  internal::g_core->AddChild(process, std::move(connection_params),
46                             process_token_, error_callback);
47}
48
49}  // namespace edk
50}  // namespace mojo
51