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 Handle app_handle) { |
| 22 MojoResult result = MOJO_RESULT_OK; |
| 23 MojoMainFunction main_function = NULL; |
| 24 |
| 25 base::NativeLibrary app_library = base::LoadNativeLibrary(app_path, NULL); |
| 26 if (!app_library) { |
| 27 LOG(ERROR) << "Failed to load library: " << app_path.value().c_str(); |
| 28 goto completed; |
| 29 } |
| 30 |
| 31 main_function = reinterpret_cast<MojoMainFunction>( |
| 32 base::GetFunctionPointerFromNativeLibrary(app_library, "MojoMain")); |
| 33 if (!main_function) { |
| 34 LOG(ERROR) << "Entrypoint MojoMain not found."; |
| 35 goto completed; |
| 36 } |
| 37 |
| 38 result = main_function(app_handle); |
| 39 if (result < MOJO_RESULT_OK) { |
| 40 LOG(ERROR) << "MojoMain returned an error: " << result; |
| 41 // TODO(*): error handling? |
| 42 goto completed; |
| 43 } |
| 44 |
| 45 completed: |
| 46 base::UnloadNativeLibrary(app_library); |
| 47 Close(app_handle); |
| 48 } |
| 49 |
| 50 AppContainer::AppContainer() |
| 51 : weak_factory_(this) { |
| 52 } |
| 53 |
| 54 AppContainer::~AppContainer() { |
| 55 } |
| 56 |
| 57 void AppContainer::LaunchApp(const base::FilePath& app_path) { |
| 58 Handle app_handle; |
| 59 MojoResult result = CreateMessagePipe(&shell_handle_, &app_handle); |
| 60 if (result < MOJO_RESULT_OK) { |
| 61 // Failure.. |
| 62 } |
| 63 |
| 64 // Launch the app on its own thread. |
| 65 // TODO(beng): Create a unique thread name. |
| 66 thread_.reset(new base::Thread("app_thread")); |
| 67 thread_->Start(); |
| 68 thread_->message_loop_proxy()->PostTaskAndReply( |
| 69 FROM_HERE, |
| 70 base::Bind(&LaunchAppOnThread, app_path, app_handle), |
| 71 base::Bind(&AppContainer::AppCompleted, weak_factory_.GetWeakPtr())); |
| 72 |
| 73 const char* hello_msg = "Hello"; |
| 74 result = WriteMessage(shell_handle_, hello_msg, strlen(hello_msg)+1, |
| 75 NULL, 0, MOJO_WRITE_MESSAGE_FLAG_NONE); |
| 76 if (result < MOJO_RESULT_OK) { |
| 77 // Failure.. |
| 78 } |
| 79 } |
| 80 |
| 81 |
| 82 void AppContainer::AppCompleted() { |
| 83 thread_.reset(); |
| 84 Close(shell_handle_); |
| 85 |
| 86 // Probably want to do something more sophisticated here, like notify someone |
| 87 // else to do this. |
| 88 base::MessageLoop::current()->Quit(); |
| 89 } |
| 90 |
| 91 } // namespace shell |
| 92 } // namespace mojo |
OLD | NEW |