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