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