• 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 
55     uint32_t subState = printJob.GetSubState();
56     switch (subState) {
57         case PRINT_JOB_COMPLETED_SUCCESS:
58             outcome_ = "success";
59             break;
60         case PRINT_JOB_COMPLETED_CANCELLED:
61             outcome_ = "canceled";
62             break;
63         case PRINT_JOB_COMPLETED_FAILED:
64             outcome_ = "failed";
65             break;
66         default:
67             break;
68     }
69 }
70 
ToJson()71 nlohmann::json PrintSecurityGuardInfo::ToJson()
72 {
73     nlohmann::json printTypeInfoJson;
74     printTypeInfoJson["ip"] = printTypeInfo_.ip;
75     printTypeInfoJson["port"] = printTypeInfo_.port;
76     printTypeInfoJson["mac"] = printTypeInfo_.mac;
77     printTypeInfoJson["domain"] = printTypeInfo_.domain;
78     printTypeInfoJson["name"] = printTypeInfo_.name;
79     targetInfo_ = printTypeInfoJson.dump();
80 
81     nlohmann::json securityGuardInfoJson;
82     securityGuardInfoJson["type"] = 0;
83     securityGuardInfoJson["subType"] = subType_;
84     securityGuardInfoJson["caller"] = caller_;
85     securityGuardInfoJson["objectInfo"] = objectInfo_;
86     securityGuardInfoJson["bootTime"] = bootTime_;
87     securityGuardInfoJson["wallTime"] = wallTime_;
88     securityGuardInfoJson["outcome"] = outcome_;
89     securityGuardInfoJson["sourceInfo"] = sourceInfo_;
90     securityGuardInfoJson["targetInfo"] = targetInfo_;
91     securityGuardInfoJson["extra"] = extra_;
92     return securityGuardInfoJson;
93 }
94 
ToJsonStr()95 std::string PrintSecurityGuardInfo::ToJsonStr()
96 {
97     return ToJson().dump();
98 }
99 } // namespace OHOS::Print
100 
101