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