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