• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define LOG_TAG "RdbICUManager"
17 #include "rdb_icu_manager.h"
18 
19 #include <dlfcn.h>
20 
21 #include "global_resource.h"
22 #include "logger.h"
23 #include "rdb_errno.h"
24 namespace OHOS::NativeRdb {
25 using namespace OHOS::Rdb;
GetInstance()26 RdbICUManager &RdbICUManager::GetInstance()
27 {
28     static RdbICUManager instance;
29     return instance;
30 }
31 
ConfigLocale(sqlite3 * db,const std::string & localeStr)32 int32_t RdbICUManager::ConfigLocale(sqlite3 *db, const std::string &localeStr)
33 {
34     auto handle = GetApiInfo();
35     if (handle.configIcuLocaleFunc == nullptr) {
36         LOG_ERROR("dlsym(ConfigLocal) failed!");
37         return E_NOT_SUPPORT;
38     }
39     return handle.configIcuLocaleFunc(db, localeStr);
40 }
41 
RdbICUManager()42 RdbICUManager::RdbICUManager()
43 {
44     handle_ = nullptr;
45 }
46 
~RdbICUManager()47 RdbICUManager::~RdbICUManager()
48 {
49     if (handle_ != nullptr) {
50         dlclose(handle_);
51         handle_ = nullptr;
52     }
53 }
54 
CleanUp()55 int32_t RdbICUManager::CleanUp()
56 {
57     auto handle = GetApiInfo();
58     if (handle.cleanUpFunc == nullptr) {
59         LOG_ERROR("dlsym(CleanUp) failed!");
60         return E_NOT_SUPPORT;
61     }
62     auto code = handle.cleanUpFunc();
63     if (code != E_OK) {
64         return code;
65     }
66     std::lock_guard<decltype(mutex_)> lock(mutex_);
67     dlclose(handle_);
68     handle_ = nullptr;
69     apiInfo_.cleanUpFunc = nullptr;
70     apiInfo_.configIcuLocaleFunc = nullptr;
71     return E_OK;
72 }
73 
GetApiInfo()74 RdbICUManager::ICUAPIInfo RdbICUManager::GetApiInfo()
75 {
76     std::lock_guard<decltype(mutex_)> lock(mutex_);
77     if (handle_ != nullptr) {
78         return apiInfo_;
79     }
80     handle_ = dlopen("librelational_store_icu.z.so", RTLD_LAZY);
81     if (handle_ == nullptr) {
82         LOG_ERROR("dlopen(librelational_store_icu) failed!");
83         return apiInfo_;
84     }
85     GlobalResource::RegisterClean(GlobalResource::ICU, []() { return RdbICUManager::GetInstance().CleanUp(); });
86     apiInfo_.configIcuLocaleFunc = reinterpret_cast<ConfigICULocaleFunc>(dlsym(handle_, "ConfigICULocale"));
87     apiInfo_.cleanUpFunc = reinterpret_cast<CleanUpFunc>(dlsym(handle_, "CleanUp"));
88     return apiInfo_;
89 }
90 } // namespace OHOS::NativeRdb