| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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 <stdint.h> | |
| 6 | |
| 7 #include <memory> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/at_exit.h" | |
| 11 #include "base/base_paths.h" | |
| 12 #include "base/base_switches.h" | |
| 13 #include "base/bind.h" | |
| 14 #include "base/command_line.h" | |
| 15 #include "base/macros.h" | |
| 16 #include "base/memory/weak_ptr.h" | |
| 17 #include "base/message_loop/message_loop.h" | |
| 18 #include "base/path_service.h" | |
| 19 #include "base/process/process.h" | |
| 20 #include "base/threading/thread_task_runner_handle.h" | |
| 21 #include "mojo/edk/embedder/embedder.h" | |
| 22 #include "mojo/edk/embedder/platform_channel_pair.h" | |
| 23 #include "mojo/edk/embedder/scoped_platform_handle.h" | |
| 24 #include "mojo/public/cpp/bindings/binding_set.h" | |
| 25 #include "services/shell/public/cpp/connection.h" | |
| 26 #include "services/shell/public/cpp/connector.h" | |
| 27 #include "services/shell/public/cpp/interface_factory.h" | |
| 28 #include "services/shell/public/cpp/service.h" | |
| 29 #include "services/shell/public/interfaces/connector.mojom.h" | |
| 30 #include "services/shell/public/interfaces/service_manager.mojom.h" | |
| 31 #include "services/shell/runner/child/test_native_main.h" | |
| 32 #include "services/shell/runner/common/client_util.h" | |
| 33 #include "services/shell/runner/common/switches.h" | |
| 34 #include "services/shell/runner/init.h" | |
| 35 #include "services/shell/tests/shell/shell_unittest.mojom.h" | |
| 36 | |
| 37 namespace { | |
| 38 | |
| 39 class Driver : public shell::Service, | |
| 40 public shell::InterfaceFactory<shell::test::mojom::Driver>, | |
| 41 public shell::test::mojom::Driver { | |
| 42 public: | |
| 43 Driver() : weak_factory_(this) {} | |
| 44 ~Driver() override {} | |
| 45 | |
| 46 private: | |
| 47 // shell::Service: | |
| 48 void OnStart(const shell::Identity& identity) override { | |
| 49 base::FilePath target_path; | |
| 50 CHECK(base::PathService::Get(base::DIR_EXE, &target_path)); | |
| 51 #if defined(OS_WIN) | |
| 52 target_path = target_path.Append( | |
| 53 FILE_PATH_LITERAL("shell_unittest_target.exe")); | |
| 54 #else | |
| 55 target_path = target_path.Append( | |
| 56 FILE_PATH_LITERAL("shell_unittest_target")); | |
| 57 #endif | |
| 58 | |
| 59 base::CommandLine child_command_line(target_path); | |
| 60 // Forward the wait-for-debugger flag but nothing else - we don't want to | |
| 61 // stamp on the platform-channel flag. | |
| 62 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 63 switches::kWaitForDebugger)) { | |
| 64 child_command_line.AppendSwitch(switches::kWaitForDebugger); | |
| 65 } | |
| 66 | |
| 67 // Create the channel to be shared with the target process. Pass one end | |
| 68 // on the command line. | |
| 69 mojo::edk::PlatformChannelPair platform_channel_pair; | |
| 70 mojo::edk::HandlePassingInformation handle_passing_info; | |
| 71 platform_channel_pair.PrepareToPassClientHandleToChildProcess( | |
| 72 &child_command_line, &handle_passing_info); | |
| 73 | |
| 74 std::string child_token = mojo::edk::GenerateRandomToken(); | |
| 75 shell::mojom::ServicePtr client = | |
| 76 shell::PassServiceRequestOnCommandLine(&child_command_line, | |
| 77 child_token); | |
| 78 shell::mojom::PIDReceiverPtr receiver; | |
| 79 | |
| 80 shell::Identity target("exe:shell_unittest_target", | |
| 81 shell::mojom::kInheritUserID); | |
| 82 shell::Connector::ConnectParams params(target); | |
| 83 params.set_client_process_connection(std::move(client), | |
| 84 GetProxy(&receiver)); | |
| 85 std::unique_ptr<shell::Connection> connection = | |
| 86 connector()->Connect(¶ms); | |
| 87 connection->AddConnectionCompletedClosure( | |
| 88 base::Bind(&Driver::OnConnectionCompleted, base::Unretained(this))); | |
| 89 | |
| 90 base::LaunchOptions options; | |
| 91 #if defined(OS_WIN) | |
| 92 options.handles_to_inherit = &handle_passing_info; | |
| 93 #elif defined(OS_POSIX) | |
| 94 options.fds_to_remap = &handle_passing_info; | |
| 95 #endif | |
| 96 target_ = base::LaunchProcess(child_command_line, options); | |
| 97 DCHECK(target_.IsValid()); | |
| 98 receiver->SetPID(target_.Pid()); | |
| 99 mojo::edk::ChildProcessLaunched(target_.Handle(), | |
| 100 platform_channel_pair.PassServerHandle(), | |
| 101 child_token); | |
| 102 } | |
| 103 | |
| 104 bool OnConnect(const shell::Identity& remote_identity, | |
| 105 shell::InterfaceRegistry* registry) override { | |
| 106 registry->AddInterface<shell::test::mojom::Driver>(this); | |
| 107 return true; | |
| 108 } | |
| 109 | |
| 110 // shell::InterfaceFactory<Driver>: | |
| 111 void Create(const shell::Identity& remote_identity, | |
| 112 shell::test::mojom::DriverRequest request) override { | |
| 113 bindings_.AddBinding(this, std::move(request)); | |
| 114 } | |
| 115 | |
| 116 // Driver: | |
| 117 void QuitDriver() override { | |
| 118 target_.Terminate(0, false); | |
| 119 base::MessageLoop::current()->QuitWhenIdle(); | |
| 120 } | |
| 121 | |
| 122 void OnConnectionCompleted() {} | |
| 123 | |
| 124 base::Process target_; | |
| 125 mojo::BindingSet<shell::test::mojom::Driver> bindings_; | |
| 126 base::WeakPtrFactory<Driver> weak_factory_; | |
| 127 | |
| 128 DISALLOW_COPY_AND_ASSIGN(Driver); | |
| 129 }; | |
| 130 | |
| 131 } // namespace | |
| 132 | |
| 133 int main(int argc, char** argv) { | |
| 134 base::AtExitManager at_exit; | |
| 135 base::CommandLine::Init(argc, argv); | |
| 136 | |
| 137 shell::InitializeLogging(); | |
| 138 | |
| 139 Driver driver; | |
| 140 return shell::TestNativeMain(&driver); | |
| 141 } | |
| OLD | NEW |