• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 "pdp_profile_ability.h"
17 
18 #include <cstdint>
19 
20 #include "ability_context.h"
21 #include "ability_loader.h"
22 #include "abs_rdb_predicates.h"
23 #include "abs_shared_result_set.h"
24 #include "data_ability_predicates.h"
25 #include "data_storage_errors.h"
26 #include "data_storage_log_wrapper.h"
27 #include "new"
28 #include "pdp_profile_data.h"
29 #include "predicates_utils.h"
30 #include "rdb_errno.h"
31 #include "uri.h"
32 #include "utility"
33 
34 namespace OHOS {
35 using AppExecFwk::AbilityLoader;
36 using AppExecFwk::Ability;
37 namespace Telephony {
OnStart(const AppExecFwk::Want & want)38 void PdpProfileAbility::OnStart(const AppExecFwk::Want &want)
39 {
40     DATA_STORAGE_LOGI("PdpProfileAbility::OnStart\n");
41     Ability::OnStart(want);
42     auto abilityContext = GetAbilityContext();
43     if (abilityContext == nullptr) {
44         DATA_STORAGE_LOGE("PdpProfileAbility::OnStart GetAbilityContext is null");
45         return;
46     }
47     // switch database dir to el1 for init before unlock
48     abilityContext->SwitchArea(0);
49     std::string path = abilityContext->GetDatabaseDir();
50     DATA_STORAGE_LOGI("GetDatabaseDir: %{public}s", path.c_str());
51     if (!path.empty()) {
52         initDatabaseDir = true;
53         path.append("/");
54         helper_.UpdateDbPath(path);
55         InitUriMap();
56         int rdbInitCode = helper_.Init();
57         if (rdbInitCode == NativeRdb::E_OK) {
58             initRdbStore = true;
59         } else {
60             DATA_STORAGE_LOGE("PdpProfileAbility::OnStart rdb init fail!");
61             initRdbStore = false;
62         }
63     } else {
64         initDatabaseDir = false;
65         DATA_STORAGE_LOGE("PdpProfileAbility::OnStart##the databaseDir is empty!");
66     }
67 }
68 
Insert(const Uri & uri,const NativeRdb::ValuesBucket & value)69 int PdpProfileAbility::Insert(const Uri &uri, const NativeRdb::ValuesBucket &value)
70 {
71     if (!TelephonyPermission::CheckPermission(Permission::SET_TELEPHONY_STATE)) {
72         DATA_STORAGE_LOGE("Permission denied!");
73         return DATA_STORAGE_ERR_PERMISSION_ERR;
74     }
75     if (!IsInitOk()) {
76         return DATA_STORAGE_ERROR;
77     }
78     std::lock_guard<std::mutex> guard(lock_);
79     Uri tempUri = uri;
80     PdpProfileUriType pdpProfileUriType = ParseUriType(tempUri);
81     int64_t id = DATA_STORAGE_ERROR;
82     if (pdpProfileUriType == PdpProfileUriType::PDP_PROFILE) {
83         helper_.Insert(id, value, TABLE_PDP_PROFILE);
84     } else {
85         DATA_STORAGE_LOGE("PdpProfileAbility::Insert##uri = %{public}s", uri.ToString().c_str());
86     }
87     return id;
88 }
89 
Query(const Uri & uri,const std::vector<std::string> & columns,const NativeRdb::DataAbilityPredicates & predicates)90 std::shared_ptr<NativeRdb::AbsSharedResultSet> PdpProfileAbility::Query(
91     const Uri &uri, const std::vector<std::string> &columns, const NativeRdb::DataAbilityPredicates &predicates)
92 {
93     if (!TelephonyPermission::CheckPermission(Permission::GET_TELEPHONY_STATE)) {
94         DATA_STORAGE_LOGE("Permission denied!");
95         return nullptr;
96     }
97     std::shared_ptr<NativeRdb::AbsSharedResultSet> resultSet = nullptr;
98     if (!IsInitOk()) {
99         return resultSet;
100     }
101     Uri tempUri = uri;
102     PdpProfileUriType pdpProfileUriType = ParseUriType(tempUri);
103     if (pdpProfileUriType == PdpProfileUriType::PDP_PROFILE) {
104         NativeRdb::AbsRdbPredicates *absRdbPredicates = new NativeRdb::AbsRdbPredicates(TABLE_PDP_PROFILE);
105         if (absRdbPredicates != nullptr) {
106             ConvertPredicates(predicates, absRdbPredicates);
107             resultSet = helper_.Query(*absRdbPredicates, columns);
108             delete absRdbPredicates;
109             absRdbPredicates = nullptr;
110         } else {
111             DATA_STORAGE_LOGE("PdpProfileAbility::Delete  NativeRdb::AbsRdbPredicates is null!");
112         }
113     } else {
114         DATA_STORAGE_LOGE("PdpProfileAbility::Query##uri = %{public}s", uri.ToString().c_str());
115     }
116     return resultSet;
117 }
118 
Update(const Uri & uri,const NativeRdb::ValuesBucket & value,const NativeRdb::DataAbilityPredicates & predicates)119 int PdpProfileAbility::Update(
120     const Uri &uri, const NativeRdb::ValuesBucket &value, const NativeRdb::DataAbilityPredicates &predicates)
121 {
122     if (!TelephonyPermission::CheckPermission(Permission::SET_TELEPHONY_STATE)) {
123         DATA_STORAGE_LOGE("Permission denied!");
124         return DATA_STORAGE_ERR_PERMISSION_ERR;
125     }
126     int result = DATA_STORAGE_ERROR;
127     if (!IsInitOk()) {
128         return result;
129     }
130     std::lock_guard<std::mutex> guard(lock_);
131     Uri tempUri = uri;
132     PdpProfileUriType pdpProfileUriType = ParseUriType(tempUri);
133     NativeRdb::AbsRdbPredicates *absRdbPredicates = nullptr;
134     switch (pdpProfileUriType) {
135         case PdpProfileUriType::PDP_PROFILE: {
136             absRdbPredicates = new NativeRdb::AbsRdbPredicates(TABLE_PDP_PROFILE);
137             break;
138         }
139         case PdpProfileUriType::RESET: {
140             result = helper_.ResetApn();
141             if (result != NativeRdb::E_OK) {
142                 DATA_STORAGE_LOGE("PdpProfileAbility::Update  ResetApn fail!");
143                 result = static_cast<int>(LoadProFileErrorType::RESET_APN_FAIL);
144             }
145             break;
146         }
147         default:
148             DATA_STORAGE_LOGE("PdpProfileAbility::Update##uri = %{public}s", uri.ToString().c_str());
149             break;
150     }
151     if (absRdbPredicates != nullptr) {
152         int changedRows = 0;
153         ConvertPredicates(predicates, absRdbPredicates);
154         result = helper_.Update(changedRows, value, *absRdbPredicates);
155         delete absRdbPredicates;
156         absRdbPredicates = nullptr;
157     } else if (result == DATA_STORAGE_ERROR) {
158         DATA_STORAGE_LOGE("PdpProfileAbility::Update  NativeRdb::AbsRdbPredicates is null!");
159     }
160     return result;
161 }
162 
Delete(const Uri & uri,const NativeRdb::DataAbilityPredicates & predicates)163 int PdpProfileAbility::Delete(const Uri &uri, const NativeRdb::DataAbilityPredicates &predicates)
164 {
165     if (!TelephonyPermission::CheckPermission(Permission::SET_TELEPHONY_STATE)) {
166         DATA_STORAGE_LOGE("Permission denied!");
167         return DATA_STORAGE_ERR_PERMISSION_ERR;
168     }
169     int result = DATA_STORAGE_ERROR;
170     if (!IsInitOk()) {
171         return result;
172     }
173     std::lock_guard<std::mutex> guard(lock_);
174     Uri tempUri = uri;
175     PdpProfileUriType pdpProfileUriType = ParseUriType(tempUri);
176     if (pdpProfileUriType == PdpProfileUriType::PDP_PROFILE) {
177         NativeRdb::AbsRdbPredicates *absRdbPredicates = new NativeRdb::AbsRdbPredicates(TABLE_PDP_PROFILE);
178         if (absRdbPredicates != nullptr) {
179             ConvertPredicates(predicates, absRdbPredicates);
180             int deletedRows = 0;
181             result = helper_.Delete(deletedRows, *absRdbPredicates);
182             delete absRdbPredicates;
183             absRdbPredicates = nullptr;
184         } else {
185             DATA_STORAGE_LOGE("PdpProfileAbility::Delete  NativeRdb::AbsRdbPredicates is null!");
186         }
187     } else {
188         DATA_STORAGE_LOGI("PdpProfileAbility::Delete##uri = %{public}s\n", uri.ToString().c_str());
189     }
190     return result;
191 }
192 
IsInitOk()193 bool PdpProfileAbility::IsInitOk()
194 {
195     if (!initDatabaseDir) {
196         DATA_STORAGE_LOGE("PdpProfileAbility::IsInitOk initDatabaseDir failed!");
197         return false;
198     }
199     if (!initRdbStore) {
200         DATA_STORAGE_LOGE("PdpProfileAbility::IsInitOk initRdbStore failed!");
201         return false;
202     }
203     return true;
204 }
205 
InitUriMap()206 void PdpProfileAbility::InitUriMap()
207 {
208     pdpProfileUriMap = {
209         {"/net/pdp_profile", PdpProfileUriType::PDP_PROFILE},
210         {"/net/pdp_profile/reset", PdpProfileUriType::RESET}
211     };
212 }
213 
GetType(const Uri & uri)214 std::string PdpProfileAbility::GetType(const Uri &uri)
215 {
216     DATA_STORAGE_LOGI("PdpProfileAbility::GetType##uri = %{public}s\n", uri.ToString().c_str());
217     std::string retval(uri.ToString());
218     return retval;
219 }
220 
OpenFile(const Uri & uri,const std::string & mode)221 int PdpProfileAbility::OpenFile(const Uri &uri, const std::string &mode)
222 {
223     DATA_STORAGE_LOGI("PdpProfileAbility::OpenFile##uri = %{public}s\n", uri.ToString().c_str());
224     Uri tempUri = uri;
225     PdpProfileUriType pdpProfileUriType = ParseUriType(tempUri);
226     return static_cast<int>(pdpProfileUriType);
227 }
228 
ParseUriType(Uri & uri)229 PdpProfileUriType PdpProfileAbility::ParseUriType(Uri &uri)
230 {
231     DATA_STORAGE_LOGI("PdpProfileAbility::ParseUriType start\n");
232     PdpProfileUriType pdpProfileUriType = PdpProfileUriType::UNKNOW;
233     std::string uriPath = uri.ToString();
234     if (!uriPath.empty()) {
235         helper_.ReplaceAllStr(uriPath, ":///", "://");
236         Uri tempUri(uriPath);
237         std::string path = tempUri.GetPath();
238         if (!path.empty()) {
239             DATA_STORAGE_LOGI("PdpProfileAbility::ParseUriType##path = %{public}s\n", path.c_str());
240             std::map<std::string, PdpProfileUriType>::iterator it = pdpProfileUriMap.find(path);
241             if (it != pdpProfileUriMap.end()) {
242                 pdpProfileUriType = it->second;
243                 DATA_STORAGE_LOGI("PdpProfileAbility::ParseUriType##pdpProfileUriType = %{public}d\n",
244                     pdpProfileUriType);
245             }
246         }
247     }
248     return pdpProfileUriType;
249 }
250 
ConvertPredicates(const NativeRdb::DataAbilityPredicates & dataPredicates,NativeRdb::AbsRdbPredicates * absRdbPredicates)251 void PdpProfileAbility::ConvertPredicates(
252     const NativeRdb::DataAbilityPredicates &dataPredicates, NativeRdb::AbsRdbPredicates *absRdbPredicates)
253 {
254     NativeRdb::PredicatesUtils::SetWhereClauseAndArgs(
255         absRdbPredicates, dataPredicates.GetWhereClause(), dataPredicates.GetWhereArgs());
256     NativeRdb::PredicatesUtils::SetAttributes(absRdbPredicates, dataPredicates.IsDistinct(),
257         dataPredicates.GetIndex(), dataPredicates.GetGroup(), dataPredicates.GetOrder(), dataPredicates.GetLimit(),
258         dataPredicates.GetOffset());
259 }
260 
261 REGISTER_AA(PdpProfileAbility);
262 }  // namespace Telephony
263 }  // namespace OHOS
264