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 };
43
OpKeyAbility()44 OpKeyAbility::OpKeyAbility() : DataShareExtAbility() {}
45
~OpKeyAbility()46 OpKeyAbility::~OpKeyAbility() {}
47
Create()48 OpKeyAbility* OpKeyAbility::Create()
49 {
50 DATA_STORAGE_LOGI("OpKeyAbility::Create begin.");
51 auto self = new OpKeyAbility();
52 self->DoInit();
53 return self;
54 }
55
DoInit()56 void OpKeyAbility::DoInit()
57 {
58 if (initDatabaseDir_ && initRdbStore_) {
59 DATA_STORAGE_LOGI("DoInit has done");
60 return;
61 }
62 auto abilityContext = AbilityRuntime::Context::GetApplicationContext();
63 if (abilityContext == nullptr) {
64 DATA_STORAGE_LOGE("DoInit GetAbilityContext is null");
65 return;
66 }
67 // switch database dir to el1 for init before unlock
68 abilityContext->SwitchArea(0);
69 std::string path = abilityContext->GetDatabaseDir();
70 DATA_STORAGE_LOGI("GetDatabaseDir: %{public}s", path.c_str());
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
Insert(const Uri & uri,const DataShare::DataShareValuesBucket & value)110 int OpKeyAbility::Insert(const Uri &uri, const DataShare::DataShareValuesBucket &value)
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 int64_t id = DATA_STORAGE_ERROR;
123 if (opKeyUriType == OpKeyUriType::OPKEY_INFO) {
124 OHOS::NativeRdb::ValuesBucket values = RdbDataShareAdapter::RdbUtils::ToValuesBucket(value);
125 helper_.Insert(id, values, TABLE_OPKEY_INFO);
126 } else {
127 DATA_STORAGE_LOGE("OpKeyAbility::Insert failed##uri = %{public}s", uri.ToString().c_str());
128 }
129 return id;
130 }
131
Query(const Uri & uri,const DataShare::DataSharePredicates & predicates,std::vector<std::string> & columns,DataShare::DatashareBusinessError & businessError)132 std::shared_ptr<DataShare::DataShareResultSet> OpKeyAbility::Query(const Uri &uri,
133 const DataShare::DataSharePredicates &predicates, std::vector<std::string> &columns,
134 DataShare::DatashareBusinessError &businessError)
135 {
136 if (!PermissionUtil::CheckPermission(Permission::GET_TELEPHONY_STATE)) {
137 DATA_STORAGE_LOGE("Permission denied!");
138 return nullptr;
139 }
140 std::shared_ptr<DataShare::DataShareResultSet> sharedPtrResult = nullptr;
141 if (!IsInitOk()) {
142 return nullptr;
143 }
144 Uri tempUri = uri;
145 OpKeyUriType opKeyUriType = ParseUriType(tempUri);
146 if (opKeyUriType == OpKeyUriType::OPKEY_INFO) {
147 NativeRdb::AbsRdbPredicates *absRdbPredicates = new NativeRdb::AbsRdbPredicates(TABLE_OPKEY_INFO);
148 if (absRdbPredicates != nullptr) {
149 NativeRdb::RdbPredicates rdbPredicates = ConvertPredicates(absRdbPredicates->GetTableName(), predicates);
150 auto resultSet = helper_.Query(rdbPredicates, columns);
151 if (resultSet == nullptr) {
152 DATA_STORAGE_LOGE("OpKeyAbility::Query NativeRdb::ResultSet is null!");
153 delete absRdbPredicates;
154 absRdbPredicates = nullptr;
155 return nullptr;
156 }
157 auto queryResultSet = RdbDataShareAdapter::RdbUtils::ToResultSetBridge(resultSet);
158 sharedPtrResult = std::make_shared<DataShare::DataShareResultSet>(queryResultSet);
159 delete absRdbPredicates;
160 absRdbPredicates = nullptr;
161 } else {
162 DATA_STORAGE_LOGE("OpKeyAbility::Query NativeRdb::AbsRdbPredicates is null!");
163 }
164 } else {
165 DATA_STORAGE_LOGE("OpKeyAbility::Query failed##uri = %{public}s", uri.ToString().c_str());
166 }
167 return sharedPtrResult;
168 }
169
Update(const Uri & uri,const DataShare::DataSharePredicates & predicates,const DataShare::DataShareValuesBucket & value)170 int OpKeyAbility::Update(
171 const Uri &uri, const DataShare::DataSharePredicates &predicates,
172 const DataShare::DataShareValuesBucket &value)
173 {
174 if (!PermissionUtil::CheckPermission(Permission::SET_TELEPHONY_STATE)) {
175 DATA_STORAGE_LOGE("Permission denied!");
176 return DATA_STORAGE_ERR_PERMISSION_ERR;
177 }
178 int result = DATA_STORAGE_ERROR;
179 if (!IsInitOk()) {
180 return result;
181 }
182 std::lock_guard<std::mutex> guard(lock_);
183 Uri tempUri = uri;
184 OpKeyUriType opKeyUriType = ParseUriType(tempUri);
185 NativeRdb::AbsRdbPredicates *absRdbPredicates = nullptr;
186 switch (opKeyUriType) {
187 case OpKeyUriType::OPKEY_INFO: {
188 absRdbPredicates = new NativeRdb::AbsRdbPredicates(TABLE_OPKEY_INFO);
189 break;
190 }
191 default:
192 DATA_STORAGE_LOGE("OpKeyAbility::Update failed##uri = %{public}s", uri.ToString().c_str());
193 break;
194 }
195 if (absRdbPredicates != nullptr) {
196 int changedRows = CHANGED_ROWS;
197 NativeRdb::RdbPredicates rdbPredicates = ConvertPredicates(absRdbPredicates->GetTableName(), predicates);
198 OHOS::NativeRdb::ValuesBucket values = RdbDataShareAdapter::RdbUtils::ToValuesBucket(value);
199 result = helper_.Update(changedRows, values, rdbPredicates);
200 delete absRdbPredicates;
201 absRdbPredicates = nullptr;
202 } else if (result == DATA_STORAGE_ERROR) {
203 DATA_STORAGE_LOGE("OpKeyAbility::Update NativeRdb::AbsRdbPredicates is null!");
204 }
205 return result;
206 }
207
Delete(const Uri & uri,const DataShare::DataSharePredicates & predicates)208 int OpKeyAbility::Delete(const Uri &uri, const DataShare::DataSharePredicates &predicates)
209 {
210 if (!PermissionUtil::CheckPermission(Permission::SET_TELEPHONY_STATE)) {
211 DATA_STORAGE_LOGE("Permission denied!");
212 return DATA_STORAGE_ERR_PERMISSION_ERR;
213 }
214 int result = DATA_STORAGE_ERROR;
215 if (!IsInitOk()) {
216 return result;
217 }
218 std::lock_guard<std::mutex> guard(lock_);
219 Uri tempUri = uri;
220 OpKeyUriType opKeyUriType = ParseUriType(tempUri);
221 if (opKeyUriType == OpKeyUriType::OPKEY_INFO) {
222 NativeRdb::AbsRdbPredicates *absRdbPredicates = new NativeRdb::AbsRdbPredicates(TABLE_OPKEY_INFO);
223 if (absRdbPredicates != nullptr) {
224 NativeRdb::RdbPredicates rdbPredicates = ConvertPredicates(absRdbPredicates->GetTableName(), predicates);
225 int deletedRows = CHANGED_ROWS;
226 result = helper_.Delete(deletedRows, rdbPredicates);
227 delete absRdbPredicates;
228 absRdbPredicates = nullptr;
229 } else {
230 DATA_STORAGE_LOGE("OpKeyAbility::Delete NativeRdb::AbsRdbPredicates is null!");
231 }
232 } else {
233 DATA_STORAGE_LOGE("OpKeyAbility::Delete failed##uri = %{public}s", uri.ToString().c_str());
234 }
235 return result;
236 }
237
IsInitOk()238 bool OpKeyAbility::IsInitOk()
239 {
240 if (!initDatabaseDir_) {
241 DATA_STORAGE_LOGE("OpKeyAbility::IsInitOk initDatabaseDir_ failed!");
242 return false;
243 }
244 if (!initRdbStore_) {
245 DATA_STORAGE_LOGE("OpKeyAbility::IsInitOk initRdbStore_ failed!");
246 return false;
247 }
248 return true;
249 }
250
GetType(const Uri & uri)251 std::string OpKeyAbility::GetType(const Uri &uri)
252 {
253 DATA_STORAGE_LOGI("OpKeyAbility::GetType##uri = %{public}s", uri.ToString().c_str());
254 std::string retval(uri.ToString());
255 return retval;
256 }
257
OpenFile(const Uri & uri,const std::string & mode)258 int OpKeyAbility::OpenFile(const Uri &uri, const std::string &mode)
259 {
260 DATA_STORAGE_LOGI("OpKeyAbility::OpenFile##uri = %{public}s", uri.ToString().c_str());
261 Uri tempUri = uri;
262 OpKeyUriType opKeyUriType = ParseUriType(tempUri);
263 return static_cast<int>(opKeyUriType);
264 }
265
ParseUriType(Uri & uri)266 OpKeyUriType OpKeyAbility::ParseUriType(Uri &uri)
267 {
268 DATA_STORAGE_LOGI("OpKeyAbility::ParseUriType start");
269 OpKeyUriType opKeyUriType = OpKeyUriType::UNKNOW;
270 std::string uriPath = uri.ToString();
271 if (!uriPath.empty()) {
272 helper_.ReplaceAllStr(uriPath, ":///", "://");
273 Uri tempUri(uriPath);
274 std::string path = tempUri.GetPath();
275 if (!path.empty() && !opKeyUriMap_.empty()) {
276 DATA_STORAGE_LOGI("OpKeyAbility::ParseUriType##path = %{public}s", path.c_str());
277 auto it = opKeyUriMap_.find(path);
278 if (it != opKeyUriMap_.end()) {
279 opKeyUriType = it->second;
280 DATA_STORAGE_LOGI("OpKeyAbility::ParseUriType##opKeyUriType = %{public}d",
281 opKeyUriType);
282 }
283 }
284 }
285 return opKeyUriType;
286 }
287
ConvertPredicates(const std::string & tableName,const DataShare::DataSharePredicates & predicates)288 OHOS::NativeRdb::RdbPredicates OpKeyAbility::ConvertPredicates(
289 const std::string &tableName, const DataShare::DataSharePredicates &predicates)
290 {
291 OHOS::NativeRdb::RdbPredicates res = RdbDataShareAdapter::RdbUtils::ToPredicates(predicates, tableName);
292 return res;
293 }
294 } // namespace Telephony
295 } // namespace OHOS
296