• 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 } // 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 
ExecRegisterCb(const sptr<SettingObserver> & observer)121 void SettingProvider::ExecRegisterCb(const sptr<SettingObserver>& observer)
122 {
123     if (observer == nullptr) {
124         POWER_HILOGE(COMP_UTILS, "observer is nullptr");
125         return;
126     }
127     observer->OnChange();
128 }
129 
RegisterObserver(const sptr<SettingObserver> & observer)130 ErrCode SettingProvider::RegisterObserver(const sptr<SettingObserver>& observer)
131 {
132     std::string callingIdentity = IPCSkeleton::ResetCallingIdentity();
133     auto uri = AssembleUri(observer->GetKey());
134     auto helper = CreateDataShareHelper();
135     if (helper == nullptr) {
136         IPCSkeleton::SetCallingIdentity(callingIdentity);
137         return ERR_NO_INIT;
138     }
139     helper->RegisterObserver(uri, observer);
140     helper->NotifyChange(uri);
141     std::thread execCb(SettingProvider::ExecRegisterCb, observer);
142     execCb.detach();
143     ReleaseDataShareHelper(helper);
144     IPCSkeleton::SetCallingIdentity(callingIdentity);
145     POWER_HILOGD(COMP_UTILS, "succeed to register observer of uri=%{public}s", uri.ToString().c_str());
146     return ERR_OK;
147 }
148 
UnregisterObserver(const sptr<SettingObserver> & observer)149 ErrCode SettingProvider::UnregisterObserver(const sptr<SettingObserver>& observer)
150 {
151     std::string callingIdentity = IPCSkeleton::ResetCallingIdentity();
152     auto uri = AssembleUri(observer->GetKey());
153     auto helper = CreateDataShareHelper();
154     if (helper == nullptr) {
155         IPCSkeleton::SetCallingIdentity(callingIdentity);
156         return ERR_NO_INIT;
157     }
158     helper->UnregisterObserver(uri, observer);
159     ReleaseDataShareHelper(helper);
160     IPCSkeleton::SetCallingIdentity(callingIdentity);
161     POWER_HILOGD(COMP_UTILS, "succeed to unregister observer of uri=%{public}s", uri.ToString().c_str());
162     return ERR_OK;
163 }
164 
Initialize(int32_t systemAbilityId)165 void SettingProvider::Initialize(int32_t systemAbilityId)
166 {
167     auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
168     if (sam == nullptr) {
169         POWER_HILOGE(COMP_UTILS, "GetSystemAbilityManager return nullptr");
170         return;
171     }
172     auto remoteObj = sam->GetSystemAbility(systemAbilityId);
173     if (remoteObj == nullptr) {
174         POWER_HILOGE(COMP_UTILS, "GetSystemAbility return nullptr, systemAbilityId=%{public}d", systemAbilityId);
175         return;
176     }
177     remoteObj_ = remoteObj;
178 }
179 
GetStringValue(const std::string & key,std::string & value)180 ErrCode SettingProvider::GetStringValue(const std::string& key, std::string& value)
181 {
182     std::string callingIdentity = IPCSkeleton::ResetCallingIdentity();
183     auto helper = CreateDataShareHelper();
184     if (helper == nullptr) {
185         IPCSkeleton::SetCallingIdentity(callingIdentity);
186         return ERR_NO_INIT;
187     }
188     std::vector<std::string> columns = {SETTING_COLUMN_VALUE};
189     DataShare::DataSharePredicates predicates;
190     predicates.EqualTo(SETTING_COLUMN_KEYWORD, key);
191     Uri uri(AssembleUri(key));
192     auto resultSet = helper->Query(uri, predicates, columns);
193     ReleaseDataShareHelper(helper);
194     if (resultSet == nullptr) {
195         POWER_HILOGE(COMP_UTILS, "helper->Query return nullptr");
196         IPCSkeleton::SetCallingIdentity(callingIdentity);
197         return ERR_INVALID_OPERATION;
198     }
199     int32_t count;
200     resultSet->GetRowCount(count);
201     if (count == 0) {
202         POWER_HILOGW(COMP_UTILS, "not found value, key=%{public}s, count=%{public}d", key.c_str(), count);
203         IPCSkeleton::SetCallingIdentity(callingIdentity);
204         return ERR_NAME_NOT_FOUND;
205     }
206     const int32_t INDEX = 0;
207     resultSet->GoToRow(INDEX);
208     int32_t ret = resultSet->GetString(INDEX, value);
209     if (ret != NativeRdb::E_OK) {
210         POWER_HILOGW(COMP_UTILS, "resultSet->GetString return not ok, ret=%{public}d", ret);
211         IPCSkeleton::SetCallingIdentity(callingIdentity);
212         return ERR_INVALID_VALUE;
213     }
214     resultSet->Close();
215     IPCSkeleton::SetCallingIdentity(callingIdentity);
216     return ERR_OK;
217 }
218 
PutStringValue(const std::string & key,const std::string & value,bool needNotify)219 ErrCode SettingProvider::PutStringValue(const std::string& key, const std::string& value, bool needNotify)
220 {
221     std::string callingIdentity = IPCSkeleton::ResetCallingIdentity();
222     auto helper = CreateDataShareHelper();
223     if (helper == nullptr) {
224         IPCSkeleton::SetCallingIdentity(callingIdentity);
225         return ERR_NO_INIT;
226     }
227     DataShare::DataShareValueObject keyObj(key);
228     DataShare::DataShareValueObject valueObj(value);
229     DataShare::DataShareValuesBucket bucket;
230     bucket.Put(SETTING_COLUMN_KEYWORD, keyObj);
231     bucket.Put(SETTING_COLUMN_VALUE, valueObj);
232     DataShare::DataSharePredicates predicates;
233     predicates.EqualTo(SETTING_COLUMN_KEYWORD, key);
234     Uri uri(AssembleUri(key));
235     if (helper->Update(uri, predicates, bucket) <= 0) {
236         POWER_HILOGD(COMP_UTILS, "no data exist, insert one row");
237         helper->Insert(uri, bucket);
238     }
239     if (needNotify) {
240         helper->NotifyChange(AssembleUri(key));
241     }
242     ReleaseDataShareHelper(helper);
243     IPCSkeleton::SetCallingIdentity(callingIdentity);
244     return ERR_OK;
245 }
246 
CreateDataShareHelper()247 std::shared_ptr<DataShare::DataShareHelper> SettingProvider::CreateDataShareHelper()
248 {
249     auto helper = DataShare::DataShareHelper::Creator(remoteObj_, SETTING_URI_PROXY);
250     if (helper == nullptr) {
251         POWER_HILOGW(COMP_UTILS, "helper is nullptr, uri=%{public}s", SETTING_URI_PROXY.c_str());
252         return nullptr;
253     }
254     return helper;
255 }
256 
ReleaseDataShareHelper(std::shared_ptr<DataShare::DataShareHelper> & helper)257 bool SettingProvider::ReleaseDataShareHelper(std::shared_ptr<DataShare::DataShareHelper>& helper)
258 {
259     if (!helper->Release()) {
260         POWER_HILOGW(COMP_UTILS, "release helper fail");
261         return false;
262     }
263     return true;
264 }
265 
AssembleUri(const std::string & key)266 Uri SettingProvider::AssembleUri(const std::string& key)
267 {
268     Uri uri(SETTING_URI_PROXY + "&key=" + key);
269     return uri;
270 }
271 } // namespace PowerMgr
272 } // namespace OHOS