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" |
| 13 | #include "content/public/test/test_mojo_service.mojom.h" |
ben | d1dd50f5 | 2016-06-26 22:10:48 | [diff] [blame] | 14 | #include "services/shell/public/cpp/interface_provider.h" |
| 15 | #include "services/shell/public/cpp/interface_registry.h" |
amistry | dc1c9106 | 2016-05-12 00:59:52 | [diff] [blame] | 16 | |
| 17 | namespace content { |
| 18 | |
| 19 | class UtilityProcessHostImplBrowserTest : public ContentBrowserTest { |
| 20 | public: |
amistry | b613891 | 2016-05-31 01:54:12 | [diff] [blame] | 21 | void RunUtilityProcess(bool elevated) { |
amistry | dc1c9106 | 2016-05-12 00:59:52 | [diff] [blame] | 22 | base::RunLoop run_loop; |
| 23 | done_closure_ = run_loop.QuitClosure(); |
| 24 | BrowserThread::PostTask( |
| 25 | BrowserThread::IO, FROM_HERE, |
| 26 | base::Bind( |
| 27 | &UtilityProcessHostImplBrowserTest::RunUtilityProcessOnIOThread, |
amistry | b613891 | 2016-05-31 01:54:12 | [diff] [blame] | 28 | base::Unretained(this), elevated)); |
amistry | dc1c9106 | 2016-05-12 00:59:52 | [diff] [blame] | 29 | run_loop.Run(); |
| 30 | } |
| 31 | |
| 32 | protected: |
amistry | b613891 | 2016-05-31 01:54:12 | [diff] [blame] | 33 | void RunUtilityProcessOnIOThread(bool elevated) { |
amistry | dc1c9106 | 2016-05-12 00:59:52 | [diff] [blame] | 34 | UtilityProcessHost* host = UtilityProcessHost::Create(nullptr, nullptr); |
| 35 | host->SetName(base::ASCIIToUTF16("TestProcess")); |
amistry | b613891 | 2016-05-31 01:54:12 | [diff] [blame] | 36 | #if defined(OS_WIN) |
| 37 | if (elevated) |
| 38 | host->ElevatePrivileges(); |
| 39 | #endif |
amistry | dc1c9106 | 2016-05-12 00:59:52 | [diff] [blame] | 40 | EXPECT_TRUE(host->Start()); |
| 41 | |
ben | d1dd50f5 | 2016-06-26 22:10:48 | [diff] [blame] | 42 | host->GetRemoteInterfaces()->GetInterface(&service_); |
amistry | dc1c9106 | 2016-05-12 00:59:52 | [diff] [blame] | 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 | |
| 57 | IN_PROC_BROWSER_TEST_F(UtilityProcessHostImplBrowserTest, LaunchProcess) { |
amistry | b613891 | 2016-05-31 01:54:12 | [diff] [blame] | 58 | RunUtilityProcess(false); |
amistry | dc1c9106 | 2016-05-12 00:59:52 | [diff] [blame] | 59 | } |
| 60 | |
amistry | b613891 | 2016-05-31 01:54:12 | [diff] [blame] | 61 | #if defined(OS_WIN) |
| 62 | IN_PROC_BROWSER_TEST_F(UtilityProcessHostImplBrowserTest, |
| 63 | LaunchElevatedProcess) { |
| 64 | RunUtilityProcess(true); |
| 65 | } |
| 66 | #endif |
| 67 | |
amistry | dc1c9106 | 2016-05-12 00:59:52 | [diff] [blame] | 68 | } // namespace content |