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