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 bool CheckUserIdInEventType(const std::string &type, int32_t callerUserId); 60 template <typename T, typename ReadFunc> readListFromParcel(Parcel & parcel,std::vector<T> & supportedList,const ReadFunc & readFunc)61 static bool readListFromParcel(Parcel &parcel, std::vector<T> &supportedList, const ReadFunc &readFunc) 62 { 63 uint32_t vecSize = parcel.ReadUint32(); 64 CHECK_IS_EXCEED_PRINT_RANGE_BOOL(vecSize); 65 supportedList.clear(); 66 supportedList.reserve(vecSize); // Allocate the required memory all at once to speed up processing efficiency. 67 for (uint32_t index = 0; index < vecSize; index++) { 68 auto item = readFunc(parcel); 69 if (item.has_value()) { 70 supportedList.emplace_back(std::move(*item)); 71 } else { 72 PRINT_HILOGE("Failed on the %{public}d-th read of the list.", index); 73 return false; 74 } 75 } 76 return true; 77 } 78 template <typename T, typename ReadFunc> readListFromParcel(Parcel & parcel,std::vector<T> & supportedList,const ReadFunc & readFunc,bool * hasSupportedPtr)79 static bool readListFromParcel(Parcel &parcel, std::vector<T> &supportedList, const ReadFunc &readFunc, 80 bool *hasSupportedPtr) 81 { 82 if (hasSupportedPtr) { 83 *hasSupportedPtr = parcel.ReadBool(); 84 if (*hasSupportedPtr) { 85 return readListFromParcel(parcel, supportedList, readFunc); 86 } 87 } else { 88 PRINT_HILOGE("Func readListFromParcel error! Ptr: hasSupportedPtr is null"); 89 return false; 90 } 91 return true; 92 } 93 94 template <typename T, typename WriteFunc> WriteListToParcel(Parcel & parcel,const std::vector<T> & list,WriteFunc writeFunc)95 static void WriteListToParcel(Parcel &parcel, const std::vector<T> &list, WriteFunc writeFunc) 96 { 97 uint32_t vecSize = static_cast<uint32_t>(list.size()); 98 parcel.WriteUint32(vecSize); 99 for (uint32_t index = 0; index < vecSize; index++) { 100 writeFunc(parcel, list[index]); 101 } 102 } 103 template <typename T, typename WriteFunc> WriteListToParcel(Parcel & parcel,const std::vector<T> & list,WriteFunc writeFunc,bool hasFlag)104 static void WriteListToParcel(Parcel &parcel, const std::vector<T> &list, WriteFunc writeFunc, bool hasFlag) 105 { 106 parcel.WriteBool(hasFlag); 107 if (hasFlag) { 108 WriteListToParcel(parcel, list, writeFunc); 109 } 110 } 111 112 template<typename T> CheckJsonType(const Json::Value & j)113 static bool CheckJsonType(const Json::Value &j) 114 { 115 if constexpr (std::is_same_v<T, int> || std::is_same_v<T, unsigned int> || std::is_same_v<T, uint32_t>) { 116 return j.isInt(); 117 } else if constexpr (std::is_same_v<T, std::string>) { 118 return j.isString(); 119 } else if constexpr (std::is_same_v<T, bool>) { 120 return j.isBool(); 121 } else { 122 return true; // For complex types, we'll do the check in the conversion function 123 } 124 } 125 126 private: 127 static Json::Value CreatePageRangeJson(const PrintAttributes &attrParam); 128 static Json::Value CreatePageSizeJson(const PrintAttributes &attrParam); 129 static Json::Value CreateMarginJson(const PrintAttributes &attrParam); 130 131 private: 132 static std::mutex instanceLock_; 133 }; 134 } // namespace OHOS::Print 135 #endif // PRINT_UTILS_H 136