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