• 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 (!IsInitOk()) {
72         return DATA_STORAGE_ERROR;
73     }
74     std::lock_guard<std::mutex> guard(lock_);
75     Uri tempUri = uri;
76     PdpProfileUriType pdpProfileUriType = ParseUriType(tempUri);
77     int64_t id = DATA_STORAGE_ERROR;
78     if (pdpProfileUriType == PdpProfileUriType::PDP_PROFILE) {
79         helper_.Insert(id, value, TABLE_PDP_PROFILE);
80     } else {
81         DATA_STORAGE_LOGE("PdpProfileAbility::Insert##uri = %{public}s", uri.ToString().c_str());
82     }
83     return id;
84 }
85 
Query(const Uri & uri,const std::vector<std::string> & columns,const NativeRdb::DataAbilityPredicates & predicates)86 std::shared_ptr<NativeRdb::AbsSharedResultSet> PdpProfileAbility::Query(
87     const Uri &uri, const std::vector<std::string> &columns, const NativeRdb::DataAbilityPredicates &predicates)
88 {
89     std::shared_ptr<NativeRdb::AbsSharedResultSet> resultSet = nullptr;
90     if (!IsInitOk()) {
91         return resultSet;
92     }
93     Uri tempUri = uri;
94     PdpProfileUriType pdpProfileUriType = ParseUriType(tempUri);
95     if (pdpProfileUriType == PdpProfileUriType::PDP_PROFILE) {
96         NativeRdb::AbsRdbPredicates *absRdbPredicates = new NativeRdb::AbsRdbPredicates(TABLE_PDP_PROFILE);
97         if (absRdbPredicates != nullptr) {
98             ConvertPredicates(predicates, absRdbPredicates);
99             resultSet = helper_.Query(*absRdbPredicates, columns);
100             delete absRdbPredicates;
101             absRdbPredicates = nullptr;
102         } else {
103             DATA_STORAGE_LOGE("PdpProfileAbility::Delete  NativeRdb::AbsRdbPredicates is null!");
104         }
105     } else {
106         DATA_STORAGE_LOGE("PdpProfileAbility::Query##uri = %{public}s", uri.ToString().c_str());
107     }
108     return resultSet;
109 }
110 
Update(const Uri & uri,const NativeRdb::ValuesBucket & value,const NativeRdb::DataAbilityPredicates & predicates)111 int PdpProfileAbility::Update(
112     const Uri &uri, const NativeRdb::ValuesBucket &value, const NativeRdb::DataAbilityPredicates &predicates)
113 {
114     int result = DATA_STORAGE_ERROR;
115     if (!IsInitOk()) {
116         return result;
117     }
118     std::lock_guard<std::mutex> guard(lock_);
119     Uri tempUri = uri;
120     PdpProfileUriType pdpProfileUriType = ParseUriType(tempUri);
121     NativeRdb::AbsRdbPredicates *absRdbPredicates = nullptr;
122     switch (pdpProfileUriType) {
123         case PdpProfileUriType::PDP_PROFILE: {
124             absRdbPredicates = new NativeRdb::AbsRdbPredicates(TABLE_PDP_PROFILE);
125             break;
126         }
127         case PdpProfileUriType::RESET: {
128             result = helper_.ResetApn();
129             if (result != NativeRdb::E_OK) {
130                 DATA_STORAGE_LOGE("PdpProfileAbility::Update  ResetApn fail!");
131                 result = static_cast<int>(LoadProFileErrorType::RESET_APN_FAIL);
132             }
133             break;
134         }
135         default:
136             DATA_STORAGE_LOGE("PdpProfileAbility::Update##uri = %{public}s", uri.ToString().c_str());
137             break;
138     }
139     if (absRdbPredicates != nullptr) {
140         int changedRows = 0;
141         ConvertPredicates(predicates, absRdbPredicates);
142         result = helper_.Update(changedRows, value, *absRdbPredicates);
143         delete absRdbPredicates;
144         absRdbPredicates = nullptr;
145     } else if (result == DATA_STORAGE_ERROR) {
146         DATA_STORAGE_LOGE("PdpProfileAbility::Update  NativeRdb::AbsRdbPredicates is null!");
147     }
148     return result;
149 }
150 
Delete(const Uri & uri,const NativeRdb::DataAbilityPredicates & predicates)151 int PdpProfileAbility::Delete(const Uri &uri, const NativeRdb::DataAbilityPredicates &predicates)
152 {
153     int result = DATA_STORAGE_ERROR;
154     if (!IsInitOk()) {
155         return result;
156     }
157     std::lock_guard<std::mutex> guard(lock_);
158     Uri tempUri = uri;
159     PdpProfileUriType pdpProfileUriType = ParseUriType(tempUri);
160     if (pdpProfileUriType == PdpProfileUriType::PDP_PROFILE) {
161         NativeRdb::AbsRdbPredicates *absRdbPredicates = new NativeRdb::AbsRdbPredicates(TABLE_PDP_PROFILE);
162         if (absRdbPredicates != nullptr) {
163             ConvertPredicates(predicates, absRdbPredicates);
164             int deletedRows = 0;
165             result = helper_.Delete(deletedRows, *absRdbPredicates);
166             delete absRdbPredicates;
167             absRdbPredicates = nullptr;
168         } else {
169             DATA_STORAGE_LOGE("PdpProfileAbility::Delete  NativeRdb::AbsRdbPredicates is null!");
170         }
171     } else {
172         DATA_STORAGE_LOGI("PdpProfileAbility::Delete##uri = %{public}s\n", uri.ToString().c_str());
173     }
174     return result;
175 }
176 
IsInitOk()177 bool PdpProfileAbility::IsInitOk()
178 {
179     if (!initDatabaseDir) {
180         DATA_STORAGE_LOGE("PdpProfileAbility::IsInitOk initDatabaseDir failed!");
181         return false;
182     }
183     if (!initRdbStore) {
184         DATA_STORAGE_LOGE("PdpProfileAbility::IsInitOk initRdbStore failed!");
185         return false;
186     }
187     return true;
188 }
189 
InitUriMap()190 void PdpProfileAbility::InitUriMap()
191 {
192     pdpProfileUriMap = {
193         {"/net/pdp_profile", PdpProfileUriType::PDP_PROFILE},
194         {"/net/pdp_profile/reset", PdpProfileUriType::RESET}
195     };
196 }
197 
GetType(const Uri & uri)198 std::string PdpProfileAbility::GetType(const Uri &uri)
199 {
200     DATA_STORAGE_LOGI("PdpProfileAbility::GetType##uri = %{public}s\n", uri.ToString().c_str());
201     std::string retval(uri.ToString());
202     return retval;
203 }
204 
OpenFile(const Uri & uri,const std::string & mode)205 int PdpProfileAbility::OpenFile(const Uri &uri, const std::string &mode)
206 {
207     DATA_STORAGE_LOGI("PdpProfileAbility::OpenFile##uri = %{public}s\n", uri.ToString().c_str());
208     Uri tempUri = uri;
209     PdpProfileUriType pdpProfileUriType = ParseUriType(tempUri);
210     return static_cast<int>(pdpProfileUriType);
211 }
212 
ParseUriType(Uri & uri)213 PdpProfileUriType PdpProfileAbility::ParseUriType(Uri &uri)
214 {
215     DATA_STORAGE_LOGI("PdpProfileAbility::ParseUriType start\n");
216     PdpProfileUriType pdpProfileUriType = PdpProfileUriType::UNKNOW;
217     std::string uriPath = uri.ToString();
218     if (!uriPath.empty()) {
219         helper_.ReplaceAllStr(uriPath, ":///", "://");
220         Uri tempUri(uriPath);
221         std::string path = tempUri.GetPath();
222         if (!path.empty()) {
223             DATA_STORAGE_LOGI("PdpProfileAbility::ParseUriType##path = %{public}s\n", path.c_str());
224             std::map<std::string, PdpProfileUriType>::iterator it = pdpProfileUriMap.find(path);
225             if (it != pdpProfileUriMap.end()) {
226                 pdpProfileUriType = it->second;
227                 DATA_STORAGE_LOGI("PdpProfileAbility::ParseUriType##pdpProfileUriType = %{public}d\n",
228                     pdpProfileUriType);
229             }
230         }
231     }
232     return pdpProfileUriType;
233 }
234 
ConvertPredicates(const NativeRdb::DataAbilityPredicates & dataPredicates,NativeRdb::AbsRdbPredicates * absRdbPredicates)235 void PdpProfileAbility::ConvertPredicates(
236     const NativeRdb::DataAbilityPredicates &dataPredicates, NativeRdb::AbsRdbPredicates *absRdbPredicates)
237 {
238     NativeRdb::PredicatesUtils::SetWhereClauseAndArgs(
239         absRdbPredicates, dataPredicates.GetWhereClause(), dataPredicates.GetWhereArgs());
240     NativeRdb::PredicatesUtils::SetAttributes(absRdbPredicates, dataPredicates.IsDistinct(),
241         dataPredicates.GetIndex(), dataPredicates.GetGroup(), dataPredicates.GetOrder(), dataPredicates.GetLimit(),
242         dataPredicates.GetOffset());
243 }
244 
245 REGISTER_AA(PdpProfileAbility);
246 }  // namespace Telephony
247 }  // namespace OHOS
248