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 #include "print_log.h"
18 
19 namespace OHOS::Print {
20 using json = nlohmann::json;
21 static const int32_t SPLIT_INDEX = 2;
22 
PrintSecurityGuardInfo(const std::string callPkg,const std::vector<std::string> & fileList)23 PrintSecurityGuardInfo::PrintSecurityGuardInfo(const std::string callPkg, const std::vector<std::string> &fileList)
24 {
25     caller_ = callPkg;
26     objectInfo_ = PrintUtil::ParseListToString(fileList);
27 #ifdef SECURITY_GUARDE_ENABLE
28     OHOS::MiscServices::TimeServiceClient *tsc = OHOS::MiscServices::TimeServiceClient::GetInstance();
29     if (tsc != nullptr) {
30         wallTime_ = std::to_string(tsc->GetWallTimeNs());
31         bootTime_ = std::to_string(tsc->GetBootTimeNs());
32         PRINT_HILOGI("PrintSecurityGuardInfo wallTime_:%{public}s bootTime_:%{public}s", wallTime_.c_str(),
33             bootTime_.c_str());
34     }
35 #endif
36 }
37 
SetPrintTypeInfo(const PrinterInfo & printerInfo,const PrintJob & printJob)38 void PrintSecurityGuardInfo::SetPrintTypeInfo(const PrinterInfo &printerInfo, const PrintJob &printJob)
39 {
40     std::string printerId = printerInfo.GetPrinterId();
41     std::string description = printerInfo.GetDescription();
42     printTypeInfo_.ip = PrintUtil::SplitStr(description, '&', SPLIT_INDEX);
43     printTypeInfo_.mac = PrintUtil::SplitStr(printerId, '/', SPLIT_INDEX);
44     subType_ = PrintSecurityGuardUtil::GetPrinterType(printerId);
45     printTypeInfo_.domain = "";
46     if (subType_ == FROM_EPRINT) {
47         if (json::accept(printerInfo.GetOption())) {
48             json optionJson = json::parse(printerInfo.GetOption());
49             if (optionJson.contains("ePrintUrl") && optionJson["ePrintUrl"].is_string()) {
50                 printTypeInfo_.domain = optionJson["ePrintUrl"].get<std::string>();
51             }
52         }
53     }
54     printTypeInfo_.copyNumber = static_cast<int32_t>(printJob.GetCopyNumber());
55     if (json::accept(printJob.GetOption())) {
56         json jobOptionJson = json::parse(printJob.GetOption());
57         if (jobOptionJson.contains("printPages") && jobOptionJson["printPages"].is_number()) {
58             printTypeInfo_.printPages = jobOptionJson["printPages"];
59         } else {
60             std::vector<uint32_t> fdList;
61             printJob.GetFdList(fdList);
62             printTypeInfo_.printPages = (int32_t)fdList.size();
63         }
64 
65         if (jobOptionJson.contains("jobName") && jobOptionJson["jobName"].is_string()) {
66             jobName_ = jobOptionJson["jobName"];
67         }
68     }
69     uint32_t subState = printJob.GetSubState();
70     switch (subState) {
71         case PRINT_JOB_COMPLETED_SUCCESS:
72             outcome_ = "success";
73             break;
74         case PRINT_JOB_COMPLETED_CANCELLED:
75             outcome_ = "canceled";
76             break;
77         case PRINT_JOB_COMPLETED_FAILED:
78             outcome_ = "failed";
79             break;
80         default:
81             PRINT_HILOGD("PrintSecurityGuardInfo SetPrintTypeInfo unknown subState:%{public}d", subState);
82             break;
83     }
84 }
85 
ToJson()86 nlohmann::json PrintSecurityGuardInfo::ToJson()
87 {
88     nlohmann::json printTypeInfoJson;
89     printTypeInfoJson["ip"] = printTypeInfo_.ip;
90     printTypeInfoJson["port"] = printTypeInfo_.port;
91     printTypeInfoJson["mac"] = printTypeInfo_.mac;
92     printTypeInfoJson["domain"] = printTypeInfo_.domain;
93     printTypeInfoJson["name"] = printTypeInfo_.name;
94     printTypeInfoJson["copyNumber"] = printTypeInfo_.copyNumber;
95     printTypeInfoJson["printPages"] = printTypeInfo_.printPages;
96     targetInfo_ = printTypeInfoJson.dump();
97 
98     nlohmann::json securityGuardInfoJson;
99     securityGuardInfoJson["type"] = 0;
100     securityGuardInfoJson["subType"] = subType_;
101     securityGuardInfoJson["caller"] = caller_;
102     securityGuardInfoJson["objectInfo"] = objectInfo_;
103     securityGuardInfoJson["bootTime"] = bootTime_;
104     securityGuardInfoJson["wallTime"] = wallTime_;
105     securityGuardInfoJson["outcome"] = outcome_;
106     securityGuardInfoJson["sourceInfo"] = sourceInfo_;
107     securityGuardInfoJson["targetInfo"] = targetInfo_;
108     securityGuardInfoJson["extra"] = extra_;
109     securityGuardInfoJson["jobName"] = jobName_;
110     return securityGuardInfoJson;
111 }
112 
ToJsonStr()113 std::string PrintSecurityGuardInfo::ToJsonStr()
114 {
115     return ToJson().dump();
116 }
117 } // namespace OHOS::Print
118 
119