Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 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 "base/bind.h" | |
| 6 #include "base/callback_forward.h" | |
| 7 #include "base/files/file_path.h" | |
| 8 #include "base/native_library.h" | |
| 9 #include "base/thread_task_runner_handle.h" | |
| 10 #include "base/threading/thread.h" | |
| 11 #include "mojo/public/system/core.h" | |
| 12 #include "mojo/shell/app_container.h" | |
| 13 | |
| 14 typedef MojoResult (*MojoMainFunction)(mojo::Handle pipe); | |
| 15 | |
| 16 namespace mojo { | |
| 17 namespace shell { | |
| 18 | |
| 19 void LaunchAppOnThread( | |
| 20 const base::FilePath& app_path, | |
| 21 mojo::Handle app_handle) { | |
| 22 base::NativeLibrary app_library = base::LoadNativeLibrary(app_path, NULL); | |
| 23 if (!app_library) { | |
| 24 LOG(ERROR) << "Failed to load library: " << app_path.value().c_str(); | |
| 25 goto completed; | |
| 26 } | |
| 27 | |
| 28 MojoMainFunction main_function = | |
| 29 reinterpret_cast<MojoMainFunction>( | |
| 30 base::GetFunctionPointerFromNativeLibrary(app_library, "MojoMain")); | |
| 31 if (!main_function) { | |
| 32 LOG(ERROR) << "Entrypoint MojoMain not found."; | |
| 33 goto completed; | |
| 34 } | |
| 35 | |
| 36 MojoResult result = main_function(app_handle); | |
| 37 if (result < MOJO_RESULT_OK) { | |
| 38 LOG(ERROR) << "MojoMain returned an error: " << result; | |
| 39 // TODO(*): error handling? | |
| 40 goto completed; | |
| 41 } | |
| 42 | |
| 43 completed: | |
| 44 base::UnloadNativeLibrary(app_library); | |
| 45 mojo::Close(app_handle); | |
|
darin (slow to review)
2013/10/08 21:30:06
nit: you could leave off the mojo:: if you wanted
| |
| 46 } | |
| 47 | |
| 48 AppContainer::AppContainer() | |
| 49 : weak_factory_(this) { | |
| 50 } | |
| 51 | |
| 52 AppContainer::~AppContainer() { | |
| 53 } | |
| 54 | |
| 55 void AppContainer::LaunchApp(const base::FilePath& app_path) { | |
| 56 mojo::Handle app_handle; | |
| 57 MojoResult result = mojo::CreateMessagePipe(&shell_handle_, &app_handle); | |
| 58 if (result < MOJO_RESULT_OK) { | |
| 59 // Failure.. | |
| 60 } | |
| 61 | |
| 62 // Launch the app on its own thread. | |
| 63 // TODO(beng): Create a unique thread name. | |
| 64 thread_.reset(new base::Thread("app_thread")); | |
| 65 thread_->Start(); | |
| 66 thread_->message_loop_proxy()->PostTaskAndReply( | |
| 67 FROM_HERE, | |
| 68 base::Bind(&LaunchAppOnThread, app_path, app_handle), | |
| 69 base::Bind(&AppContainer::AppCompleted, weak_factory_.GetWeakPtr())); | |
| 70 | |
| 71 const char* hello_msg = "Hello"; | |
| 72 result = mojo::WriteMessage(shell_handle_, hello_msg, strlen(hello_msg)+1, | |
| 73 NULL, 0, MOJO_WRITE_MESSAGE_FLAG_NONE); | |
| 74 if (result < MOJO_RESULT_OK) { | |
| 75 // Failure.. | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 | |
| 80 void AppContainer::AppCompleted() { | |
| 81 thread_.reset(); | |
| 82 mojo::Close(shell_handle_); | |
| 83 | |
| 84 // Probably want to do something more sophisticated here, like notify someone | |
| 85 // else to do this. | |
| 86 base::MessageLoop::current()->Quit(); | |
| 87 } | |
| 88 | |
| 89 } // namespace shell | |
| 90 } // namespace mojo | |
| OLD | NEW |