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 #ifndef PRINTER_CAPABILITY_HELPER_H 16 #define PRINTER_CAPABILITY_HELPER_H 17 18 #define MAX_ARRAY_LENGTH 128 19 20 #include <map> 21 #include "napi/native_api.h" 22 #include "printer_capability.h" 23 #include "print_log.h" 24 #include "napi_print_utils.h" 25 26 namespace OHOS::Print { 27 class PrinterCapabilityHelper { 28 public: 29 static napi_value MakeJsObject(napi_env env, const PrinterCapability &cap); 30 static std::shared_ptr<PrinterCapability> BuildFromJs(napi_env env, napi_value jsValue); 31 static bool BuildSimplePropertyFromJs(napi_env env, napi_value jsValue, 32 std::shared_ptr<PrinterCapability> nativeObj); 33 static bool BuildArrayPropertyFromJs(napi_env env, napi_value jsValue, 34 std::shared_ptr<PrinterCapability> nativeObj); 35 36 private: 37 static bool CreatePageSizeList(napi_env env, napi_value &jsPrinterCap, const PrinterCapability &cap); 38 static bool CreateResolutionList(napi_env env, napi_value &jsPrinterCap, const PrinterCapability &cap); 39 static bool CreateSupportedColorModeList(napi_env env, napi_value &jsPrinterCap, const PrinterCapability &cap); 40 static bool CreateSupportedDuplexModeList(napi_env env, napi_value &jsPrinterCap, const PrinterCapability &cap); 41 static bool CreateSupportedMediaTypeList(napi_env env, napi_value &jsPrinterCap, const PrinterCapability &cap); 42 static bool CreateSupportedQualityList(napi_env env, napi_value &jsPrinterCap, const PrinterCapability &cap); 43 static bool CreateSupportedOrientationList(napi_env env, napi_value &jsPrinterCap, const PrinterCapability &cap); 44 static bool buildSupportedPageSizes(napi_env env, napi_value jsValue, 45 std::shared_ptr<PrinterCapability> nativeObj); 46 static bool buildSupportedResolutions(napi_env env, napi_value jsValue, 47 std::shared_ptr<PrinterCapability> nativeObj); 48 static bool buildSupportedColorModes(napi_env env, napi_value jsValue, 49 std::shared_ptr<PrinterCapability> nativeObj); 50 static bool buildSupportedDuplexModes(napi_env env, napi_value jsValue, 51 std::shared_ptr<PrinterCapability> nativeObj); 52 static bool buildSupportedQualities(napi_env env, napi_value jsValue, 53 std::shared_ptr<PrinterCapability> nativeObj); 54 static bool buildSupportedMediaTypes(napi_env env, napi_value jsValue, 55 std::shared_ptr<PrinterCapability> nativeObj); 56 static bool buildSupportedOrientations(napi_env env, napi_value jsValue, 57 std::shared_ptr<PrinterCapability> nativeObj); 58 static bool ValidateProperty(napi_env env, napi_value object); 59 60 template<typename T> ProcessJsArrayProperty(napi_env env,napi_value jsValue,const char * propertyName,std::function<void (const std::vector<T> &)> setFunction,std::function<std::shared_ptr<T> (napi_env,napi_value)> buildFunction)61 static bool ProcessJsArrayProperty(napi_env env, napi_value jsValue, const char *propertyName, 62 std::function<void(const std::vector<T> &)> setFunction, 63 std::function<std::shared_ptr<T>(napi_env, napi_value)> buildFunction) 64 { 65 if (!setFunction) { 66 PRINT_HILOGE("setFunction is illegal"); 67 return false; 68 } 69 if (!buildFunction) { 70 PRINT_HILOGE("buildFunction is illegal"); 71 return false; 72 } 73 napi_value jsArray; 74 bool hasProperty = NapiPrintUtils::HasNamedProperty(env, jsValue, propertyName); 75 if (!hasProperty) { 76 return true; 77 } 78 79 napi_status status = napi_get_named_property(env, jsValue, propertyName, &jsArray); 80 if (status != napi_ok) { 81 return false; 82 } 83 84 bool isArray = false; 85 napi_is_array(env, jsArray, &isArray); 86 if (!isArray) { 87 PRINT_HILOGE("Can not get an array for property %{public}s", propertyName); 88 return false; 89 } 90 91 uint32_t length = 0; 92 napi_get_array_length(env, jsArray, &length); 93 if (length > MAX_ARRAY_LENGTH) { 94 PRINT_HILOGE("the array length is over %{public}d", MAX_ARRAY_LENGTH); 95 return false; 96 } 97 std::vector<T> items; 98 items.reserve(length); 99 100 for (uint32_t i = 0; i < length; ++i) { 101 napi_value jsItem; 102 napi_get_element(env, jsArray, i, &jsItem); 103 auto item = buildFunction(env, jsItem); 104 if (!item) { 105 PRINT_HILOGE("Failed to build item for property %{public}s", propertyName); 106 return false; 107 } 108 items.push_back(*item); 109 } 110 111 setFunction(items); 112 return true; 113 } 114 }; 115 } // namespace OHOS::Print 116 #endif // PRINTER_CAPABILITY_HELPER_H 117