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