1 /*
2  * Copyright (c) 2022 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 #ifndef PRINT_UTILS_H
17 #define PRINT_UTILS_H
18 
19 #include <string>
20 #include "want.h"
21 #include "bundle_mgr_client.h"
22 #include "print_constant.h"
23 #include "print_log.h"
24 #include <nlohmann/json.hpp>
25 #include <mutex>
26 
27 #include <print_attributes.h>
28 
29 namespace OHOS::Print {
30 struct AdapterParam {
31     std::string documentName;
32     bool isCheckFdList;
33     PrintAttributes printAttributes;
34     std::string jobId;
35 };
36 
37 class PrintUtils {
38 public:
39     static std::string ToLower(const std::string &s);
40     static std::string GetExtensionId(const std::string &globalId);
41     static std::string GetGlobalId(const std::string &extensionId, const std::string &localId);
42     static std::string GetLocalId(const std::string &globalId, const std::string &extensionId);
43     static std::string EncodeExtensionCid(const std::string &extensionId, uint32_t callbackId);
44     static bool DecodeExtensionCid(const std::string &cid, std::string &extensionId, uint32_t &callbackId);
45     static std::string GetTaskEventId(const std::string &taskId, const std::string &type);
46     static int32_t OpenFile(const std::string &filePath);
47     static bool IsPathValid(const std::string &filePath);
48     static uint32_t GetIdFromFdPath(const std::string &fdPath);
49     static std::string GetJobStateChar(const uint32_t state);
50 
51     static void BuildAdapterParam(const std::shared_ptr<AdapterParam> &adapterParam, AAFwk::Want &want);
52     static void BuildPrintAttributesParam(const std::shared_ptr<AdapterParam> &adapterParam, AAFwk::Want &want);
53     static void ParseAttributesObjectParamForJson(const PrintAttributes &attrParam, nlohmann::json &attrJson);
54     static std::string GetBundleNameForUid(const int uid);
55     static std::string GetPrintJobId();
56     static std::string GetEventTypeWithToken(int32_t userId, int64_t pid, const std::string &type);
57     static std::string GetEventType(const std::string &type);
58     static bool CheckUserIdInEventType(const std::string &type, int32_t callerUserId);
59     template <typename T, typename ReadFunc>
readListFromParcel(Parcel & parcel,std::vector<T> & supportedList,const ReadFunc & readFunc)60     static bool readListFromParcel(Parcel &parcel, std::vector<T> &supportedList, const ReadFunc &readFunc)
61     {
62         uint32_t vecSize = parcel.ReadUint32();
63         CHECK_IS_EXCEED_PRINT_RANGE_BOOL(vecSize);
64         supportedList.clear();
65         supportedList.reserve(vecSize);  // Allocate the required memory all at once to speed up processing efficiency.
66         for (uint32_t index = 0; index < vecSize; index++) {
67             auto item = readFunc(parcel);
68             if (item.has_value()) {
69                 supportedList.emplace_back(std::move(*item));
70             } else {
71                 PRINT_HILOGE("Failed on the %{public}d-th read of the list.", index);
72                 return false;
73             }
74         }
75         return true;
76     }
77     template <typename T, typename ReadFunc>
readListFromParcel(Parcel & parcel,std::vector<T> & supportedList,const ReadFunc & readFunc,bool * hasSupportedPtr)78     static bool readListFromParcel(Parcel &parcel, std::vector<T> &supportedList, const ReadFunc &readFunc,
79                                    bool *hasSupportedPtr)
80     {
81         if (hasSupportedPtr) {
82             *hasSupportedPtr = parcel.ReadBool();
83             if (*hasSupportedPtr) {
84                 return readListFromParcel(parcel, supportedList, readFunc);
85             }
86         } else {
87             PRINT_HILOGE("Func readListFromParcel error! Ptr: hasSupportedPtr is null");
88             return false;
89         }
90         return true;
91     }
92 
93     template <typename T, typename WriteFunc>
WriteListToParcel(Parcel & parcel,const std::vector<T> & list,WriteFunc writeFunc)94     static void WriteListToParcel(Parcel &parcel, const std::vector<T> &list, WriteFunc writeFunc)
95     {
96         uint32_t vecSize = static_cast<uint32_t>(list.size());
97         parcel.WriteUint32(vecSize);
98         for (uint32_t index = 0; index < vecSize; index++) {
99             writeFunc(parcel, list[index]);
100         }
101     }
102     template <typename T, typename WriteFunc>
WriteListToParcel(Parcel & parcel,const std::vector<T> & list,WriteFunc writeFunc,bool hasFlag)103     static void WriteListToParcel(Parcel &parcel, const std::vector<T> &list, WriteFunc writeFunc, bool hasFlag)
104     {
105         parcel.WriteBool(hasFlag);
106         if (hasFlag) {
107             WriteListToParcel(parcel, list, writeFunc);
108         }
109     }
110 
111     template<typename T>
CheckJsonType(const nlohmann::json & j)112     static bool CheckJsonType(const nlohmann::json &j)
113     {
114         if constexpr (std::is_same_v<T, int> || std::is_same_v<T, unsigned int> || std::is_same_v<T, uint32_t>) {
115             return j.is_number_integer();
116         } else if constexpr (std::is_same_v<T, std::string>) {
117             return j.is_string();
118         } else if constexpr (std::is_same_v<T, bool>) {
119             return j.is_boolean();
120         } else {
121             return true; // For complex types, we'll do the check in the conversion function
122         }
123     }
124 
125 private:
126     static std::mutex instanceLock_;
127 };
128 }  // namespace OHOS::Print
129 #endif  // PRINT_UTILS_H
130