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