| 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 #include "services/service_manager/public/cpp/service_runner.h" | 5 #include "services/service_manager/public/cpp/service_runner.h" |
| 6 | 6 |
| 7 #include "base/at_exit.h" | 7 #include "base/at_exit.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 } | 30 } |
| 31 | 31 |
| 32 void ServiceRunner::set_message_loop_type(base::MessageLoop::Type type) { | 32 void ServiceRunner::set_message_loop_type(base::MessageLoop::Type type) { |
| 33 DCHECK_NE(base::MessageLoop::TYPE_CUSTOM, type); | 33 DCHECK_NE(base::MessageLoop::TYPE_CUSTOM, type); |
| 34 DCHECK(!has_run_); | 34 DCHECK(!has_run_); |
| 35 | 35 |
| 36 message_loop_type_ = type; | 36 message_loop_type_ = type; |
| 37 } | 37 } |
| 38 | 38 |
| 39 MojoResult ServiceRunner::Run(MojoHandle service_request_handle, | 39 MojoResult ServiceRunner::Run(MojoHandle service_request_handle, |
| 40 bool init_base) { | 40 bool init_base) { |
| 41 DCHECK(!has_run_); | 41 DCHECK(!has_run_); |
| 42 has_run_ = true; | 42 has_run_ = true; |
| 43 | 43 |
| 44 std::unique_ptr<base::AtExitManager> at_exit; | 44 std::unique_ptr<base::AtExitManager> at_exit; |
| 45 if (init_base) { | 45 if (init_base) { |
| 46 InitBaseCommandLine(); | 46 InitBaseCommandLine(); |
| 47 at_exit.reset(new base::AtExitManager); | 47 at_exit.reset(new base::AtExitManager); |
| 48 } | 48 } |
| 49 | 49 |
| 50 { | 50 { |
| 51 std::unique_ptr<base::MessageLoop> loop; | 51 std::unique_ptr<base::MessageLoop> loop; |
| 52 loop.reset(new base::MessageLoop(message_loop_type_)); | 52 loop.reset(new base::MessageLoop(message_loop_type_)); |
| 53 | 53 |
| 54 std::unique_ptr<ServiceContext> context(new ServiceContext( | 54 context_.reset(new ServiceContext( |
| 55 service_.get(), | 55 std::move(service_), |
| 56 mojo::MakeRequest<mojom::Service>(mojo::MakeScopedHandle( | 56 mojo::MakeRequest<mojom::Service>(mojo::MakeScopedHandle( |
| 57 mojo::MessagePipeHandle(service_request_handle))))); | 57 mojo::MessagePipeHandle(service_request_handle))))); |
| 58 base::RunLoop run_loop; | 58 base::RunLoop run_loop; |
| 59 context->SetConnectionLostClosure(run_loop.QuitClosure()); | 59 context_->SetConnectionLostClosure(run_loop.QuitClosure()); |
| 60 service_->set_context(std::move(context)); | |
| 61 run_loop.Run(); | 60 run_loop.Run(); |
| 62 // It's very common for the service to cache the app and terminate on | 61 // It's very common for the service to cache the app and terminate on |
| 63 // errors. If we don't delete the service before the app we run the risk of | 62 // errors. If we don't delete the service before the app we run the risk of |
| 64 // the service having a stale reference to the app and trying to use it. | 63 // the service having a stale reference to the app and trying to use it. |
| 65 // Note that we destruct the message loop first because that might trigger | 64 // Note that we destruct the message loop first because that might trigger |
| 66 // connection error handlers and they might access objects created by the | 65 // connection error handlers and they might access objects created by the |
| 67 // service. | 66 // service. |
| 68 loop.reset(); | 67 loop.reset(); |
| 69 service_.reset(); | 68 context_.reset(); |
| 70 } | 69 } |
| 71 return MOJO_RESULT_OK; | 70 return MOJO_RESULT_OK; |
| 72 } | 71 } |
| 73 | 72 |
| 74 MojoResult ServiceRunner::Run(MojoHandle service_request_handle) { | 73 MojoResult ServiceRunner::Run(MojoHandle service_request_handle) { |
| 75 bool init_base = true; | 74 bool init_base = true; |
| 76 if (base::CommandLine::InitializedForCurrentProcess()) { | 75 if (base::CommandLine::InitializedForCurrentProcess()) { |
| 77 init_base = | 76 init_base = |
| 78 !base::CommandLine::ForCurrentProcess()->HasSwitch("single-process"); | 77 !base::CommandLine::ForCurrentProcess()->HasSwitch("single-process"); |
| 79 } | 78 } |
| 80 return Run(service_request_handle, init_base); | 79 return Run(service_request_handle, init_base); |
| 81 } | 80 } |
| 82 | 81 |
| 83 void ServiceRunner::DestroyServiceContext() { | |
| 84 service_->set_context(std::unique_ptr<ServiceContext>()); | |
| 85 } | |
| 86 | |
| 87 void ServiceRunner::Quit() { | 82 void ServiceRunner::Quit() { |
| 88 base::MessageLoop::current()->QuitWhenIdle(); | 83 base::MessageLoop::current()->QuitWhenIdle(); |
| 89 } | 84 } |
| 90 | 85 |
| 91 } // namespace service_manager | 86 } // namespace service_manager |
| OLD | NEW |