blob: b8a496be6a50515b27418984714749cc2b0e74e0 [file] [log] [blame]
amistrydc1c91062016-05-12 00:59:521// Copyright 2016 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 "base/bind.h"
6#include "base/run_loop.h"
7#include "base/strings/utf_string_conversions.h"
8#include "content/public/browser/browser_thread.h"
9#include "content/public/browser/utility_process_host.h"
10#include "content/public/browser/utility_process_host_client.h"
11#include "content/public/common/service_registry.h"
12#include "content/public/test/content_browser_test.h"
13#include "content/public/test/content_browser_test_utils.h"
14#include "content/public/test/test_mojo_service.mojom.h"
15
16namespace content {
17
18class UtilityProcessHostImplBrowserTest : public ContentBrowserTest {
19 public:
amistryb6138912016-05-31 01:54:1220 void RunUtilityProcess(bool elevated) {
amistrydc1c91062016-05-12 00:59:5221 base::RunLoop run_loop;
22 done_closure_ = run_loop.QuitClosure();
23 BrowserThread::PostTask(
24 BrowserThread::IO, FROM_HERE,
25 base::Bind(
26 &UtilityProcessHostImplBrowserTest::RunUtilityProcessOnIOThread,
amistryb6138912016-05-31 01:54:1227 base::Unretained(this), elevated));
amistrydc1c91062016-05-12 00:59:5228 run_loop.Run();
29 }
30
31 protected:
amistryb6138912016-05-31 01:54:1232 void RunUtilityProcessOnIOThread(bool elevated) {
amistrydc1c91062016-05-12 00:59:5233 UtilityProcessHost* host = UtilityProcessHost::Create(nullptr, nullptr);
34 host->SetName(base::ASCIIToUTF16("TestProcess"));
amistryb6138912016-05-31 01:54:1235#if defined(OS_WIN)
36 if (elevated)
37 host->ElevatePrivileges();
38#endif
amistrydc1c91062016-05-12 00:59:5239 EXPECT_TRUE(host->Start());
40
41 ServiceRegistry* service_registry = host->GetServiceRegistry();
42 service_registry->ConnectToRemoteService(mojo::GetProxy(&service_));
43 service_->DoSomething(base::Bind(
44 &UtilityProcessHostImplBrowserTest::OnSomething,
45 base::Unretained(this)));
46 }
47
48 void OnSomething() {
49 service_.reset();
50 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, done_closure_);
51 }
52
53 mojom::TestMojoServicePtr service_;
54 base::Closure done_closure_;
55};
56
57IN_PROC_BROWSER_TEST_F(UtilityProcessHostImplBrowserTest, LaunchProcess) {
amistryb6138912016-05-31 01:54:1258 RunUtilityProcess(false);
amistrydc1c91062016-05-12 00:59:5259}
60
amistryb6138912016-05-31 01:54:1261#if defined(OS_WIN)
62IN_PROC_BROWSER_TEST_F(UtilityProcessHostImplBrowserTest,
63 LaunchElevatedProcess) {
64 RunUtilityProcess(true);
65}
66#endif
67
amistrydc1c91062016-05-12 00:59:5268} // namespace content