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 #define LOG_TAG "UdmfServiceImpl"
17
18 #include "udmf_service_impl.h"
19
20 #include "iservice_registry.h"
21 #include "ipc_skeleton.h"
22 #include "tokenid_kit.h"
23
24 #include "accesstoken_kit.h"
25 #include "checker_manager.h"
26 #include "dfx_types.h"
27 #include "distributed_kv_data_manager.h"
28 #include "file.h"
29 #include "lifecycle/lifecycle_manager.h"
30 #include "log_print.h"
31 #include "preprocess_utils.h"
32 #include "reporter.h"
33 #include "uri_permission_manager.h"
34 #include "uri.h"
35 #include "utd/custom_utd_installer.h"
36 #include "udmf_conversion.h"
37 #include "udmf_radar_reporter.h"
38
39 namespace OHOS {
40 namespace UDMF {
41 using namespace Security::AccessToken;
42 using FeatureSystem = DistributedData::FeatureSystem;
43 using UdmfBehaviourMsg = OHOS::DistributedDataDfx::UdmfBehaviourMsg;
44 using Reporter = OHOS::DistributedDataDfx::Reporter;
45 using namespace RadarReporter;
46 constexpr const char *DRAG_AUTHORIZED_PROCESSES[] = {"msdp_sa", "collaboration_service"};
47 constexpr const char *DATA_PREFIX = "udmf://";
48 constexpr const char *FILE_SCHEME = "file";
49 constexpr const char *PRIVILEGE_READ_AND_KEEP = "readAndKeep";
50 __attribute__((used)) UdmfServiceImpl::Factory UdmfServiceImpl::factory_;
Factory()51 UdmfServiceImpl::Factory::Factory()
52 {
53 ZLOGI("Register udmf creator!");
54 FeatureSystem::GetInstance().RegisterCreator("udmf", [this]() {
55 if (product_ == nullptr) {
56 product_ = std::make_shared<UdmfServiceImpl>();
57 }
58 return product_;
59 }, FeatureSystem::BIND_NOW);
60 staticActs_ = std::make_shared<UdmfStatic>();
61 FeatureSystem::GetInstance().RegisterStaticActs("udmf", staticActs_);
62 }
63
~Factory()64 UdmfServiceImpl::Factory::~Factory()
65 {
66 product_ = nullptr;
67 }
68
UdmfServiceImpl()69 UdmfServiceImpl::UdmfServiceImpl()
70 {
71 CheckerManager::GetInstance().LoadCheckers();
72 }
73
SetData(CustomOption & option,UnifiedData & unifiedData,std::string & key)74 int32_t UdmfServiceImpl::SetData(CustomOption &option, UnifiedData &unifiedData, std::string &key)
75 {
76 ZLOGD("start");
77 int32_t res = E_OK;
78 UdmfBehaviourMsg msg;
79 auto find = UD_INTENTION_MAP.find(option.intention);
80 msg.channel = find == UD_INTENTION_MAP.end() ? "invalid" : find->second;
81 msg.operation = "insert";
82 std::string bundleName;
83 if (!PreProcessUtils::GetHapBundleNameByToken(option.tokenId, bundleName)) {
84 msg.appId = "unknown";
85 res = E_ERROR;
86 } else {
87 msg.appId = bundleName;
88 res = SaveData(option, unifiedData, key);
89 }
90 auto errFind = ERROR_MAP.find(res);
91 msg.result = errFind == ERROR_MAP.end() ? "E_ERROR" : errFind->second;
92 msg.dataType = unifiedData.GetTypes();
93 msg.dataSize = unifiedData.GetSize();
94 Reporter::GetInstance()->BehaviourReporter()->UDMFReport(msg);
95 return res;
96 }
97
SaveData(CustomOption & option,UnifiedData & unifiedData,std::string & key)98 int32_t UdmfServiceImpl::SaveData(CustomOption &option, UnifiedData &unifiedData, std::string &key)
99 {
100 if (!unifiedData.IsValid()) {
101 ZLOGE("UnifiedData is invalid.");
102 return E_INVALID_PARAMETERS;
103 }
104
105 if (!UnifiedDataUtils::IsValidIntention(option.intention)) {
106 ZLOGE("Invalid parameters intention: %{public}d.", option.intention);
107 return E_INVALID_PARAMETERS;
108 }
109
110 // imput runtime info before put it into store and save one privilege
111 if (PreProcessUtils::RuntimeDataImputation(unifiedData, option) != E_OK) {
112 ZLOGE("Imputation failed");
113 return E_ERROR;
114 }
115
116 std::string intention = unifiedData.GetRuntime()->key.intention;
117 if (intention == UD_INTENTION_MAP.at(UD_INTENTION_DRAG)) {
118 int32_t ret = PreProcessUtils::SetRemoteUri(option.tokenId, unifiedData);
119 if (ret != E_OK) {
120 ZLOGE("SetRemoteUri failed, ret: %{public}d, bundleName:%{public}s.", ret,
121 unifiedData.GetRuntime()->createPackage.c_str());
122 return ret;
123 }
124 }
125
126 for (const auto &record : unifiedData.GetRecords()) {
127 record->SetUid(PreProcessUtils::GenerateId());
128 }
129
130 auto store = StoreCache::GetInstance().GetStore(intention);
131 if (store == nullptr) {
132 ZLOGE("Get store failed, intention: %{public}s.", intention.c_str());
133 return E_DB_ERROR;
134 }
135
136 if (!UnifiedDataUtils::IsPersist(intention) && store->Clear() != E_OK) {
137 ZLOGE("Clear store failed, intention: %{public}s.", intention.c_str());
138 return E_DB_ERROR;
139 }
140
141 UdmfConversion::InitValueObject(unifiedData);
142 if (store->Put(unifiedData) != E_OK) {
143 ZLOGE("Put unified data failed, intention: %{public}s.", intention.c_str());
144 return E_DB_ERROR;
145 }
146 key = unifiedData.GetRuntime()->key.GetUnifiedKey();
147 ZLOGD("Put unified data successful, key: %{public}s.", key.c_str());
148 return E_OK;
149 }
150
GetData(const QueryOption & query,UnifiedData & unifiedData)151 int32_t UdmfServiceImpl::GetData(const QueryOption &query, UnifiedData &unifiedData)
152 {
153 ZLOGD("start");
154 int32_t res = E_OK;
155 UdmfBehaviourMsg msg;
156 auto find = UD_INTENTION_MAP.find(query.intention);
157 msg.channel = find == UD_INTENTION_MAP.end() ? "invalid" : find->second;
158 msg.operation = "insert";
159 std::string bundleName;
160 if (!PreProcessUtils::GetHapBundleNameByToken(query.tokenId, bundleName)) {
161 msg.appId = "unknown";
162 res = E_ERROR;
163 } else {
164 msg.appId = bundleName;
165 res = RetrieveData(query, unifiedData);
166 }
167 auto errFind = ERROR_MAP.find(res);
168 msg.result = errFind == ERROR_MAP.end() ? "E_ERROR" : errFind->second;
169 msg.dataType = unifiedData.GetTypes();
170 msg.dataSize = unifiedData.GetSize();
171 Reporter::GetInstance()->BehaviourReporter()->UDMFReport(msg);
172 return res;
173 }
174
RetrieveData(const QueryOption & query,UnifiedData & unifiedData)175 int32_t UdmfServiceImpl::RetrieveData(const QueryOption &query, UnifiedData &unifiedData)
176 {
177 UnifiedKey key(query.key);
178 if (!key.IsValid()) {
179 ZLOGE("Unified key: %{public}s is invalid.", query.key.c_str());
180 return E_INVALID_PARAMETERS;
181 }
182 auto store = StoreCache::GetInstance().GetStore(key.intention);
183 if (store == nullptr) {
184 ZLOGE("Get store failed, intention: %{public}s.", key.intention.c_str());
185 return E_DB_ERROR;
186 }
187 int32_t res = store->Get(query.key, unifiedData);
188 if (res != E_OK) {
189 ZLOGE("Get data from store failed, res: %{public}d, key: %{public}s.", res, query.key.c_str());
190 return res;
191 }
192
193 if (!unifiedData.IsComplete()) {
194 ZLOGE("Get data from DB is incomplete, key: %{public}s.", query.key.c_str());
195 return E_NOT_FOUND;
196 }
197
198 CheckerManager::CheckInfo info;
199 info.tokenId = query.tokenId;
200 std::shared_ptr<Runtime> runtime = unifiedData.GetRuntime();
201 if (runtime == nullptr) {
202 return E_DB_ERROR;
203 }
204 if (!CheckerManager::GetInstance().IsValid(runtime->privileges, info) && !IsPermissionInCache(query)) {
205 RadarReporterAdapter::ReportFail(std::string(__FUNCTION__),
206 BizScene::GET_DATA, GetDataStage::VERIFY_PRIVILEGE, StageRes::FAILED, E_NO_PERMISSION);
207 return E_NO_PERMISSION;
208 }
209
210 if (key.intention == UD_INTENTION_MAP.at(UD_INTENTION_DRAG)) {
211 int32_t ret = ProcessUri(query, unifiedData);
212 if (ret != E_OK) {
213 ZLOGE("ProcessUri failed. ret=%{public}d", ret);
214 return E_NO_PERMISSION;
215 }
216 }
217 if (!IsReadAndKeep(runtime->privileges, query)) {
218 if (LifeCycleManager::GetInstance().OnGot(key) != E_OK) {
219 ZLOGE("Remove data failed, intention: %{public}s.", key.intention.c_str());
220 return E_DB_ERROR;
221 }
222 }
223
224 privilegeCache_.erase(query.key);
225
226 PreProcessUtils::SetRemoteData(unifiedData);
227 return E_OK;
228 }
229
IsPermissionInCache(const QueryOption & query)230 bool UdmfServiceImpl::IsPermissionInCache(const QueryOption &query)
231 {
232 auto iter = privilegeCache_.find(query.key);
233 if (iter != privilegeCache_.end() && iter->second.tokenId == query.tokenId) {
234 return true;
235 }
236 return false;
237 }
238
IsReadAndKeep(const std::vector<Privilege> & privileges,const QueryOption & query)239 bool UdmfServiceImpl::IsReadAndKeep(const std::vector<Privilege> &privileges, const QueryOption &query)
240 {
241 for (const auto &privilege : privileges) {
242 if (privilege.tokenId == query.tokenId && privilege.readPermission == PRIVILEGE_READ_AND_KEEP) {
243 return true;
244 }
245 }
246
247 auto iter = privilegeCache_.find(query.key);
248 if (iter != privilegeCache_.end() && iter->second.tokenId == query.tokenId &&
249 iter->second.readPermission == PRIVILEGE_READ_AND_KEEP) {
250 return true;
251 }
252 return false;
253 }
254
ProcessUri(const QueryOption & query,UnifiedData & unifiedData)255 int32_t UdmfServiceImpl::ProcessUri(const QueryOption &query, UnifiedData &unifiedData)
256 {
257 std::string localDeviceId = PreProcessUtils::GetLocalDeviceId();
258 auto records = unifiedData.GetRecords();
259 if (unifiedData.GetRuntime() == nullptr) {
260 return E_DB_ERROR;
261 }
262 std::string sourceDeviceId = unifiedData.GetRuntime()->deviceId;
263 if (localDeviceId != sourceDeviceId) {
264 SetRemoteUri(query, records);
265 }
266 std::string bundleName;
267 if (!PreProcessUtils::GetHapBundleNameByToken(query.tokenId, bundleName)) {
268 ZLOGE("GetHapBundleNameByToken fail, key=%{public}s, tokenId=%{private}d.", query.key.c_str(), query.tokenId);
269 return E_ERROR;
270 }
271 if (localDeviceId == sourceDeviceId && query.tokenId == unifiedData.GetRuntime()->tokenId) {
272 ZLOGW("No need to grant uri permissions, queryKey=%{public}s.", query.key.c_str());
273 return E_OK;
274 }
275 std::vector<Uri> allUri;
276 for (auto record : records) {
277 if (record != nullptr && PreProcessUtils::IsFileType(record->GetType())) {
278 auto file = static_cast<File *>(record.get());
279 if (file->GetUri().empty()) {
280 ZLOGW("Get uri is empty, key=%{public}s.", query.key.c_str());
281 continue;
282 }
283 Uri uri(file->GetUri());
284 std::string scheme = uri.GetScheme();
285 std::transform(scheme.begin(), scheme.end(), scheme.begin(), ::tolower);
286 if (uri.GetAuthority().empty() || scheme != FILE_SCHEME) {
287 ZLOGW("Get authority is empty or uri scheme not equals to file, key=%{public}s.", query.key.c_str());
288 continue;
289 }
290 allUri.push_back(uri);
291 }
292 }
293 if (UriPermissionManager::GetInstance().GrantUriPermission(allUri, query.tokenId, query.key) != E_OK) {
294 RadarReporterAdapter::ReportFail(std::string(__FUNCTION__),
295 BizScene::GET_DATA, GetDataStage::GRANT_URI_PERMISSION, StageRes::FAILED, E_NO_PERMISSION);
296 ZLOGE("GrantUriPermission fail, bundleName=%{public}s, key=%{public}s.",
297 bundleName.c_str(), query.key.c_str());
298 return E_NO_PERMISSION;
299 }
300 return E_OK;
301 }
302
SetRemoteUri(const QueryOption & query,std::vector<std::shared_ptr<UnifiedRecord>> & records)303 void UdmfServiceImpl::SetRemoteUri(const QueryOption &query, std::vector<std::shared_ptr<UnifiedRecord>> &records)
304 {
305 for (auto record : records) {
306 if (record != nullptr && PreProcessUtils::IsFileType(record->GetType())) {
307 auto file = static_cast<File *>(record.get());
308 std::string remoteUri = file->GetRemoteUri();
309 if (remoteUri.empty()) {
310 ZLOGW("Get remoteUri is empyt, key=%{public}s.", query.key.c_str());
311 continue;
312 }
313 file->SetUri(remoteUri); // cross dev, need dis path.
314 }
315 }
316 }
317
GetBatchData(const QueryOption & query,std::vector<UnifiedData> & unifiedDataSet)318 int32_t UdmfServiceImpl::GetBatchData(const QueryOption &query, std::vector<UnifiedData> &unifiedDataSet)
319 {
320 ZLOGD("start");
321 std::vector<UnifiedData> dataSet;
322 std::shared_ptr<Store> store;
323 auto status = QueryDataCommon(query, dataSet, store);
324 if (status != E_OK) {
325 ZLOGE("QueryDataCommon failed.");
326 return status;
327 }
328 if (dataSet.empty()) {
329 ZLOGW("DataSet has no data, key: %{public}s, intention: %{public}d.", query.key.c_str(), query.intention);
330 return E_OK;
331 }
332 for (auto &data : dataSet) {
333 PreProcessUtils::SetRemoteData(data);
334 unifiedDataSet.push_back(data);
335 }
336 return E_OK;
337 }
338
UpdateData(const QueryOption & query,UnifiedData & unifiedData)339 int32_t UdmfServiceImpl::UpdateData(const QueryOption &query, UnifiedData &unifiedData)
340 {
341 UnifiedKey key(query.key);
342 if (!unifiedData.IsValid() || !key.IsValid()) {
343 ZLOGE("data is invalid, or key is invalid. key=%{public}s.", query.key.c_str());
344 return E_INVALID_PARAMETERS;
345 }
346 std::string bundleName;
347 PreProcessUtils::GetHapBundleNameByToken(query.tokenId, bundleName);
348 if (key.bundleName != bundleName) {
349 ZLOGE("update data failed by %{public}s, key: %{public}s.", bundleName.c_str(), query.key.c_str());
350 return E_INVALID_PARAMETERS;
351 }
352 auto store = StoreCache::GetInstance().GetStore(key.intention);
353 if (store == nullptr) {
354 ZLOGE("Get store failed, intention: %{public}s.", key.intention.c_str());
355 return E_DB_ERROR;
356 }
357 UnifiedData data;
358 int32_t res = store->Get(query.key, data);
359 if (res != E_OK) {
360 ZLOGE("Get data from store failed, intention: %{public}s.", key.intention.c_str());
361 return res;
362 }
363 if (data.IsEmpty()) {
364 ZLOGE("Invalid parameter, unified data has no record; intention: %{public}s.", key.intention.c_str());
365 return E_INVALID_PARAMETERS;
366 }
367 std::shared_ptr<Runtime> runtime = data.GetRuntime();
368 if (runtime == nullptr) {
369 return E_DB_ERROR;
370 }
371 if (runtime->tokenId != query.tokenId) {
372 ZLOGE("update data failed, query option tokenId not equals data's tokenId");
373 return E_INVALID_PARAMETERS;
374 }
375 runtime->lastModifiedTime = PreProcessUtils::GetTimestamp();
376 unifiedData.SetRuntime(*runtime);
377 for (auto &record : unifiedData.GetRecords()) {
378 record->SetUid(PreProcessUtils::GenerateId());
379 }
380 UdmfConversion::InitValueObject(unifiedData);
381 if (store->Update(unifiedData) != E_OK) {
382 ZLOGE("Update unified data failed, intention: %{public}s.", key.intention.c_str());
383 return E_DB_ERROR;
384 }
385 return E_OK;
386 }
387
DeleteData(const QueryOption & query,std::vector<UnifiedData> & unifiedDataSet)388 int32_t UdmfServiceImpl::DeleteData(const QueryOption &query, std::vector<UnifiedData> &unifiedDataSet)
389 {
390 ZLOGD("start");
391 std::vector<UnifiedData> dataSet;
392 std::shared_ptr<Store> store;
393 auto status = QueryDataCommon(query, dataSet, store);
394 if (status != E_OK) {
395 ZLOGE("QueryDataCommon failed.");
396 return status;
397 }
398 if (dataSet.empty()) {
399 ZLOGW("DataSet has no data, key: %{public}s, intention: %{public}d.", query.key.c_str(), query.intention);
400 return E_OK;
401 }
402 std::shared_ptr<Runtime> runtime;
403 std::vector<std::string> deleteKeys;
404 for (const auto &data : dataSet) {
405 runtime = data.GetRuntime();
406 if (runtime == nullptr) {
407 return E_DB_ERROR;
408 }
409 if (runtime->tokenId == query.tokenId) {
410 unifiedDataSet.push_back(data);
411 deleteKeys.push_back(runtime->key.key);
412 }
413 }
414 if (deleteKeys.empty()) {
415 ZLOGE("Delete nothing. There is no data belonging to this application");
416 return E_OK;
417 }
418 ZLOGI("Delete data start. size: %{public}zu.", deleteKeys.size());
419 if (store->DeleteBatch(deleteKeys) != E_OK) {
420 ZLOGE("Remove data failed.");
421 return E_DB_ERROR;
422 }
423 return E_OK;
424 }
425
GetSummary(const QueryOption & query,Summary & summary)426 int32_t UdmfServiceImpl::GetSummary(const QueryOption &query, Summary &summary)
427 {
428 ZLOGD("start");
429 UnifiedKey key(query.key);
430 if (!key.IsValid()) {
431 ZLOGE("Unified key: %{public}s is invalid.", query.key.c_str());
432 return E_INVALID_PARAMETERS;
433 }
434
435 auto store = StoreCache::GetInstance().GetStore(key.intention);
436 if (store == nullptr) {
437 ZLOGE("Get store failed, intention: %{public}s.", key.intention.c_str());
438 return E_DB_ERROR;
439 }
440
441 if (store->GetSummary(query.key, summary) != E_OK) {
442 ZLOGE("Store get summary failed, intention: %{public}s.", key.intention.c_str());
443 return E_DB_ERROR;
444 }
445 return E_OK;
446 }
447
AddPrivilege(const QueryOption & query,Privilege & privilege)448 int32_t UdmfServiceImpl::AddPrivilege(const QueryOption &query, Privilege &privilege)
449 {
450 ZLOGD("start");
451 UnifiedKey key(query.key);
452 if (!key.IsValid()) {
453 ZLOGE("Unified key: %{public}s is invalid.", query.key.c_str());
454 return E_INVALID_PARAMETERS;
455 }
456
457 std::string processName;
458 if (!PreProcessUtils::GetNativeProcessNameByToken(query.tokenId, processName)) {
459 return E_ERROR;
460 }
461
462 if (key.intention == UD_INTENTION_MAP.at(UD_INTENTION_DRAG)) {
463 if (find(DRAG_AUTHORIZED_PROCESSES, std::end(DRAG_AUTHORIZED_PROCESSES), processName) ==
464 std::end(DRAG_AUTHORIZED_PROCESSES)) {
465 ZLOGE("Process: %{public}s has no permission to intention: drag", processName.c_str());
466 return E_NO_PERMISSION;
467 }
468 } else {
469 ZLOGE("Intention: %{public}s has no authorized processes", key.intention.c_str());
470 return E_NO_PERMISSION;
471 }
472
473 auto store = StoreCache::GetInstance().GetStore(key.intention);
474 if (store == nullptr) {
475 ZLOGE("Get store failed, intention: %{public}s.", key.intention.c_str());
476 return E_DB_ERROR;
477 }
478
479 UnifiedData data;
480 int32_t res = store->Get(query.key, data);
481 if (res == E_NOT_FOUND) {
482 privilegeCache_[query.key] = privilege;
483 ZLOGW("Add privilege in cache, key: %{public}s.", query.key.c_str());
484 return E_OK;
485 }
486 if (res != E_OK) {
487 ZLOGE("Get data from store failed, res:%{public}d,intention: %{public}s.", res, key.intention.c_str());
488 return res;
489 }
490 if (data.GetRuntime() == nullptr) {
491 return E_DB_ERROR;
492 }
493 data.GetRuntime()->privileges.emplace_back(privilege);
494 UdmfConversion::InitValueObject(data);
495 if (store->Update(data) != E_OK) {
496 ZLOGE("Update unified data failed, intention: %{public}s.", key.intention.c_str());
497 return E_DB_ERROR;
498 }
499 return E_OK;
500 }
501
Sync(const QueryOption & query,const std::vector<std::string> & devices)502 int32_t UdmfServiceImpl::Sync(const QueryOption &query, const std::vector<std::string> &devices)
503 {
504 ZLOGD("start");
505 RadarReporterAdapter::ReportNormal(std::string(__FUNCTION__),
506 BizScene::SYNC_DATA, SyncDataStage::SYNC_BEGIN, StageRes::IDLE, BizState::DFX_BEGIN);
507 UnifiedKey key(query.key);
508 if (!key.IsValid()) {
509 ZLOGE("Unified key: %{public}s is invalid.", query.key.c_str());
510 return E_INVALID_PARAMETERS;
511 }
512
513 auto store = StoreCache::GetInstance().GetStore(key.intention);
514 if (store == nullptr) {
515 ZLOGE("Get store failed, intention: %{public}s.", key.intention.c_str());
516 return E_DB_ERROR;
517 }
518
519 if (store->Sync(devices) != E_OK) {
520 ZLOGE("Store sync failed, intention: %{public}s.", key.intention.c_str());
521 RadarReporterAdapter::ReportFail(std::string(__FUNCTION__),
522 BizScene::SYNC_DATA, SyncDataStage::SYNC_END, StageRes::FAILED, E_DB_ERROR, BizState::DFX_ABNORMAL_END);
523 return E_DB_ERROR;
524 }
525 RadarReporterAdapter::ReportNormal(std::string(__FUNCTION__),
526 BizScene::SYNC_DATA, SyncDataStage::SYNC_END, StageRes::SUCCESS, BizState::DFX_NORMAL_END);
527 return E_OK;
528 }
529
IsRemoteData(const QueryOption & query,bool & result)530 int32_t UdmfServiceImpl::IsRemoteData(const QueryOption &query, bool &result)
531 {
532 UnifiedKey key(query.key);
533 if (!key.IsValid()) {
534 ZLOGE("Unified key: %{public}s is invalid.", query.key.c_str());
535 return E_INVALID_PARAMETERS;
536 }
537
538 auto store = StoreCache::GetInstance().GetStore(key.intention);
539 if (store == nullptr) {
540 ZLOGE("Get store failed, intention: %{public}s.", key.intention.c_str());
541 return E_DB_ERROR;
542 }
543
544 UnifiedData unifiedData;
545 if (store->Get(query.key, unifiedData) != E_OK) {
546 ZLOGE("Store get unifiedData failed, intention: %{public}s.", key.intention.c_str());
547 return E_DB_ERROR;
548 }
549 std::shared_ptr<Runtime> runtime = unifiedData.GetRuntime();
550 if (runtime == nullptr) {
551 ZLOGE("Store get runtime failed, key: %{public}s.", query.key.c_str());
552 return E_DB_ERROR;
553 }
554
555 std::string localDeviceId = PreProcessUtils::GetLocalDeviceId();
556 if (localDeviceId != runtime->deviceId) {
557 result = true;
558 }
559 return E_OK;
560 }
561
SetAppShareOption(const std::string & intention,int32_t shareOption)562 int32_t UdmfServiceImpl::SetAppShareOption(const std::string &intention, int32_t shareOption)
563 {
564 if (intention.empty() || shareOption >= SHARE_OPTIONS_BUTT || shareOption < IN_APP) {
565 ZLOGE("SetAppShareOption : para is invalid, intention: %{public}s, shareOption:%{public}d.",
566 intention.c_str(), shareOption);
567 return E_INVALID_PARAMETERS;
568 }
569
570 uint64_t accessTokenIDEx = IPCSkeleton::GetCallingFullTokenID();
571 bool isSystemApp = TokenIdKit::IsSystemAppByFullTokenID(accessTokenIDEx);
572 if (!isSystemApp) {
573 ZLOGE("no system permission, intention: %{public}s.", intention.c_str());
574 return E_NO_PERMISSION;
575 }
576 auto store = StoreCache::GetInstance().GetStore(intention);
577 if (store == nullptr) {
578 ZLOGE("Get store failed, intention: %{public}s.", intention.c_str());
579 return E_DB_ERROR;
580 }
581
582 std::string shareOptionTmp;
583 if (store->GetLocal(std::to_string(accessTokenIDEx), shareOptionTmp) == E_OK) {
584 ZLOGE("SetAppShareOption failed, shareOption has already been set, %{public}s.", shareOptionTmp.c_str());
585 return E_SETTINGS_EXISTED;
586 }
587
588 if (store->PutLocal(std::to_string(accessTokenIDEx), ShareOptionsUtil::GetEnumStr(shareOption)) != E_OK) {
589 ZLOGE("Store get unifiedData failed, intention: %{public}d.", shareOption);
590 return E_DB_ERROR;
591 }
592 return E_OK;
593 }
594
GetAppShareOption(const std::string & intention,int32_t & shareOption)595 int32_t UdmfServiceImpl::GetAppShareOption(const std::string &intention, int32_t &shareOption)
596 {
597 if (intention.empty()) {
598 ZLOGE("GetAppShareOption : para is invalid, %{public}s is invalid.", intention.c_str());
599 return E_INVALID_PARAMETERS;
600 }
601 uint64_t accessTokenIDEx = IPCSkeleton::GetCallingFullTokenID();
602 auto store = StoreCache::GetInstance().GetStore(intention);
603 if (store == nullptr) {
604 ZLOGE("Get store failed, intention: %{public}s.", intention.c_str());
605 return E_DB_ERROR;
606 }
607 std::string appShareOption;
608 int32_t ret = store->GetLocal(std::to_string(accessTokenIDEx), appShareOption);
609 if (ret != E_OK) {
610 ZLOGE("GetAppShareOption empty, intention: %{public}s.", intention.c_str());
611 return ret;
612 }
613 ZLOGI("GetAppShareOption, intention: %{public}s, appShareOption:%{public}s.",
614 intention.c_str(), appShareOption.c_str());
615 shareOption = ShareOptionsUtil::GetEnumNum(appShareOption);
616 return E_OK;
617 }
618
RemoveAppShareOption(const std::string & intention)619 int32_t UdmfServiceImpl::RemoveAppShareOption(const std::string &intention)
620 {
621 if (intention.empty()) {
622 ZLOGE("intention: %{public}s is invalid.", intention.c_str());
623 return E_INVALID_PARAMETERS;
624 }
625 uint64_t accessTokenIDEx = IPCSkeleton::GetCallingFullTokenID();
626 bool isSystemApp = TokenIdKit::IsSystemAppByFullTokenID(accessTokenIDEx);
627 if (!isSystemApp) {
628 ZLOGE("no system permission, intention: %{public}s.", intention.c_str());
629 return E_NO_PERMISSION;
630 }
631 auto store = StoreCache::GetInstance().GetStore(intention);
632 if (store == nullptr) {
633 ZLOGE("Get store failed, intention: %{public}s.", intention.c_str());
634 return E_DB_ERROR;
635 }
636
637 UnifiedData unifiedData;
638 if (store->DeleteLocal(std::to_string(accessTokenIDEx)) != E_OK) {
639 ZLOGE("Store DeleteLocal failed, intention: %{public}s.", intention.c_str());
640 return E_DB_ERROR;
641 }
642 return E_OK;
643 }
644
OnInitialize()645 int32_t UdmfServiceImpl::OnInitialize()
646 {
647 ZLOGD("start");
648 Status status = LifeCycleManager::GetInstance().OnStart();
649 if (status != E_OK) {
650 ZLOGE("OnStart execute failed, status: %{public}d", status);
651 }
652 status = LifeCycleManager::GetInstance().StartLifeCycleTimer();
653 if (status != E_OK) {
654 ZLOGE("StartLifeCycleTimer start failed, status: %{public}d", status);
655 }
656 return DistributedData::FeatureSystem::STUB_SUCCESS;
657 }
658
QueryDataCommon(const QueryOption & query,std::vector<UnifiedData> & dataSet,std::shared_ptr<Store> & store)659 int32_t UdmfServiceImpl::QueryDataCommon(
660 const QueryOption &query, std::vector<UnifiedData> &dataSet, std::shared_ptr<Store> &store)
661 {
662 auto find = UD_INTENTION_MAP.find(query.intention);
663 std::string intention = find == UD_INTENTION_MAP.end() ? intention : find->second;
664 if (!UnifiedDataUtils::IsValidOptions(query.key, intention)) {
665 ZLOGE("Unified key: %{public}s and intention: %{public}s is invalid.", query.key.c_str(), intention.c_str());
666 return E_INVALID_PARAMETERS;
667 }
668 std::string dataPrefix = DATA_PREFIX + intention;
669 UnifiedKey key(query.key);
670 key.IsValid();
671 if (intention.empty()) {
672 dataPrefix = key.key;
673 intention = key.intention;
674 }
675 ZLOGD("dataPrefix = %{public}s, intention: %{public}s.", dataPrefix.c_str(), intention.c_str());
676 store = StoreCache::GetInstance().GetStore(intention);
677 if (store == nullptr) {
678 ZLOGE("Get store failed, intention: %{public}s.", intention.c_str());
679 return E_DB_ERROR;
680 }
681 if (store->GetBatchData(dataPrefix, dataSet) != E_OK) {
682 ZLOGE("Get dataSet failed, dataPrefix: %{public}s.", dataPrefix.c_str());
683 return E_DB_ERROR;
684 }
685 return E_OK;
686 }
687
OnBind(const BindInfo & bindInfo)688 int32_t UdmfServiceImpl::OnBind(const BindInfo &bindInfo)
689 {
690 executors_ = bindInfo.executors;
691 StoreCache::GetInstance().SetThreadPool(bindInfo.executors);
692 LifeCycleManager::GetInstance().SetThreadPool(bindInfo.executors);
693 UriPermissionManager::GetInstance().SetThreadPool(bindInfo.executors);
694 return 0;
695 }
696
OnAppInstall(const std::string & bundleName,int32_t user,int32_t index)697 int32_t UdmfServiceImpl::UdmfStatic::OnAppInstall(const std::string &bundleName, int32_t user,
698 int32_t index)
699 {
700 ZLOGD("Bundle: %{public}s installed.", bundleName.c_str());
701 auto status = CustomUtdInstaller::GetInstance().InstallUtd(bundleName, user);
702 if (status != E_OK) {
703 ZLOGE("Install Utd failed, bundleName: %{public}s, status: %{public}d", bundleName.c_str(), status);
704 }
705 return status;
706 }
707
OnAppUpdate(const std::string & bundleName,int32_t user,int32_t index)708 int32_t UdmfServiceImpl::UdmfStatic::OnAppUpdate(const std::string &bundleName, int32_t user,
709 int32_t index)
710 {
711 ZLOGD("Bundle: %{public}s Update.", bundleName.c_str());
712 auto status = CustomUtdInstaller::GetInstance().UninstallUtd(bundleName, user);
713 if (status != E_OK) {
714 ZLOGE("Uninstall utd failed, bundleName: %{public}s, status: %{public}d.", bundleName.c_str(), status);
715 return status;
716 }
717 status = CustomUtdInstaller::GetInstance().InstallUtd(bundleName, user);
718 if (status != E_OK) {
719 ZLOGE("Install utd failed, bundleName: %{public}s, status: %{public}d.", bundleName.c_str(), status);
720 }
721 return status;
722 }
723
OnAppUninstall(const std::string & bundleName,int32_t user,int32_t index)724 int32_t UdmfServiceImpl::UdmfStatic::OnAppUninstall(const std::string &bundleName, int32_t user,
725 int32_t index)
726 {
727 ZLOGD("Bundle: %{public}s uninstalled.", bundleName.c_str());
728 auto status = CustomUtdInstaller::GetInstance().UninstallUtd(bundleName, user);
729 if (status != E_OK) {
730 ZLOGE("Uninstall utd failed, bundleName: %{public}s, status: %{public}d.", bundleName.c_str(), status);
731 }
732 return status;
733 }
734 } // namespace UDMF
735 } // namespace OHOS