Ken Rockot | b86921c | 2020-05-29 20:35:50 | [diff] [blame] | 1 | // Copyright 2020 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 | |
Maksim Sisov | 864826d | 2020-08-18 20:18:50 | [diff] [blame] | 5 | #include "base/base_switches.h" |
Ken Rockot | 0634566b | 2020-07-10 00:34:26 | [diff] [blame] | 6 | #include "base/cfi_buildflags.h" |
Ken Rockot | b86921c | 2020-05-29 20:35:50 | [diff] [blame] | 7 | #include "base/command_line.h" |
| 8 | #include "base/files/file_util.h" |
| 9 | #include "base/files/scoped_temp_dir.h" |
| 10 | #include "base/process/launch.h" |
| 11 | #include "base/run_loop.h" |
Ken Rockot | 2fc0926 | 2020-06-19 03:21:30 | [diff] [blame] | 12 | #include "base/strings/utf_string_conversions.h" |
Guido Urdaneta | ef4e9194 | 2020-11-09 15:06:24 | [diff] [blame] | 13 | #include "base/test/bind.h" |
Ken Rockot | b86921c | 2020-05-29 20:35:50 | [diff] [blame] | 14 | #include "build/build_config.h" |
Yuta Hijikata | d0a8b6b | 2020-11-20 16:36:15 | [diff] [blame] | 15 | #include "build/chromeos_buildflags.h" |
Ken Rockot | 2fc0926 | 2020-06-19 03:21:30 | [diff] [blame] | 16 | #include "content/public/common/content_switches.h" |
Ken Rockot | b86921c | 2020-05-29 20:35:50 | [diff] [blame] | 17 | #include "content/public/test/browser_test.h" |
| 18 | #include "content/public/test/content_browser_test.h" |
| 19 | #include "content/shell/common/shell_controller.test-mojom.h" |
| 20 | #include "content/shell/common/shell_switches.h" |
| 21 | #include "mojo/public/cpp/bindings/remote.h" |
| 22 | #include "mojo/public/cpp/platform/platform_channel.h" |
| 23 | #include "mojo/public/cpp/system/invitation.h" |
Ken Rockot | b86921c | 2020-05-29 20:35:50 | [diff] [blame] | 24 | #include "testing/gtest/include/gtest/gtest.h" |
| 25 | |
Maksim Sisov | 864826d | 2020-08-18 20:18:50 | [diff] [blame] | 26 | #if defined(USE_OZONE) |
| 27 | #include "ui/ozone/public/ozone_switches.h" |
| 28 | #endif |
| 29 | |
Yuta Hijikata | d0a8b6b | 2020-11-20 16:36:15 | [diff] [blame] | 30 | #if BUILDFLAG(IS_CHROMEOS_ASH) |
Ken Rockot | ef1887e4 | 2020-09-29 18:18:12 | [diff] [blame] | 31 | #include "ui/gl/gl_switches.h" |
| 32 | #endif |
| 33 | |
Ken Rockot | b86921c | 2020-05-29 20:35:50 | [diff] [blame] | 34 | namespace content { |
| 35 | namespace { |
| 36 | |
Xiaohan Wang | 1ecfd00 | 2022-01-19 22:33:10 | [diff] [blame] | 37 | #if BUILDFLAG(IS_WIN) |
Ken Rockot | b86921c | 2020-05-29 20:35:50 | [diff] [blame] | 38 | const char kShellExecutableName[] = "content_shell.exe"; |
| 39 | #else |
| 40 | const char kShellExecutableName[] = "content_shell"; |
Ken Rockot | 2fc0926 | 2020-06-19 03:21:30 | [diff] [blame] | 41 | const char kMojoCoreLibraryName[] = "libmojo_core.so"; |
Ken Rockot | b86921c | 2020-05-29 20:35:50 | [diff] [blame] | 42 | #endif |
| 43 | |
| 44 | base::FilePath GetCurrentDirectory() { |
| 45 | base::FilePath current_directory; |
| 46 | CHECK(base::GetCurrentDirectory(¤t_directory)); |
| 47 | return current_directory; |
| 48 | } |
| 49 | |
| 50 | class LaunchAsMojoClientBrowserTest : public ContentBrowserTest { |
| 51 | public: |
| 52 | LaunchAsMojoClientBrowserTest() { CHECK(temp_dir_.CreateUniqueTempDir()); } |
| 53 | |
| 54 | ~LaunchAsMojoClientBrowserTest() override { |
| 55 | // Ensure that the launched Content Shell process is dead before the test |
| 56 | // tears down, otherwise the temp profile dir may fail to delete. Note that |
| 57 | // tests must explicitly request shutdown through ShellController before |
| 58 | // finishing, otherwise this will time out. |
| 59 | CHECK(content_shell_process_.WaitForExit(nullptr)); |
| 60 | CHECK(temp_dir_.Delete()); |
| 61 | } |
| 62 | |
| 63 | base::CommandLine MakeShellCommandLine() { |
| 64 | base::CommandLine command_line( |
| 65 | GetFilePathNextToCurrentExecutable(kShellExecutableName)); |
| 66 | command_line.AppendSwitchPath(switches::kContentShellDataPath, |
| 67 | temp_dir_.GetPath()); |
Maksim Sisov | 2ac2c31 | 2021-11-03 08:00:12 | [diff] [blame] | 68 | #if defined(USE_OZONE) |
Maksim Sisov | 864826d | 2020-08-18 20:18:50 | [diff] [blame] | 69 | const base::CommandLine& cmdline = *base::CommandLine::ForCurrentProcess(); |
Maksim Sisov | 2ac2c31 | 2021-11-03 08:00:12 | [diff] [blame] | 70 | const char* kSwitchesToCopy[] = { |
| 71 | // Keep the kOzonePlatform switch that the Ozone must use. |
| 72 | switches::kOzonePlatform, |
| 73 | }; |
Maksim Sisov | 864826d | 2020-08-18 20:18:50 | [diff] [blame] | 74 | command_line.CopySwitchesFrom(cmdline, kSwitchesToCopy, |
Daniel Cheng | ad44af2f | 2022-02-26 18:07:54 | [diff] [blame] | 75 | std::size(kSwitchesToCopy)); |
Maksim Sisov | 2ac2c31 | 2021-11-03 08:00:12 | [diff] [blame] | 76 | #endif |
Ken Rockot | ef1887e4 | 2020-09-29 18:18:12 | [diff] [blame] | 77 | |
Yuta Hijikata | d0a8b6b | 2020-11-20 16:36:15 | [diff] [blame] | 78 | #if BUILDFLAG(IS_CHROMEOS_ASH) |
Alexis Hetu | bda66cce | 2021-04-08 13:04:56 | [diff] [blame] | 79 | command_line.AppendSwitchASCII(switches::kUseGL, |
Alexis Hetu | 8a3885d | 2022-01-17 16:16:15 | [diff] [blame] | 80 | gl::kGLImplementationANGLEName); |
| 81 | command_line.AppendSwitchASCII(switches::kUseANGLE, |
| 82 | gl::kANGLEImplementationSwiftShaderName); |
Ken Rockot | ef1887e4 | 2020-09-29 18:18:12 | [diff] [blame] | 83 | #endif |
Ken Rockot | b86921c | 2020-05-29 20:35:50 | [diff] [blame] | 84 | return command_line; |
| 85 | } |
| 86 | |
| 87 | mojo::Remote<mojom::ShellController> LaunchContentShell( |
| 88 | const base::CommandLine& command_line) { |
| 89 | mojo::PlatformChannel channel; |
| 90 | base::LaunchOptions options; |
| 91 | base::CommandLine shell_command_line(command_line); |
| 92 | channel.PrepareToPassRemoteEndpoint(&options, &shell_command_line); |
| 93 | content_shell_process_ = base::LaunchProcess(shell_command_line, options); |
| 94 | channel.RemoteProcessLaunchAttempted(); |
| 95 | |
| 96 | mojo::OutgoingInvitation invitation; |
Hidehiko Abe | 23e45e8 | 2020-08-25 08:41:48 | [diff] [blame] | 97 | mojo::Remote<mojom::ShellController> controller( |
| 98 | mojo::PendingRemote<mojom::ShellController>( |
Ken Rockot | b86921c | 2020-05-29 20:35:50 | [diff] [blame] | 99 | invitation.AttachMessagePipe(0), /*version=*/0)); |
| 100 | mojo::OutgoingInvitation::Send(std::move(invitation), |
| 101 | content_shell_process_.Handle(), |
| 102 | channel.TakeLocalEndpoint()); |
Ken Rockot | b86921c | 2020-05-29 20:35:50 | [diff] [blame] | 103 | return controller; |
| 104 | } |
| 105 | |
Xiaohan Wang | 1ecfd00 | 2022-01-19 22:33:10 | [diff] [blame] | 106 | #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) |
Ken Rockot | 2fc0926 | 2020-06-19 03:21:30 | [diff] [blame] | 107 | base::FilePath GetMojoCoreLibraryPath() { |
| 108 | return GetFilePathNextToCurrentExecutable(kMojoCoreLibraryName); |
| 109 | } |
| 110 | #endif |
| 111 | |
Ken Rockot | b86921c | 2020-05-29 20:35:50 | [diff] [blame] | 112 | private: |
| 113 | base::FilePath GetFilePathNextToCurrentExecutable( |
| 114 | const std::string& filename) { |
| 115 | base::FilePath executable_dir = |
| 116 | base::CommandLine::ForCurrentProcess()->GetProgram().DirName(); |
| 117 | if (executable_dir.IsAbsolute()) |
| 118 | return executable_dir.AppendASCII(filename); |
| 119 | |
| 120 | // If the current executable path is relative, resolve it to an absolute |
| 121 | // path before swapping in |filename|. This ensures that the path is OK to |
| 122 | // use with base::LaunchProcess. Otherwise we could end up with a path |
| 123 | // containing only |filename|, and this can fail to execute in environments |
| 124 | // where "." is not in the PATH (common on e.g. Linux). |
| 125 | return current_directory_.Append(executable_dir).AppendASCII(filename); |
| 126 | } |
| 127 | |
| 128 | base::ScopedTempDir temp_dir_; |
| 129 | const base::FilePath current_directory_ = GetCurrentDirectory(); |
| 130 | base::Process content_shell_process_; |
| 131 | mojo::Remote<mojom::ShellController> shell_controller_; |
| 132 | }; |
| 133 | |
| 134 | IN_PROC_BROWSER_TEST_F(LaunchAsMojoClientBrowserTest, LaunchAndBindInterface) { |
| 135 | // Verifies that we can launch an instance of Content Shell with a Mojo |
| 136 | // invitation on the command line and reach the new browser process's exposed |
| 137 | // ShellController interface. |
| 138 | |
| 139 | const char kExtraSwitchName[] = "extra-switch-for-testing"; |
| 140 | const char kExtraSwitchValue[] = "42"; |
| 141 | |
| 142 | base::CommandLine command_line = MakeShellCommandLine(); |
| 143 | command_line.AppendSwitchASCII(kExtraSwitchName, kExtraSwitchValue); |
| 144 | mojo::Remote<mojom::ShellController> shell_controller = |
| 145 | LaunchContentShell(command_line); |
| 146 | |
| 147 | base::RunLoop loop; |
| 148 | shell_controller->GetSwitchValue( |
| 149 | kExtraSwitchName, |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 150 | base::BindLambdaForTesting([&](const absl::optional<std::string>& value) { |
Ken Rockot | b86921c | 2020-05-29 20:35:50 | [diff] [blame] | 151 | ASSERT_TRUE(value); |
| 152 | EXPECT_EQ(kExtraSwitchValue, *value); |
| 153 | loop.Quit(); |
| 154 | })); |
| 155 | loop.Run(); |
| 156 | |
| 157 | shell_controller->ShutDown(); |
| 158 | } |
| 159 | |
Xiaohan Wang | 1ecfd00 | 2022-01-19 22:33:10 | [diff] [blame] | 160 | #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) |
Ken Rockot | b66b0b1 | 2021-12-09 02:53:15 | [diff] [blame] | 161 | // TODO(crbug.com/1259557): This test implementation fundamentally conflicts |
| 162 | // with a fix for the linked bug because it causes a browser process to behave |
| 163 | // partially as a broker and partially as a non-broker. This can be re-enabled |
| 164 | // when we migrate away from the current Mojo implementation. It's OK to disable |
| 165 | // for now because no production code relies on this feature. |
Ken Rockot | 0634566b | 2020-07-10 00:34:26 | [diff] [blame] | 166 | IN_PROC_BROWSER_TEST_F(LaunchAsMojoClientBrowserTest, |
Ken Rockot | b66b0b1 | 2021-12-09 02:53:15 | [diff] [blame] | 167 | DISABLED_WithMojoCoreLibrary) { |
Ken Rockot | 2fc0926 | 2020-06-19 03:21:30 | [diff] [blame] | 168 | // Instructs a newly launched Content Shell browser to initialize Mojo Core |
| 169 | // dynamically from a shared library, rather than using the version linked |
| 170 | // into the Content Shell binary. |
| 171 | // |
| 172 | // This exercises end-to-end JS in order to cover real IPC behavior between |
| 173 | // the browser and a renderer. |
| 174 | |
| 175 | base::CommandLine command_line = MakeShellCommandLine(); |
| 176 | command_line.AppendSwitchPath(switches::kMojoCoreLibraryPath, |
| 177 | GetMojoCoreLibraryPath()); |
| 178 | mojo::Remote<mojom::ShellController> shell_controller = |
| 179 | LaunchContentShell(command_line); |
| 180 | |
| 181 | // Indisputable proof that we're evaluating JavaScript. |
| 182 | const std::string kExpressionToEvaluate = "'ba'+ +'a'+'as'"; |
| 183 | const base::Value kExpectedValue("baNaNas"); |
| 184 | |
| 185 | base::RunLoop loop; |
| 186 | shell_controller->ExecuteJavaScript( |
| 187 | base::ASCIIToUTF16(kExpressionToEvaluate), |
| 188 | base::BindLambdaForTesting([&](base::Value value) { |
| 189 | EXPECT_EQ(kExpectedValue, value); |
| 190 | loop.Quit(); |
| 191 | })); |
| 192 | loop.Run(); |
| 193 | |
| 194 | shell_controller->ShutDown(); |
| 195 | } |
Xiaohan Wang | 1ecfd00 | 2022-01-19 22:33:10 | [diff] [blame] | 196 | #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) |
Ken Rockot | 2fc0926 | 2020-06-19 03:21:30 | [diff] [blame] | 197 | |
Ken Rockot | b86921c | 2020-05-29 20:35:50 | [diff] [blame] | 198 | } // namespace |
| 199 | } // namespace content |