1 /*
2  * Copyright (c) 2021-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_storage_errors.h"
23 #include "data_storage_log_wrapper.h"
24 #include "datashare_ext_ability.h"
25 #include "datashare_predicates.h"
26 #include "new"
27 #include "opkey_data.h"
28 #include "permission_util.h"
29 #include "rdb_errno.h"
30 #include "rdb_utils.h"
31 #include "telephony_datashare_stub_impl.h"
32 #include "uri.h"
33 #include "utility"
34 
35 namespace OHOS {
36 using AppExecFwk::AbilityLoader;
37 using AppExecFwk::Ability;
38 namespace Telephony {
39 const int32_t CHANGED_ROWS = 0;
40 static const std::map<std::string, OpKeyUriType> opKeyUriMap_ = {
41     { "/opkey/opkey_info", OpKeyUriType::OPKEY_INFO },
42     {"/opkey/opkey_init", OpKeyUriType::OPKEY_INIT},
43 };
44 
OpKeyAbility()45 OpKeyAbility::OpKeyAbility() : DataShareExtAbility() {}
46 
~OpKeyAbility()47 OpKeyAbility::~OpKeyAbility() {}
48 
Create()49 OpKeyAbility* OpKeyAbility::Create()
50 {
51     DATA_STORAGE_LOGI("OpKeyAbility::Create begin.");
52     auto self =  new OpKeyAbility();
53     self->DoInit();
54     return self;
55 }
56 
DoInit()57 void OpKeyAbility::DoInit()
58 {
59     if (initDatabaseDir_ && initRdbStore_) {
60         DATA_STORAGE_LOGI("DoInit has done");
61         return;
62     }
63     auto abilityContext = AbilityRuntime::Context::GetApplicationContext();
64     if (abilityContext == nullptr) {
65         DATA_STORAGE_LOGE("DoInit GetAbilityContext is null");
66         return;
67     }
68     // switch database dir to el1 for init before unlock
69     abilityContext->SwitchArea(0);
70     std::string path = abilityContext->GetDatabaseDir();
71     if (!path.empty()) {
72         initDatabaseDir_ = true;
73         path.append("/");
74         helper_.UpdateDbPath(path);
75         int rdbInitCode = helper_.Init();
76         if (rdbInitCode == NativeRdb::E_OK) {
77             initRdbStore_ = true;
78         } else {
79             DATA_STORAGE_LOGE("DoInit rdb init failed!");
80             initRdbStore_ = false;
81         }
82     } else {
83         DATA_STORAGE_LOGE("DoInit##databaseDir is empty!");
84         initDatabaseDir_ = false;
85     }
86 }
87 
OnConnect(const AAFwk::Want & want)88 sptr<IRemoteObject> OpKeyAbility::OnConnect(const AAFwk::Want &want)
89 {
90     DATA_STORAGE_LOGI("OpKeyAbility::OnConnect begin.");
91     Extension::OnConnect(want);
92     sptr<DataShare::TelephonyDataShareStubImpl> remoteObject =
93         new (std::nothrow) DataShare::TelephonyDataShareStubImpl();
94     if (remoteObject == nullptr) {
95         DATA_STORAGE_LOGE("%{public}s No memory allocated for DataShareStubImpl", __func__);
96         return nullptr;
97     }
98     remoteObject->SetOpKeyAbility(std::static_pointer_cast<OpKeyAbility>(shared_from_this()));
99     DATA_STORAGE_LOGI("OpKeyAbility %{public}s end.", __func__);
100     return remoteObject->AsObject();
101 }
102 
OnStart(const AppExecFwk::Want & want)103 void OpKeyAbility::OnStart(const AppExecFwk::Want &want)
104 {
105     DATA_STORAGE_LOGI("OpKeyAbility::OnStart");
106     Extension::OnStart(want);
107     DoInit();
108 }
109 
BatchInsert(const Uri & uri,const std::vector<DataShare::DataShareValuesBucket> & values)110 int OpKeyAbility::BatchInsert(const Uri &uri, const std::vector<DataShare::DataShareValuesBucket> &values)
111 {
112     if (!PermissionUtil::CheckPermission(Permission::SET_TELEPHONY_STATE)) {
113         DATA_STORAGE_LOGE("Permission denied!");
114         return DATA_STORAGE_ERR_PERMISSION_ERR;
115     }
116     if (!IsInitOk()) {
117         return DATA_STORAGE_ERROR;
118     }
119     std::lock_guard<std::mutex> guard(lock_);
120     Uri tempUri = uri;
121     OpKeyUriType opKeyUriType = ParseUriType(tempUri);
122     if (opKeyUriType == OpKeyUriType::OPKEY_INIT) {
123         return helper_.InitOpKeyDatabase();
124     }
125     return DATA_STORAGE_ERROR;
126 }
127 
Insert(const Uri & uri,const DataShare::DataShareValuesBucket & value)128 int OpKeyAbility::Insert(const Uri &uri, const DataShare::DataShareValuesBucket &value)
129 {
130     if (!PermissionUtil::CheckPermission(Permission::SET_TELEPHONY_STATE)) {
131         DATA_STORAGE_LOGE("Permission denied!");
132         return DATA_STORAGE_ERR_PERMISSION_ERR;
133     }
134     if (!IsInitOk()) {
135         return DATA_STORAGE_ERROR;
136     }
137     std::lock_guard<std::mutex> guard(lock_);
138     Uri tempUri = uri;
139     OpKeyUriType opKeyUriType = ParseUriType(tempUri);
140     int64_t id = DATA_STORAGE_ERROR;
141     if (opKeyUriType == OpKeyUriType::OPKEY_INFO) {
142         OHOS::NativeRdb::ValuesBucket values = RdbDataShareAdapter::RdbUtils::ToValuesBucket(value);
143         helper_.Insert(id, values, TABLE_OPKEY_INFO);
144     } else {
145         DATA_STORAGE_LOGE("OpKeyAbility::Insert failed##uri = %{public}s", uri.ToString().c_str());
146     }
147     return id;
148 }
149 
Query(const Uri & uri,const DataShare::DataSharePredicates & predicates,std::vector<std::string> & columns,DataShare::DatashareBusinessError & businessError)150 std::shared_ptr<DataShare::DataShareResultSet> OpKeyAbility::Query(const Uri &uri,
151     const DataShare::DataSharePredicates &predicates, std::vector<std::string> &columns,
152     DataShare::DatashareBusinessError &businessError)
153 {
154     if (!PermissionUtil::CheckPermission(Permission::GET_TELEPHONY_STATE)) {
155         DATA_STORAGE_LOGE("Permission denied!");
156         return nullptr;
157     }
158     std::shared_ptr<DataShare::DataShareResultSet> sharedPtrResult = nullptr;
159     if (!IsInitOk()) {
160         return nullptr;
161     }
162     Uri tempUri = uri;
163     OpKeyUriType opKeyUriType = ParseUriType(tempUri);
164     if (opKeyUriType == OpKeyUriType::OPKEY_INFO) {
165         NativeRdb::AbsRdbPredicates *absRdbPredicates = new NativeRdb::AbsRdbPredicates(TABLE_OPKEY_INFO);
166         if (absRdbPredicates != nullptr) {
167             NativeRdb::RdbPredicates rdbPredicates = ConvertPredicates(absRdbPredicates->GetTableName(), predicates);
168             auto resultSet = helper_.Query(rdbPredicates, columns);
169             if (resultSet == nullptr) {
170                 DATA_STORAGE_LOGE("OpKeyAbility::Query  NativeRdb::ResultSet is null!");
171                 delete absRdbPredicates;
172                 absRdbPredicates = nullptr;
173                 return nullptr;
174             }
175             auto queryResultSet = RdbDataShareAdapter::RdbUtils::ToResultSetBridge(resultSet);
176             sharedPtrResult = std::make_shared<DataShare::DataShareResultSet>(queryResultSet);
177             delete absRdbPredicates;
178             absRdbPredicates = nullptr;
179         } else {
180             DATA_STORAGE_LOGE("OpKeyAbility::Query  NativeRdb::AbsRdbPredicates is null!");
181         }
182     } else {
183         DATA_STORAGE_LOGE("OpKeyAbility::Query failed##uri = %{public}s", uri.ToString().c_str());
184     }
185     return sharedPtrResult;
186 }
187 
Update(const Uri & uri,const DataShare::DataSharePredicates & predicates,const DataShare::DataShareValuesBucket & value)188 int OpKeyAbility::Update(
189     const Uri &uri, const DataShare::DataSharePredicates &predicates,
190     const DataShare::DataShareValuesBucket &value)
191 {
192     if (!PermissionUtil::CheckPermission(Permission::SET_TELEPHONY_STATE)) {
193         DATA_STORAGE_LOGE("Permission denied!");
194         return DATA_STORAGE_ERR_PERMISSION_ERR;
195     }
196     int result = DATA_STORAGE_ERROR;
197     if (!IsInitOk()) {
198         return result;
199     }
200     std::lock_guard<std::mutex> guard(lock_);
201     Uri tempUri = uri;
202     OpKeyUriType opKeyUriType = ParseUriType(tempUri);
203     NativeRdb::AbsRdbPredicates *absRdbPredicates = nullptr;
204     switch (opKeyUriType) {
205         case OpKeyUriType::OPKEY_INFO: {
206             absRdbPredicates = new NativeRdb::AbsRdbPredicates(TABLE_OPKEY_INFO);
207             break;
208         }
209         default:
210             DATA_STORAGE_LOGE("OpKeyAbility::Update failed##uri = %{public}s", uri.ToString().c_str());
211             break;
212     }
213     if (absRdbPredicates != nullptr) {
214         int changedRows = CHANGED_ROWS;
215         NativeRdb::RdbPredicates rdbPredicates = ConvertPredicates(absRdbPredicates->GetTableName(), predicates);
216         OHOS::NativeRdb::ValuesBucket values = RdbDataShareAdapter::RdbUtils::ToValuesBucket(value);
217         result = helper_.Update(changedRows, values, rdbPredicates);
218         delete absRdbPredicates;
219         absRdbPredicates = nullptr;
220     } else if (result == DATA_STORAGE_ERROR) {
221         DATA_STORAGE_LOGE("OpKeyAbility::Update  NativeRdb::AbsRdbPredicates is null!");
222     }
223     return result;
224 }
225 
Delete(const Uri & uri,const DataShare::DataSharePredicates & predicates)226 int OpKeyAbility::Delete(const Uri &uri, const DataShare::DataSharePredicates &predicates)
227 {
228     if (!PermissionUtil::CheckPermission(Permission::SET_TELEPHONY_STATE)) {
229         DATA_STORAGE_LOGE("Permission denied!");
230         return DATA_STORAGE_ERR_PERMISSION_ERR;
231     }
232     int result = DATA_STORAGE_ERROR;
233     if (!IsInitOk()) {
234         return result;
235     }
236     std::lock_guard<std::mutex> guard(lock_);
237     Uri tempUri = uri;
238     OpKeyUriType opKeyUriType = ParseUriType(tempUri);
239     if (opKeyUriType == OpKeyUriType::OPKEY_INFO) {
240         NativeRdb::AbsRdbPredicates *absRdbPredicates = new NativeRdb::AbsRdbPredicates(TABLE_OPKEY_INFO);
241         if (absRdbPredicates != nullptr) {
242             NativeRdb::RdbPredicates rdbPredicates = ConvertPredicates(absRdbPredicates->GetTableName(), predicates);
243             int deletedRows = CHANGED_ROWS;
244             result = helper_.Delete(deletedRows, rdbPredicates);
245             delete absRdbPredicates;
246             absRdbPredicates = nullptr;
247         } else {
248             DATA_STORAGE_LOGE("OpKeyAbility::Delete  NativeRdb::AbsRdbPredicates is null!");
249         }
250     } else {
251         DATA_STORAGE_LOGE("OpKeyAbility::Delete failed##uri = %{public}s", uri.ToString().c_str());
252     }
253     return result;
254 }
255 
IsInitOk()256 bool OpKeyAbility::IsInitOk()
257 {
258     if (!initDatabaseDir_) {
259         DATA_STORAGE_LOGE("OpKeyAbility::IsInitOk initDatabaseDir_ failed!");
260         return false;
261     }
262     if (!initRdbStore_) {
263         DATA_STORAGE_LOGE("OpKeyAbility::IsInitOk initRdbStore_ failed!");
264         return false;
265     }
266     return true;
267 }
268 
GetType(const Uri & uri)269 std::string OpKeyAbility::GetType(const Uri &uri)
270 {
271     DATA_STORAGE_LOGI("OpKeyAbility::GetType##uri = %{public}s", uri.ToString().c_str());
272     std::string retval(uri.ToString());
273     return retval;
274 }
275 
OpenFile(const Uri & uri,const std::string & mode)276 int OpKeyAbility::OpenFile(const Uri &uri, const std::string &mode)
277 {
278     DATA_STORAGE_LOGI("OpKeyAbility::OpenFile##uri = %{public}s", uri.ToString().c_str());
279     Uri tempUri = uri;
280     OpKeyUriType opKeyUriType = ParseUriType(tempUri);
281     return static_cast<int>(opKeyUriType);
282 }
283 
ParseUriType(Uri & uri)284 OpKeyUriType OpKeyAbility::ParseUriType(Uri &uri)
285 {
286     DATA_STORAGE_LOGI("OpKeyAbility::ParseUriType start");
287     OpKeyUriType opKeyUriType = OpKeyUriType::UNKNOW;
288     std::string uriPath = uri.ToString();
289     if (!uriPath.empty()) {
290         helper_.ReplaceAllStr(uriPath, ":///", "://");
291         Uri tempUri(uriPath);
292         std::string path = tempUri.GetPath();
293         if (!path.empty() && !opKeyUriMap_.empty()) {
294             DATA_STORAGE_LOGI("OpKeyAbility::ParseUriType##path = %{public}s", path.c_str());
295             auto it = opKeyUriMap_.find(path);
296             if (it != opKeyUriMap_.end()) {
297                 opKeyUriType = it->second;
298                 DATA_STORAGE_LOGI("OpKeyAbility::ParseUriType##opKeyUriType = %{public}d",
299                     opKeyUriType);
300             }
301         }
302     }
303     return opKeyUriType;
304 }
305 
ConvertPredicates(const std::string & tableName,const DataShare::DataSharePredicates & predicates)306 OHOS::NativeRdb::RdbPredicates OpKeyAbility::ConvertPredicates(
307     const std::string &tableName, const DataShare::DataSharePredicates &predicates)
308 {
309     OHOS::NativeRdb::RdbPredicates res = RdbDataShareAdapter::RdbUtils::ToPredicates(predicates, tableName);
310     return res;
311 }
312 } // namespace Telephony
313 } // namespace OHOS
314