• 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 "dataobs_mgr_client.h"
17 #include "datashare_ext_ability_context.h"
18 #include "data_storage_errors.h"
19 #include "data_storage_log_wrapper.h"
20 #include "opkey_ability.h"
21 #include "pdp_profile_ability.h"
22 #include "sim_ability.h"
23 #include "sms_mms_ability.h"
24 #include "telephony_datashare_stub_impl.h"
25 
26 namespace OHOS {
27 namespace DataShare {
28 using DataObsMgrClient = OHOS::AAFwk::DataObsMgrClient;
29 using namespace OHOS::Telephony;
30 
SetOpKeyAbility(std::shared_ptr<DataShareExtAbility> extension)31 void TelephonyDataShareStubImpl::SetOpKeyAbility(std::shared_ptr<DataShareExtAbility> extension)
32 {
33     std::lock_guard<std::mutex> lock(opKeyMutex_);
34     opKeyAbility_ = extension;
35 }
36 
SetPdpProfileAbility(std::shared_ptr<DataShareExtAbility> extension)37 void TelephonyDataShareStubImpl::SetPdpProfileAbility(std::shared_ptr<DataShareExtAbility> extension)
38 {
39     std::lock_guard<std::mutex> lock(pdpProfileMutex_);
40     pdpProfileAbility_ = extension;
41 }
42 
SetSimAbility(std::shared_ptr<DataShareExtAbility> extension)43 void TelephonyDataShareStubImpl::SetSimAbility(std::shared_ptr<DataShareExtAbility> extension)
44 {
45     std::lock_guard<std::mutex> lock(simMutex_);
46     simAbility_ = extension;
47 }
48 
SetSmsMmsAbility(std::shared_ptr<DataShareExtAbility> extension)49 void TelephonyDataShareStubImpl::SetSmsMmsAbility(std::shared_ptr<DataShareExtAbility> extension)
50 {
51     std::lock_guard<std::mutex> lock(smsMmsMutex_);
52     smsMmsAbility_ = extension;
53 }
54 
GetTelephonyDataAbility()55 std::shared_ptr<DataShareExtAbility> TelephonyDataShareStubImpl::GetTelephonyDataAbility()
56 {
57     return telephonyDataAbility_;
58 }
59 
GetOpKeyAbility()60 std::shared_ptr<DataShareExtAbility> TelephonyDataShareStubImpl::GetOpKeyAbility()
61 {
62     std::lock_guard<std::mutex> lock(opKeyMutex_);
63     if (opKeyAbility_ == nullptr) {
64         opKeyAbility_.reset(OpKeyAbility::Create());
65     }
66     return opKeyAbility_;
67 }
68 
GetPdpProfileAbility()69 std::shared_ptr<DataShareExtAbility> TelephonyDataShareStubImpl::GetPdpProfileAbility()
70 {
71     std::lock_guard<std::mutex> lock(pdpProfileMutex_);
72     if (pdpProfileAbility_ == nullptr) {
73         pdpProfileAbility_.reset(PdpProfileAbility::Create());
74     }
75     return pdpProfileAbility_;
76 }
77 
GetSimAbility()78 std::shared_ptr<DataShareExtAbility> TelephonyDataShareStubImpl::GetSimAbility()
79 {
80     std::lock_guard<std::mutex> lock(simMutex_);
81     if (simAbility_ == nullptr) {
82         simAbility_.reset(SimAbility::Create());
83     }
84     return simAbility_;
85 }
86 
GetSmsMmsAbility()87 std::shared_ptr<DataShareExtAbility> TelephonyDataShareStubImpl::GetSmsMmsAbility()
88 {
89     std::lock_guard<std::mutex> lock(smsMmsMutex_);
90     if (smsMmsAbility_ == nullptr) {
91         smsMmsAbility_.reset(SmsMmsAbility::Create());
92     }
93     return smsMmsAbility_;
94 }
95 
GetOwner(const Uri & uri)96 std::shared_ptr<DataShareExtAbility> TelephonyDataShareStubImpl::GetOwner(const Uri &uri)
97 {
98     OHOS::Uri uriTemp = uri;
99     std::string path = uriTemp.GetPath();
100     DATA_STORAGE_LOGD("GetOwner uri: %{public}s", path.c_str());
101     if (path.find("com.ohos.telephonydataability") != std::string::npos) {
102         return GetTelephonyDataAbility();
103     }
104     if (path.find("com.ohos.opkeyability") != std::string::npos) {
105         return GetOpKeyAbility();
106     }
107     if (path.find("com.ohos.pdpprofileability") != std::string::npos) {
108         return GetPdpProfileAbility();
109     }
110     if (path.find("com.ohos.simability") != std::string::npos) {
111         return GetSimAbility();
112     }
113     if (path.find("com.ohos.smsmmsability") != std::string::npos) {
114         return GetSmsMmsAbility();
115     }
116     return nullptr;
117 }
118 
Insert(const Uri & uri,const DataShareValuesBucket & value)119 int TelephonyDataShareStubImpl::Insert(const Uri &uri, const DataShareValuesBucket &value)
120 {
121     DATA_STORAGE_LOGI("Insert begin.");
122     int ret = 0;
123     auto extension = GetOwner(uri);
124     if (extension == nullptr) {
125         DATA_STORAGE_LOGE("Insert failed, extension is null.");
126         return ret;
127     }
128     ret = extension->Insert(uri, value);
129     DATA_STORAGE_LOGI("Insert end successfully. ret: %{public}d", ret);
130     if (ret != Telephony::OPERATION_ERROR) {
131         NotifyChange(uri);
132     }
133     return ret;
134 }
135 
Update(const Uri & uri,const DataSharePredicates & predicates,const DataShareValuesBucket & value)136 int TelephonyDataShareStubImpl::Update(const Uri &uri, const DataSharePredicates &predicates,
137     const DataShareValuesBucket &value)
138 {
139     DATA_STORAGE_LOGI("Update begin.");
140     int ret = 0;
141     auto extension = GetOwner(uri);
142     if (extension == nullptr) {
143         DATA_STORAGE_LOGE("Update failed, extension is null.");
144         return ret;
145     }
146     ret = extension->Update(uri, predicates, value);
147     DATA_STORAGE_LOGI("Update end successfully. ret: %{public}d", ret);
148     if (ret != Telephony::OPERATION_ERROR) {
149         NotifyChange(uri);
150     }
151     return ret;
152 }
153 
Delete(const Uri & uri,const DataSharePredicates & predicates)154 int TelephonyDataShareStubImpl::Delete(const Uri &uri, const DataSharePredicates &predicates)
155 {
156     DATA_STORAGE_LOGI("Delete begin.");
157     int ret = 0;
158     auto extension = GetOwner(uri);
159     if (extension == nullptr) {
160         DATA_STORAGE_LOGE("Delete failed, extension is null.");
161         return ret;
162     }
163     ret = extension->Delete(uri, predicates);
164     DATA_STORAGE_LOGI("Delete end successfully. ret: %{public}d", ret);
165     if (ret != Telephony::OPERATION_ERROR) {
166         NotifyChange(uri);
167     }
168     return ret;
169 }
170 
Query(const Uri & uri,const DataSharePredicates & predicates,std::vector<std::string> & columns,DatashareBusinessError & businessError)171 std::shared_ptr<DataShareResultSet> TelephonyDataShareStubImpl::Query(const Uri &uri,
172     const DataSharePredicates &predicates, std::vector<std::string> &columns, DatashareBusinessError &businessError)
173 {
174     DATA_STORAGE_LOGD("Query begin.");
175     auto extension = GetOwner(uri);
176     if (extension == nullptr) {
177         DATA_STORAGE_LOGE("Query failed, extension is null.");
178         return nullptr;
179     }
180     auto resultSet = extension->Query(uri, predicates, columns, businessError);
181     DATA_STORAGE_LOGD("Query end successfully.");
182     return resultSet;
183 }
184 
BatchInsert(const Uri & uri,const std::vector<DataShareValuesBucket> & values)185 int TelephonyDataShareStubImpl::BatchInsert(const Uri &uri, const std::vector<DataShareValuesBucket> &values)
186 {
187     DATA_STORAGE_LOGI("BatchInsert begin.");
188     int ret = 0;
189     auto extension = GetOwner(uri);
190     if (extension == nullptr) {
191         DATA_STORAGE_LOGE("BatchInsert failed, extension is null.");
192         return ret;
193     }
194     ret = extension->BatchInsert(uri, values);
195     DATA_STORAGE_LOGI("BatchInsert end successfully. ret: %{public}d", ret);
196     if (ret != Telephony::OPERATION_ERROR) {
197         NotifyChange(uri);
198     }
199     return ret;
200 }
201 
GetFileTypes(const Uri & uri,const std::string & mimeTypeFilter)202 std::vector<std::string> TelephonyDataShareStubImpl::GetFileTypes(const Uri &uri, const std::string &mimeTypeFilter)
203 {
204     DATA_STORAGE_LOGI("GetFileTypes not supported.");
205     std::vector<std::string> result;
206     return result;
207 }
208 
OpenFile(const Uri & uri,const std::string & mode)209 int TelephonyDataShareStubImpl::OpenFile(const Uri &uri, const std::string &mode)
210 {
211     DATA_STORAGE_LOGI("OpenFile not supported.");
212     return -1;
213 }
214 
OpenRawFile(const Uri & uri,const std::string & mode)215 int TelephonyDataShareStubImpl::OpenRawFile(const Uri &uri, const std::string &mode)
216 {
217     DATA_STORAGE_LOGI("OpenRawFile not supported.");
218     return -1;
219 }
220 
GetType(const Uri & uri)221 std::string TelephonyDataShareStubImpl::GetType(const Uri &uri)
222 {
223     DATA_STORAGE_LOGI("GetType not supported.");
224     return "";
225 }
226 
RegisterObserver(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver)227 bool TelephonyDataShareStubImpl::RegisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
228 {
229     DATA_STORAGE_LOGI("%{public}s begin.", __func__);
230     auto obsMgrClient = DataObsMgrClient::GetInstance();
231     if (obsMgrClient == nullptr) {
232         DATA_STORAGE_LOGE("%{public}s obsMgrClient is nullptr", __func__);
233         return false;
234     }
235 
236     ErrCode ret = obsMgrClient->RegisterObserver(uri, dataObserver);
237     if (ret != ERR_OK) {
238         DATA_STORAGE_LOGE("%{public}s obsMgrClient->RegisterObserver error return %{public}d", __func__, ret);
239         return false;
240     }
241     return true;
242 }
243 
UnregisterObserver(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver)244 bool TelephonyDataShareStubImpl::UnregisterObserver(const Uri &uri,
245     const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
246 {
247     DATA_STORAGE_LOGI("%{public}s begin.", __func__);
248     auto obsMgrClient = DataObsMgrClient::GetInstance();
249     if (obsMgrClient == nullptr) {
250         DATA_STORAGE_LOGE("%{public}s obsMgrClient is nullptr", __func__);
251         return false;
252     }
253 
254     ErrCode ret = obsMgrClient->UnregisterObserver(uri, dataObserver);
255     if (ret != ERR_OK) {
256         DATA_STORAGE_LOGE("%{public}s obsMgrClient->UnregisterObserver error return %{public}d", __func__, ret);
257         return false;
258     }
259     return true;
260 }
261 
NotifyChange(const Uri & uri)262 bool TelephonyDataShareStubImpl::NotifyChange(const Uri &uri)
263 {
264     auto obsMgrClient = DataObsMgrClient::GetInstance();
265     if (obsMgrClient == nullptr) {
266         DATA_STORAGE_LOGE("%{public}s obsMgrClient is nullptr", __func__);
267         return false;
268     }
269 
270     ErrCode ret = obsMgrClient->NotifyChange(uri);
271     if (ret != ERR_OK) {
272         DATA_STORAGE_LOGE("%{public}s obsMgrClient->NotifyChange error return %{public}d", __func__, ret);
273         return false;
274     }
275     return true;
276 }
277 
NormalizeUri(const Uri & uri)278 Uri TelephonyDataShareStubImpl::NormalizeUri(const Uri &uri)
279 {
280     DATA_STORAGE_LOGI("NormalizeUri not supported.");
281     return uri;
282 }
283 
DenormalizeUri(const Uri & uri)284 Uri TelephonyDataShareStubImpl::DenormalizeUri(const Uri &uri)
285 {
286     DATA_STORAGE_LOGI("DenormalizeUri not supported.");
287     return uri;
288 }
289 } // namespace DataShare
290 } // namespace OHOS
291