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 "mojo/shell/in_process_dynamic_service_runner.h" | 5 #include "mojo/shell/in_process_dynamic_service_runner.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback_helpers.h" | 8 #include "base/callback_helpers.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/logging.h" | |
11 #include "base/message_loop/message_loop_proxy.h" | 10 #include "base/message_loop/message_loop_proxy.h" |
12 #include "mojo/public/platform/native/gles2_impl_chromium_sync_point_thunks.h" | |
13 #include "mojo/public/platform/native/gles2_impl_chromium_texture_mailbox_thunks
.h" | |
14 #include "mojo/public/platform/native/gles2_impl_thunks.h" | |
15 #include "mojo/public/platform/native/gles2_thunks.h" | |
16 #include "mojo/public/platform/native/system_thunks.h" | |
17 | 11 |
18 namespace mojo { | 12 namespace mojo { |
19 namespace shell { | 13 namespace shell { |
20 | 14 |
21 namespace { | |
22 | |
23 template <typename Thunks> | |
24 bool SetThunks(Thunks (*make_thunks)(), | |
25 const char* function_name, | |
26 base::ScopedNativeLibrary* library) { | |
27 typedef size_t (*SetThunksFn)(const Thunks* thunks); | |
28 SetThunksFn set_thunks = | |
29 reinterpret_cast<SetThunksFn>(library->GetFunctionPointer(function_name)); | |
30 if (!set_thunks) | |
31 return false; | |
32 Thunks thunks = make_thunks(); | |
33 size_t expected_size = set_thunks(&thunks); | |
34 if (expected_size > sizeof(Thunks)) { | |
35 LOG(ERROR) << "Invalid app library: expected " << function_name | |
36 << " to return thunks of size: " << expected_size; | |
37 return false; | |
38 } | |
39 return true; | |
40 } | |
41 } | |
42 | |
43 InProcessDynamicServiceRunner::InProcessDynamicServiceRunner( | 15 InProcessDynamicServiceRunner::InProcessDynamicServiceRunner( |
44 Context* context) { | 16 Context* context) : app_library_(nullptr) { |
45 } | 17 } |
46 | 18 |
47 InProcessDynamicServiceRunner::~InProcessDynamicServiceRunner() { | 19 InProcessDynamicServiceRunner::~InProcessDynamicServiceRunner() { |
48 if (thread_) { | 20 if (thread_) { |
49 DCHECK(thread_->HasBeenStarted()); | 21 DCHECK(thread_->HasBeenStarted()); |
50 DCHECK(!thread_->HasBeenJoined()); | 22 DCHECK(!thread_->HasBeenJoined()); |
51 thread_->Join(); | 23 thread_->Join(); |
52 } | 24 } |
53 | 25 |
54 // It is important to let the thread exit before unloading the DSO because | 26 // It is important to let the thread exit before unloading the DSO because |
55 // the library may have registered thread-local data and destructors to run | 27 // the library may have registered thread-local data and destructors to run |
56 // on thread termination. | 28 // on thread termination. |
57 app_library_.Reset(base::NativeLibrary()); | 29 if (app_library_) |
| 30 base::UnloadNativeLibrary(app_library_); |
58 } | 31 } |
59 | 32 |
60 void InProcessDynamicServiceRunner::Start( | 33 void InProcessDynamicServiceRunner::Start( |
61 const base::FilePath& app_path, | 34 const base::FilePath& app_path, |
62 ScopedMessagePipeHandle service_handle, | 35 ScopedMessagePipeHandle service_handle, |
63 const base::Closure& app_completed_callback) { | 36 const base::Closure& app_completed_callback) { |
64 app_path_ = app_path; | 37 app_path_ = app_path; |
65 | 38 |
66 DCHECK(!service_handle_.is_valid()); | 39 DCHECK(!service_handle_.is_valid()); |
67 service_handle_ = service_handle.Pass(); | 40 service_handle_ = service_handle.Pass(); |
68 | 41 |
69 DCHECK(app_completed_callback_runner_.is_null()); | 42 DCHECK(app_completed_callback_runner_.is_null()); |
70 app_completed_callback_runner_ = base::Bind(&base::TaskRunner::PostTask, | 43 app_completed_callback_runner_ = base::Bind(&base::TaskRunner::PostTask, |
71 base::MessageLoopProxy::current(), | 44 base::MessageLoopProxy::current(), |
72 FROM_HERE, | 45 FROM_HERE, |
73 app_completed_callback); | 46 app_completed_callback); |
74 | 47 |
75 DCHECK(!thread_); | 48 DCHECK(!thread_); |
76 thread_.reset(new base::DelegateSimpleThread(this, "app_thread")); | 49 thread_.reset(new base::DelegateSimpleThread(this, "app_thread")); |
77 thread_->Start(); | 50 thread_->Start(); |
78 } | 51 } |
79 | 52 |
80 void InProcessDynamicServiceRunner::Run() { | 53 void InProcessDynamicServiceRunner::Run() { |
81 DVLOG(2) << "Loading/running Mojo app in process from library: " | 54 DVLOG(2) << "Loading/running Mojo app in process from library: " |
82 << app_path_.value(); | 55 << app_path_.value(); |
83 | 56 |
84 do { | 57 app_library_ = LoadAndRunService(app_path_, service_handle_.Pass()); |
85 base::NativeLibraryLoadError error; | |
86 app_library_.Reset(base::LoadNativeLibrary(app_path_, &error)); | |
87 if (!app_library_.is_valid()) { | |
88 LOG(ERROR) << "Failed to load app library (error: " << error.ToString() | |
89 << ")"; | |
90 break; | |
91 } | |
92 // Go shared library support requires us to initialize the runtime before we | |
93 // start running any go code. This is a temporary patch. | |
94 typedef void (*InitGoRuntimeFn)(); | |
95 InitGoRuntimeFn runtime = reinterpret_cast<InitGoRuntimeFn>( | |
96 app_library_.GetFunctionPointer("InitGoRuntime")); | |
97 if (runtime) { | |
98 DVLOG(2) << "InitGoRuntime: Initializing Go Runtime found in app"; | |
99 runtime(); | |
100 } | |
101 if (!SetThunks( | |
102 &MojoMakeSystemThunks, "MojoSetSystemThunks", &app_library_)) { | |
103 // In the component build, Mojo Apps link against mojo_system_impl. | |
104 #if !defined(COMPONENT_BUILD) | |
105 // Strictly speaking this is not required, but it's very unusual to have | |
106 // an app that doesn't require the basic system library. | |
107 LOG(WARNING) << "MojoSetSystemThunks not found in app library"; | |
108 #endif | |
109 } | |
110 | |
111 if (SetThunks(&MojoMakeGLES2ControlThunks, | |
112 "MojoSetGLES2ControlThunks", | |
113 &app_library_)) { | |
114 // If we have the control thunks, we probably also have the | |
115 // GLES2 implementation thunks. | |
116 if (!SetThunks(&MojoMakeGLES2ImplThunks, | |
117 "MojoSetGLES2ImplThunks", | |
118 &app_library_)) { | |
119 // In the component build, Mojo Apps link against mojo_gles2_impl. | |
120 #if !defined(COMPONENT_BUILD) | |
121 // Warn on this really weird case: The library requires the GLES2 | |
122 // control functions, but doesn't require the GLES2 implementation. | |
123 LOG(WARNING) << app_path_.value() | |
124 << " has MojoSetGLES2ControlThunks, " | |
125 "but doesn't have MojoSetGLES2ImplThunks."; | |
126 #endif | |
127 } | |
128 | |
129 // If the application is using GLES2 extension points, register those | |
130 // thunks. Applications may use or not use any of these, so don't warn if | |
131 // they are missing. | |
132 SetThunks(MojoMakeGLES2ImplChromiumTextureMailboxThunks, | |
133 "MojoSetGLES2ImplChromiumTextureMailboxThunks", | |
134 &app_library_); | |
135 SetThunks(MojoMakeGLES2ImplChromiumSyncPointThunks, | |
136 "MojoSetGLES2ImplChromiumSyncPointThunks", | |
137 &app_library_); | |
138 } | |
139 // Unlike system thunks, we don't warn on a lack of GLES2 thunks because | |
140 // not everything is a visual app. | |
141 | |
142 typedef MojoResult (*MojoMainFunction)(MojoHandle); | |
143 MojoMainFunction main_function = reinterpret_cast<MojoMainFunction>( | |
144 app_library_.GetFunctionPointer("MojoMain")); | |
145 if (!main_function) { | |
146 LOG(ERROR) << "Entrypoint MojoMain not found: " << app_path_.value(); | |
147 break; | |
148 } | |
149 | |
150 // |MojoMain()| takes ownership of the service handle. | |
151 MojoResult result = main_function(service_handle_.release().value()); | |
152 if (result < MOJO_RESULT_OK) | |
153 LOG(ERROR) << "MojoMain returned an error: " << result; | |
154 } while (false); | |
155 | |
156 app_completed_callback_runner_.Run(); | 58 app_completed_callback_runner_.Run(); |
157 app_completed_callback_runner_.Reset(); | 59 app_completed_callback_runner_.Reset(); |
158 } | 60 } |
159 | 61 |
160 } // namespace shell | 62 } // namespace shell |
161 } // namespace mojo | 63 } // namespace mojo |
OLD | NEW |