| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 // Note: This file also tests child_process.*. | 5 #include "services/service_manager/runner/host/service_process_launcher.h" |
| 6 | |
| 7 #include "services/service_manager/runner/host/child_process_host.h" | |
| 8 | 6 |
| 9 #include <memory> | 7 #include <memory> |
| 10 #include <utility> | 8 #include <utility> |
| 11 | 9 |
| 12 #include "base/bind.h" | 10 #include "base/bind.h" |
| 13 #include "base/callback.h" | 11 #include "base/callback.h" |
| 14 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 15 #include "base/logging.h" | 13 #include "base/logging.h" |
| 16 #include "base/macros.h" | 14 #include "base/macros.h" |
| 17 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 18 #include "base/path_service.h" | 16 #include "base/path_service.h" |
| 19 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
| 20 #include "base/threading/thread.h" | 18 #include "base/threading/thread.h" |
| 21 #include "mojo/edk/embedder/embedder.h" | 19 #include "mojo/edk/embedder/embedder.h" |
| 22 #include "mojo/edk/embedder/process_delegate.h" | 20 #include "mojo/edk/embedder/process_delegate.h" |
| 23 #include "services/service_manager/native_runner_delegate.h" | |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 22 |
| 26 namespace service_manager { | 23 namespace service_manager { |
| 27 namespace { | 24 namespace { |
| 28 | 25 |
| 29 const char kTestServiceName[] = "host_test_service"; | 26 const char kTestServiceName[] = "host_test_service"; |
| 30 | 27 |
| 31 const base::FilePath::CharType kPackagesPath[] = FILE_PATH_LITERAL("Packages"); | 28 const base::FilePath::CharType kPackagesPath[] = FILE_PATH_LITERAL("Packages"); |
| 32 const base::FilePath::CharType kServiceExtension[] = | 29 const base::FilePath::CharType kServiceExtension[] = |
| 33 FILE_PATH_LITERAL(".service"); | 30 FILE_PATH_LITERAL(".service"); |
| 34 | 31 |
| 35 void ProcessReadyCallbackAdapater(const base::Closure& callback, | 32 void ProcessReadyCallbackAdapater(const base::Closure& callback, |
| 36 base::ProcessId process_id) { | 33 base::ProcessId process_id) { |
| 37 callback.Run(); | 34 callback.Run(); |
| 38 } | 35 } |
| 39 | 36 |
| 40 class ProcessDelegate : public mojo::edk::ProcessDelegate { | 37 class ProcessDelegate : public mojo::edk::ProcessDelegate { |
| 41 public: | 38 public: |
| 42 ProcessDelegate() {} | 39 ProcessDelegate() {} |
| 43 ~ProcessDelegate() override {} | 40 ~ProcessDelegate() override {} |
| 44 | 41 |
| 45 private: | 42 private: |
| 46 void OnShutdownComplete() override {} | 43 void OnShutdownComplete() override {} |
| 47 DISALLOW_COPY_AND_ASSIGN(ProcessDelegate); | 44 DISALLOW_COPY_AND_ASSIGN(ProcessDelegate); |
| 48 }; | 45 }; |
| 49 | 46 |
| 50 class NativeRunnerDelegateImpl : public NativeRunnerDelegate { | 47 class ServiceProcessLauncherDelegateImpl |
| 48 : public ServiceProcessLauncher::Delegate { |
| 51 public: | 49 public: |
| 52 NativeRunnerDelegateImpl() {} | 50 ServiceProcessLauncherDelegateImpl() {} |
| 53 ~NativeRunnerDelegateImpl() override {} | 51 ~ServiceProcessLauncherDelegateImpl() override {} |
| 54 | 52 |
| 55 size_t get_and_clear_adjust_count() { | 53 size_t get_and_clear_adjust_count() { |
| 56 size_t count = 0; | 54 size_t count = 0; |
| 57 std::swap(count, adjust_count_); | 55 std::swap(count, adjust_count_); |
| 58 return count; | 56 return count; |
| 59 } | 57 } |
| 60 | 58 |
| 61 private: | 59 private: |
| 62 // NativeRunnerDelegate: | 60 // ServiceProcessLauncher::Delegate: |
| 63 void AdjustCommandLineArgumentsForTarget( | 61 void AdjustCommandLineArgumentsForTarget( |
| 64 const Identity& target, | 62 const Identity& target, |
| 65 base::CommandLine* command_line) override { | 63 base::CommandLine* command_line) override { |
| 66 adjust_count_++; | 64 adjust_count_++; |
| 67 } | 65 } |
| 68 | 66 |
| 69 size_t adjust_count_ = 0; | 67 size_t adjust_count_ = 0; |
| 70 | 68 |
| 71 DISALLOW_COPY_AND_ASSIGN(NativeRunnerDelegateImpl); | 69 DISALLOW_COPY_AND_ASSIGN(ServiceProcessLauncherDelegateImpl); |
| 72 }; | 70 }; |
| 73 | 71 |
| 74 #if defined(OS_ANDROID) | 72 #if defined(OS_ANDROID) |
| 75 // TODO(qsr): Multiprocess service manager tests are not supported on android. | 73 // TODO(qsr): Multiprocess service manager tests are not supported on android. |
| 76 #define MAYBE_StartJoin DISABLED_StartJoin | 74 #define MAYBE_StartJoin DISABLED_StartJoin |
| 77 #else | 75 #else |
| 78 #define MAYBE_StartJoin StartJoin | 76 #define MAYBE_StartJoin StartJoin |
| 79 #endif // defined(OS_ANDROID) | 77 #endif // defined(OS_ANDROID) |
| 80 TEST(ChildProcessHostTest, MAYBE_StartJoin) { | 78 TEST(ServieProcessLauncherTest, MAYBE_StartJoin) { |
| 81 base::FilePath service_manager_dir; | 79 base::FilePath service_manager_dir; |
| 82 PathService::Get(base::DIR_MODULE, &service_manager_dir); | 80 PathService::Get(base::DIR_MODULE, &service_manager_dir); |
| 83 base::MessageLoop message_loop; | 81 base::MessageLoop message_loop; |
| 84 scoped_refptr<base::SequencedWorkerPool> blocking_pool( | 82 scoped_refptr<base::SequencedWorkerPool> blocking_pool( |
| 85 new base::SequencedWorkerPool(3, "blocking_pool", | 83 new base::SequencedWorkerPool(3, "blocking_pool", |
| 86 base::TaskPriority::USER_VISIBLE)); | 84 base::TaskPriority::USER_VISIBLE)); |
| 87 | 85 |
| 88 base::Thread io_thread("io_thread"); | 86 base::Thread io_thread("io_thread"); |
| 89 base::Thread::Options options; | 87 base::Thread::Options options; |
| 90 options.message_loop_type = base::MessageLoop::TYPE_IO; | 88 options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 91 io_thread.StartWithOptions(options); | 89 io_thread.StartWithOptions(options); |
| 92 | 90 |
| 93 ProcessDelegate delegate; | 91 ProcessDelegate delegate; |
| 94 mojo::edk::InitIPCSupport(&delegate, io_thread.task_runner()); | 92 mojo::edk::InitIPCSupport(&delegate, io_thread.task_runner()); |
| 95 | 93 |
| 96 base::FilePath test_service_path = | 94 base::FilePath test_service_path = |
| 97 base::FilePath(kPackagesPath).AppendASCII(kTestServiceName) | 95 base::FilePath(kPackagesPath).AppendASCII(kTestServiceName) |
| 98 .AppendASCII(kTestServiceName) .AddExtension(kServiceExtension); | 96 .AppendASCII(kTestServiceName) .AddExtension(kServiceExtension); |
| 99 | 97 |
| 100 NativeRunnerDelegateImpl native_runner_delegate; | 98 ServiceProcessLauncherDelegateImpl service_process_launcher_delegate; |
| 101 ChildProcessHost child_process_host(blocking_pool.get(), | 99 ServiceProcessLauncher launcher(blocking_pool.get(), |
| 102 &native_runner_delegate, false, | 100 &service_process_launcher_delegate, |
| 103 Identity(), test_service_path); | 101 test_service_path); |
| 104 base::RunLoop run_loop; | 102 base::RunLoop run_loop; |
| 105 child_process_host.Start( | 103 launcher.Start( |
| 106 Identity(), | 104 Identity(), |
| 107 base::Bind(&ProcessReadyCallbackAdapater, run_loop.QuitClosure()), | 105 false, |
| 108 base::Bind(&base::DoNothing)); | 106 base::Bind(&ProcessReadyCallbackAdapater, run_loop.QuitClosure())); |
| 109 run_loop.Run(); | 107 run_loop.Run(); |
| 110 | 108 |
| 111 child_process_host.Join(); | 109 launcher.Join(); |
| 112 blocking_pool->Shutdown(); | 110 blocking_pool->Shutdown(); |
| 113 mojo::edk::ShutdownIPCSupport(); | 111 mojo::edk::ShutdownIPCSupport(); |
| 114 EXPECT_EQ(1u, native_runner_delegate.get_and_clear_adjust_count()); | 112 EXPECT_EQ(1u, service_process_launcher_delegate.get_and_clear_adjust_count()); |
| 115 } | 113 } |
| 116 | 114 |
| 117 } // namespace | 115 } // namespace |
| 118 } // namespace service_manager | 116 } // namespace service_manager |
| OLD | NEW |