Roll src/third_party/WebKit a05b987:7eb2976 (svn 202510:202511)
[chromium-blink-merge.git] / mojo / runner / native_application_support.cc
blob02af9ddd84ffaf65f97978f41c0fb0e644b2645d
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_copy_texture_thunks.h"
13 #include "mojo/public/platform/native/gles2_impl_chromium_framebuffer_multisample_thunks.h"
14 #include "mojo/public/platform/native/gles2_impl_chromium_image_thunks.h"
15 #include "mojo/public/platform/native/gles2_impl_chromium_miscellaneous_thunks.h"
16 #include "mojo/public/platform/native/gles2_impl_chromium_pixel_transfer_buffer_object_thunks.h"
17 #include "mojo/public/platform/native/gles2_impl_chromium_sub_image_thunks.h"
18 #include "mojo/public/platform/native/gles2_impl_chromium_sync_point_thunks.h"
19 #include "mojo/public/platform/native/gles2_impl_chromium_texture_mailbox_thunks.h"
20 #include "mojo/public/platform/native/gles2_impl_occlusion_query_ext_thunks.h"
21 #include "mojo/public/platform/native/gles2_impl_thunks.h"
22 #include "mojo/public/platform/native/gles2_thunks.h"
23 #include "mojo/public/platform/native/system_thunks.h"
25 namespace mojo {
26 namespace runner {
28 namespace {
30 template <typename Thunks>
31 bool SetThunks(Thunks (*make_thunks)(),
32 const char* function_name,
33 base::NativeLibrary library) {
34 typedef size_t (*SetThunksFn)(const Thunks* thunks);
35 SetThunksFn set_thunks = reinterpret_cast<SetThunksFn>(
36 base::GetFunctionPointerFromNativeLibrary(library, function_name));
37 if (!set_thunks)
38 return false;
39 Thunks thunks = make_thunks();
40 size_t expected_size = set_thunks(&thunks);
41 if (expected_size > sizeof(Thunks)) {
42 LOG(ERROR) << "Invalid app library: expected " << function_name
43 << " to return thunks of size: " << expected_size;
44 return false;
46 return true;
49 } // namespace
51 base::NativeLibrary LoadNativeApplication(const base::FilePath& app_path) {
52 DVLOG(2) << "Loading Mojo app in process from library: " << app_path.value();
54 base::NativeLibraryLoadError error;
55 base::NativeLibrary app_library = base::LoadNativeLibrary(app_path, &error);
56 LOG_IF(ERROR, !app_library)
57 << "Failed to load app library (error: " << error.ToString() << ")";
58 return app_library;
61 bool RunNativeApplication(base::NativeLibrary app_library,
62 InterfaceRequest<Application> application_request) {
63 // Tolerate |app_library| being null, to make life easier for callers.
64 if (!app_library)
65 return false;
67 if (!SetThunks(&MojoMakeSystemThunks, "MojoSetSystemThunks", app_library)) {
68 LOG(ERROR) << "MojoSetSystemThunks not found";
69 return false;
72 if (SetThunks(&MojoMakeGLES2ControlThunks, "MojoSetGLES2ControlThunks",
73 app_library)) {
74 // If we have the control thunks, we should also have the GLES2
75 // implementation thunks.
76 if (!SetThunks(&MojoMakeGLES2ImplThunks, "MojoSetGLES2ImplThunks",
77 app_library)) {
78 LOG(ERROR)
79 << "MojoSetGLES2ControlThunks found, but not MojoSetGLES2ImplThunks";
80 return false;
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
85 // they are missing.
86 SetThunks(MojoMakeGLES2ImplChromiumCopyTextureThunks,
87 "MojoSetGLES2ImplChromiumCopyTextureThunks", app_library);
88 SetThunks(MojoMakeGLES2ImplChromiumFramebufferMultisampleThunks,
89 "MojoSetGLES2ImplChromiumFramebufferMultisampleThunks",
90 app_library);
91 SetThunks(MojoMakeGLES2ImplChromiumImageThunks,
92 "MojoSetGLES2ImplChromiumImageThunks", app_library);
93 SetThunks(MojoMakeGLES2ImplChromiumMiscellaneousThunks,
94 "MojoSetGLES2ImplChromiumMiscellaneousThunks", app_library);
95 SetThunks(MojoMakeGLES2ImplChromiumPixelTransferBufferObjectThunks,
96 "MojoSetGLES2ImplChromiumPixelTransferBufferObjectThunks", app_library);
97 SetThunks(MojoMakeGLES2ImplChromiumSubImageThunks,
98 "MojoSetGLES2ImplChromiumSubImageThunks", app_library);
99 SetThunks(MojoMakeGLES2ImplChromiumTextureMailboxThunks,
100 "MojoSetGLES2ImplChromiumTextureMailboxThunks", app_library);
101 SetThunks(MojoMakeGLES2ImplChromiumSyncPointThunks,
102 "MojoSetGLES2ImplChromiumSyncPointThunks", app_library);
103 SetThunks(MojoMakeGLES2ImplOcclusionQueryExtThunks,
104 "MojoSetGLES2ImplOcclusionQueryExtThunks", app_library);
106 // Unlike system thunks, we don't warn on a lack of GLES2 thunks because
107 // not everything is a visual app.
109 // Go shared library support requires us to initialize the runtime before we
110 // start running any go code. This is a temporary patch.
111 typedef void (*InitGoRuntimeFn)();
112 InitGoRuntimeFn init_go_runtime = reinterpret_cast<InitGoRuntimeFn>(
113 base::GetFunctionPointerFromNativeLibrary(app_library, "InitGoRuntime"));
114 if (init_go_runtime) {
115 DVLOG(2) << "InitGoRuntime: Initializing Go Runtime found in app";
116 init_go_runtime();
119 #if !defined(OS_WIN)
120 // On Windows, initializing base::CommandLine with null parameters gets the
121 // process's command line from the OS. Other platforms need it to be passed
122 // in. This needs to be passed in before the app initializes the command line,
123 // which is done as soon as it loads.
124 typedef void (*InitCommandLineArgs)(int, const char* const*);
125 InitCommandLineArgs init_command_line_args =
126 reinterpret_cast<InitCommandLineArgs>(
127 base::GetFunctionPointerFromNativeLibrary(app_library,
128 "InitCommandLineArgs"));
129 if (init_command_line_args) {
130 int argc = 0;
131 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
132 const char** argv = new const char* [cmd_line->argv().size()];
133 for (auto& arg : cmd_line->argv())
134 argv[argc++] = arg.c_str();
135 init_command_line_args(argc, argv);
137 #endif
139 // Apps need not include platform handle thunks.
140 SetThunks(&MojoMakePlatformHandlePrivateThunks,
141 "MojoSetPlatformHandlePrivateThunks", app_library);
143 typedef MojoResult (*MojoMainFunction)(MojoHandle);
144 MojoMainFunction main_function = reinterpret_cast<MojoMainFunction>(
145 base::GetFunctionPointerFromNativeLibrary(app_library, "MojoMain"));
146 if (!main_function) {
147 LOG(ERROR) << "MojoMain not found";
148 return false;
150 // |MojoMain()| takes ownership of the service handle.
151 MojoHandle handle = application_request.PassMessagePipe().release().value();
152 MojoResult result = main_function(handle);
153 if (result != MOJO_RESULT_OK) {
154 LOG(ERROR) << "MojoMain returned error (result: " << result << ")";
156 return true;
159 } // namespace runner
160 } // namespace mojo