amistry | dc1c9106 | 2016-05-12 00:59:52 | [diff] [blame] | 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 |
| 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" |
amistry | dc1c9106 | 2016-05-12 00:59:52 | [diff] [blame] | 11 | #include "content/public/test/content_browser_test.h" |
| 12 | #include "content/public/test/content_browser_test_utils.h" |
ben | ff46163 | 2016-10-06 14:27:33 | [diff] [blame] | 13 | #include "content/public/test/test_service.mojom.h" |
amistry | dc1c9106 | 2016-05-12 00:59:52 | [diff] [blame] | 14 | |
| 15 | namespace content { |
| 16 | |
| 17 | class UtilityProcessHostImplBrowserTest : public ContentBrowserTest { |
| 18 | public: |
amistry | b613891 | 2016-05-31 01:54:12 | [diff] [blame] | 19 | void RunUtilityProcess(bool elevated) { |
amistry | dc1c9106 | 2016-05-12 00:59:52 | [diff] [blame] | 20 | base::RunLoop run_loop; |
| 21 | done_closure_ = run_loop.QuitClosure(); |
| 22 | BrowserThread::PostTask( |
| 23 | BrowserThread::IO, FROM_HERE, |
tzik | e2aca99 | 2017-09-05 08:50:54 | [diff] [blame^] | 24 | base::BindOnce( |
amistry | dc1c9106 | 2016-05-12 00:59:52 | [diff] [blame] | 25 | &UtilityProcessHostImplBrowserTest::RunUtilityProcessOnIOThread, |
amistry | b613891 | 2016-05-31 01:54:12 | [diff] [blame] | 26 | base::Unretained(this), elevated)); |
amistry | dc1c9106 | 2016-05-12 00:59:52 | [diff] [blame] | 27 | run_loop.Run(); |
| 28 | } |
| 29 | |
| 30 | protected: |
amistry | b613891 | 2016-05-31 01:54:12 | [diff] [blame] | 31 | void RunUtilityProcessOnIOThread(bool elevated) { |
amistry | dc1c9106 | 2016-05-12 00:59:52 | [diff] [blame] | 32 | UtilityProcessHost* host = UtilityProcessHost::Create(nullptr, nullptr); |
| 33 | host->SetName(base::ASCIIToUTF16("TestProcess")); |
amistry | b613891 | 2016-05-31 01:54:12 | [diff] [blame] | 34 | #if defined(OS_WIN) |
| 35 | if (elevated) |
| 36 | host->ElevatePrivileges(); |
| 37 | #endif |
amistry | dc1c9106 | 2016-05-12 00:59:52 | [diff] [blame] | 38 | EXPECT_TRUE(host->Start()); |
| 39 | |
ben | a06f7d6 | 2017-04-04 20:48:52 | [diff] [blame] | 40 | BindInterface(host, &service_); |
tzik | e2aca99 | 2017-09-05 08:50:54 | [diff] [blame^] | 41 | service_->DoSomething( |
| 42 | base::BindOnce(&UtilityProcessHostImplBrowserTest::OnSomething, |
| 43 | base::Unretained(this))); |
amistry | dc1c9106 | 2016-05-12 00:59:52 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void OnSomething() { |
| 47 | service_.reset(); |
| 48 | BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, done_closure_); |
| 49 | } |
| 50 | |
ben | ff46163 | 2016-10-06 14:27:33 | [diff] [blame] | 51 | mojom::TestServicePtr service_; |
amistry | dc1c9106 | 2016-05-12 00:59:52 | [diff] [blame] | 52 | base::Closure done_closure_; |
| 53 | }; |
| 54 | |
| 55 | IN_PROC_BROWSER_TEST_F(UtilityProcessHostImplBrowserTest, LaunchProcess) { |
amistry | b613891 | 2016-05-31 01:54:12 | [diff] [blame] | 56 | RunUtilityProcess(false); |
amistry | dc1c9106 | 2016-05-12 00:59:52 | [diff] [blame] | 57 | } |
| 58 | |
amistry | b613891 | 2016-05-31 01:54:12 | [diff] [blame] | 59 | #if defined(OS_WIN) |
| 60 | IN_PROC_BROWSER_TEST_F(UtilityProcessHostImplBrowserTest, |
| 61 | LaunchElevatedProcess) { |
| 62 | RunUtilityProcess(true); |
| 63 | } |
| 64 | #endif |
| 65 | |
amistry | dc1c9106 | 2016-05-12 00:59:52 | [diff] [blame] | 66 | } // namespace content |