1 /*
2 * Copyright (c) 2022 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_provider.h"
17
18 #include "datashare_predicates.h"
19 #include "datashare_result_set.h"
20 #include "datashare_values_bucket.h"
21 #include "ipc_skeleton.h"
22 #include "iservice_registry.h"
23 #include "power_log.h"
24 #include "rdb_errno.h"
25 #include "result_set.h"
26 #include "uri.h"
27
28 namespace OHOS {
29 namespace PowerMgr {
30 SettingProvider* SettingProvider::instance_;
31 std::mutex SettingProvider::mutex_;
32 sptr<IRemoteObject> SettingProvider::remoteObj_;
33 namespace {
34 const std::string SETTING_COLUMN_KEYWORD = "KEYWORD";
35 const std::string SETTING_COLUMN_VALUE = "VALUE";
36 const std::string SETTING_URI_PROXY = "datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true";
37 } // namespace
38
~SettingProvider()39 SettingProvider::~SettingProvider()
40 {
41 instance_ = nullptr;
42 remoteObj_ = nullptr;
43 }
44
GetInstance(int32_t systemAbilityId)45 SettingProvider& SettingProvider::GetInstance(int32_t systemAbilityId)
46 {
47 if (instance_ == nullptr) {
48 std::lock_guard<std::mutex> lock(mutex_);
49 if (instance_ == nullptr) {
50 instance_ = new SettingProvider();
51 Initialize(systemAbilityId);
52 }
53 }
54 return *instance_;
55 }
56
GetIntValue(const std::string & key,int32_t & value)57 ErrCode SettingProvider::GetIntValue(const std::string& key, int32_t& value)
58 {
59 int64_t valueLong;
60 ErrCode ret = GetLongValue(key, valueLong);
61 if (ret != ERR_OK) {
62 return ret;
63 }
64 value = static_cast<int32_t>(valueLong);
65 return ERR_OK;
66 }
67
GetLongValue(const std::string & key,int64_t & value)68 ErrCode SettingProvider::GetLongValue(const std::string& key, int64_t& value)
69 {
70 std::string valueStr;
71 ErrCode ret = GetStringValue(key, valueStr);
72 if (ret != ERR_OK) {
73 return ret;
74 }
75 value = static_cast<int64_t>(strtoll(valueStr.c_str(), nullptr, 10));
76 return ERR_OK;
77 }
78
GetBoolValue(const std::string & key,bool & value)79 ErrCode SettingProvider::GetBoolValue(const std::string& key, bool& value)
80 {
81 std::string valueStr;
82 ErrCode ret = GetStringValue(key, valueStr);
83 if (ret != ERR_OK) {
84 return ret;
85 }
86 value = (valueStr == "true");
87 return ERR_OK;
88 }
89
PutIntValue(const std::string & key,int32_t value,bool needNotify)90 ErrCode SettingProvider::PutIntValue(const std::string& key, int32_t value, bool needNotify)
91 {
92 return PutStringValue(key, std::to_string(value), needNotify);
93 }
94
PutLongValue(const std::string & key,int64_t value,bool needNotify)95 ErrCode SettingProvider::PutLongValue(const std::string& key, int64_t value, bool needNotify)
96 {
97 return PutStringValue(key, std::to_string(value), needNotify);
98 }
99
PutBoolValue(const std::string & key,bool value,bool needNotify)100 ErrCode SettingProvider::PutBoolValue(const std::string& key, bool value, bool needNotify)
101 {
102 std::string valueStr = value ? "true" : "false";
103 return PutStringValue(key, valueStr, needNotify);
104 }
105
IsValidKey(const std::string & key)106 bool SettingProvider::IsValidKey(const std::string& key)
107 {
108 std::string value;
109 ErrCode ret = GetStringValue(key, value);
110 return (ret != ERR_NAME_NOT_FOUND) && (!value.empty());
111 }
112
CreateObserver(const std::string & key,SettingObserver::UpdateFunc & func)113 sptr<SettingObserver> SettingProvider::CreateObserver(const std::string& key, SettingObserver::UpdateFunc& func)
114 {
115 sptr<SettingObserver> observer = new SettingObserver();
116 observer->SetKey(key);
117 observer->SetUpdateFunc(func);
118 return observer;
119 }
120
RegisterObserver(const sptr<SettingObserver> & observer)121 ErrCode SettingProvider::RegisterObserver(const sptr<SettingObserver>& observer)
122 {
123 std::string callingIdentity = IPCSkeleton::ResetCallingIdentity();
124 auto uri = AssembleUri(observer->GetKey());
125 auto helper = CreateDataShareHelper();
126 if (helper == nullptr) {
127 IPCSkeleton::SetCallingIdentity(callingIdentity);
128 return ERR_NO_INIT;
129 }
130 helper->RegisterObserver(uri, observer);
131 ReleaseDataShareHelper(helper);
132 IPCSkeleton::SetCallingIdentity(callingIdentity);
133 POWER_HILOGD(COMP_UTILS, "succeed to register observer of uri=%{public}s", uri.ToString().c_str());
134 return ERR_OK;
135 }
136
UnregisterObserver(const sptr<SettingObserver> & observer)137 ErrCode SettingProvider::UnregisterObserver(const sptr<SettingObserver>& observer)
138 {
139 std::string callingIdentity = IPCSkeleton::ResetCallingIdentity();
140 auto uri = AssembleUri(observer->GetKey());
141 auto helper = CreateDataShareHelper();
142 if (helper == nullptr) {
143 IPCSkeleton::SetCallingIdentity(callingIdentity);
144 return ERR_NO_INIT;
145 }
146 helper->UnregisterObserver(uri, observer);
147 ReleaseDataShareHelper(helper);
148 IPCSkeleton::SetCallingIdentity(callingIdentity);
149 POWER_HILOGD(COMP_UTILS, "succeed to unregister observer of uri=%{public}s", uri.ToString().c_str());
150 return ERR_OK;
151 }
152
Initialize(int32_t systemAbilityId)153 void SettingProvider::Initialize(int32_t systemAbilityId)
154 {
155 auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
156 if (sam == nullptr) {
157 POWER_HILOGE(COMP_UTILS, "GetSystemAbilityManager return nullptr");
158 return;
159 }
160 auto remoteObj = sam->GetSystemAbility(systemAbilityId);
161 if (remoteObj == nullptr) {
162 POWER_HILOGE(COMP_UTILS, "GetSystemAbility return nullptr, systemAbilityId=%{public}d", systemAbilityId);
163 return;
164 }
165 remoteObj_ = remoteObj;
166 }
167
GetStringValue(const std::string & key,std::string & value)168 ErrCode SettingProvider::GetStringValue(const std::string& key, std::string& value)
169 {
170 std::string callingIdentity = IPCSkeleton::ResetCallingIdentity();
171 auto helper = CreateDataShareHelper();
172 if (helper == nullptr) {
173 IPCSkeleton::SetCallingIdentity(callingIdentity);
174 return ERR_NO_INIT;
175 }
176 std::vector<std::string> columns = {SETTING_COLUMN_VALUE};
177 DataShare::DataSharePredicates predicates;
178 predicates.EqualTo(SETTING_COLUMN_KEYWORD, key);
179 POWER_HILOGD(COMP_UTILS, "key=%{public}s", key.c_str());
180 Uri uri(AssembleUri(key));
181 auto resultSet = helper->Query(uri, predicates, columns);
182 ReleaseDataShareHelper(helper);
183 if (resultSet == nullptr) {
184 POWER_HILOGE(COMP_UTILS, "helper->Query return nullptr");
185 IPCSkeleton::SetCallingIdentity(callingIdentity);
186 return ERR_INVALID_OPERATION;
187 }
188 int32_t count;
189 resultSet->GetRowCount(count);
190 if (count == 0) {
191 POWER_HILOGW(COMP_UTILS, "not found value, key=%{public}s, count=%{public}d", key.c_str(), count);
192 IPCSkeleton::SetCallingIdentity(callingIdentity);
193 return ERR_NAME_NOT_FOUND;
194 }
195 const int32_t INDEX = 0;
196 resultSet->GoToRow(INDEX);
197 int32_t ret = resultSet->GetString(INDEX, value);
198 if (ret != NativeRdb::E_OK) {
199 POWER_HILOGW(COMP_UTILS, "resultSet->GetString return not ok, ret=%{public}d", ret);
200 IPCSkeleton::SetCallingIdentity(callingIdentity);
201 return ERR_INVALID_VALUE;
202 }
203 resultSet->Close();
204 IPCSkeleton::SetCallingIdentity(callingIdentity);
205 return ERR_OK;
206 }
207
PutStringValue(const std::string & key,const std::string & value,bool needNotify)208 ErrCode SettingProvider::PutStringValue(const std::string& key, const std::string& value, bool needNotify)
209 {
210 std::string callingIdentity = IPCSkeleton::ResetCallingIdentity();
211 auto helper = CreateDataShareHelper();
212 if (helper == nullptr) {
213 IPCSkeleton::SetCallingIdentity(callingIdentity);
214 return ERR_NO_INIT;
215 }
216 POWER_HILOGD(COMP_UTILS, "key=%{public}s, value=%{public}s", key.c_str(), value.c_str());
217 DataShare::DataShareValueObject keyObj(key);
218 DataShare::DataShareValueObject valueObj(value);
219 DataShare::DataShareValuesBucket bucket;
220 bucket.Put(SETTING_COLUMN_KEYWORD, keyObj);
221 bucket.Put(SETTING_COLUMN_VALUE, valueObj);
222 DataShare::DataSharePredicates predicates;
223 predicates.EqualTo(SETTING_COLUMN_KEYWORD, key);
224 Uri uri(AssembleUri(key));
225 if (helper->Update(uri, predicates, bucket) <= 0) {
226 POWER_HILOGD(COMP_UTILS, "no data exist, insert one row");
227 helper->Insert(uri, bucket);
228 }
229 if (needNotify) {
230 helper->NotifyChange(AssembleUri(key));
231 }
232 ReleaseDataShareHelper(helper);
233 IPCSkeleton::SetCallingIdentity(callingIdentity);
234 return ERR_OK;
235 }
236
CreateDataShareHelper()237 std::shared_ptr<DataShare::DataShareHelper> SettingProvider::CreateDataShareHelper()
238 {
239 auto helper = DataShare::DataShareHelper::Creator(remoteObj_, SETTING_URI_PROXY);
240 if (helper == nullptr) {
241 POWER_HILOGW(COMP_UTILS, "helper is nullptr, uri=%{public}s", SETTING_URI_PROXY.c_str());
242 return nullptr;
243 }
244 return helper;
245 }
246
ReleaseDataShareHelper(std::shared_ptr<DataShare::DataShareHelper> & helper)247 bool SettingProvider::ReleaseDataShareHelper(std::shared_ptr<DataShare::DataShareHelper>& helper)
248 {
249 if (!helper->Release()) {
250 POWER_HILOGW(COMP_UTILS, "release helper fail");
251 return false;
252 }
253 return true;
254 }
255
AssembleUri(const std::string & key)256 Uri SettingProvider::AssembleUri(const std::string& key)
257 {
258 Uri uri(SETTING_URI_PROXY + "&key=" + key);
259 return uri;
260 }
261 } // namespace PowerMgr
262 } // namespace OHOS