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 #define LOG_TAG "UdmfServiceImpl"
16
17 #include "udmf_service_impl.h"
18
19 #include "iservice_registry.h"
20
21 #include "data_manager.h"
22 #include "lifecycle/lifecycle_manager.h"
23 #include "log_print.h"
24 #include "preprocess_utils.h"
25 #include "reporter.h"
26
27 namespace OHOS {
28 namespace UDMF {
29 using FeatureSystem = DistributedData::FeatureSystem;
30 using UdmfBehaviourMsg = OHOS::DistributedDataDfx::UdmfBehaviourMsg;
31 using Reporter = OHOS::DistributedDataDfx::Reporter;
32 __attribute__((used)) UdmfServiceImpl::Factory UdmfServiceImpl::factory_;
Factory()33 UdmfServiceImpl::Factory::Factory()
34 {
35 ZLOGI("Register udmf creator!");
36 FeatureSystem::GetInstance().RegisterCreator("udmf", [this]() {
37 if (product_ == nullptr) {
38 product_ = std::make_shared<UdmfServiceImpl>();
39 }
40 return product_;
41 });
42 }
43
~Factory()44 UdmfServiceImpl::Factory::~Factory()
45 {
46 product_ = nullptr;
47 }
48
SetData(CustomOption & option,UnifiedData & unifiedData,std::string & key)49 int32_t UdmfServiceImpl::SetData(CustomOption &option, UnifiedData &unifiedData, std::string &key)
50 {
51 ZLOGD("start");
52 int32_t res = E_OK;
53 UdmfBehaviourMsg msg;
54 auto find = UD_INTENTION_MAP.find(option.intention);
55 msg.channel = find == UD_INTENTION_MAP.end() ? "invalid" : find->second;
56 msg.operation = "insert";
57 std::string bundleName;
58 if (!PreProcessUtils::GetHapBundleNameByToken(option.tokenId, bundleName)) {
59 msg.appId = "unknown";
60 res = E_ERROR;
61 } else {
62 msg.appId = bundleName;
63 res = DataManager::GetInstance().SaveData(option, unifiedData, key);
64 }
65 auto errFind = ERROR_MAP.find(res);
66 msg.result = errFind == ERROR_MAP.end() ? "E_ERROR" : errFind->second;
67 msg.dataType = unifiedData.GetTypes();
68 msg.dataSize = unifiedData.GetSize();
69 Reporter::GetInstance()->BehaviourReporter()->UDMFReport(msg);
70 return res;
71 }
72
GetData(const QueryOption & query,UnifiedData & unifiedData)73 int32_t UdmfServiceImpl::GetData(const QueryOption &query, UnifiedData &unifiedData)
74 {
75 ZLOGD("start");
76 int32_t res = E_OK;
77 UdmfBehaviourMsg msg;
78 auto find = UD_INTENTION_MAP.find(query.intention);
79 msg.channel = find == UD_INTENTION_MAP.end() ? "invalid" : find->second;
80 msg.operation = "insert";
81 std::string bundleName;
82 if (!PreProcessUtils::GetHapBundleNameByToken(query.tokenId, bundleName)) {
83 msg.appId = "unknown";
84 res = E_ERROR;
85 } else {
86 msg.appId = bundleName;
87 res = DataManager::GetInstance().RetrieveData(query, unifiedData);
88 }
89 auto errFind = ERROR_MAP.find(res);
90 msg.result = errFind == ERROR_MAP.end() ? "E_ERROR" : errFind->second;
91 msg.dataType = unifiedData.GetTypes();
92 msg.dataSize = unifiedData.GetSize();
93 Reporter::GetInstance()->BehaviourReporter()->UDMFReport(msg);
94 return res;
95 }
96
GetBatchData(const QueryOption & query,std::vector<UnifiedData> & unifiedDataSet)97 int32_t UdmfServiceImpl::GetBatchData(const QueryOption &query, std::vector<UnifiedData> &unifiedDataSet)
98 {
99 ZLOGD("start");
100 return DataManager::GetInstance().RetrieveBatchData(query, unifiedDataSet);
101 }
102
UpdateData(const QueryOption & query,UnifiedData & unifiedData)103 int32_t UdmfServiceImpl::UpdateData(const QueryOption &query, UnifiedData &unifiedData)
104 {
105 ZLOGD("start");
106 return DataManager::GetInstance().UpdateData(query, unifiedData);
107 }
108
DeleteData(const QueryOption & query,std::vector<UnifiedData> & unifiedDataSet)109 int32_t UdmfServiceImpl::DeleteData(const QueryOption &query, std::vector<UnifiedData> &unifiedDataSet)
110 {
111 ZLOGD("start");
112 return DataManager::GetInstance().DeleteData(query, unifiedDataSet);
113 }
114
GetSummary(const QueryOption & query,Summary & summary)115 int32_t UdmfServiceImpl::GetSummary(const QueryOption &query, Summary &summary)
116 {
117 ZLOGD("start");
118 return DataManager::GetInstance().GetSummary(query, summary);
119 }
120
AddPrivilege(const QueryOption & query,Privilege & privilege)121 int32_t UdmfServiceImpl::AddPrivilege(const QueryOption &query, Privilege &privilege)
122 {
123 ZLOGD("start");
124 return DataManager::GetInstance().AddPrivilege(query, privilege);
125 }
126
Sync(const QueryOption & query,const std::vector<std::string> & devices)127 int32_t UdmfServiceImpl::Sync(const QueryOption &query, const std::vector<std::string> &devices)
128 {
129 ZLOGD("start");
130 return DataManager::GetInstance().Sync(query, devices);
131 }
132
OnInitialize()133 int32_t UdmfServiceImpl::OnInitialize()
134 {
135 ZLOGD("start");
136 Status status = LifeCycleManager::GetInstance().DeleteOnStart();
137 if (status != E_OK) {
138 ZLOGE("DeleteOnStart execute failed, status: %{public}d", status);
139 }
140 status = LifeCycleManager::GetInstance().DeleteOnSchedule();
141 if (status != E_OK) {
142 ZLOGE("ScheduleTask start failed, status: %{public}d", status);
143 }
144 return DistributedData::FeatureSystem::STUB_SUCCESS;
145 }
146 } // namespace UDMF
147 } // namespace OHOS