1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "global.h"
17 #include "inputmethod_extension.h"
18 #include <dlfcn.h>
19
20 namespace OHOS {
21 namespace AbilityRuntime {
22 namespace {
23 constexpr char CJ_INPUTMETHOD_EXT_LIB_NAME[] = "libcj_inputmethod_extension_ffi.z.so";
24
25 using CreateFunc = InputMethodExtension *(*)();
26 static constexpr char CJ_INPUTMETHOD_EXT_CREATE_FUNC[] = "OHOS_ABILITY_CjInputMethodExtension";
27
28 } // namespace
29
CreateCjInputMethodExtension()30 InputMethodExtension *CreateCjInputMethodExtension()
31 {
32 void *handle = dlopen(CJ_INPUTMETHOD_EXT_LIB_NAME, RTLD_LAZY);
33 if (handle == nullptr) {
34 IMSA_HILOGE("open cj_inputmethod_extension library %{public}s failed, reason: %{public}s",
35 CJ_INPUTMETHOD_EXT_LIB_NAME, dlerror());
36 return new (std::nothrow) InputMethodExtension();
37 }
38
39 auto entry = reinterpret_cast<CreateFunc>(dlsym(handle, CJ_INPUTMETHOD_EXT_CREATE_FUNC));
40 if (entry == nullptr) {
41 dlclose(handle);
42 IMSA_HILOGE("get cj_inputmethod_extension symbol %{public}s in %{public}s failed",
43 CJ_INPUTMETHOD_EXT_CREATE_FUNC, CJ_INPUTMETHOD_EXT_LIB_NAME);
44 return new (std::nothrow) InputMethodExtension();
45 }
46
47 auto instance = entry();
48 if (instance == nullptr) {
49 dlclose(handle);
50 IMSA_HILOGE("get cj_inputmethod_extension instance in %{public}s failed", CJ_INPUTMETHOD_EXT_LIB_NAME);
51 return new (std::nothrow) InputMethodExtension();
52 }
53
54 return instance;
55 }
56 } // namespace AbilityRuntime
57 } // namespace OHOS