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 "runner_runtime/ets_test_runner_instance.h"
17
18 #include <cstddef>
19 #include <dlfcn.h>
20
21 #include "hilog_tag_wrapper.h"
22
23 namespace OHOS {
24 namespace RunnerRuntime {
25 namespace {
26 const char *ETS_ANI_LIBNAME = "libtest_runner_ani.z.so";
27 const char *ETS_ANI_Create_FUNC = "OHOS_ETS_Test_Runner_Create";
28 using CreateETSTestRunnerFunc = AppExecFwk::TestRunner*(*)(const std::unique_ptr<AbilityRuntime::Runtime>&,
29 const std::shared_ptr<AppExecFwk::AbilityDelegatorArgs>&, const AppExecFwk::BundleInfo&);
30 CreateETSTestRunnerFunc g_etsCreateFunc = nullptr;
31 }
32
CreateETSTestRunner(const std::unique_ptr<AbilityRuntime::Runtime> & runtime,const std::shared_ptr<AppExecFwk::AbilityDelegatorArgs> & args,const AppExecFwk::BundleInfo & bundleInfo)33 AppExecFwk::TestRunner *CreateETSTestRunner(const std::unique_ptr<AbilityRuntime::Runtime> &runtime,
34 const std::shared_ptr<AppExecFwk::AbilityDelegatorArgs> &args, const AppExecFwk::BundleInfo &bundleInfo)
35 {
36 if (g_etsCreateFunc != nullptr) {
37 return g_etsCreateFunc(runtime, args, bundleInfo);
38 }
39 auto handle = dlopen(ETS_ANI_LIBNAME, RTLD_LAZY);
40 if (handle == nullptr) {
41 TAG_LOGE(AAFwkTag::DELEGATOR, "dlopen failed %{public}s, %{public}s", ETS_ANI_LIBNAME, dlerror());
42 return nullptr;
43 }
44 auto symbol = dlsym(handle, ETS_ANI_Create_FUNC);
45 if (symbol == nullptr) {
46 TAG_LOGE(AAFwkTag::DELEGATOR, "dlsym failed %{public}s, %{public}s", ETS_ANI_Create_FUNC, dlerror());
47 dlclose(handle);
48 return nullptr;
49 }
50 g_etsCreateFunc = reinterpret_cast<CreateETSTestRunnerFunc>(symbol);
51 return g_etsCreateFunc(runtime, args, bundleInfo);
52 }
53 } // namespace RunnerRuntime
54 } // namespace OHOS