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
16 #include "setting_datashare.h"
17
18 #include <thread>
19
20 #include "datashare_predicates.h"
21 #include "datashare_result_set.h"
22 #include "datashare_values_bucket.h"
23 #include "ipc_skeleton.h"
24 #include "iservice_registry.h"
25 #include "mmi_log.h"
26 #include "rdb_errno.h"
27 #include "result_set.h"
28 #include "system_ability_definition.h"
29 #include "uri.h"
30
31 #undef MMI_LOG_DOMAIN
32 #define MMI_LOG_DOMAIN MMI_LOG_HANDLER
33 #undef MMI_LOG_TAG
34 #define MMI_LOG_TAG "setting_DataShare"
35
36 namespace OHOS {
37 namespace MMI {
38 std::shared_ptr<SettingDataShare> SettingDataShare::instance_ = nullptr;
39 std::mutex SettingDataShare::mutex_;
40 sptr<IRemoteObject> SettingDataShare::remoteObj_;
41 namespace {
42 const std::string SETTING_COLUMN_KEYWORD { "KEYWORD" };
43 const std::string SETTING_COLUMN_VALUE { "VALUE" };
44 const std::string SETTING_URI_PROXY { "datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true" };
45 const std::string SETTING_URI_USER_PROXY {
46 "datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_SECURE_100?Proxy=true" };
47 const std::string SETTINGS_DATA_EXT_URI { "datashare:///com.ohos.settingsdata.DataAbility" };
48 constexpr int32_t DECIMAL_BASE { 10 };
49 constexpr const int32_t E_OK{ 0 };
50 constexpr const int32_t E_DATA_SHARE_NOT_READY { 1055 };
51 } // namespace
52
~SettingDataShare()53 SettingDataShare::~SettingDataShare() {}
54
GetInstance(int32_t systemAbilityId)55 SettingDataShare& SettingDataShare::GetInstance(int32_t systemAbilityId)
56 {
57 if (instance_ == nullptr) {
58 std::lock_guard<std::mutex> lock(mutex_);
59 if (instance_ == nullptr) {
60 instance_ = std::make_shared<SettingDataShare>();
61 }
62 }
63 return *instance_;
64 }
65
GetIntValue(const std::string & key,int32_t & value,const std::string & strUri)66 ErrCode SettingDataShare::GetIntValue(const std::string& key, int32_t& value, const std::string &strUri)
67 {
68 int64_t valueLong;
69 ErrCode ret = GetLongValue(key, valueLong, strUri);
70 if (ret != ERR_OK) {
71 MMI_HILOGE("Get int value fail");
72 return ret;
73 }
74 value = static_cast<int32_t>(valueLong);
75 return ERR_OK;
76 }
77
GetLongValue(const std::string & key,int64_t & value,const std::string & strUri)78 ErrCode SettingDataShare::GetLongValue(const std::string& key, int64_t& value, const std::string &strUri)
79 {
80 std::string valueStr;
81 ErrCode ret = GetStringValue(key, valueStr, strUri);
82 if (ret != ERR_OK) {
83 MMI_HILOGE("Get long value fail");
84 return ret;
85 }
86 value = static_cast<int64_t>(strtoll(valueStr.c_str(), nullptr, DECIMAL_BASE));
87 return ERR_OK;
88 }
89
GetBoolValue(const std::string & key,bool & value,const std::string & strUri)90 ErrCode SettingDataShare::GetBoolValue(const std::string& key, bool& value, const std::string &strUri)
91 {
92 std::string valueStr;
93 ErrCode ret = GetStringValue(key, valueStr, strUri);
94 if (ret != ERR_OK) {
95 MMI_HILOGE("Get bool value fail");
96 return ret;
97 }
98 value = ((valueStr == "true") || (valueStr == "1"));
99 return ERR_OK;
100 }
101
PutIntValue(const std::string & key,int32_t value,bool needNotify,const std::string & strUri)102 ErrCode SettingDataShare::PutIntValue(
103 const std::string& key, int32_t value, bool needNotify, const std::string &strUri)
104 {
105 return PutStringValue(key, std::to_string(value), needNotify, strUri);
106 }
107
PutLongValue(const std::string & key,int64_t value,bool needNotify,const std::string & strUri)108 ErrCode SettingDataShare::PutLongValue(
109 const std::string& key, int64_t value, bool needNotify, const std::string &strUri)
110 {
111 return PutStringValue(key, std::to_string(value), needNotify, strUri);
112 }
113
PutBoolValue(const std::string & key,bool value,bool needNotify,const std::string & strUri)114 ErrCode SettingDataShare::PutBoolValue(
115 const std::string& key, bool value, bool needNotify, const std::string &strUri)
116 {
117 std::string valueStr = value ? "true" : "false";
118 return PutStringValue(key, valueStr, needNotify, strUri);
119 }
120
IsValidKey(const std::string & key,const std::string & strUri)121 bool SettingDataShare::IsValidKey(const std::string& key, const std::string &strUri)
122 {
123 std::string value;
124 ErrCode ret = GetStringValue(key, value, strUri);
125 return (ret != ERR_NAME_NOT_FOUND) && (!value.empty());
126 }
127
CreateObserver(const std::string & key,SettingObserver::UpdateFunc & func)128 sptr<SettingObserver> SettingDataShare::CreateObserver(const std::string& key, SettingObserver::UpdateFunc& func)
129 {
130 sptr<SettingObserver> observer = new (std::nothrow) SettingObserver();
131 CHKPP(observer);
132 observer->SetKey(key);
133 observer->SetUpdateFunc(func);
134 return observer;
135 }
136
ExecRegisterCb(const sptr<SettingObserver> & observer)137 void SettingDataShare::ExecRegisterCb(const sptr<SettingObserver>& observer)
138 {
139 CHKPV(observer);
140 observer->OnChange();
141 }
142
RegisterObserver(const sptr<SettingObserver> & observer,const std::string & strUri)143 ErrCode SettingDataShare::RegisterObserver(const sptr<SettingObserver>& observer, const std::string &strUri)
144 {
145 CHKPR(observer, RET_ERR);
146 std::string callingIdentity = IPCSkeleton::ResetCallingIdentity();
147 auto uri = AssembleUri(observer->GetKey(), strUri);
148 auto helper = CreateDataShareHelper(strUri);
149 if (helper == nullptr) {
150 IPCSkeleton::SetCallingIdentity(callingIdentity);
151 return ERR_NO_INIT;
152 }
153 helper->RegisterObserver(uri, observer);
154 helper->NotifyChange(uri);
155 std::thread execCb([this, observer] { this->ExecRegisterCb(observer); });
156 execCb.detach();
157 ReleaseDataShareHelper(helper);
158 IPCSkeleton::SetCallingIdentity(callingIdentity);
159 return ERR_OK;
160 }
161
UnregisterObserver(const sptr<SettingObserver> & observer,const std::string & strUri)162 ErrCode SettingDataShare::UnregisterObserver(const sptr<SettingObserver>& observer, const std::string &strUri)
163 {
164 CHKPR(observer, RET_ERR);
165 std::string callingIdentity = IPCSkeleton::ResetCallingIdentity();
166 auto uri = AssembleUri(observer->GetKey(), strUri);
167 auto helper = CreateDataShareHelper(strUri);
168 if (helper == nullptr) {
169 IPCSkeleton::SetCallingIdentity(callingIdentity);
170 return ERR_NO_INIT;
171 }
172 helper->UnregisterObserver(uri, observer);
173 ReleaseDataShareHelper(helper);
174 IPCSkeleton::SetCallingIdentity(callingIdentity);
175 return ERR_OK;
176 }
177
GetStringValue(const std::string & key,std::string & value,const std::string & strUri)178 ErrCode SettingDataShare::GetStringValue(const std::string& key, std::string& value, const std::string &strUri)
179 {
180 std::string callingIdentity = IPCSkeleton::ResetCallingIdentity();
181 auto helper = CreateDataShareHelper(strUri);
182 if (helper == nullptr) {
183 IPCSkeleton::SetCallingIdentity(callingIdentity);
184 return ERR_NO_INIT;
185 }
186 std::vector<std::string> columns = {SETTING_COLUMN_VALUE};
187 DataShare::DataSharePredicates predicates;
188 predicates.EqualTo(SETTING_COLUMN_KEYWORD, key);
189 Uri uri(AssembleUri(key, strUri));
190 auto resultSet = helper->Query(uri, predicates, columns);
191 ReleaseDataShareHelper(helper);
192 if (resultSet == nullptr) {
193 IPCSkeleton::SetCallingIdentity(callingIdentity);
194 return ERR_INVALID_OPERATION;
195 }
196 int32_t count = 0;
197 resultSet->GetRowCount(count);
198 if (count == 0) {
199 IPCSkeleton::SetCallingIdentity(callingIdentity);
200 return ERR_NAME_NOT_FOUND;
201 }
202 const int32_t tmpRow = 0;
203 resultSet->GoToRow(tmpRow);
204 int32_t ret = resultSet->GetString(tmpRow, value);
205 if (ret != RET_OK) {
206 IPCSkeleton::SetCallingIdentity(callingIdentity);
207 return ERR_INVALID_VALUE;
208 }
209 resultSet->Close();
210 IPCSkeleton::SetCallingIdentity(callingIdentity);
211 return ERR_OK;
212 }
213
PutStringValue(const std::string & key,const std::string & value,bool needNotify,const std::string & strUri)214 ErrCode SettingDataShare::PutStringValue(
215 const std::string& key, const std::string& value, bool needNotify, const std::string &strUri)
216 {
217 std::string callingIdentity = IPCSkeleton::ResetCallingIdentity();
218 auto helper = CreateDataShareHelper(strUri);
219 if (helper == nullptr) {
220 IPCSkeleton::SetCallingIdentity(callingIdentity);
221 return ERR_NO_INIT;
222 }
223 DataShare::DataShareValueObject keyObj(key);
224 DataShare::DataShareValueObject valueObj(value);
225 DataShare::DataShareValuesBucket bucket;
226 bucket.Put(SETTING_COLUMN_KEYWORD, keyObj);
227 bucket.Put(SETTING_COLUMN_VALUE, valueObj);
228 DataShare::DataSharePredicates predicates;
229 predicates.EqualTo(SETTING_COLUMN_KEYWORD, key);
230 Uri uri(AssembleUri(key, strUri));
231 if (helper->Update(uri, predicates, bucket) <= 0) {
232 helper->Insert(uri, bucket);
233 }
234 if (needNotify) {
235 helper->NotifyChange(AssembleUri(key, strUri));
236 }
237 ReleaseDataShareHelper(helper);
238 IPCSkeleton::SetCallingIdentity(callingIdentity);
239 return ERR_OK;
240 }
241
CreateDataShareHelper(const std::string & strUri)242 std::shared_ptr<DataShare::DataShareHelper> SettingDataShare::CreateDataShareHelper(const std::string &strUri)
243 {
244 auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
245 CHKPP(sam);
246 auto remoteObj = sam->CheckSystemAbility(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID);
247 if (remoteObj == nullptr) {
248 MMI_HILOGI("Data share not start");
249 return nullptr;
250 }
251 if (remoteObj_ == nullptr) {
252 std::lock_guard<std::mutex> lock(mutex_);
253 remoteObj_ = sam->CheckSystemAbility(MULTIMODAL_INPUT_SERVICE_ID);
254 }
255 std::pair<int, std::shared_ptr<DataShare::DataShareHelper>> ret;
256 if (strUri.empty()) {
257 ret = DataShare::DataShareHelper::Create(remoteObj_, SETTING_URI_PROXY, SETTINGS_DATA_EXT_URI.c_str());
258 } else {
259 ret = DataShare::DataShareHelper::Create(remoteObj_, strUri, "");
260 }
261 return ret.second;
262 }
263
ReleaseDataShareHelper(std::shared_ptr<DataShare::DataShareHelper> & helper)264 bool SettingDataShare::ReleaseDataShareHelper(std::shared_ptr<DataShare::DataShareHelper>& helper)
265 {
266 if (!helper->Release()) {
267 return false;
268 }
269 return true;
270 }
271
AssembleUri(const std::string & key,const std::string & strUri)272 Uri SettingDataShare::AssembleUri(const std::string& key, const std::string &strUri)
273 {
274 if (strUri.empty()) {
275 if (key == "close_fingerprint_nav_event_key" || key == "close_fingerprint_event_key") {
276 return Uri(SETTING_URI_USER_PROXY + "&key=" + key);
277 }
278 return Uri(SETTING_URI_PROXY + "&key=" + key);
279 } else {
280 return Uri(strUri + "&key=" + key);
281 }
282 }
283
CheckIfSettingsDataReady()284 bool SettingDataShare::CheckIfSettingsDataReady()
285 {
286 if (isDataShareReady_) {
287 return true;
288 }
289 if (remoteObj_ == nullptr) {
290 std::lock_guard<std::mutex> lock(mutex_);
291 if (remoteObj_ == nullptr) {
292 auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
293 CHKPF(sam);
294 remoteObj_ = sam->CheckSystemAbility(MULTIMODAL_INPUT_SERVICE_ID);
295 }
296 }
297 CHKPF(remoteObj_);
298 std::pair<int, std::shared_ptr<DataShare::DataShareHelper>> ret =
299 DataShare::DataShareHelper::Create(remoteObj_, SETTING_URI_PROXY, SETTINGS_DATA_EXT_URI);
300 MMI_HILOGD("create data_share helper, ret=%{public}d", ret.first);
301 if (ret.first == E_OK) {
302 MMI_HILOGD("create data_share helper success");
303 auto helper = ret.second;
304 if (helper != nullptr) {
305 bool releaseRet = helper->Release();
306 MMI_HILOGD("release data_share helper, releaseRet=%{public}d", releaseRet);
307 }
308 isDataShareReady_ = true;
309 return true;
310 } else if (ret.first == E_DATA_SHARE_NOT_READY) {
311 MMI_HILOGE("create data_share helper failed");
312 isDataShareReady_ = false;
313 return false;
314 }
315 MMI_HILOGE("data_share unknown");
316 return true;
317 }
318 }
319 } // namespace OHOS