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 #include <dlfcn.h>
16
17 #include "base/log/log_wrapper.h"
18 #include "core/common/share/text_share_loader.h"
19
20 namespace OHOS::Ace {
21 namespace {
22 #ifdef __aarch64__
23 constexpr char TEXT_SHARE_SO_PATH[] = "system/lib64/libsystemshare_innerkits.z.so";
24 #else
25 constexpr char TEXT_SHARE_SO_PATH[] = "system/lib/libsystemshare_innerkits.z.so";
26 #endif
27 } // namespace
28
29 // static
Load()30 std::shared_ptr<TextShareLoader> TextShareLoader::Load()
31 {
32 auto engLib(std::make_shared<TextShareLoader>());
33 return engLib->Init() ? engLib : nullptr;
34 }
35
~TextShareLoader()36 TextShareLoader::~TextShareLoader()
37 {
38 Close();
39 }
40
Init()41 bool TextShareLoader::Init()
42 {
43 mLibraryHandle_ = dlopen(TEXT_SHARE_SO_PATH, RTLD_LAZY);
44 if (mLibraryHandle_ == nullptr) {
45 return false;
46 }
47 mCreateTextShareInstance_ = (TextShareInterface* (*)())dlsym(mLibraryHandle_,
48 "OHOS_ACE_CreateTextShareInterface");
49 mDestoryTextShareInstance_ = (void (*)(TextShareInterface*))dlsym(
50 mLibraryHandle_, "OHOS_ACE_DestroyTextShareInterface");
51 if (mCreateTextShareInstance_ == nullptr || mDestoryTextShareInstance_ == nullptr) {
52 LOGE("Could not find engine interface function in %s", TEXT_SHARE_SO_PATH);
53 Close();
54 return false;
55 }
56 return true;
57 }
58
CreateTextShare()59 TextShareInstance TextShareLoader::CreateTextShare()
60 {
61 if (mCreateTextShareInstance_ == nullptr || mDestoryTextShareInstance_ == nullptr) {
62 return TextShareInstance();
63 }
64 return TextShareInstance(mCreateTextShareInstance_(), [lib = shared_from_this(),
65 destroy = mDestoryTextShareInstance_](TextShareInterface* e) {
66 destroy(e);
67 });
68 }
69
Close()70 void TextShareLoader::Close()
71 {
72 if (mLibraryHandle_ != nullptr) {
73 dlclose(mLibraryHandle_);
74 }
75 mLibraryHandle_ = nullptr;
76 mCreateTextShareInstance_ = nullptr;
77 mDestoryTextShareInstance_ = nullptr;
78 }
79 } // namespace OHOS::Ace