Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(251)

Side by Side Diff: chrome/test/base/mojo_test_connector.cc

Issue 1882423004: Move shell service to toplevel shell namespace (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/test/base/mojo_test_connector.h" 5 #include "chrome/test/base/mojo_test_connector.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/callback.h" 8 #include "base/callback.h"
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
(...skipping 11 matching lines...) Expand all
22 #include "services/shell/background/tests/test_catalog_store.h" 22 #include "services/shell/background/tests/test_catalog_store.h"
23 #include "services/shell/native_runner_delegate.h" 23 #include "services/shell/native_runner_delegate.h"
24 #include "services/shell/public/cpp/connector.h" 24 #include "services/shell/public/cpp/connector.h"
25 #include "services/shell/public/cpp/shell_client.h" 25 #include "services/shell/public/cpp/shell_client.h"
26 #include "services/shell/public/cpp/shell_connection.h" 26 #include "services/shell/public/cpp/shell_connection.h"
27 #include "services/shell/runner/common/client_util.h" 27 #include "services/shell/runner/common/client_util.h"
28 #include "services/shell/runner/common/switches.h" 28 #include "services/shell/runner/common/switches.h"
29 #include "services/shell/shell.h" 29 #include "services/shell/shell.h"
30 #include "services/shell/switches.h" 30 #include "services/shell/switches.h"
31 31
32 using mojo::shell::mojom::ShellClient; 32 using shell::mojom::ShellClient;
33 using mojo::shell::mojom::ShellClientPtr; 33 using shell::mojom::ShellClientPtr;
34 34
35 namespace { 35 namespace {
36 36
37 const char kTestRunnerName[] = "mojo:test-runner"; 37 const char kTestRunnerName[] = "mojo:test-runner";
38 const char kTestName[] = "exe:chrome"; 38 const char kTestName[] = "exe:chrome";
39 39
40 // Returns the Dictionary value of |parent| under the specified key, creating 40 // Returns the Dictionary value of |parent| under the specified key, creating
41 // and adding as necessary. 41 // and adding as necessary.
42 base::DictionaryValue* EnsureDictionary(base::DictionaryValue* parent, 42 base::DictionaryValue* EnsureDictionary(base::DictionaryValue* parent,
43 const char* key) { 43 const char* key) {
44 base::DictionaryValue* dictionary = nullptr; 44 base::DictionaryValue* dictionary = nullptr;
45 if (parent->GetDictionary(key, &dictionary)) 45 if (parent->GetDictionary(key, &dictionary))
46 return dictionary; 46 return dictionary;
47 47
48 std::unique_ptr<base::DictionaryValue> owned_dictionary( 48 std::unique_ptr<base::DictionaryValue> owned_dictionary(
49 new base::DictionaryValue); 49 new base::DictionaryValue);
50 dictionary = owned_dictionary.get(); 50 dictionary = owned_dictionary.get();
51 parent->Set(key, std::move(owned_dictionary)); 51 parent->Set(key, std::move(owned_dictionary));
52 return dictionary; 52 return dictionary;
53 } 53 }
54 54
55 // This builds a permissive catalog with the addition of the 'instance_name' 55 // This builds a permissive catalog with the addition of the 'instance_name'
56 // permission. 56 // permission.
57 std::unique_ptr<mojo::shell::TestCatalogStore> BuildTestCatalogStore() { 57 std::unique_ptr<shell::TestCatalogStore> BuildTestCatalogStore() {
58 std::unique_ptr<base::ListValue> apps(new base::ListValue); 58 std::unique_ptr<base::ListValue> apps(new base::ListValue);
59 std::unique_ptr<base::DictionaryValue> test_app_config = 59 std::unique_ptr<base::DictionaryValue> test_app_config =
60 mojo::shell::BuildPermissiveSerializedAppInfo(kTestRunnerName, "test"); 60 shell::BuildPermissiveSerializedAppInfo(kTestRunnerName, "test");
61 base::DictionaryValue* capabilities = 61 base::DictionaryValue* capabilities =
62 EnsureDictionary(test_app_config.get(), catalog::Store::kCapabilitiesKey); 62 EnsureDictionary(test_app_config.get(), catalog::Store::kCapabilitiesKey);
63 base::DictionaryValue* required_capabilities = 63 base::DictionaryValue* required_capabilities =
64 EnsureDictionary(capabilities, catalog::Store::kCapabilities_RequiredKey); 64 EnsureDictionary(capabilities, catalog::Store::kCapabilities_RequiredKey);
65 std::unique_ptr<base::ListValue> required_shell_classes(new base::ListValue); 65 std::unique_ptr<base::ListValue> required_shell_classes(new base::ListValue);
66 required_shell_classes->AppendString("instance_name"); 66 required_shell_classes->AppendString("instance_name");
67 required_shell_classes->AppendString("client_process"); 67 required_shell_classes->AppendString("client_process");
68 std::unique_ptr<base::DictionaryValue> shell_caps(new base::DictionaryValue); 68 std::unique_ptr<base::DictionaryValue> shell_caps(new base::DictionaryValue);
69 shell_caps->Set(catalog::Store::kCapabilities_ClassesKey, 69 shell_caps->Set(catalog::Store::kCapabilities_ClassesKey,
70 std::move(required_shell_classes)); 70 std::move(required_shell_classes));
71 required_capabilities->Set("mojo:shell", std::move(shell_caps)); 71 required_capabilities->Set("mojo:shell", std::move(shell_caps));
72 apps->Append(std::move(test_app_config)); 72 apps->Append(std::move(test_app_config));
73 return base::WrapUnique(new mojo::shell::TestCatalogStore(std::move(apps))); 73 return base::WrapUnique(new shell::TestCatalogStore(std::move(apps)));
74 } 74 }
75 75
76 // BackgroundTestState maintains all the state necessary to bind the test to 76 // BackgroundTestState maintains all the state necessary to bind the test to
77 // mojo. This class is only used on the thread created by BackgroundShell. 77 // mojo. This class is only used on the thread created by BackgroundShell.
78 class BackgroundTestState { 78 class BackgroundTestState {
79 public: 79 public:
80 BackgroundTestState() {} 80 BackgroundTestState() {}
81 ~BackgroundTestState() {} 81 ~BackgroundTestState() {}
82 82
83 // Prepares the command line and other setup for connecting the test to mojo. 83 // Prepares the command line and other setup for connecting the test to mojo.
84 // Must be paired with a clal to ChildProcessLaunched(). 84 // Must be paired with a clal to ChildProcessLaunched().
85 void Connect(base::CommandLine* command_line, 85 void Connect(base::CommandLine* command_line,
86 mojo::shell::Shell* shell, 86 shell::Shell* shell,
87 const std::string& instance, 87 const std::string& instance,
88 base::TestLauncher::LaunchOptions* test_launch_options) { 88 base::TestLauncher::LaunchOptions* test_launch_options) {
89 command_line->AppendSwitch(MojoTestConnector::kTestSwitch); 89 command_line->AppendSwitch(MojoTestConnector::kTestSwitch);
90 command_line->AppendSwitch(switches::kWaitForMojoShell); 90 command_line->AppendSwitch(switches::kWaitForMojoShell);
91 command_line->AppendSwitch(switches::kChildProcess); 91 command_line->AppendSwitch(switches::kChildProcess);
92 mojo_ipc_channel_.reset(new mojo::edk::PlatformChannelPair); 92 mojo_ipc_channel_.reset(new mojo::edk::PlatformChannelPair);
93 mojo_ipc_channel_->PrepareToPassClientHandleToChildProcess( 93 mojo_ipc_channel_->PrepareToPassClientHandleToChildProcess(
94 command_line, &handle_passing_info_); 94 command_line, &handle_passing_info_);
95 #if defined(OS_WIN) 95 #if defined(OS_WIN)
96 test_launch_options->inherit_handles = true; 96 test_launch_options->inherit_handles = true;
97 test_launch_options->handles_to_inherit = &handle_passing_info_; 97 test_launch_options->handles_to_inherit = &handle_passing_info_;
98 #if defined(OFFICIAL_BUILD) 98 #if defined(OFFICIAL_BUILD)
99 CHECK(false) << "Launching mojo process with inherit_handles is insecure!"; 99 CHECK(false) << "Launching mojo process with inherit_handles is insecure!";
100 #endif 100 #endif
101 #elif defined(OS_POSIX) 101 #elif defined(OS_POSIX)
102 test_launch_options->fds_to_remap = &handle_passing_info_; 102 test_launch_options->fds_to_remap = &handle_passing_info_;
103 #else 103 #else
104 #error "Unsupported" 104 #error "Unsupported"
105 #endif 105 #endif
106 mojo::shell::mojom::ShellClientPtr client = 106 shell::mojom::ShellClientPtr client =
107 mojo::shell::PassShellClientRequestOnCommandLine(command_line); 107 shell::PassShellClientRequestOnCommandLine(command_line);
108 108
109 std::unique_ptr<mojo::shell::ConnectParams> params( 109 std::unique_ptr<shell::ConnectParams> params(new shell::ConnectParams);
110 new mojo::shell::ConnectParams); 110 params->set_source(shell::CreateShellIdentity());
111 params->set_source(mojo::shell::CreateShellIdentity());
112 params->set_target( 111 params->set_target(
113 mojo::Identity(kTestName, mojo::shell::mojom::kRootUserID, instance)); 112 shell::Identity(kTestName, shell::mojom::kRootUserID, instance));
114 113
115 mojo::shell::mojom::ClientProcessConnectionPtr client_process_connection = 114 shell::mojom::ClientProcessConnectionPtr client_process_connection =
116 mojo::shell::mojom::ClientProcessConnection::New(); 115 shell::mojom::ClientProcessConnection::New();
117 client_process_connection->shell_client = 116 client_process_connection->shell_client =
118 client.PassInterface().PassHandle(); 117 client.PassInterface().PassHandle();
119 client_process_connection->pid_receiver_request = 118 client_process_connection->pid_receiver_request =
120 mojo::GetProxy(&pid_receiver_).PassMessagePipe(); 119 mojo::GetProxy(&pid_receiver_).PassMessagePipe();
121 params->set_client_process_connection(std::move(client_process_connection)); 120 params->set_client_process_connection(std::move(client_process_connection));
122 shell->Connect(std::move(params)); 121 shell->Connect(std::move(params));
123 } 122 }
124 123
125 // Called after the test process has launched. Completes the registration done 124 // Called after the test process has launched. Completes the registration done
126 // in Connect(). 125 // in Connect().
127 void ChildProcessLaunched(base::ProcessHandle handle, base::ProcessId pid) { 126 void ChildProcessLaunched(base::ProcessHandle handle, base::ProcessId pid) {
128 pid_receiver_->SetPID(pid); 127 pid_receiver_->SetPID(pid);
129 mojo_ipc_channel_->ChildProcessLaunched(); 128 mojo_ipc_channel_->ChildProcessLaunched();
130 mojo::edk::ChildProcessLaunched( 129 mojo::edk::ChildProcessLaunched(
131 handle, mojo::edk::ScopedPlatformHandle(mojo::edk::PlatformHandle( 130 handle, mojo::edk::ScopedPlatformHandle(mojo::edk::PlatformHandle(
132 mojo_ipc_channel_->PassServerHandle().release().handle))); 131 mojo_ipc_channel_->PassServerHandle().release().handle)));
133 } 132 }
134 133
135 private: 134 private:
136 // Used to back the NodeChannel between the parent and child node. 135 // Used to back the NodeChannel between the parent and child node.
137 std::unique_ptr<mojo::edk::PlatformChannelPair> mojo_ipc_channel_; 136 std::unique_ptr<mojo::edk::PlatformChannelPair> mojo_ipc_channel_;
138 137
139 mojo::edk::HandlePassingInformation handle_passing_info_; 138 mojo::edk::HandlePassingInformation handle_passing_info_;
140 139
141 mojo::shell::mojom::PIDReceiverPtr pid_receiver_; 140 shell::mojom::PIDReceiverPtr pid_receiver_;
142 141
143 DISALLOW_COPY_AND_ASSIGN(BackgroundTestState); 142 DISALLOW_COPY_AND_ASSIGN(BackgroundTestState);
144 }; 143 };
145 144
146 // Called used destroy BackgroundTestState on the background thread. 145 // Called used destroy BackgroundTestState on the background thread.
147 void DestroyBackgroundStateOnBackgroundThread( 146 void DestroyBackgroundStateOnBackgroundThread(
148 std::unique_ptr<BackgroundTestState> state, 147 std::unique_ptr<BackgroundTestState> state,
149 mojo::shell::Shell* shell) {} 148 shell::Shell* shell) {}
150 149
151 // State created per test. Manages creation of the corresponding 150 // State created per test. Manages creation of the corresponding
152 // BackgroundTestState and making sure processing runs on the right threads. 151 // BackgroundTestState and making sure processing runs on the right threads.
153 class MojoTestState : public content::TestState { 152 class MojoTestState : public content::TestState {
154 public: 153 public:
155 explicit MojoTestState(mojo::shell::BackgroundShell* background_shell) 154 explicit MojoTestState(shell::BackgroundShell* background_shell)
156 : background_shell_(background_shell) {} 155 : background_shell_(background_shell) {}
157 156
158 ~MojoTestState() override { 157 ~MojoTestState() override {
159 DCHECK(background_state_); 158 DCHECK(background_state_);
160 // BackgroundState needs to be destroyed on the background thread. We're 159 // BackgroundState needs to be destroyed on the background thread. We're
161 // guaranteed |background_shell_| has been created by the time we reach 160 // guaranteed |background_shell_| has been created by the time we reach
162 // here as Init() blocks until |background_shell_| has been created. 161 // here as Init() blocks until |background_shell_| has been created.
163 background_shell_->ExecuteOnShellThread( 162 background_shell_->ExecuteOnShellThread(
164 base::Bind(&DestroyBackgroundStateOnBackgroundThread, 163 base::Bind(&DestroyBackgroundStateOnBackgroundThread,
165 base::Passed(&background_state_))); 164 base::Passed(&background_state_)));
(...skipping 19 matching lines...) Expand all
185 base::WaitableEvent signal(true, false); 184 base::WaitableEvent signal(true, false);
186 background_shell_->ExecuteOnShellThread( 185 background_shell_->ExecuteOnShellThread(
187 base::Bind(&MojoTestState::ChildProcessLaunchedOnBackgroundThread, 186 base::Bind(&MojoTestState::ChildProcessLaunchedOnBackgroundThread,
188 base::Unretained(this), handle, pid, &signal)); 187 base::Unretained(this), handle, pid, &signal));
189 signal.Wait(); 188 signal.Wait();
190 } 189 }
191 190
192 void ChildProcessLaunchedOnBackgroundThread(base::ProcessHandle handle, 191 void ChildProcessLaunchedOnBackgroundThread(base::ProcessHandle handle,
193 base::ProcessId pid, 192 base::ProcessId pid,
194 base::WaitableEvent* signal, 193 base::WaitableEvent* signal,
195 mojo::shell::Shell* shell) { 194 shell::Shell* shell) {
196 background_state_->ChildProcessLaunched(handle, pid); 195 background_state_->ChildProcessLaunched(handle, pid);
197 signal->Signal(); 196 signal->Signal();
198 } 197 }
199 198
200 void BindOnBackgroundThread( 199 void BindOnBackgroundThread(
201 base::WaitableEvent* signal, 200 base::WaitableEvent* signal,
202 base::CommandLine* command_line, 201 base::CommandLine* command_line,
203 base::TestLauncher::LaunchOptions* test_launch_options, 202 base::TestLauncher::LaunchOptions* test_launch_options,
204 mojo::shell::Shell* shell) { 203 shell::Shell* shell) {
205 static int instance_id = 0; 204 static int instance_id = 0;
206 const std::string instance_name = 205 const std::string instance_name =
207 "instance-" + base::IntToString(instance_id++); 206 "instance-" + base::IntToString(instance_id++);
208 background_state_.reset(new BackgroundTestState); 207 background_state_.reset(new BackgroundTestState);
209 background_state_->Connect(command_line, shell, instance_name, 208 background_state_->Connect(command_line, shell, instance_name,
210 test_launch_options); 209 test_launch_options);
211 signal->Signal(); 210 signal->Signal();
212 } 211 }
213 212
214 mojo::shell::BackgroundShell* background_shell_; 213 shell::BackgroundShell* background_shell_;
215 std::unique_ptr<BackgroundTestState> background_state_; 214 std::unique_ptr<BackgroundTestState> background_state_;
216 215
217 DISALLOW_COPY_AND_ASSIGN(MojoTestState); 216 DISALLOW_COPY_AND_ASSIGN(MojoTestState);
218 }; 217 };
219 218
220 } // namespace 219 } // namespace
221 220
222 class MojoTestConnector::NativeRunnerDelegateImpl 221 class MojoTestConnector::NativeRunnerDelegateImpl
223 : public mojo::shell::NativeRunnerDelegate { 222 : public shell::NativeRunnerDelegate {
224 public: 223 public:
225 NativeRunnerDelegateImpl() {} 224 NativeRunnerDelegateImpl() {}
226 ~NativeRunnerDelegateImpl() override {} 225 ~NativeRunnerDelegateImpl() override {}
227 226
228 private: 227 private:
229 // mojo::shell::NativeRunnerDelegate: 228 // shell::NativeRunnerDelegate:
230 void AdjustCommandLineArgumentsForTarget( 229 void AdjustCommandLineArgumentsForTarget(
231 const mojo::Identity& target, 230 const shell::Identity& target,
232 base::CommandLine* command_line) override { 231 base::CommandLine* command_line) override {
233 if (target.name() == "exe:chrome") 232 if (target.name() == "exe:chrome")
234 command_line->AppendSwitch(switches::kWaitForMojoShell); 233 command_line->AppendSwitch(switches::kWaitForMojoShell);
235 } 234 }
236 235
237 DISALLOW_COPY_AND_ASSIGN(NativeRunnerDelegateImpl); 236 DISALLOW_COPY_AND_ASSIGN(NativeRunnerDelegateImpl);
238 }; 237 };
239 238
240 // static 239 // static
241 const char MojoTestConnector::kTestSwitch[] = "is_test"; 240 const char MojoTestConnector::kTestSwitch[] = "is_test";
242 241
243 MojoTestConnector::MojoTestConnector() {} 242 MojoTestConnector::MojoTestConnector() {}
244 243
245 mojo::shell::mojom::ShellClientRequest MojoTestConnector::Init() { 244 shell::mojom::ShellClientRequest MojoTestConnector::Init() {
246 native_runner_delegate_.reset(new NativeRunnerDelegateImpl); 245 native_runner_delegate_.reset(new NativeRunnerDelegateImpl);
247 std::unique_ptr<mojo::shell::BackgroundShell::InitParams> init_params( 246 std::unique_ptr<shell::BackgroundShell::InitParams> init_params(
248 new mojo::shell::BackgroundShell::InitParams); 247 new shell::BackgroundShell::InitParams);
249 init_params->catalog_store = BuildTestCatalogStore(); 248 init_params->catalog_store = BuildTestCatalogStore();
250 // When running in single_process mode chrome initializes the edk. 249 // When running in single_process mode chrome initializes the edk.
251 init_params->init_edk = !base::CommandLine::ForCurrentProcess()->HasSwitch( 250 init_params->init_edk = !base::CommandLine::ForCurrentProcess()->HasSwitch(
252 content::kSingleProcessTestsFlag); 251 content::kSingleProcessTestsFlag);
253 init_params->native_runner_delegate = native_runner_delegate_.get(); 252 init_params->native_runner_delegate = native_runner_delegate_.get();
254 background_shell_.Init(std::move(init_params)); 253 background_shell_.Init(std::move(init_params));
255 return background_shell_.CreateShellClientRequest(kTestRunnerName); 254 return background_shell_.CreateShellClientRequest(kTestRunnerName);
256 } 255 }
257 256
258 MojoTestConnector::~MojoTestConnector() {} 257 MojoTestConnector::~MojoTestConnector() {}
259 258
260 std::unique_ptr<content::TestState> MojoTestConnector::PrepareForTest( 259 std::unique_ptr<content::TestState> MojoTestConnector::PrepareForTest(
261 base::CommandLine* command_line, 260 base::CommandLine* command_line,
262 base::TestLauncher::LaunchOptions* test_launch_options) { 261 base::TestLauncher::LaunchOptions* test_launch_options) {
263 std::unique_ptr<MojoTestState> test_state( 262 std::unique_ptr<MojoTestState> test_state(
264 new MojoTestState(&background_shell_)); 263 new MojoTestState(&background_shell_));
265 test_state->Init(command_line, test_launch_options); 264 test_state->Init(command_line, test_launch_options);
266 return std::move(test_state); 265 return std::move(test_state);
267 } 266 }
OLDNEW
« no previous file with comments | « chrome/test/base/mojo_test_connector.h ('k') | chromecast/browser/cast_content_browser_client.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698