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