| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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/shell/runner/host/in_process_native_runner.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/callback_helpers.h" | |
| 11 #include "base/location.h" | |
| 12 #include "base/strings/utf_string_conversions.h" | |
| 13 #include "base/task_runner.h" | |
| 14 #include "base/thread_task_runner_handle.h" | |
| 15 #include "base/threading/platform_thread.h" | |
| 16 #include "mojo/public/cpp/bindings/interface_request.h" | |
| 17 #include "mojo/shell/runner/host/native_application_support.h" | |
| 18 #include "mojo/shell/runner/host/out_of_process_native_runner.h" | |
| 19 #include "mojo/shell/runner/init.h" | |
| 20 | |
| 21 namespace mojo { | |
| 22 namespace shell { | |
| 23 | |
| 24 InProcessNativeRunner::InProcessNativeRunner() : app_library_(nullptr) {} | |
| 25 | |
| 26 InProcessNativeRunner::~InProcessNativeRunner() { | |
| 27 // It is important to let the thread exit before unloading the DSO (when | |
| 28 // app_library_ is destructed), because the library may have registered | |
| 29 // thread-local data and destructors to run on thread termination. | |
| 30 if (thread_) { | |
| 31 DCHECK(thread_->HasBeenStarted()); | |
| 32 DCHECK(!thread_->HasBeenJoined()); | |
| 33 thread_->Join(); | |
| 34 } | |
| 35 } | |
| 36 | |
| 37 mojom::ShellClientPtr InProcessNativeRunner::Start( | |
| 38 const base::FilePath& app_path, | |
| 39 const Identity& target, | |
| 40 bool start_sandboxed, | |
| 41 const base::Callback<void(base::ProcessId)>& pid_available_callback, | |
| 42 const base::Closure& app_completed_callback) { | |
| 43 app_path_ = app_path; | |
| 44 | |
| 45 DCHECK(!request_.is_pending()); | |
| 46 mojom::ShellClientPtr client; | |
| 47 request_ = GetProxy(&client); | |
| 48 | |
| 49 DCHECK(app_completed_callback_runner_.is_null()); | |
| 50 app_completed_callback_runner_ = base::Bind( | |
| 51 &base::TaskRunner::PostTask, base::ThreadTaskRunnerHandle::Get(), | |
| 52 FROM_HERE, app_completed_callback); | |
| 53 | |
| 54 DCHECK(!thread_); | |
| 55 std::string thread_name = "mojo:app_thread"; | |
| 56 #if defined(OS_WIN) | |
| 57 thread_name = base::WideToUTF8(app_path_.BaseName().value()); | |
| 58 #endif | |
| 59 thread_.reset(new base::DelegateSimpleThread(this, thread_name)); | |
| 60 thread_->Start(); | |
| 61 pid_available_callback.Run(base::kNullProcessId); | |
| 62 | |
| 63 return client; | |
| 64 } | |
| 65 | |
| 66 void InProcessNativeRunner::Run() { | |
| 67 DVLOG(2) << "Loading/running Mojo app in process from library: " | |
| 68 << app_path_.value() | |
| 69 << " thread id=" << base::PlatformThread::CurrentId(); | |
| 70 | |
| 71 // TODO(vtl): ScopedNativeLibrary doesn't have a .get() method! | |
| 72 base::NativeLibrary app_library = LoadNativeApplication(app_path_); | |
| 73 app_library_.Reset(app_library); | |
| 74 // This hangs on Windows in the component build, so skip it since it's | |
| 75 // unnecessary. | |
| 76 #if !(defined(COMPONENT_BUILD) && defined(OS_WIN)) | |
| 77 CallLibraryEarlyInitialization(app_library); | |
| 78 #endif | |
| 79 RunNativeApplication(app_library, std::move(request_)); | |
| 80 app_completed_callback_runner_.Run(); | |
| 81 app_completed_callback_runner_.Reset(); | |
| 82 } | |
| 83 | |
| 84 scoped_ptr<NativeRunner> InProcessNativeRunnerFactory::Create( | |
| 85 const base::FilePath& app_path) { | |
| 86 // Non-Mojo apps are always run in a new process. | |
| 87 if (!app_path.MatchesExtension(FILE_PATH_LITERAL(".mojo"))) { | |
| 88 return make_scoped_ptr( | |
| 89 new OutOfProcessNativeRunner(launch_process_runner_, nullptr)); | |
| 90 } | |
| 91 return make_scoped_ptr(new InProcessNativeRunner); | |
| 92 } | |
| 93 | |
| 94 } // namespace shell | |
| 95 } // namespace mojo | |
| OLD | NEW |