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 "settings_data_manager.h"
17 
18 #include "iam_logger.h"
19 #include "iservice_registry.h"
20 #include "system_ability_definition.h"
21 #include "uri.h"
22 
23 #define LOG_TAG "PIN_AUTH_SDK"
24 
25 namespace OHOS {
26 namespace UserIam {
27 namespace PinAuth {
28 namespace {
29 const std::string SETTING_COLUMN_KEYWORD = "KEYWORD";
30 const std::string SETTING_COLUMN_VALUE = "VALUE";
31 const char *PIN_SETTING_URI_PROXY = "datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_SECURE_";
32 const char *SETTINGS_DATA_EXT_URI = "datashare:///com.ohos.settingsdata.DataAbility";
33 } // namespace
34 
GetIntValue(int32_t userId,const std::string & key,int32_t & value)35 bool SettingsDataManager::GetIntValue(int32_t userId, const std::string &key, int32_t &value)
36 {
37     std::string valueStr = "";
38     SettingsDataManager settingsDataManager;
39     if (!settingsDataManager.GetStringValue(userId, key, valueStr)) {
40         IAM_LOGE("GetStringValue failed");
41         return false;
42     }
43     const int32_t DECIMAL = 10;
44     value = static_cast<int32_t>(strtoll(valueStr.c_str(), nullptr, DECIMAL));
45     return true;
46 }
47 
GetStringValue(int32_t userId,const std::string & key,std::string & value)48 bool SettingsDataManager::GetStringValue(int32_t userId, const std::string &key, std::string &value)
49 {
50     auto helper = CreateDataShareHelper(userId);
51     if (helper == nullptr) {
52         return false;
53     }
54     std::vector<std::string> columns = {SETTING_COLUMN_VALUE};
55     DataShare::DataSharePredicates predicates;
56     predicates.EqualTo(SETTING_COLUMN_KEYWORD, key);
57     Uri uri(AssembleUri(userId, key));
58     auto resultSet = helper->Query(uri, predicates, columns);
59     ReleaseDataShareHelper(helper);
60     if (resultSet == nullptr) {
61         IAM_LOGE("helper->Query return nullptr");
62         return false;
63     }
64     int32_t count;
65     resultSet->GetRowCount(count);
66     if (count == 0) {
67         IAM_LOGE("not found value, key=%{public}s, count=%{public}d", key.c_str(), count);
68         resultSet->Close();
69         return false;
70     }
71     const int32_t index = 0;
72     resultSet->GoToRow(index);
73     int32_t ret = resultSet->GetString(index, value);
74     resultSet->Close();
75     if (ret != DataShare::E_OK) {
76         IAM_LOGE("resultSet->GetString return not ok, ret=%{public}d", ret);
77         return false;
78     }
79     return true;
80 }
81 
CreateDataShareHelper(int32_t userId)82 std::shared_ptr<DataShare::DataShareHelper> SettingsDataManager::CreateDataShareHelper(int32_t userId)
83 {
84     sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
85     if (sam == nullptr) {
86         IAM_LOGE("GetSystemAbilityManager return nullptr");
87         return nullptr;
88     }
89     auto remoteObj = sam->GetSystemAbility(SUBSYS_USERIAM_SYS_ABILITY_PINAUTH);
90     if (remoteObj == nullptr) {
91         IAM_LOGE("GetSystemAbility return nullptr");
92         return nullptr;
93     }
94 
95     std::string uriStr = std::string(PIN_SETTING_URI_PROXY) + std::to_string(userId) + "?Proxy=true";
96     std::string extUriStr(SETTINGS_DATA_EXT_URI);
97     auto helper = DataShare::DataShareHelper::Creator(remoteObj, uriStr, extUriStr);
98     if (helper == nullptr) {
99         IAM_LOGE("helper is nullptr, uri=%{public}s", uriStr.c_str());
100         return nullptr;
101     }
102     return helper;
103 }
104 
ReleaseDataShareHelper(std::shared_ptr<DataShare::DataShareHelper> & helper)105 void SettingsDataManager::ReleaseDataShareHelper(std::shared_ptr<DataShare::DataShareHelper> &helper)
106 {
107     if (!helper->Release()) {
108         IAM_LOGE("release helper fail");
109     }
110 }
111 
AssembleUri(int32_t userId,const std::string & key)112 Uri SettingsDataManager::AssembleUri(int32_t userId, const std::string &key)
113 {
114     std::string uriStr = std::string(PIN_SETTING_URI_PROXY) + std::to_string(userId) + "?Proxy=true";
115     Uri uri(uriStr + "&key=" + key);
116     return uri;
117 }
118 } // namespace PinAuth
119 } // namespace UserIam
120 } // namespace OHOS