OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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/run_application.h" | 5 #include "mojo/public/cpp/application/run_application.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/at_exit.h" | 9 #include "base/at_exit.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/debug/stack_trace.h" | 11 #include "base/debug/stack_trace.h" |
12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
13 #include "base/threading/thread_local_storage.h" | 13 #include "base/threading/thread_local_storage.h" |
14 #include "build/build_config.h" | 14 #include "build/build_config.h" |
| 15 #include "mojo/application/run_application_options_chromium.h" |
15 #include "mojo/message_pump/message_pump_mojo.h" | 16 #include "mojo/message_pump/message_pump_mojo.h" |
16 #include "mojo/public/cpp/application/application_impl_base.h" | 17 #include "mojo/public/cpp/application/application_impl_base.h" |
17 #include "mojo/public/cpp/system/message_pipe.h" | 18 #include "mojo/public/cpp/system/message_pipe.h" |
18 | 19 |
19 namespace mojo { | 20 namespace mojo { |
20 | 21 |
21 namespace { | 22 namespace { |
22 | 23 |
23 base::ThreadLocalStorage::StaticSlot g_current_result_holder = TLS_INITIALIZER; | 24 base::ThreadLocalStorage::StaticSlot g_current_result_holder = TLS_INITIALIZER; |
24 | 25 |
25 // We store a pointer to a |ResultHolder|, which just stores a |MojoResult|, in | 26 // We store a pointer to a |ResultHolder|, which just stores a |MojoResult|, in |
26 // TLS so that |TerminateApplication()| can provide the result that | 27 // TLS so that |TerminateApplication()| can provide the result that |
27 // |RunApplication()| will return. (The |ResultHolder| is just on | 28 // |RunApplication()| will return. (The |ResultHolder| is just on |
28 // |RunApplication()|'s stack.) | 29 // |RunApplication()|'s stack.) |
29 struct ResultHolder { | 30 struct ResultHolder { |
30 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) | 31 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
31 bool is_set = false; | 32 bool is_set = false; |
32 #endif | 33 #endif |
33 // TODO(vtl): The default result should probably be |MOJO_RESULT_UNKNOWN|, but | 34 // TODO(vtl): The default result should probably be |MOJO_RESULT_UNKNOWN|, but |
34 // |ApplicationRunnerChromium| always returned |MOJO_RESULT_OK|. | 35 // |ApplicationRunnerChromium| always returned |MOJO_RESULT_OK|. |
35 MojoResult result = MOJO_RESULT_OK; | 36 MojoResult result = MOJO_RESULT_OK; |
36 }; | 37 }; |
37 | 38 |
38 } // namespace | 39 } // namespace |
39 | 40 |
40 MojoResult RunMainApplication(MojoHandle application_request_handle, | 41 MojoResult RunMainApplication(MojoHandle application_request_handle, |
41 ApplicationImplBase* application_impl) { | 42 ApplicationImplBase* application_impl, |
| 43 const RunApplicationOptions* options) { |
42 base::CommandLine::Init(0, nullptr); | 44 base::CommandLine::Init(0, nullptr); |
43 base::AtExitManager at_exit; | 45 base::AtExitManager at_exit; |
44 | 46 |
45 g_current_result_holder.Initialize(nullptr); | 47 g_current_result_holder.Initialize(nullptr); |
46 | 48 |
47 #if !defined(NDEBUG) && !defined(OS_NACL) | 49 #if !defined(NDEBUG) && !defined(OS_NACL) |
48 base::debug::EnableInProcessStackDumping(); | 50 base::debug::EnableInProcessStackDumping(); |
49 #endif | 51 #endif |
50 | 52 |
51 return RunApplication(application_request_handle, application_impl); | 53 return RunApplication(application_request_handle, application_impl, options); |
52 } | 54 } |
53 | 55 |
54 MojoResult RunApplication(MojoHandle application_request_handle, | 56 MojoResult RunApplication(MojoHandle application_request_handle, |
55 ApplicationImplBase* application_impl) { | 57 ApplicationImplBase* application_impl, |
| 58 const RunApplicationOptions* options) { |
56 DCHECK(!g_current_result_holder.Get()); | 59 DCHECK(!g_current_result_holder.Get()); |
57 | 60 |
58 ResultHolder result_holder; | 61 ResultHolder result_holder; |
59 g_current_result_holder.Set(&result_holder); | 62 g_current_result_holder.Set(&result_holder); |
60 | 63 |
61 std::unique_ptr<base::MessageLoop> loop; | 64 // Note: If |options| is non-null, it better point to a |
62 // TODO(vtl): Support other types of message loops. (That's why I'm leaving | 65 // |RunApplicationOptionsChromium|. |
63 // |loop| as a unique_ptr.) | 66 base::MessageLoop::Type message_loop_type = |
64 loop.reset(new base::MessageLoop(common::MessagePumpMojo::Create())); | 67 options |
| 68 ? static_cast<const RunApplicationOptionsChromium*>(options) |
| 69 ->message_loop_type |
| 70 : base::MessageLoop::TYPE_CUSTOM; |
| 71 std::unique_ptr<base::MessageLoop> loop( |
| 72 (message_loop_type == base::MessageLoop::TYPE_CUSTOM) |
| 73 ? new base::MessageLoop(common::MessagePumpMojo::Create()) |
| 74 : new base::MessageLoop(message_loop_type)); |
65 application_impl->Bind(InterfaceRequest<Application>( | 75 application_impl->Bind(InterfaceRequest<Application>( |
66 MakeScopedHandle(MessagePipeHandle(application_request_handle)))); | 76 MakeScopedHandle(MessagePipeHandle(application_request_handle)))); |
67 loop->Run(); | 77 loop->Run(); |
68 | 78 |
69 g_current_result_holder.Set(nullptr); | 79 g_current_result_holder.Set(nullptr); |
70 | 80 |
71 // TODO(vtl): We'd like to enable the following assertion, but we quit the | 81 // TODO(vtl): We'd like to enable the following assertion, but we quit the |
72 // current message loop directly in various places. | 82 // current message loop directly in various places. |
73 // DCHECK(result_holder.is_set); | 83 // DCHECK(result_holder.is_set); |
74 | 84 |
(...skipping 12 matching lines...) Expand all Loading... |
87 static_cast<ResultHolder*>(g_current_result_holder.Get()); | 97 static_cast<ResultHolder*>(g_current_result_holder.Get()); |
88 DCHECK(result_holder); | 98 DCHECK(result_holder); |
89 DCHECK(!result_holder->is_set); | 99 DCHECK(!result_holder->is_set); |
90 result_holder->result = result; | 100 result_holder->result = result; |
91 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) | 101 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
92 result_holder->is_set = true; | 102 result_holder->is_set = true; |
93 #endif | 103 #endif |
94 } | 104 } |
95 | 105 |
96 } // namespace mojo | 106 } // namespace mojo |
OLD | NEW |