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