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     std::string securityInfo = "";
35     auto it = securityMap_.find(jobId);
36     if (it != securityMap_.end() && it->second != nullptr) {
37         PRINT_HILOGI("find PrintSecurityGuardInfo");
38         auto securityGuard = it->second;
39         securityGuard->SetPrintTypeInfo(printerInfo, printJob);
40         securityInfo = securityGuard->ToJsonStr();
41     } else {
42         PRINT_HILOGI("find PrintSecurityGuardInfo empty");
43         std::vector<std::string> fileList;
44         auto securityGuard = std::make_shared<PrintSecurityGuardInfo>("", fileList);
45         securityGuard->SetPrintTypeInfo(printerInfo, printJob);
46         securityInfo = securityGuard->ToJsonStr();
47     }
48     ReportSecurityInfo(EVENT_ID, VERSION, securityInfo);
49     clearSecurityMap(jobId);
50 }
51 
clearSecurityMap(const std::string jobId)52 void PrintSecurityGuardManager::clearSecurityMap(const std::string jobId)
53 {
54     securityMap_.erase(jobId);
55 }
56 
ReportSecurityInfo(const int32_t eventId,const std::string version,const std::string content)57 void PrintSecurityGuardManager::ReportSecurityInfo(const int32_t eventId, const std::string version,
58     const std::string content)
59 {
60 #ifdef SECURITY_GUARDE_ENABLE
61     PRINT_HILOGI("start to push data to security_guard service, eventId:%{public}d, content:%{public}s",
62         eventId, content.c_str());
63     auto eventInfo = std::make_shared<Security::SecurityGuard::EventInfo>(eventId, version, content);
64     int res = OHOS::Security::SecurityGuard::NativeDataCollectKit::ReportSecurityInfo(eventInfo);
65     PRINT_HILOGI("end to push data to security_guard service status:%{public}d", res);
66 #endif
67 }
68 } // namespace OHOS::Print
69