1 /*
2  * Copyright (c) 2024 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_data_manager.h"
17 
18 #include <charconv>
19 
20 #include "ipc_skeleton_utils.h"
21 #include "iservice_registry.h"
22 #include "system_ability_definition.h"
23 #include "ui_appearance_log.h"
24 
25 namespace OHOS::ArkUi::UiAppearance {
26 namespace {
27 constexpr const char* SETTING_DATA_URI =
28     "datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true";
29 constexpr const char* SETTING_DATA_USER_URI_PREFIX =
30     "datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_";
31 constexpr const char* SETTING_DATA_USER_URI_SUFFIX = "?Proxy=true";
32 constexpr const char* SETTING_DATA_EXT_URI = "datashare:///com.ohos.settingsdata.DataAbility";
33 constexpr const char* SETTING_DATA_KEY_URI = "&key=";
34 constexpr const char* SETTING_DATA_COLUMN_KEYWORD = "KEYWORD";
35 constexpr const char* SETTING_DATA_COLUMN_VALUE = "VALUE";
36 constexpr int32_t INDEX0 = 0;
37 }
38 
GetInstance()39 SettingDataManager &SettingDataManager::GetInstance()
40 {
41     static SettingDataManager instance;
42     return instance;
43 }
44 
Initialize()45 ErrCode SettingDataManager::Initialize()
46 {
47     std::lock_guard guard(initializeMutex_);
48     if (isInitialized_) {
49         LOGD("already initialized");
50         return ERR_OK;
51     }
52     const auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
53     if (saMgr == nullptr) {
54         LOGE("saMgr is null");
55         return ERR_NO_INIT;
56     }
57     const auto remoteObj = saMgr->GetSystemAbility(ARKUI_UI_APPEARANCE_SERVICE_ID);
58     if (remoteObj == nullptr) {
59         LOGE("remoteObj is null");
60         return ERR_NO_INIT;
61     }
62     remoteObject_ = remoteObj;
63     isInitialized_ = true;
64     return ERR_OK;
65 }
66 
IsInitialized() const67 bool SettingDataManager::IsInitialized() const
68 {
69     return isInitialized_;
70 }
71 
RegisterObserver(const std::string & key,const SettingDataObserver::UpdateFunc & updateFunc,const int32_t userId)72 ErrCode SettingDataManager::RegisterObserver(const std::string& key, const SettingDataObserver::UpdateFunc& updateFunc,
73     const int32_t userId)
74 {
75     if (!isInitialized_) {
76         LOGE("SettingDataManager not initialized");
77         return ERR_NO_INIT;
78     }
79 
80     std::lock_guard guard(observersMutex_);
81     const std::string observerName = GenerateObserverName(key, userId);
82     const auto& iter = observers_.find(observerName);
83     if (iter != observers_.end()) {
84         LOGD("observerName: %{public}s is exist", observerName.c_str());
85         return ERR_OK;
86     }
87 
88     sptr<SettingDataObserver> observer = CreateObserver(key, updateFunc, userId);
89     ErrCode code = RegisterObserverInner(observer);
90     if (code != ERR_OK) {
91         return code;
92     }
93     observers_.emplace(observerName, observer);
94     return ERR_OK;
95 }
96 
UnregisterObserver(const std::string & key,const int32_t userId)97 ErrCode SettingDataManager::UnregisterObserver(const std::string& key, const int32_t userId)
98 {
99     const std::string observerName = GenerateObserverName(key, userId);
100     std::lock_guard guard(observersMutex_);
101     const auto& iter = observers_.find(observerName);
102     if (iter == observers_.end()) {
103         LOGE("observerName: %{public}s is not found", observerName.c_str());
104         return ERR_INVALID_VALUE;
105     }
106     if (iter->second == nullptr) {
107         LOGE("observerName: %{public}s observer is null", observerName.c_str());
108         observers_.erase(iter);
109         return ERR_INVALID_OPERATION;
110     }
111 
112     ErrCode code = UnregisterObserverInner(iter->second);
113     observers_.erase(iter);
114     return code;
115 }
116 
GetStringValue(const std::string & key,std::string & value,const int32_t userId) const117 ErrCode SettingDataManager::GetStringValue(const std::string& key, std::string& value, const int32_t userId) const
118 {
119     std::string uriString;
120     ResetCallingIdentityScope scope;
121     std::shared_ptr<DataShare::DataShareHelper> helper;
122     CreateDataShareHelperAndUri(userId, key, uriString, helper);
123     if (helper == nullptr) {
124         LOGE("helper is null, userId: %{public}d", userId);
125         return ERR_NO_INIT;
126     }
127 
128     std::vector<std::string> columns = { SETTING_DATA_COLUMN_VALUE };
129     DataShare::DataSharePredicates predicates;
130     predicates.EqualTo(SETTING_DATA_COLUMN_KEYWORD, key);
131     Uri uri(uriString);
132     auto result = helper->Query(uri, predicates, columns);
133     ReleaseDataShareHelper(helper);
134     if (result == nullptr) {
135         LOGE("query return null, key: %{public}s, userId: %{public}d", key.c_str(), userId);
136         return ERR_INVALID_OPERATION;
137     }
138     int32_t count = 0;
139     result->GetRowCount(count);
140     if (count == 0) {
141         LOGI("not found, key: %{public}s, userId: %{public}d", key.c_str(), userId);
142         result->Close();
143         return ERR_NAME_NOT_FOUND;
144     }
145 
146     result->GoToRow(INDEX0);
147     int32_t code = result->GetString(INDEX0, value);
148     if (code != 0) {
149         LOGE("get string failed, key: %{public}s, userId: %{public}d, ret: %{public}d", key.c_str(), userId, code);
150         result->Close();
151         return ERR_INVALID_VALUE;
152     }
153     result->Close();
154     LOGD("Get key: %{public}s, userId: %{public}d, value: %{public}s", key.c_str(), userId, value.c_str());
155     return ERR_OK;
156 }
157 
GetInt32Value(const std::string & key,int32_t & value,const int32_t userId) const158 ErrCode SettingDataManager::GetInt32Value(const std::string& key, int32_t& value, const int32_t userId) const
159 {
160     std::string valueString;
161     ErrCode code = GetStringValue(key, valueString, userId);
162     if (code != ERR_OK) {
163         return code;
164     }
165 
166     auto res = std::from_chars(valueString.c_str(), valueString.c_str() + valueString.size(), value);
167     if (res.ec != std::errc()) {
168         LOGE("key: %{public}s, userId: %{public}d, value: %{public}s failed to convert to int",
169             key.c_str(), userId, valueString.c_str());
170         return ERR_INVALID_VALUE;
171     }
172     return ERR_OK;
173 }
174 
GetInt32ValueStrictly(const std::string & key,int32_t & value,const int32_t userId) const175 ErrCode SettingDataManager::GetInt32ValueStrictly(const std::string& key, int32_t& value, const int32_t userId) const
176 {
177     std::string valueString;
178     ErrCode code = GetStringValue(key, valueString, userId);
179     if (code != ERR_OK) {
180         return code;
181     }
182     int32_t convertedValue;
183     auto res = std::from_chars(valueString.c_str(), valueString.c_str() + valueString.size(), convertedValue);
184     if (res.ec != std::errc()) {
185         LOGE("key: %{public}s, userId: %{public}d, value: %{public}s failed to convert to int",
186             key.c_str(), userId, valueString.c_str());
187         return ERR_INVALID_VALUE;
188     }
189     if (std::to_string(convertedValue) != valueString) {
190         LOGE("key: %{public}s, userId: %{public}d, value: %{public}s is not strict int",
191             key.c_str(), userId, valueString.c_str());
192         return ERR_INVALID_VALUE;
193     }
194     value = convertedValue;
195     return ERR_OK;
196 }
197 
GetBoolValue(const std::string & key,bool & value,const int32_t userId) const198 ErrCode SettingDataManager::GetBoolValue(const std::string& key, bool& value, const int32_t userId) const
199 {
200     std::string valueString;
201     ErrCode code = GetStringValue(key, valueString, userId);
202     if (code != ERR_OK) {
203         return code;
204     }
205     if (valueString == "true") {
206         value = true;
207         return ERR_OK;
208     } else if (valueString == "false") {
209         value = false;
210         return ERR_OK;
211     } else {
212         LOGE("key: %{public}s, userId: %{public}d, value: %{public}s failed to convert to bool",
213             key.c_str(), userId, valueString.c_str());
214         return ERR_INVALID_VALUE;
215     }
216 }
217 
SetStringValue(const std::string & key,const std::string & value,int32_t userId,bool needNotify) const218 ErrCode SettingDataManager::SetStringValue(const std::string& key, const std::string& value, int32_t userId,
219     bool needNotify) const
220 {
221     std::string uriString;
222     ResetCallingIdentityScope scope;
223     std::shared_ptr<DataShare::DataShareHelper> helper;
224     CreateDataShareHelperAndUri(userId, key, uriString, helper);
225     if (helper == nullptr) {
226         LOGE("helper is null, userId: %{public}d", userId);
227         return ERR_NO_INIT;
228     }
229 
230     DataShare::DataShareValueObject keyObj(key);
231     DataShare::DataShareValueObject valueObj(value);
232     DataShare::DataShareValuesBucket bucket;
233     bucket.Put(SETTING_DATA_COLUMN_KEYWORD, keyObj);
234     bucket.Put(SETTING_DATA_COLUMN_VALUE, valueObj);
235     DataShare::DataSharePredicates predicates;
236     predicates.EqualTo(SETTING_DATA_COLUMN_KEYWORD, key);
237     Uri uri(uriString);
238     int32_t result = helper->Update(uri, predicates, bucket);
239     if (result <= 0) {
240         LOGD("data not exist, key: %{public}s, userId: %{public}d", key.c_str(), userId);
241         result = helper->Insert(uri, bucket);
242         if (result <= 0) {
243             ReleaseDataShareHelper(helper);
244             LOGE("put string failed, key: %{public}s, userId: %{public}d, result: %{public}d",
245                 key.c_str(), userId, result);
246             return ERR_INVALID_OPERATION;
247         }
248     }
249     if (needNotify) {
250         helper->NotifyChange(uri);
251     }
252     ReleaseDataShareHelper(helper);
253     LOGD("put key: %{public}s, userId: %{public}d, value: %{public}s", key.c_str(), userId, value.c_str());
254     return ERR_OK;
255 }
256 
SetInt32Value(const std::string & key,const int32_t value,const int32_t userId,const bool needNotify) const257 ErrCode SettingDataManager::SetInt32Value(const std::string& key, const int32_t value, const int32_t userId,
258     const bool needNotify) const
259 {
260     const std::string valueString = std::to_string(value);
261     return SetStringValue(key, valueString, userId, needNotify);
262 }
263 
SetBoolValue(const std::string & key,const bool value,const int32_t userId,const bool needNotify) const264 ErrCode SettingDataManager::SetBoolValue(const std::string& key, const bool value, const int32_t userId,
265     const bool needNotify) const
266 {
267     const std::string valueString = value ? "true" : "false";
268     return SetStringValue(key, valueString, userId, needNotify);
269 }
270 
IsValidKey(const std::string & key,const int32_t userId) const271 bool SettingDataManager::IsValidKey(const std::string& key, const int32_t userId) const
272 {
273     std::string value;
274     return (ERR_OK == GetStringValue(key, value, userId)) && (!value.empty());
275 }
276 
RegisterObserverInner(const sptr<SettingDataObserver> & observer) const277 ErrCode SettingDataManager::RegisterObserverInner(const sptr<SettingDataObserver>& observer) const
278 {
279     if (observer == nullptr) {
280         LOGE("observer is null");
281         return ERR_INVALID_OPERATION;
282     }
283 
284     ResetCallingIdentityScope scope;
285     std::string uriString;
286     std::shared_ptr<DataShare::DataShareHelper> helper;
287     CreateDataShareHelperAndUri(observer->GetUserId(), observer->GetKey(), uriString, helper);
288     if (helper == nullptr) {
289         LOGE("helper is null, userId: %{public}d", observer->GetUserId());
290         return ERR_NO_INIT;
291     }
292 
293     const Uri uri(uriString);
294     helper->RegisterObserver(uri, observer);
295     helper->NotifyChange(uri);
296     ReleaseDataShareHelper(helper);
297     LOGD("register observer key: %{public}s, userId: %{public}d", observer->GetKey().c_str(), observer->GetUserId());
298     return ERR_OK;
299 }
300 
UnregisterObserverInner(const sptr<SettingDataObserver> & observer) const301 ErrCode SettingDataManager::UnregisterObserverInner(const sptr<SettingDataObserver>& observer) const
302 {
303     if (observer == nullptr) {
304         LOGE("observer is null");
305         return ERR_INVALID_OPERATION;
306     }
307 
308     ResetCallingIdentityScope scope;
309     std::string uriString;
310     std::shared_ptr<DataShare::DataShareHelper> helper;
311     CreateDataShareHelperAndUri(observer->GetUserId(), observer->GetKey(), uriString, helper);
312     if (helper == nullptr) {
313         LOGE("helper is null");
314         return ERR_NO_INIT;
315     }
316 
317     const Uri uri(uriString);
318     helper->UnregisterObserver(uri, observer);
319     ReleaseDataShareHelper(helper);
320     LOGD("unregister observer key: %{public}s, userId: %{public}d", observer->GetKey().c_str(), observer->GetUserId());
321     return ERR_OK;
322 }
323 
CreateDataShareHelperAndUri(const int32_t userId,const std::string & key,std::string & uri,std::shared_ptr<DataShare::DataShareHelper> & helper) const324 void SettingDataManager::CreateDataShareHelperAndUri(const int32_t userId, const std::string& key,
325     std::string& uri, std::shared_ptr<DataShare::DataShareHelper>& helper) const
326 {
327     if (userId == INVALID_USER_ID) {
328         uri = AssembleUri(key);
329         helper = CreateDataShareHelper();
330     } else {
331         uri = AssembleUserDbUri(userId, key);
332         helper = CreateUserDbDataShareHelper(userId);
333     }
334 }
335 
CreateDataShareHelper() const336 std::shared_ptr<DataShare::DataShareHelper> SettingDataManager::CreateDataShareHelper() const
337 {
338     if (remoteObject_ == nullptr) {
339         LOGE("remoteObject_ is null");
340         return nullptr;
341     }
342     const auto [errCode, helper] =
343         DataShare::DataShareHelper::Create(remoteObject_, SETTING_DATA_URI, SETTING_DATA_EXT_URI);
344     if (errCode != DataShare::E_OK) {
345         LOGE("create data share helper failed: %{public}d", errCode);
346         return nullptr;
347     }
348     return helper;
349 }
350 
CreateUserDbDataShareHelper(const int32_t userId) const351 std::shared_ptr<DataShare::DataShareHelper> SettingDataManager::CreateUserDbDataShareHelper(const int32_t userId) const
352 {
353     if (remoteObject_ == nullptr) {
354         LOGE("remoteObject_ is null");
355         return nullptr;
356     }
357     std::string userUri = SETTING_DATA_USER_URI_PREFIX + std::to_string(userId) + SETTING_DATA_USER_URI_SUFFIX;
358     const auto [errCode, helper] =
359         DataShare::DataShareHelper::Create(remoteObject_, userUri, SETTING_DATA_EXT_URI);
360     if (errCode != DataShare::E_OK) {
361         LOGE("create user data share helper failed: %{public}d, userId: %{public}d", errCode, userId);
362         return nullptr;
363     }
364     return helper;
365 }
366 
ReleaseDataShareHelper(const std::shared_ptr<DataShare::DataShareHelper> & helper)367 bool SettingDataManager::ReleaseDataShareHelper(const std::shared_ptr<DataShare::DataShareHelper>& helper)
368 {
369     if (helper == nullptr) {
370         LOGE("helper is null");
371         return false;
372     }
373     if (!helper->Release()) {
374         LOGE("release data share helper failed");
375         return false;
376     }
377     return true;
378 }
379 
GenerateObserverName(const std::string & key,const int32_t userId)380 std::string SettingDataManager::GenerateObserverName(const std::string& key, const int32_t userId)
381 {
382     return key + "::" + std::to_string(userId);
383 }
384 
CreateObserver(const std::string & key,const SettingDataObserver::UpdateFunc & updateFunc,const int32_t userId)385 sptr<SettingDataObserver> SettingDataManager::CreateObserver(const std::string& key,
386     const SettingDataObserver::UpdateFunc& updateFunc, const int32_t userId)
387 {
388     sptr<SettingDataObserver> observer = sptr<SettingDataObserver>::MakeSptr();
389     if (observer == nullptr) {
390         LOGE("observer is null");
391         return observer;
392     }
393     observer->SetKey(key);
394     observer->SetUpdateFunc(updateFunc);
395     observer->SetUserId(userId);
396     return observer;
397 }
398 
AssembleUri(const std::string & key)399 std::string SettingDataManager::AssembleUri(const std::string& key)
400 {
401     std::string uriString = SETTING_DATA_URI;
402     uriString.append(SETTING_DATA_KEY_URI + key);
403     return uriString;
404 }
405 
AssembleUserDbUri(const int32_t userId,const std::string & key)406 std::string SettingDataManager::AssembleUserDbUri(const int32_t userId, const std::string& key)
407 {
408     std::string uriString = SETTING_DATA_USER_URI_PREFIX;
409     uriString.append(std::to_string(userId) + SETTING_DATA_USER_URI_SUFFIX);
410     uriString.append(SETTING_DATA_KEY_URI + key);
411     return uriString;
412 }
413 } // namespace OHOS::ArkUi::UiAppearance
414