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