Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/shell/app_container.h" | 5 #include "mojo/shell/app_container.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback_forward.h" | 8 #include "base/callback_forward.h" |
| 9 #include "base/callback_helpers.h" | 9 #include "base/callback_helpers.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/native_library.h" | 12 #include "base/native_library.h" |
| 13 #include "base/scoped_native_library.h" | 13 #include "base/scoped_native_library.h" |
| 14 #include "base/thread_task_runner_handle.h" | 14 #include "base/thread_task_runner_handle.h" |
| 15 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
| 16 #include "mojo/public/system/core.h" | 16 #include "mojo/public/system/core.h" |
| 17 #include "mojo/public/utility/scoped_handle.h" | 17 #include "mojo/public/utility/scoped_handle.h" |
| 18 #include "mojo/services/native_viewport/native_viewport_controller.h" | 18 #include "mojo/services/native_viewport/native_viewport_controller.h" |
| 19 #include "mojo/shell/context.h" | 19 #include "mojo/shell/context.h" |
| 20 | 20 |
| 21 typedef MojoResult (*MojoMainFunction)(mojo::Handle pipe); | 21 typedef MojoResult (*MojoMainFunction)(mojo::Handle pipe); |
| 22 | 22 |
| 23 namespace mojo { | 23 namespace mojo { |
| 24 namespace shell { | 24 namespace shell { |
| 25 | 25 |
| 26 void LaunchAppOnThread( | |
| 27 const base::FilePath& app_path, | |
| 28 Handle app_handle_raw) { | |
| 29 base::ScopedClosureRunner app_deleter( | |
| 30 base::Bind(base::IgnoreResult(&base::DeleteFile), app_path, false)); | |
| 31 ScopedHandle app_handle(app_handle_raw); | |
| 32 | |
| 33 base::ScopedNativeLibrary app_library( | |
| 34 base::LoadNativeLibrary(app_path, NULL)); | |
| 35 if (!app_library.is_valid()) { | |
| 36 LOG(ERROR) << "Failed to load library: " << app_path.value().c_str(); | |
| 37 return; | |
| 38 } | |
| 39 | |
| 40 MojoMainFunction main_function = reinterpret_cast<MojoMainFunction>( | |
| 41 app_library.GetFunctionPointer("MojoMain")); | |
| 42 if (!main_function) { | |
| 43 LOG(ERROR) << "Entrypoint MojoMain not found."; | |
| 44 return; | |
| 45 } | |
| 46 | |
| 47 MojoResult result = main_function(app_handle.get()); | |
| 48 if (result < MOJO_RESULT_OK) { | |
| 49 LOG(ERROR) << "MojoMain returned an error: " << result; | |
| 50 return; | |
| 51 } | |
| 52 | |
| 53 LOG(INFO) << "MojoMain succeeded: " << result; | |
| 54 } | |
| 55 | |
| 56 AppContainer::AppContainer(Context* context) | 26 AppContainer::AppContainer(Context* context) |
| 57 : context_(context), | 27 : context_(context), |
| 28 app_handle_raw_(mojo::kInvalidHandle), | |
| 58 weak_factory_(this) { | 29 weak_factory_(this) { |
| 59 } | 30 } |
| 60 | 31 |
| 61 AppContainer::~AppContainer() { | 32 AppContainer::~AppContainer() { |
| 62 } | 33 } |
| 63 | 34 |
| 64 void AppContainer::Load(const GURL& app_url) { | 35 void AppContainer::Load(const GURL& app_url) { |
| 65 request_ = context_->loader()->Load(app_url, this); | 36 request_ = context_->loader()->Load(app_url, this); |
| 66 } | 37 } |
| 67 | 38 |
| 68 void AppContainer::DidCompleteLoad(const GURL& app_url, | 39 void AppContainer::DidCompleteLoad(const GURL& app_url, |
| 69 const base::FilePath& app_path) { | 40 const base::FilePath& app_path) { |
| 70 Handle shell_handle; | 41 Handle shell_handle; |
| 71 Handle app_handle; | 42 MojoResult result = CreateMessagePipe(&shell_handle, &app_handle_raw_); |
| 72 MojoResult result = CreateMessagePipe(&shell_handle, &app_handle); | |
| 73 if (result < MOJO_RESULT_OK) { | 43 if (result < MOJO_RESULT_OK) { |
| 74 // Failure.. | 44 // Failure.. |
| 75 } | 45 } |
| 76 | 46 |
| 77 hello_world_service_.reset( | 47 hello_world_service_.reset( |
| 78 new examples::HelloWorldServiceImpl(shell_handle)); | 48 new examples::HelloWorldServiceImpl(shell_handle)); |
| 79 | 49 |
| 80 // Launch the app on its own thread. | 50 // Launch the app on its own thread. |
| 81 // TODO(beng): Create a unique thread name. | 51 // TODO(beng): Create a unique thread name. |
| 82 thread_.reset(new base::Thread("app_thread")); | 52 app_path_ = app_path; |
| 53 main_message_loop_proxy_ = base::MessageLoopProxy::current(); | |
| 54 thread_.reset(new base::DelegateSimpleThread(this, "app_thread")); | |
| 83 thread_->Start(); | 55 thread_->Start(); |
| 84 thread_->message_loop_proxy()->PostTaskAndReply( | |
| 85 FROM_HERE, | |
| 86 base::Bind(&LaunchAppOnThread, app_path, app_handle), | |
| 87 base::Bind(&AppContainer::AppCompleted, weak_factory_.GetWeakPtr())); | |
| 88 | 56 |
| 89 // TODO(beng): This should be created on demand by the NativeViewportService | 57 // TODO(beng): This should be created on demand by the NativeViewportService |
| 90 // when it is retrieved by the app. | 58 // when it is retrieved by the app. |
| 91 // native_viewport_controller_.reset( | 59 // native_viewport_controller_.reset( |
| 92 // new services::NativeViewportController(context_, shell_handle_)); | 60 // new services::NativeViewportController(context_, shell_handle_)); |
| 93 } | 61 } |
| 94 | 62 |
| 63 void AppContainer::Run() { | |
| 64 base::ScopedClosureRunner app_deleter( | |
| 65 base::Bind(base::IgnoreResult(&base::DeleteFile), app_path_, false)); | |
| 66 ScopedHandle app_handle(app_handle_raw_); | |
| 67 | |
| 68 base::ScopedNativeLibrary app_library( | |
| 69 base::LoadNativeLibrary(app_path_, NULL)); | |
| 70 if (!app_library.is_valid()) { | |
| 71 LOG(ERROR) << "Failed to load library: " << app_path_.value().c_str(); | |
| 72 return; | |
| 73 } | |
| 74 | |
| 75 MojoMainFunction main_function = reinterpret_cast<MojoMainFunction>( | |
| 76 app_library.GetFunctionPointer("MojoMain")); | |
| 77 if (!main_function) { | |
| 78 LOG(ERROR) << "Entrypoint MojoMain not found."; | |
| 79 return; | |
| 80 } | |
| 81 | |
| 82 printf("Calling main_function\n"); | |
| 83 MojoResult result = main_function(app_handle.get()); | |
| 84 if (result < MOJO_RESULT_OK) { | |
| 85 LOG(ERROR) << "MojoMain returned an error: " << result; | |
| 86 return; | |
| 87 } | |
| 88 LOG(INFO) << "MojoMain succeeded: " << result; | |
| 89 main_message_loop_proxy_->PostTask( | |
| 90 FROM_HERE, | |
| 91 base::Bind(&AppContainer::AppCompleted, weak_factory_.GetWeakPtr())); | |
|
darin (slow to review)
2013/11/15 00:01:37
WeakPtr is not thread-safe. you need to call GetWe
DaveMoore
2013/11/15 00:11:33
Done.
| |
| 92 } | |
| 93 | |
| 95 void AppContainer::AppCompleted() { | 94 void AppContainer::AppCompleted() { |
| 96 hello_world_service_.reset(); | 95 hello_world_service_.reset(); |
| 97 // TODO(aa): This code gets replaced once we have a service manager. | 96 // TODO(aa): This code gets replaced once we have a service manager. |
| 98 // native_viewport_controller_->Close(); | 97 // native_viewport_controller_->Close(); |
| 99 | 98 |
| 99 thread_->Join(); | |
| 100 thread_.reset(); | 100 thread_.reset(); |
| 101 } | 101 } |
| 102 | 102 |
| 103 } // namespace shell | 103 } // namespace shell |
| 104 } // namespace mojo | 104 } // namespace mojo |
| OLD | NEW |