Upload upstream chromium 114.0.5735.31
[platform/framework/web/chromium-efl.git] / fuchsia_web / shell / shell_relauncher.cc
1 // Copyright 2023 The Chromium Authors
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 "fuchsia_web/shell/shell_relauncher.h"
6
7 #include <fuchsia/component/cpp/fidl.h>
8 #include <lib/sys/component/cpp/testing/realm_builder.h>
9 #include <zircon/types.h>
10
11 #include <string>
12 #include <utility>
13
14 #include "base/command_line.h"
15 #include "base/functional/callback.h"
16 #include "base/run_loop.h"
17 #include "fuchsia_web/common/test/test_realm_support.h"
18
19 absl::optional<int> RelaunchForWebInstanceHostIfParent(
20     base::StringPiece relative_component_url,
21     const base::CommandLine& command_line) {
22   // Nothing to do if running from the context of a relaunched process.
23   static constexpr char kNoRelaunch[] = "no-relaunch";
24   if (command_line.HasSwitch(kNoRelaunch)) {
25     return absl::nullopt;
26   }
27
28   auto realm_builder = component_testing::RealmBuilder::CreateFromRelativeUrl(
29       {relative_component_url.data(), relative_component_url.length()});
30   test::AppendCommandLineArgumentsForRealm(realm_builder,  // IN-TEST
31                                            command_line);
32
33   auto realm = realm_builder.Build();
34
35   fuchsia::component::BinderPtr binder_proxy =
36       realm.component().Connect<fuchsia::component::Binder>();
37
38   // Wait for binder_proxy to be closed.
39   base::RunLoop run_loop;
40   binder_proxy.set_error_handler(
41       [quit_closure = run_loop.QuitClosure()](zx_status_t status) {
42         std::move(quit_closure).Run();
43       });
44   run_loop.Run();
45
46   // Nothing depends on the process exit code of web_engine_shell today, so
47   // simply return success in all cases.
48   return 0;
49 }