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/native_application_support.h"
7 #include "base/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h"
10 #include "base/logging.h"
11 #include "mojo/platform_handle/platform_handle_private_thunks.h"
12 #include "mojo/public/platform/native/gles2_impl_chromium_miscellaneous_thunks.h"
13 #include "mojo/public/platform/native/gles2_impl_chromium_sub_image_thunks.h"
14 #include "mojo/public/platform/native/gles2_impl_chromium_sync_point_thunks.h"
15 #include "mojo/public/platform/native/gles2_impl_chromium_texture_mailbox_thunks.h"
16 #include "mojo/public/platform/native/gles2_impl_occlusion_query_ext_thunks.h"
17 #include "mojo/public/platform/native/gles2_impl_thunks.h"
18 #include "mojo/public/platform/native/gles2_thunks.h"
19 #include "mojo/public/platform/native/system_thunks.h"
26 template <typename Thunks
>
27 bool SetThunks(Thunks (*make_thunks
)(),
28 const char* function_name
,
29 base::NativeLibrary library
) {
30 typedef size_t (*SetThunksFn
)(const Thunks
* thunks
);
31 SetThunksFn set_thunks
= reinterpret_cast<SetThunksFn
>(
32 base::GetFunctionPointerFromNativeLibrary(library
, function_name
));
35 Thunks thunks
= make_thunks();
36 size_t expected_size
= set_thunks(&thunks
);
37 if (expected_size
> sizeof(Thunks
)) {
38 LOG(ERROR
) << "Invalid app library: expected " << function_name
39 << " to return thunks of size: " << expected_size
;
47 base::NativeLibrary
LoadNativeApplication(
48 const base::FilePath
& app_path
,
49 shell::NativeApplicationCleanup cleanup
) {
50 DVLOG(2) << "Loading Mojo app in process from library: " << app_path
.value();
52 base::NativeLibraryLoadError error
;
53 base::NativeLibrary app_library
= base::LoadNativeLibrary(app_path
, &error
);
54 if (cleanup
== shell::NativeApplicationCleanup::DELETE
)
55 DeleteFile(app_path
, false);
56 LOG_IF(ERROR
, !app_library
)
57 << "Failed to load app library (error: " << error
.ToString() << ")";
61 bool RunNativeApplication(base::NativeLibrary app_library
,
62 InterfaceRequest
<Application
> application_request
) {
63 // Tolerate |app_library| being null, to make life easier for callers.
67 if (!SetThunks(&MojoMakeSystemThunks
, "MojoSetSystemThunks", app_library
)) {
68 LOG(ERROR
) << "MojoSetSystemThunks not found";
72 if (SetThunks(&MojoMakeGLES2ControlThunks
, "MojoSetGLES2ControlThunks",
74 // If we have the control thunks, we should also have the GLES2
75 // implementation thunks.
76 if (!SetThunks(&MojoMakeGLES2ImplThunks
, "MojoSetGLES2ImplThunks",
79 << "MojoSetGLES2ControlThunks found, but not MojoSetGLES2ImplThunks";
83 // If the application is using GLES2 extension points, register those
84 // thunks. Applications may use or not use any of these, so don't warn if
86 SetThunks(MojoMakeGLES2ImplChromiumMiscellaneousThunks
,
87 "MojoSetGLES2ImplChromiumMiscellaneousThunks", app_library
);
88 SetThunks(MojoMakeGLES2ImplChromiumSubImageThunks
,
89 "MojoSetGLES2ImplChromiumSubImageThunks", app_library
);
90 SetThunks(MojoMakeGLES2ImplChromiumTextureMailboxThunks
,
91 "MojoSetGLES2ImplChromiumTextureMailboxThunks", app_library
);
92 SetThunks(MojoMakeGLES2ImplChromiumSyncPointThunks
,
93 "MojoSetGLES2ImplChromiumSyncPointThunks", app_library
);
94 SetThunks(MojoMakeGLES2ImplOcclusionQueryExtThunks
,
95 "MojoSetGLES2ImplOcclusionQueryExtThunks", app_library
);
97 // Unlike system thunks, we don't warn on a lack of GLES2 thunks because
98 // not everything is a visual app.
100 // Go shared library support requires us to initialize the runtime before we
101 // start running any go code. This is a temporary patch.
102 typedef void (*InitGoRuntimeFn
)();
103 InitGoRuntimeFn init_go_runtime
= reinterpret_cast<InitGoRuntimeFn
>(
104 base::GetFunctionPointerFromNativeLibrary(app_library
, "InitGoRuntime"));
105 if (init_go_runtime
) {
106 DVLOG(2) << "InitGoRuntime: Initializing Go Runtime found in app";
111 // On Windows, initializing base::CommandLine with null parameters gets the
112 // process's command line from the OS. Other platforms need it to be passed
113 // in. This needs to be passed in before the app initializes the command line,
114 // which is done as soon as it loads.
115 typedef void (*InitCommandLineArgs
)(int, const char* const*);
116 InitCommandLineArgs init_command_line_args
=
117 reinterpret_cast<InitCommandLineArgs
>(
118 base::GetFunctionPointerFromNativeLibrary(app_library
,
119 "InitCommandLineArgs"));
120 if (init_command_line_args
) {
122 base::CommandLine
* cmd_line
= base::CommandLine::ForCurrentProcess();
123 const char** argv
= new const char* [cmd_line
->argv().size()];
124 for (auto& arg
: cmd_line
->argv())
125 argv
[argc
++] = arg
.c_str();
126 init_command_line_args(argc
, argv
);
130 // Apps need not include platform handle thunks.
131 SetThunks(&MojoMakePlatformHandlePrivateThunks
,
132 "MojoSetPlatformHandlePrivateThunks", app_library
);
134 typedef MojoResult (*MojoMainFunction
)(MojoHandle
);
135 MojoMainFunction main_function
= reinterpret_cast<MojoMainFunction
>(
136 base::GetFunctionPointerFromNativeLibrary(app_library
, "MojoMain"));
137 if (!main_function
) {
138 LOG(ERROR
) << "MojoMain not found";
141 // |MojoMain()| takes ownership of the service handle.
142 MojoHandle handle
= application_request
.PassMessagePipe().release().value();
143 MojoResult result
= main_function(handle
);
144 if (result
!= MOJO_RESULT_OK
) {
145 LOG(ERROR
) << "MojoMain returned error (result: " << result
<< ")";
150 } // namespace runner