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