Remove 'RemoveTrailingSeparators' function from SimpleMenuModel
[chromium-blink-merge.git] / mojo / shell / native_application_support.cc
blob9df3ed887f0975f090035e082af7ef672ad9d1fe
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/shell/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/public/platform/native/gles2_impl_chromium_miscellaneous_thunks.h"
12 #include "mojo/public/platform/native/gles2_impl_chromium_sub_image_thunks.h"
13 #include "mojo/public/platform/native/gles2_impl_chromium_sync_point_thunks.h"
14 #include "mojo/public/platform/native/gles2_impl_chromium_texture_mailbox_thunks.h"
15 #include "mojo/public/platform/native/gles2_impl_occlusion_query_ext_thunks.h"
16 #include "mojo/public/platform/native/gles2_impl_thunks.h"
17 #include "mojo/public/platform/native/gles2_thunks.h"
18 #include "mojo/public/platform/native/system_thunks.h"
20 namespace mojo {
21 namespace shell {
23 namespace {
25 template <typename Thunks>
26 bool SetThunks(Thunks (*make_thunks)(),
27 const char* function_name,
28 base::NativeLibrary library) {
29 typedef size_t (*SetThunksFn)(const Thunks* thunks);
30 SetThunksFn set_thunks = reinterpret_cast<SetThunksFn>(
31 base::GetFunctionPointerFromNativeLibrary(library, function_name));
32 if (!set_thunks)
33 return false;
34 Thunks thunks = make_thunks();
35 size_t expected_size = set_thunks(&thunks);
36 if (expected_size > sizeof(Thunks)) {
37 LOG(ERROR) << "Invalid app library: expected " << function_name
38 << " to return thunks of size: " << expected_size;
39 return false;
41 return true;
44 } // namespace
46 base::NativeLibrary LoadNativeApplication(const base::FilePath& app_path,
47 NativeApplicationCleanup cleanup) {
48 DVLOG(2) << "Loading Mojo app in process from library: " << app_path.value();
50 base::NativeLibraryLoadError error;
51 base::NativeLibrary app_library = base::LoadNativeLibrary(app_path, &error);
52 if (cleanup == NativeApplicationCleanup::DELETE)
53 DeleteFile(app_path, false);
54 LOG_IF(ERROR, !app_library)
55 << "Failed to load app library (error: " << error.ToString() << ")";
56 return app_library;
59 bool RunNativeApplication(base::NativeLibrary app_library,
60 InterfaceRequest<Application> application_request) {
61 // Tolerate |app_library| being null, to make life easier for callers.
62 if (!app_library)
63 return false;
65 if (!SetThunks(&MojoMakeSystemThunks, "MojoSetSystemThunks", app_library)) {
66 LOG(ERROR) << "MojoSetSystemThunks not found";
67 return false;
70 if (SetThunks(&MojoMakeGLES2ControlThunks, "MojoSetGLES2ControlThunks",
71 app_library)) {
72 // If we have the control thunks, we should also have the GLES2
73 // implementation thunks.
74 if (!SetThunks(&MojoMakeGLES2ImplThunks, "MojoSetGLES2ImplThunks",
75 app_library)) {
76 LOG(ERROR)
77 << "MojoSetGLES2ControlThunks found, but not MojoSetGLES2ImplThunks";
78 return false;
81 // If the application is using GLES2 extension points, register those
82 // thunks. Applications may use or not use any of these, so don't warn if
83 // they are missing.
84 SetThunks(MojoMakeGLES2ImplChromiumMiscellaneousThunks,
85 "MojoSetGLES2ImplChromiumMiscellaneousThunks", app_library);
86 SetThunks(MojoMakeGLES2ImplChromiumSubImageThunks,
87 "MojoSetGLES2ImplChromiumSubImageThunks", app_library);
88 SetThunks(MojoMakeGLES2ImplChromiumTextureMailboxThunks,
89 "MojoSetGLES2ImplChromiumTextureMailboxThunks", app_library);
90 SetThunks(MojoMakeGLES2ImplChromiumSyncPointThunks,
91 "MojoSetGLES2ImplChromiumSyncPointThunks", app_library);
92 SetThunks(MojoMakeGLES2ImplOcclusionQueryExtThunks,
93 "MojoSetGLES2ImplOcclusionQueryExtThunks", app_library);
95 // Unlike system thunks, we don't warn on a lack of GLES2 thunks because
96 // not everything is a visual app.
98 // Go shared library support requires us to initialize the runtime before we
99 // start running any go code. This is a temporary patch.
100 typedef void (*InitGoRuntimeFn)();
101 InitGoRuntimeFn init_go_runtime = reinterpret_cast<InitGoRuntimeFn>(
102 base::GetFunctionPointerFromNativeLibrary(app_library, "InitGoRuntime"));
103 if (init_go_runtime) {
104 DVLOG(2) << "InitGoRuntime: Initializing Go Runtime found in app";
105 init_go_runtime();
108 #if !defined(OS_WIN)
109 // On Windows, initializing base::CommandLine with null parameters gets the
110 // process's command line from the OS. Other platforms need it to be passed
111 // in. This needs to be passed in before the app initializes the command line,
112 // which is done as soon as it loads.
113 typedef void (*InitCommandLineArgs)(int, const char* const*);
114 InitCommandLineArgs init_command_line_args =
115 reinterpret_cast<InitCommandLineArgs>(
116 base::GetFunctionPointerFromNativeLibrary(app_library,
117 "InitCommandLineArgs"));
118 if (init_command_line_args) {
119 int argc = 0;
120 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
121 const char** argv = new const char* [cmd_line->argv().size()];
122 for (auto& arg : cmd_line->argv())
123 argv[argc++] = arg.c_str();
124 init_command_line_args(argc, argv);
126 #endif
128 typedef MojoResult (*MojoMainFunction)(MojoHandle);
129 MojoMainFunction main_function = reinterpret_cast<MojoMainFunction>(
130 base::GetFunctionPointerFromNativeLibrary(app_library, "MojoMain"));
131 if (!main_function) {
132 LOG(ERROR) << "MojoMain not found";
133 return false;
135 // |MojoMain()| takes ownership of the service handle.
136 MojoHandle handle = application_request.PassMessagePipe().release().value();
137 MojoResult result = main_function(handle);
138 if (result < MOJO_RESULT_OK) {
139 LOG(ERROR) << "MojoMain returned error (result: " << result << ")";
141 return true;
144 } // namespace shell
145 } // namespace mojo