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/runner/host/native_application_support.h" | |
6 | |
7 #include <stddef.h> | |
8 | |
9 #include "base/command_line.h" | |
10 #include "base/files/file_path.h" | |
11 #include "base/files/file_util.h" | |
12 #include "base/logging.h" | |
13 #include "mojo/platform_handle/platform_handle_private_thunks.h" | |
14 #include "mojo/public/platform/native/system_thunks.h" | |
15 | |
16 namespace mojo { | |
17 namespace shell { | |
18 | |
19 namespace { | |
20 | |
21 template <typename Thunks> | |
22 bool SetThunks(Thunks (*make_thunks)(), | |
23 const char* function_name, | |
24 base::NativeLibrary library) { | |
25 typedef size_t (*SetThunksFn)(const Thunks* thunks); | |
26 SetThunksFn set_thunks = reinterpret_cast<SetThunksFn>( | |
27 base::GetFunctionPointerFromNativeLibrary(library, function_name)); | |
28 if (!set_thunks) | |
29 return false; | |
30 Thunks thunks = make_thunks(); | |
31 size_t expected_size = set_thunks(&thunks); | |
32 if (expected_size > sizeof(Thunks)) { | |
33 LOG(ERROR) << "Invalid app library: expected " << function_name | |
34 << " to return thunks of size: " << expected_size; | |
35 return false; | |
36 } | |
37 return true; | |
38 } | |
39 | |
40 } // namespace | |
41 | |
42 base::NativeLibrary LoadNativeApplication(const base::FilePath& app_path) { | |
43 DVLOG(2) << "Loading Mojo app in process from library: " << app_path.value(); | |
44 | |
45 base::NativeLibraryLoadError error; | |
46 base::NativeLibrary app_library = base::LoadNativeLibrary(app_path, &error); | |
47 LOG_IF(ERROR, !app_library) | |
48 << "Failed to load app library (path: " << app_path.value() << ")"; | |
49 return app_library; | |
50 } | |
51 | |
52 bool RunNativeApplication( | |
53 base::NativeLibrary app_library, | |
54 InterfaceRequest<mojom::ShellClient> request) { | |
55 // Tolerate |app_library| being null, to make life easier for callers. | |
56 if (!app_library) | |
57 return false; | |
58 | |
59 // Thunks aren't needed/used in component build, since the thunked methods | |
60 // just live in their own dynamically loaded library. | |
61 #if !defined(COMPONENT_BUILD) | |
62 if (!SetThunks(&MojoMakeSystemThunks, "MojoSetSystemThunks", app_library)) { | |
63 LOG(ERROR) << "MojoSetSystemThunks not found"; | |
64 return false; | |
65 } | |
66 | |
67 #if !defined(OS_WIN) | |
68 // On Windows, initializing base::CommandLine with null parameters gets the | |
69 // process's command line from the OS. Other platforms need it to be passed | |
70 // in. This needs to be passed in before the app initializes the command line, | |
71 // which is done as soon as it loads. | |
72 typedef void (*InitCommandLineArgs)(int, const char* const*); | |
73 InitCommandLineArgs init_command_line_args = | |
74 reinterpret_cast<InitCommandLineArgs>( | |
75 base::GetFunctionPointerFromNativeLibrary(app_library, | |
76 "InitCommandLineArgs")); | |
77 if (init_command_line_args) { | |
78 int argc = 0; | |
79 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); | |
80 const char** argv = new const char*[cmd_line->argv().size()]; | |
81 for (auto& arg : cmd_line->argv()) | |
82 argv[argc++] = arg.c_str(); | |
83 init_command_line_args(argc, argv); | |
84 } | |
85 #endif | |
86 | |
87 // Apps need not include platform handle thunks. | |
88 SetThunks(&MojoMakePlatformHandlePrivateThunks, | |
89 "MojoSetPlatformHandlePrivateThunks", app_library); | |
90 #endif | |
91 | |
92 typedef MojoResult (*MojoMainFunction)(MojoHandle); | |
93 MojoMainFunction main_function = reinterpret_cast<MojoMainFunction>( | |
94 base::GetFunctionPointerFromNativeLibrary(app_library, "MojoMain")); | |
95 if (!main_function) { | |
96 LOG(ERROR) << "MojoMain not found"; | |
97 return false; | |
98 } | |
99 // |MojoMain()| takes ownership of the service handle. | |
100 MojoHandle handle = request.PassMessagePipe().release().value(); | |
101 MojoResult result = main_function(handle); | |
102 if (result != MOJO_RESULT_OK) { | |
103 LOG(ERROR) << "MojoMain returned error (result: " << result << ")"; | |
104 } | |
105 return true; | |
106 } | |
107 | |
108 } // namespace shell | |
109 } // namespace mojo | |
OLD | NEW |