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 "mojo/shell/native_application_support.h" |
| 6 |
| 7 #include "base/files/file_path.h" |
| 8 #include "base/files/file_util.h" |
| 9 #include "base/logging.h" |
| 10 #include "mojo/public/platform/native/gles2_impl_chromium_miscellaneous_thunks.h
" |
| 11 #include "mojo/public/platform/native/gles2_impl_chromium_sub_image_thunks.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_occlusion_query_ext_thunks.h" |
| 15 #include "mojo/public/platform/native/gles2_impl_thunks.h" |
| 16 #include "mojo/public/platform/native/gles2_thunks.h" |
| 17 #include "mojo/public/platform/native/system_thunks.h" |
| 18 |
| 19 namespace mojo { |
| 20 namespace shell { |
| 21 |
| 22 namespace { |
| 23 |
| 24 template <typename Thunks> |
| 25 bool SetThunks(Thunks (*make_thunks)(), |
| 26 const char* function_name, |
| 27 base::NativeLibrary library) { |
| 28 typedef size_t (*SetThunksFn)(const Thunks* thunks); |
| 29 SetThunksFn set_thunks = reinterpret_cast<SetThunksFn>( |
| 30 base::GetFunctionPointerFromNativeLibrary(library, function_name)); |
| 31 if (!set_thunks) |
| 32 return false; |
| 33 Thunks thunks = make_thunks(); |
| 34 size_t expected_size = set_thunks(&thunks); |
| 35 if (expected_size > sizeof(Thunks)) { |
| 36 LOG(ERROR) << "Invalid app library: expected " << function_name |
| 37 << " to return thunks of size: " << expected_size; |
| 38 return false; |
| 39 } |
| 40 return true; |
| 41 } |
| 42 |
| 43 } // namespace |
| 44 |
| 45 base::NativeLibrary LoadNativeApplication(const base::FilePath& app_path, |
| 46 NativeApplicationCleanup cleanup) { |
| 47 DVLOG(2) << "Loading Mojo app in process from library: " << app_path.value(); |
| 48 |
| 49 base::NativeLibraryLoadError error; |
| 50 base::NativeLibrary app_library = base::LoadNativeLibrary(app_path, &error); |
| 51 if (cleanup == NativeApplicationCleanup::DELETE) |
| 52 DeleteFile(app_path, false); |
| 53 LOG_IF(ERROR, !app_library) |
| 54 << "Failed to load app library (error: " << error.ToString() << ")"; |
| 55 return app_library; |
| 56 } |
| 57 |
| 58 bool RunNativeApplication(base::NativeLibrary app_library, |
| 59 InterfaceRequest<Application> application_request) { |
| 60 // Tolerate |app_library| being null, to make life easier for callers. |
| 61 if (!app_library) |
| 62 return false; |
| 63 |
| 64 if (!SetThunks(&MojoMakeSystemThunks, "MojoSetSystemThunks", app_library)) { |
| 65 LOG(ERROR) << "MojoSetSystemThunks not found"; |
| 66 return false; |
| 67 } |
| 68 |
| 69 if (SetThunks(&MojoMakeGLES2ControlThunks, "MojoSetGLES2ControlThunks", |
| 70 app_library)) { |
| 71 // If we have the control thunks, we should also have the GLES2 |
| 72 // implementation thunks. |
| 73 if (!SetThunks(&MojoMakeGLES2ImplThunks, "MojoSetGLES2ImplThunks", |
| 74 app_library)) { |
| 75 LOG(ERROR) |
| 76 << "MojoSetGLES2ControlThunks found, but not MojoSetGLES2ImplThunks"; |
| 77 return false; |
| 78 } |
| 79 |
| 80 // If the application is using GLES2 extension points, register those |
| 81 // thunks. Applications may use or not use any of these, so don't warn if |
| 82 // they are missing. |
| 83 SetThunks(MojoMakeGLES2ImplChromiumMiscellaneousThunks, |
| 84 "MojoSetGLES2ImplChromiumMiscellaneousThunks", app_library); |
| 85 SetThunks(MojoMakeGLES2ImplChromiumSubImageThunks, |
| 86 "MojoSetGLES2ImplChromiumSubImageThunks", app_library); |
| 87 SetThunks(MojoMakeGLES2ImplChromiumTextureMailboxThunks, |
| 88 "MojoSetGLES2ImplChromiumTextureMailboxThunks", app_library); |
| 89 SetThunks(MojoMakeGLES2ImplChromiumSyncPointThunks, |
| 90 "MojoSetGLES2ImplChromiumSyncPointThunks", app_library); |
| 91 SetThunks(MojoMakeGLES2ImplOcclusionQueryExtThunks, |
| 92 "MojoSetGLES2ImplOcclusionQueryExtThunks", app_library); |
| 93 } |
| 94 // Unlike system thunks, we don't warn on a lack of GLES2 thunks because |
| 95 // not everything is a visual app. |
| 96 |
| 97 // Go shared library support requires us to initialize the runtime before we |
| 98 // start running any go code. This is a temporary patch. |
| 99 typedef void (*InitGoRuntimeFn)(); |
| 100 InitGoRuntimeFn init_go_runtime = reinterpret_cast<InitGoRuntimeFn>( |
| 101 base::GetFunctionPointerFromNativeLibrary(app_library, "InitGoRuntime")); |
| 102 if (init_go_runtime) { |
| 103 DVLOG(2) << "InitGoRuntime: Initializing Go Runtime found in app"; |
| 104 init_go_runtime(); |
| 105 } |
| 106 |
| 107 typedef MojoResult (*MojoMainFunction)(MojoHandle); |
| 108 MojoMainFunction main_function = reinterpret_cast<MojoMainFunction>( |
| 109 base::GetFunctionPointerFromNativeLibrary(app_library, "MojoMain")); |
| 110 if (!main_function) { |
| 111 LOG(ERROR) << "MojoMain not found"; |
| 112 return false; |
| 113 } |
| 114 // |MojoMain()| takes ownership of the service handle. |
| 115 MojoHandle handle = application_request.PassMessagePipe().release().value(); |
| 116 MojoResult result = main_function(handle); |
| 117 if (result < MOJO_RESULT_OK) { |
| 118 LOG(ERROR) << "MojoMain returned error (result: " << result << ")"; |
| 119 } |
| 120 return true; |
| 121 } |
| 122 |
| 123 } // namespace shell |
| 124 } // namespace mojo |
OLD | NEW |