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 "work_datashare_helper.h"
17 #include "iservice_registry.h"
18 #include "system_ability_definition.h"
19 #include "work_sched_hilog.h"
20
21 namespace OHOS {
22 namespace WorkScheduler {
23 namespace {
24 const std::string SETTING_COLUMN_KEYWORD = "KEYWORD";
25 const std::string SETTING_COLUMN_VALUE = "VALUE";
26 const std::string SETTING_URI_PROXY = "datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true";
27 constexpr const char *SETTINGS_DATA_EXT_URI = "datashare:///com.ohos.settingsdata.DataAbility";
28 }
29
~WorkDatashareHelper()30 WorkDatashareHelper::~WorkDatashareHelper() { }
31
GetInstance()32 WorkDatashareHelper& WorkDatashareHelper::GetInstance()
33 {
34 static WorkDatashareHelper workDatashareHelper;
35 return workDatashareHelper;
36 }
37
GetStringValue(const std::string & key,std::string & value)38 __attribute__((no_sanitize("cfi"))) bool WorkDatashareHelper::GetStringValue(const std::string& key, std::string& value)
39 {
40 auto helper = CreateDataShareHelper();
41 if (helper == nullptr) {
42 return false;
43 }
44 std::vector<std::string> columns = {SETTING_COLUMN_VALUE};
45 DataShare::DataSharePredicates predicates;
46 predicates.EqualTo(SETTING_COLUMN_KEYWORD, key);
47 Uri uri(SETTING_URI_PROXY + "&key=" + key);
48 auto resultSet = helper->Query(uri, predicates, columns);
49 ReleaseDataShareHelper(helper);
50 if (resultSet == nullptr) {
51 WS_HILOGE("helper->Query return nullptr");
52 return false;
53 }
54 int32_t count;
55 resultSet->GetRowCount(count);
56 if (count == 0) {
57 WS_HILOGW("not found value, key=%{public}s, count=%{public}d", key.c_str(), count);
58 resultSet->Close();
59 return false;
60 }
61 const int32_t INDEX = 0;
62 resultSet->GoToRow(INDEX);
63 int32_t ret = resultSet->GetString(INDEX, value);
64 if (ret != DataShare::E_OK) {
65 WS_HILOGW("resultSet->GetString return not ok, ret=%{public}d", ret);
66 resultSet->Close();
67 return false;
68 }
69 resultSet->Close();
70 return true;
71 }
72
CreateDataShareHelper()73 std::shared_ptr<DataShare::DataShareHelper> WorkDatashareHelper::CreateDataShareHelper()
74 {
75 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
76 if (samgr == nullptr) {
77 WS_HILOGE("GetSystemAbilityManager return nullptr");
78 return nullptr;
79 }
80 auto remoteObj = samgr->GetSystemAbility(WORK_SCHEDULE_SERVICE_ID);
81 if (remoteObj == nullptr) {
82 WS_HILOGE("GetSystemAbility return nullptr");
83 return nullptr;
84 }
85
86 auto helper = DataShare::DataShareHelper::Creator(remoteObj, SETTING_URI_PROXY, SETTINGS_DATA_EXT_URI);
87 if (helper == nullptr) {
88 WS_HILOGW("helper is nullptr, uri=%{public}s", SETTING_URI_PROXY.c_str());
89 return nullptr;
90 }
91 return helper;
92 }
93
ReleaseDataShareHelper(std::shared_ptr<DataShare::DataShareHelper> & helper)94 bool WorkDatashareHelper::ReleaseDataShareHelper(std::shared_ptr<DataShare::DataShareHelper>& helper)
95 {
96 if (!helper->Release()) {
97 WS_HILOGW("release helper fail");
98 return false;
99 }
100 return true;
101 }
102
103 } // namespace WorkScheduler
104 } // namespace OHOS