OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "services/service_manager/public/cpp/service_runner.h" | |
6 | |
7 #include "base/at_exit.h" | |
8 #include "base/bind.h" | |
9 #include "base/command_line.h" | |
10 #include "base/memory/ptr_util.h" | |
11 #include "base/message_loop/message_loop.h" | |
12 #include "base/process/launch.h" | |
13 #include "base/run_loop.h" | |
14 #include "services/service_manager/public/cpp/service.h" | |
15 #include "services/service_manager/public/cpp/service_context.h" | |
16 | |
17 namespace service_manager { | |
18 | |
19 int g_service_runner_argc; | |
20 const char* const* g_service_runner_argv; | |
21 | |
22 ServiceRunner::ServiceRunner(Service* service) | |
23 : service_(base::WrapUnique(service)), | |
24 message_loop_type_(base::MessageLoop::TYPE_DEFAULT), | |
25 has_run_(false) {} | |
26 | |
27 ServiceRunner::~ServiceRunner() {} | |
28 | |
29 void ServiceRunner::InitBaseCommandLine() { | |
30 base::CommandLine::Init(g_service_runner_argc, g_service_runner_argv); | |
31 } | |
32 | |
33 void ServiceRunner::set_message_loop_type(base::MessageLoop::Type type) { | |
34 DCHECK_NE(base::MessageLoop::TYPE_CUSTOM, type); | |
35 DCHECK(!has_run_); | |
36 | |
37 message_loop_type_ = type; | |
38 } | |
39 | |
40 MojoResult ServiceRunner::Run(MojoHandle service_request_handle, | |
41 bool init_base) { | |
42 DCHECK(!has_run_); | |
43 has_run_ = true; | |
44 | |
45 std::unique_ptr<base::AtExitManager> at_exit; | |
46 if (init_base) { | |
47 InitBaseCommandLine(); | |
48 at_exit.reset(new base::AtExitManager); | |
49 } | |
50 | |
51 { | |
52 std::unique_ptr<base::MessageLoop> loop; | |
53 loop.reset(new base::MessageLoop(message_loop_type_)); | |
54 | |
55 context_.reset(new ServiceContext( | |
56 std::move(service_), | |
57 mojo::MakeRequest<mojom::Service>(mojo::MakeScopedHandle( | |
58 mojo::MessagePipeHandle(service_request_handle))))); | |
59 base::RunLoop run_loop; | |
60 context_->SetQuitClosure(run_loop.QuitClosure()); | |
61 run_loop.Run(); | |
62 context_.reset(); | |
63 } | |
64 return MOJO_RESULT_OK; | |
65 } | |
66 | |
67 MojoResult ServiceRunner::Run(MojoHandle service_request_handle) { | |
68 return Run(service_request_handle, false); | |
69 } | |
70 | |
71 void ServiceRunner::Quit() { | |
72 base::MessageLoop::current()->QuitWhenIdle(); | |
73 } | |
74 | |
75 } // namespace service_manager | |
OLD | NEW |