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