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 uint32_t subState = printJob.GetSubState();
66 switch (subState) {
67 case PRINT_JOB_COMPLETED_SUCCESS:
68 outcome_ = "success";
69 break;
70 case PRINT_JOB_COMPLETED_CANCELLED:
71 outcome_ = "canceled";
72 break;
73 case PRINT_JOB_COMPLETED_FAILED:
74 outcome_ = "failed";
75 break;
76 default:
77 PRINT_HILOGD("PrintSecurityGuardInfo setPrintTypeInfo unknown subState:%{public}d", subState);
78 break;
79 }
80 }
81
ToJson()82 nlohmann::json PrintSecurityGuardInfo::ToJson()
83 {
84 nlohmann::json printTypeInfoJson;
85 printTypeInfoJson["ip"] = printTypeInfo_.ip;
86 printTypeInfoJson["port"] = printTypeInfo_.port;
87 printTypeInfoJson["mac"] = printTypeInfo_.mac;
88 printTypeInfoJson["domain"] = printTypeInfo_.domain;
89 printTypeInfoJson["name"] = printTypeInfo_.name;
90 printTypeInfoJson["copyNumber"] = printTypeInfo_.copyNumber;
91 printTypeInfoJson["printPages"] = printTypeInfo_.printPages;
92 targetInfo_ = printTypeInfoJson.dump();
93
94 nlohmann::json securityGuardInfoJson;
95 securityGuardInfoJson["type"] = 0;
96 securityGuardInfoJson["subType"] = subType_;
97 securityGuardInfoJson["caller"] = caller_;
98 securityGuardInfoJson["objectInfo"] = objectInfo_;
99 securityGuardInfoJson["bootTime"] = bootTime_;
100 securityGuardInfoJson["wallTime"] = wallTime_;
101 securityGuardInfoJson["outcome"] = outcome_;
102 securityGuardInfoJson["sourceInfo"] = sourceInfo_;
103 securityGuardInfoJson["targetInfo"] = targetInfo_;
104 securityGuardInfoJson["extra"] = extra_;
105 return securityGuardInfoJson;
106 }
107
ToJsonStr()108 std::string PrintSecurityGuardInfo::ToJsonStr()
109 {
110 return ToJson().dump();
111 }
112 } // namespace OHOS::Print
113
114