1 /*
2  * Copyright (c) 2023 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 "print_security_guard_manager.h"
17 
18 namespace OHOS::Print {
19 static int64_t EVENT_ID = 1011015004;
20 static std::string VERSION = "1.0";
21 
receiveBaseInfo(const std::string jobId, const std::string callPkg, const std::vector<std::string> &fileList)22 void PrintSecurityGuardManager::receiveBaseInfo(const std::string jobId, const std::string callPkg,
23     const std::vector<std::string> &fileList)
24 {
25     PRINT_HILOGI("receiveBaseInfo start jobId:%{public}s, callPkg:%{public}s", jobId.c_str(), callPkg.c_str());
26     auto securityGuard = std::make_shared<PrintSecurityGuardInfo>(callPkg, fileList);
27     securityMap_.insert(std::make_pair(jobId, securityGuard));
28 }
29 
receiveJobStateUpdate(const std::string jobId, const PrinterInfo &printerInfo, const PrintJob &printJob)30 void PrintSecurityGuardManager::receiveJobStateUpdate(const std::string jobId, const PrinterInfo &printerInfo,
31     const PrintJob &printJob)
32 {
33     PRINT_HILOGI("receiveJobStateUpdate jobId:%{public}s, state:%{public}d", jobId.c_str(), printJob.GetJobState());
34     auto it = securityMap_.find(jobId);
35     if (it != securityMap_.end() && it->second != nullptr) {
36         auto securityGuard = it->second;
37         securityGuard->setPrintTypeInfo(printerInfo, printJob);
38 
39         ReportSecurityInfo(EVENT_ID, VERSION, securityGuard->ToJsonStr());
40     } else {
41         PRINT_HILOGI("find PrintSecurityGuardInfo empty");
42     }
43 
44     clearSecurityMap(jobId);
45 }
46 
clearSecurityMap(const std::string jobId)47 void PrintSecurityGuardManager::clearSecurityMap(const std::string jobId)
48 {
49     securityMap_.erase(jobId);
50 }
51 
ReportSecurityInfo(const int32_t eventId, const std::string version, const std::string content)52 void PrintSecurityGuardManager::ReportSecurityInfo(const int32_t eventId, const std::string version,
53     const std::string content)
54 {
55 #ifdef SECURITY_GUARDE_ENABLE
56     PRINT_HILOGI("start to push data to security_guard service, eventId:%{public}d, content:%{public}s",
57         eventId, content.c_str());
58     auto eventInfo = std::make_shared<Security::SecurityGuard::EventInfo>(eventId, version, content);
59     int res = OHOS::Security::SecurityGuard::NativeDataCollectKit::ReportSecurityInfo(eventInfo);
60     PRINT_HILOGI("end to push data to security_guard service status:%{public}d", res);
61 #endif
62 }
63 } // namespace OHOS::Print
64