1 /*
2 * Copyright (c) 2021 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 #ifdef RELATIONAL_STORE
16 #include "runtime_config.h"
17
18 #include "db_common.h"
19 #include "db_constant.h"
20 #include "db_dfx_adapter.h"
21 #include "db_properties.h"
22 #include "kvdb_manager.h"
23 #include "kv_store_errno.h"
24 #include "log_print.h"
25 #include "network_adapter.h"
26 #include "param_check_utils.h"
27 #include "runtime_context.h"
28
29 namespace DistributedDB {
30 std::mutex RuntimeConfig::communicatorMutex_;
31 std::mutex RuntimeConfig::multiUserMutex_;
32 std::shared_ptr<IProcessCommunicator> RuntimeConfig::processCommunicator_ = nullptr;
33
34 // Used to set the process userid and appId
SetProcessLabel(const std::string & appId,const std::string & userId)35 DBStatus RuntimeConfig::SetProcessLabel(const std::string &appId, const std::string &userId)
36 {
37 if (appId.size() > DBConstant::MAX_APP_ID_LENGTH || appId.empty() ||
38 userId.size() > DBConstant::MAX_USER_ID_LENGTH || userId.empty()) {
39 LOGE("Invalid app or user info[%zu]-[%zu]", appId.length(), userId.length());
40 return INVALID_ARGS;
41 }
42
43 int errCode = KvDBManager::SetProcessLabel(appId, userId);
44 if (errCode != E_OK) {
45 LOGE("Failed to set the process label:%d", errCode);
46 return DB_ERROR;
47 }
48 return OK;
49 }
50
51 // Set process communicator.
SetProcessCommunicator(const std::shared_ptr<IProcessCommunicator> & inCommunicator)52 DBStatus RuntimeConfig::SetProcessCommunicator(const std::shared_ptr<IProcessCommunicator> &inCommunicator)
53 {
54 std::lock_guard<std::mutex> lock(communicatorMutex_);
55 if (processCommunicator_ != nullptr) {
56 LOGE("processCommunicator_ is not null!");
57 return DB_ERROR;
58 }
59
60 std::string processLabel = RuntimeContext::GetInstance()->GetProcessLabel();
61 if (processLabel.empty()) {
62 LOGE("ProcessLabel is not set!");
63 return DB_ERROR;
64 }
65
66 auto *adapter = new (std::nothrow) NetworkAdapter(processLabel, inCommunicator);
67 if (adapter == nullptr) {
68 LOGE("New NetworkAdapter failed!");
69 return DB_ERROR;
70 }
71 processCommunicator_ = inCommunicator;
72 if (RuntimeContext::GetInstance()->SetCommunicatorAdapter(adapter) != E_OK) {
73 LOGE("SetProcessCommunicator not support!");
74 delete adapter;
75 return DB_ERROR;
76 }
77 KvDBManager::RestoreSyncableKvStore();
78 return OK;
79 }
80
SetPermissionCheckCallback(const PermissionCheckCallbackV2 & callback)81 DBStatus RuntimeConfig::SetPermissionCheckCallback(const PermissionCheckCallbackV2 &callback)
82 {
83 int errCode = RuntimeContext::GetInstance()->SetPermissionCheckCallback(callback);
84 return TransferDBErrno(errCode);
85 }
86
SetPermissionCheckCallback(const PermissionCheckCallbackV3 & callback)87 DBStatus RuntimeConfig::SetPermissionCheckCallback(const PermissionCheckCallbackV3 &callback)
88 {
89 int errCode = RuntimeContext::GetInstance()->SetPermissionCheckCallback(callback);
90 return TransferDBErrno(errCode);
91 }
92
SetProcessSystemAPIAdapter(const std::shared_ptr<IProcessSystemApiAdapter> & adapter)93 DBStatus RuntimeConfig::SetProcessSystemAPIAdapter(const std::shared_ptr<IProcessSystemApiAdapter> &adapter)
94 {
95 return TransferDBErrno(RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(adapter));
96 }
97
Dump(int fd,const std::vector<std::u16string> & args)98 void RuntimeConfig::Dump(int fd, const std::vector<std::u16string> &args)
99 {
100 DBDfxAdapter::Dump(fd, args);
101 }
102
SetSyncActivationCheckCallback(const SyncActivationCheckCallback & callback)103 DBStatus RuntimeConfig::SetSyncActivationCheckCallback(const SyncActivationCheckCallback &callback)
104 {
105 std::lock_guard<std::mutex> lock(multiUserMutex_);
106 int errCode = RuntimeContext::GetInstance()->SetSyncActivationCheckCallback(callback);
107 return TransferDBErrno(errCode);
108 }
109
NotifyUserChanged()110 DBStatus RuntimeConfig::NotifyUserChanged()
111 {
112 std::lock_guard<std::mutex> lock(multiUserMutex_);
113 int errCode = RuntimeContext::GetInstance()->NotifyUserChanged();
114 return TransferDBErrno(errCode);
115 }
116
IsProcessSystemApiAdapterValid()117 bool RuntimeConfig::IsProcessSystemApiAdapterValid()
118 {
119 return RuntimeContext::GetInstance()->IsProcessSystemApiAdapterValid();
120 }
121
SetSyncActivationCheckCallback(const SyncActivationCheckCallbackV2 & callback)122 DBStatus RuntimeConfig::SetSyncActivationCheckCallback(const SyncActivationCheckCallbackV2 &callback)
123 {
124 std::lock_guard<std::mutex> lock(multiUserMutex_);
125 int errCode = RuntimeContext::GetInstance()->SetSyncActivationCheckCallback(callback);
126 return TransferDBErrno(errCode);
127 }
128
SetPermissionConditionCallback(const PermissionConditionCallback & callback)129 DBStatus RuntimeConfig::SetPermissionConditionCallback(const PermissionConditionCallback &callback)
130 {
131 int errCode = RuntimeContext::GetInstance()->SetPermissionConditionCallback(callback);
132 return TransferDBErrno(errCode);
133 }
134
SetTranslateToDeviceIdCallback(const DistributedDB::TranslateToDeviceIdCallback & callback)135 void RuntimeConfig::SetTranslateToDeviceIdCallback(const DistributedDB::TranslateToDeviceIdCallback &callback)
136 {
137 RuntimeContext::GetInstance()->SetTranslateToDeviceIdCallback(callback);
138 }
139
SetAutoLaunchRequestCallback(const AutoLaunchRequestCallback & callback,DBType type)140 void RuntimeConfig::SetAutoLaunchRequestCallback(const AutoLaunchRequestCallback &callback, DBType type)
141 {
142 DBTypeInner innerType = DBTypeInner::DB_INVALID;
143 if (type == DBType::DB_KV) {
144 innerType = DBTypeInner::DB_KV;
145 } else if (type == DBType::DB_RELATION) {
146 innerType = DBTypeInner::DB_RELATION;
147 }
148 RuntimeContext::GetInstance()->SetAutoLaunchRequestCallback(callback, innerType);
149 }
150
GetStoreIdentifier(const std::string & userId,const std::string & appId,const std::string & storeId,bool syncDualTupleMode)151 std::string RuntimeConfig::GetStoreIdentifier(const std::string &userId, const std::string &appId,
152 const std::string &storeId, bool syncDualTupleMode)
153 {
154 if (!ParamCheckUtils::CheckStoreParameter(storeId, appId, userId, syncDualTupleMode)) {
155 return "";
156 }
157 if (syncDualTupleMode) {
158 return DBCommon::TransferHashString(appId + "-" + storeId);
159 }
160 return DBCommon::TransferHashString(userId + "-" + appId + "-" + storeId);
161 }
162
ReleaseAutoLaunch(const std::string & userId,const std::string & appId,const std::string & storeId,DBType type)163 void RuntimeConfig::ReleaseAutoLaunch(const std::string &userId, const std::string &appId, const std::string &storeId,
164 DBType type)
165 {
166 DBProperties properties;
167 properties.SetIdentifier(userId, appId, storeId);
168
169 DBTypeInner innerType = (type == DBType::DB_KV ? DBTypeInner::DB_KV : DBTypeInner::DB_RELATION);
170 RuntimeContext::GetInstance()->CloseAutoLaunchConnection(innerType, properties);
171 }
172
SetThreadPool(const std::shared_ptr<IThreadPool> & threadPool)173 void RuntimeConfig::SetThreadPool(const std::shared_ptr<IThreadPool> &threadPool)
174 {
175 RuntimeContext::GetInstance()->SetThreadPool(threadPool);
176 }
177
SetCloudTranslate(const std::shared_ptr<ICloudDataTranslate> & dataTranslate)178 void RuntimeConfig::SetCloudTranslate(const std::shared_ptr<ICloudDataTranslate> &dataTranslate)
179 {
180 RuntimeContext::GetInstance()->SetCloudTranslate(dataTranslate);
181 }
182 } // namespace DistributedDB
183 #endif