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/converters/network/network_type_converters.h" | |
22 #include "mojo/edk/embedder/embedder.h" | |
23 #include "mojo/edk/embedder/platform_channel_pair.h" | |
24 #include "mojo/edk/embedder/scoped_platform_handle.h" | |
25 #include "mojo/public/cpp/bindings/weak_binding_set.h" | |
26 #include "mojo/shell/application_manager_apptests.mojom.h" | |
27 #include "mojo/shell/public/cpp/connection.h" | |
28 #include "mojo/shell/public/cpp/interface_factory.h" | |
29 #include "mojo/shell/public/cpp/shell.h" | |
30 #include "mojo/shell/public/cpp/shell_client.h" | |
31 #include "mojo/shell/public/interfaces/application_manager.mojom.h" | |
32 #include "mojo/shell/runner/child/test_native_main.h" | |
33 #include "mojo/shell/runner/common/switches.h" | |
34 #include "mojo/shell/runner/init.h" | |
35 | |
36 using mojo::shell::test::mojom::CreateInstanceForHandleTestPtr; | |
37 using mojo::shell::test::mojom::Driver; | |
38 | |
39 namespace { | |
40 | |
41 class TargetApplicationDelegate : public mojo::ShellClient, | |
42 public mojo::InterfaceFactory<Driver>, | |
43 public Driver { | |
44 public: | |
45 TargetApplicationDelegate() : shell_(nullptr), weak_factory_(this) {} | |
46 ~TargetApplicationDelegate() override {} | |
47 | |
48 private: | |
49 // mojo::ShellClient: | |
50 void Initialize(mojo::Shell* shell, const std::string& url, | |
51 uint32_t id) override { | |
52 shell_ = shell; | |
53 | |
54 base::FilePath target_path; | |
55 CHECK(base::PathService::Get(base::DIR_EXE, &target_path)); | |
56 #if defined(OS_WIN) | |
57 target_path = target_path.Append( | |
58 FILE_PATH_LITERAL("application_manager_apptest_target.exe")); | |
59 #else | |
60 target_path = target_path.Append( | |
61 FILE_PATH_LITERAL("application_manager_apptest_target")); | |
62 #endif | |
63 | |
64 base::CommandLine child_command_line(target_path); | |
65 // Forward the wait-for-debugger flag but nothing else - we don't want to | |
66 // stamp on the platform-channel flag. | |
67 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
68 switches::kWaitForDebugger)) { | |
69 child_command_line.AppendSwitch(switches::kWaitForDebugger); | |
70 } | |
71 | |
72 mojo::shell::mojom::PIDReceiverPtr receiver; | |
73 mojo::InterfaceRequest<mojo::shell::mojom::PIDReceiver> request = | |
74 GetProxy(&receiver); | |
75 | |
76 // Create the channel to be shared with the target process. Pass one end | |
77 // on the command line. | |
78 mojo::edk::PlatformChannelPair platform_channel_pair; | |
79 mojo::edk::HandlePassingInformation handle_passing_info; | |
80 platform_channel_pair.PrepareToPassClientHandleToChildProcess( | |
81 &child_command_line, &handle_passing_info); | |
82 | |
83 // Generate a token for the child to find and connect to a primordial pipe | |
84 // and pass that as well. | |
85 std::string primordial_pipe_token = mojo::edk::GenerateRandomToken(); | |
86 child_command_line.AppendSwitchASCII(switches::kPrimordialPipeToken, | |
87 primordial_pipe_token); | |
88 | |
89 // Allocate the pipe locally. | |
90 mojo::ScopedMessagePipeHandle pipe = | |
91 mojo::edk::CreateParentMessagePipe(primordial_pipe_token); | |
92 | |
93 mojo::shell::mojom::CapabilityFilterPtr filter( | |
94 mojo::shell::mojom::CapabilityFilter::New()); | |
95 mojo::Array<mojo::String> test_interfaces; | |
96 test_interfaces.push_back( | |
97 mojo::shell::test::mojom::CreateInstanceForHandleTest::Name_); | |
98 filter->filter.insert("mojo:mojo_shell_apptests", | |
99 std::move(test_interfaces)); | |
100 | |
101 mojo::shell::mojom::ApplicationManagerPtr application_manager; | |
102 shell_->ConnectToInterface("mojo:shell", &application_manager); | |
103 application_manager->CreateInstanceForHandle( | |
104 mojo::ScopedHandle(mojo::Handle(pipe.release().value())), | |
105 "exe:application_manager_apptest_target", std::move(filter), | |
106 std::move(request)); | |
107 | |
108 base::LaunchOptions options; | |
109 #if defined(OS_WIN) | |
110 options.handles_to_inherit = &handle_passing_info; | |
111 #elif defined(OS_POSIX) | |
112 options.fds_to_remap = &handle_passing_info; | |
113 #endif | |
114 target_ = base::LaunchProcess(child_command_line, options); | |
115 DCHECK(target_.IsValid()); | |
116 receiver->SetPID(target_.Pid()); | |
117 mojo::edk::ChildProcessLaunched(target_.Handle(), | |
118 platform_channel_pair.PassServerHandle()); | |
119 } | |
120 | |
121 bool AcceptConnection(mojo::Connection* connection) override { | |
122 connection->AddInterface<Driver>(this); | |
123 return true; | |
124 } | |
125 | |
126 // mojo::InterfaceFactory<Driver>: | |
127 void Create(mojo::Connection* connection, | |
128 mojo::InterfaceRequest<Driver> request) override { | |
129 bindings_.AddBinding(this, std::move(request)); | |
130 } | |
131 | |
132 // Driver: | |
133 void QuitDriver() override { | |
134 target_.Terminate(0, false); | |
135 shell_->Quit(); | |
136 } | |
137 | |
138 mojo::Shell* shell_; | |
139 base::Process target_; | |
140 mojo::WeakBindingSet<Driver> bindings_; | |
141 base::WeakPtrFactory<TargetApplicationDelegate> weak_factory_; | |
142 | |
143 DISALLOW_COPY_AND_ASSIGN(TargetApplicationDelegate); | |
144 }; | |
145 | |
146 } // namespace | |
147 | |
148 int main(int argc, char** argv) { | |
149 base::AtExitManager at_exit; | |
150 base::CommandLine::Init(argc, argv); | |
151 | |
152 mojo::shell::InitializeLogging(); | |
153 | |
154 TargetApplicationDelegate delegate; | |
155 return mojo::shell::TestNativeMain(&delegate); | |
156 } | |
OLD | NEW |