• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 = (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             break;
78     }
79 }
80 
ToJson()81 nlohmann::json PrintSecurityGuardInfo::ToJson()
82 {
83     nlohmann::json printTypeInfoJson;
84     printTypeInfoJson["ip"] = printTypeInfo_.ip;
85     printTypeInfoJson["port"] = printTypeInfo_.port;
86     printTypeInfoJson["mac"] = printTypeInfo_.mac;
87     printTypeInfoJson["domain"] = printTypeInfo_.domain;
88     printTypeInfoJson["name"] = printTypeInfo_.name;
89     printTypeInfoJson["copyNumber"] = printTypeInfo_.copyNumber;
90     printTypeInfoJson["printPages"] = printTypeInfo_.printPages;
91     targetInfo_ = printTypeInfoJson.dump();
92 
93     nlohmann::json securityGuardInfoJson;
94     securityGuardInfoJson["type"] = 0;
95     securityGuardInfoJson["subType"] = subType_;
96     securityGuardInfoJson["caller"] = caller_;
97     securityGuardInfoJson["objectInfo"] = objectInfo_;
98     securityGuardInfoJson["bootTime"] = bootTime_;
99     securityGuardInfoJson["wallTime"] = wallTime_;
100     securityGuardInfoJson["outcome"] = outcome_;
101     securityGuardInfoJson["sourceInfo"] = sourceInfo_;
102     securityGuardInfoJson["targetInfo"] = targetInfo_;
103     securityGuardInfoJson["extra"] = extra_;
104     return securityGuardInfoJson;
105 }
106 
ToJsonStr()107 std::string PrintSecurityGuardInfo::ToJsonStr()
108 {
109     return ToJson().dump();
110 }
111 } // namespace OHOS::Print
112 
113