Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / mojo / runner / in_process_native_runner.cc
blobff4f0ba3ad54372793a27b4015488c3016bbfd89
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.
5 #include "mojo/runner/in_process_native_runner.h"
7 #include "base/bind.h"
8 #include "base/callback_helpers.h"
9 #include "base/location.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "base/threading/platform_thread.h"
12 #include "mojo/runner/native_application_support.h"
14 namespace mojo {
15 namespace runner {
17 InProcessNativeRunner::InProcessNativeRunner(Context* context)
18 : app_library_(nullptr) {}
20 InProcessNativeRunner::~InProcessNativeRunner() {
21 // It is important to let the thread exit before unloading the DSO (when
22 // app_library_ is destructed), because the library may have registered
23 // thread-local data and destructors to run on thread termination.
24 if (thread_) {
25 DCHECK(thread_->HasBeenStarted());
26 DCHECK(!thread_->HasBeenJoined());
27 thread_->Join();
31 void InProcessNativeRunner::Start(
32 const base::FilePath& app_path,
33 bool start_sandboxed,
34 InterfaceRequest<Application> application_request,
35 const base::Closure& app_completed_callback) {
36 app_path_ = app_path;
38 DCHECK(!application_request_.is_pending());
39 application_request_ = application_request.Pass();
41 DCHECK(app_completed_callback_runner_.is_null());
42 app_completed_callback_runner_ = base::Bind(
43 &base::TaskRunner::PostTask, base::ThreadTaskRunnerHandle::Get(),
44 FROM_HERE, app_completed_callback);
46 DCHECK(!thread_);
47 thread_.reset(new base::DelegateSimpleThread(this, "app_thread"));
48 thread_->Start();
51 void InProcessNativeRunner::Run() {
52 DVLOG(2) << "Loading/running Mojo app in process from library: "
53 << app_path_.value()
54 << " thread id=" << base::PlatformThread::CurrentId();
56 // TODO(vtl): ScopedNativeLibrary doesn't have a .get() method!
57 base::NativeLibrary app_library = LoadNativeApplication(app_path_);
58 app_library_.Reset(app_library);
59 RunNativeApplication(app_library, application_request_.Pass());
60 app_completed_callback_runner_.Run();
61 app_completed_callback_runner_.Reset();
64 scoped_ptr<shell::NativeRunner> InProcessNativeRunnerFactory::Create() {
65 return make_scoped_ptr(new InProcessNativeRunner(context_));
68 } // namespace runner
69 } // namespace mojo