1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50

fuchsia_web / shell / shell_relauncher.cc [blame]

// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "fuchsia_web/shell/shell_relauncher.h"

#include <fuchsia/component/cpp/fidl.h>
#include <lib/sys/component/cpp/testing/realm_builder.h>
#include <zircon/types.h>

#include <string>
#include <string_view>
#include <utility>

#include "base/command_line.h"
#include "base/functional/callback.h"
#include "base/run_loop.h"
#include "fuchsia_web/common/test/test_realm_support.h"

std::optional<int> RelaunchForWebInstanceHostIfParent(
    std::string_view relative_component_url,
    const base::CommandLine& command_line) {
  // Nothing to do if running from the context of a relaunched process.
  static constexpr char kNoRelaunch[] = "no-relaunch";
  if (command_line.HasSwitch(kNoRelaunch)) {
    return std::nullopt;
  }

  auto realm_builder = component_testing::RealmBuilder::CreateFromRelativeUrl(
      {relative_component_url.data(), relative_component_url.length()});
  test::AppendCommandLineArgumentsForRealm(realm_builder,  // IN-TEST
                                           command_line);

  auto realm = realm_builder.Build();

  fuchsia::component::BinderPtr binder_proxy =
      realm.component().Connect<fuchsia::component::Binder>();

  // Wait for binder_proxy to be closed.
  base::RunLoop run_loop;
  binder_proxy.set_error_handler(
      [quit_closure = run_loop.QuitClosure()](zx_status_t status) {
        std::move(quit_closure).Run();
      });
  run_loop.Run();

  // Nothing depends on the process exit code of web_engine_shell today, so
  // simply return success in all cases.
  return 0;
}