| 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 <utility> | |
| 8 | |
| 9 #include "base/at_exit.h" | |
| 10 #include "base/base_paths.h" | |
| 11 #include "base/base_switches.h" | |
| 12 #include "base/bind.h" | |
| 13 #include "base/command_line.h" | |
| 14 #include "base/macros.h" | |
| 15 #include "base/memory/scoped_ptr.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/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 "mojo/shell/public/cpp/connection.h" | |
| 26 #include "mojo/shell/public/cpp/connector.h" | |
| 27 #include "mojo/shell/public/cpp/interface_factory.h" | |
| 28 #include "mojo/shell/public/cpp/shell_client.h" | |
| 29 #include "mojo/shell/public/interfaces/application_manager.mojom.h" | |
| 30 #include "mojo/shell/public/interfaces/shell.mojom.h" | |
| 31 #include "mojo/shell/runner/child/test_native_main.h" | |
| 32 #include "mojo/shell/runner/common/switches.h" | |
| 33 #include "mojo/shell/runner/init.h" | |
| 34 #include "mojo/shell/tests/application_manager/application_manager_unittest.mojo
m.h" | |
| 35 | |
| 36 namespace { | |
| 37 | |
| 38 class Driver : public mojo::ShellClient, | |
| 39 public mojo::InterfaceFactory<mojo::shell::test::mojom::Driver>, | |
| 40 public mojo::shell::test::mojom::Driver { | |
| 41 public: | |
| 42 Driver() : weak_factory_(this) {} | |
| 43 ~Driver() override {} | |
| 44 | |
| 45 private: | |
| 46 // mojo::ShellClient: | |
| 47 void Initialize(mojo::Connector* connector, const std::string& name, | |
| 48 uint32_t id, uint32_t user_id) 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("application_manager_unittest_target.exe")); | |
| 54 #else | |
| 55 target_path = target_path.Append( | |
| 56 FILE_PATH_LITERAL("application_manager_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 mojo::shell::mojom::PIDReceiverPtr receiver; | |
| 68 mojo::InterfaceRequest<mojo::shell::mojom::PIDReceiver> request = | |
| 69 GetProxy(&receiver); | |
| 70 | |
| 71 // Create the channel to be shared with the target process. Pass one end | |
| 72 // on the command line. | |
| 73 mojo::edk::PlatformChannelPair platform_channel_pair; | |
| 74 mojo::edk::HandlePassingInformation handle_passing_info; | |
| 75 platform_channel_pair.PrepareToPassClientHandleToChildProcess( | |
| 76 &child_command_line, &handle_passing_info); | |
| 77 | |
| 78 // Generate a token for the child to find and connect to a primordial pipe | |
| 79 // and pass that as well. | |
| 80 std::string primordial_pipe_token = mojo::edk::GenerateRandomToken(); | |
| 81 child_command_line.AppendSwitchASCII(switches::kPrimordialPipeToken, | |
| 82 primordial_pipe_token); | |
| 83 | |
| 84 // Allocate the pipe locally. | |
| 85 mojo::ScopedMessagePipeHandle pipe = | |
| 86 mojo::edk::CreateParentMessagePipe(primordial_pipe_token); | |
| 87 | |
| 88 mojo::shell::mojom::CapabilityFilterPtr filter( | |
| 89 mojo::shell::mojom::CapabilityFilter::New()); | |
| 90 mojo::Array<mojo::String> test_interfaces; | |
| 91 test_interfaces.push_back( | |
| 92 mojo::shell::test::mojom::CreateInstanceForHandleTest::Name_); | |
| 93 filter->filter.insert("mojo:application_manager_unittest", | |
| 94 std::move(test_interfaces)); | |
| 95 | |
| 96 mojo::shell::mojom::ShellClientFactoryPtr factory; | |
| 97 factory.Bind(mojo::InterfacePtrInfo<mojo::shell::mojom::ShellClientFactory>( | |
| 98 std::move(pipe), 0u)); | |
| 99 | |
| 100 mojo::shell::mojom::ApplicationManagerPtr application_manager; | |
| 101 connector->ConnectToInterface("mojo:shell", &application_manager); | |
| 102 application_manager->CreateInstanceForFactory( | |
| 103 std::move(factory), "exe:application_manager_unittest_target", | |
| 104 mojo::shell::mojom::Connector::kUserInherit, std::move(filter), | |
| 105 std::move(request)); | |
| 106 | |
| 107 base::LaunchOptions options; | |
| 108 #if defined(OS_WIN) | |
| 109 options.handles_to_inherit = &handle_passing_info; | |
| 110 #elif defined(OS_POSIX) | |
| 111 options.fds_to_remap = &handle_passing_info; | |
| 112 #endif | |
| 113 target_ = base::LaunchProcess(child_command_line, options); | |
| 114 DCHECK(target_.IsValid()); | |
| 115 receiver->SetPID(target_.Pid()); | |
| 116 mojo::edk::ChildProcessLaunched(target_.Handle(), | |
| 117 platform_channel_pair.PassServerHandle()); | |
| 118 } | |
| 119 | |
| 120 bool AcceptConnection(mojo::Connection* connection) override { | |
| 121 connection->AddInterface<mojo::shell::test::mojom::Driver>(this); | |
| 122 return true; | |
| 123 } | |
| 124 | |
| 125 // mojo::InterfaceFactory<Driver>: | |
| 126 void Create(mojo::Connection* connection, | |
| 127 mojo::shell::test::mojom::DriverRequest request) override { | |
| 128 bindings_.AddBinding(this, std::move(request)); | |
| 129 } | |
| 130 | |
| 131 // Driver: | |
| 132 void QuitDriver() override { | |
| 133 target_.Terminate(0, false); | |
| 134 base::MessageLoop::current()->QuitWhenIdle(); | |
| 135 } | |
| 136 | |
| 137 base::Process target_; | |
| 138 mojo::BindingSet<mojo::shell::test::mojom::Driver> bindings_; | |
| 139 base::WeakPtrFactory<Driver> weak_factory_; | |
| 140 | |
| 141 DISALLOW_COPY_AND_ASSIGN(Driver); | |
| 142 }; | |
| 143 | |
| 144 } // namespace | |
| 145 | |
| 146 int main(int argc, char** argv) { | |
| 147 base::AtExitManager at_exit; | |
| 148 base::CommandLine::Init(argc, argv); | |
| 149 | |
| 150 mojo::shell::InitializeLogging(); | |
| 151 | |
| 152 Driver driver; | |
| 153 return mojo::shell::TestNativeMain(&driver); | |
| 154 } | |
| OLD | NEW |