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 "mojo/public/cpp/application/application_runner.h" | 5 #include "mojo/public/cpp/application/application_runner.h" |
6 | 6 |
7 #include "mojo/public/cpp/application/application_delegate.h" | 7 #include "mojo/public/cpp/application/application_delegate.h" |
8 #include "mojo/public/cpp/application/application_impl.h" | 8 #include "mojo/public/cpp/application/application_impl.h" |
9 #include "mojo/public/cpp/environment/environment.h" | 9 #include "mojo/public/cpp/environment/environment.h" |
10 #include "mojo/public/cpp/environment/logging.h" | 10 #include "mojo/public/cpp/environment/logging.h" |
11 #include "mojo/public/cpp/utility/run_loop.h" | 11 #include "mojo/public/cpp/utility/run_loop.h" |
12 | 12 |
13 namespace mojo { | 13 namespace mojo { |
14 namespace { | 14 namespace { |
15 bool g_running = false; | 15 bool g_running = false; |
16 } // namespace | 16 } // namespace |
17 | 17 |
18 // static | 18 // static |
19 void ApplicationImpl::Terminate() { | 19 void ApplicationImpl::Terminate() { |
20 RunLoop::current()->Quit(); | 20 RunLoop::current()->Quit(); |
21 } | 21 } |
22 | 22 |
23 ApplicationRunner::ApplicationRunner(ApplicationDelegate* delegate) | 23 ApplicationRunner::ApplicationRunner( |
24 : delegate_(delegate) {} | 24 std::unique_ptr<ApplicationDelegate> delegate) |
| 25 : delegate_(std::move(delegate)) {} |
25 | 26 |
26 ApplicationRunner::~ApplicationRunner() { | 27 ApplicationRunner::~ApplicationRunner() { |
27 assert(!delegate_); | 28 assert(!delegate_); |
28 } | 29 } |
29 | 30 |
30 // static | 31 // static |
31 void ApplicationRunner::SetDefaultLogger(const MojoLogger* logger) { | 32 void ApplicationRunner::SetDefaultLogger(const MojoLogger* logger) { |
32 MOJO_DCHECK(g_running); | 33 MOJO_DCHECK(g_running); |
33 Environment::SetDefaultLogger(logger); | 34 Environment::SetDefaultLogger(logger); |
34 } | 35 } |
35 | 36 |
36 // static | 37 // static |
37 const MojoLogger* ApplicationRunner::GetDefaultLogger() { | 38 const MojoLogger* ApplicationRunner::GetDefaultLogger() { |
38 MOJO_DCHECK(g_running); | 39 MOJO_DCHECK(g_running); |
39 return Environment::GetDefaultLogger(); | 40 return Environment::GetDefaultLogger(); |
40 } | 41 } |
41 | 42 |
42 MojoResult ApplicationRunner::Run(MojoHandle app_request_handle) { | 43 MojoResult ApplicationRunner::Run(MojoHandle app_request_handle) { |
43 MOJO_DCHECK(!g_running) | 44 MOJO_DCHECK(!g_running) |
44 << "Another ApplicationRunner::Run() is already running!"; | 45 << "Another ApplicationRunner::Run() is already running!"; |
45 | 46 |
46 g_running = true; | 47 g_running = true; |
47 Environment env; | 48 Environment env; |
48 { | 49 { |
49 RunLoop loop; | 50 RunLoop loop; |
50 ApplicationImpl app(delegate_, MakeRequest<Application>(MakeScopedHandle( | 51 ApplicationImpl app(delegate_.get(), |
51 MessagePipeHandle(app_request_handle)))); | 52 MakeRequest<Application>(MakeScopedHandle( |
| 53 MessagePipeHandle(app_request_handle)))); |
52 loop.Run(); | 54 loop.Run(); |
53 } | 55 } |
54 | 56 |
55 delete delegate_; | 57 delegate_.reset(); |
56 delegate_ = nullptr; | |
57 | 58 |
58 g_running = false; | 59 g_running = false; |
59 | 60 |
60 return MOJO_RESULT_OK; | 61 return MOJO_RESULT_OK; |
61 } | 62 } |
62 | 63 |
63 } // namespace mojo | 64 } // namespace mojo |
OLD | NEW |