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 #include "printer_info_helper.h"
17 #include "napi_print_utils.h"
18 #include "print_constant.h"
19 #include "printer_capability_helper.h"
20 #include "print_log.h"
21
22 namespace OHOS::Print {
23 static constexpr const char *PARAM_INFO_PRINTERID = "printerId";
24 static constexpr const char *PARAM_INFO_PRINTERNAME = "printerName";
25 static constexpr const char *PARAM_INFO_PRINTERSTATE = "printerState"; // Property in API 10, deprecated in API 12
26 static constexpr const char *PARAM_INFO_PRINTERICON = "printerIcon";
27 static constexpr const char *PARAM_INFO_DESCRIPTION = "description";
28 static constexpr const char *PARAM_INFO_CAPABILITY = "capability";
29 static constexpr const char *PARAM_JOB_OPTION = "options";
30 static constexpr const char *PARAM_INFO_IS_DAFAULT_PRINTER = "isDefaultPrinter";
31 static constexpr const char *PARAM_INFO_IS_LAST_USED_PRINTER = "isLastUsedPrinter";
32 static constexpr const char *PARAM_INFO_PRINTER_STATUS = "printerStatus";
33 static constexpr const char *PARAM_INFO_PRINTER_MAKE = "printerMake";
34 static constexpr const char *PARAM_INFO_URI = "uri";
35
MakeJsObject(napi_env env,const PrinterInfo & info)36 napi_value PrinterInfoHelper::MakeJsObject(napi_env env, const PrinterInfo &info)
37 {
38 napi_value jsObj = nullptr;
39 PRINT_CALL(env, napi_create_object(env, &jsObj));
40 NapiPrintUtils::SetStringPropertyUtf8(env, jsObj, PARAM_INFO_PRINTERID, info.GetPrinterId());
41 NapiPrintUtils::SetStringPropertyUtf8(env, jsObj, PARAM_INFO_PRINTERNAME, info.GetPrinterName());
42 NapiPrintUtils::SetUint32Property(env, jsObj, PARAM_INFO_PRINTERSTATE, info.GetPrinterState());
43
44 if (info.GetPrinterIcon() != PRINT_INVALID_ID) {
45 NapiPrintUtils::SetUint32Property(env, jsObj, PARAM_INFO_PRINTERICON, info.GetPrinterIcon());
46 }
47
48 if (info.GetDescription() != "") {
49 NapiPrintUtils::SetStringPropertyUtf8(env, jsObj, PARAM_INFO_DESCRIPTION, info.GetDescription());
50 }
51
52 if (info.HasCapability()) {
53 PrinterCapability cap;
54 info.GetCapability(cap);
55 napi_value jsCapability = PrinterCapabilityHelper::MakeJsObject(env, cap);
56 PRINT_CALL(env, napi_set_named_property(env, jsObj, PARAM_INFO_CAPABILITY, jsCapability));
57 }
58
59 if (info.HasPrinterMake()) {
60 NapiPrintUtils::SetStringPropertyUtf8(env, jsObj, PARAM_INFO_PRINTER_MAKE, info.GetPrinterMake());
61 }
62
63 if (info.HasUri()) {
64 NapiPrintUtils::SetStringPropertyUtf8(env, jsObj, PARAM_INFO_URI, info.GetUri());
65 }
66
67 if (info.HasOption()) {
68 NapiPrintUtils::SetStringPropertyUtf8(env, jsObj, PARAM_JOB_OPTION, info.GetOption());
69 }
70
71 if (info.HasIsDefaultPrinter()) {
72 NapiPrintUtils::SetBooleanProperty(env, jsObj, PARAM_INFO_IS_DAFAULT_PRINTER, info.GetIsDefaultPrinter());
73 }
74
75 if (info.HasIsLastUsedPrinter()) {
76 NapiPrintUtils::SetBooleanProperty(env, jsObj, PARAM_INFO_IS_LAST_USED_PRINTER, info.GetIsLastUsedPrinter());
77 }
78
79 if (info.HasPrinterStatus()) {
80 NapiPrintUtils::SetUint32Property(env, jsObj, PARAM_INFO_PRINTER_STATUS, info.GetPrinterStatus());
81 }
82 return jsObj;
83 }
84
BuildFromJs(napi_env env,napi_value jsValue)85 std::shared_ptr<PrinterInfo> PrinterInfoHelper::BuildFromJs(napi_env env, napi_value jsValue)
86 {
87 auto nativeObj = std::make_shared<PrinterInfo>();
88
89 if (!ValidateProperty(env, jsValue)) {
90 PRINT_HILOGE("Invalid property of printer info");
91 return nullptr;
92 }
93
94 std::string printerId = NapiPrintUtils::GetStringPropertyUtf8(env, jsValue, PARAM_INFO_PRINTERID);
95 std::string printerName = NapiPrintUtils::GetStringPropertyUtf8(env, jsValue, PARAM_INFO_PRINTERNAME);
96 if (printerId == "" || printerName == "") {
97 PRINT_HILOGE("Invalid printer id[%{private}s] or printer name[%{private}s]",
98 printerId.c_str(), printerName.c_str());
99 return nullptr;
100 }
101 nativeObj->SetPrinterId(printerId);
102 nativeObj->SetPrinterName(printerName);
103
104 auto jsIcon = NapiPrintUtils::GetNamedProperty(env, jsValue, PARAM_INFO_PRINTERICON);
105 if (jsIcon != nullptr) {
106 nativeObj->SetPrinterIcon(NapiPrintUtils::GetUint32Property(env, jsValue, PARAM_INFO_PRINTERICON));
107 }
108
109 auto jsDesc = NapiPrintUtils::GetNamedProperty(env, jsValue, PARAM_INFO_DESCRIPTION);
110 if (jsDesc != nullptr) {
111 nativeObj->SetDescription(NapiPrintUtils::GetStringPropertyUtf8(env, jsValue, PARAM_INFO_DESCRIPTION));
112 }
113
114 auto jsCapability = NapiPrintUtils::GetNamedProperty(env, jsValue, PARAM_INFO_CAPABILITY);
115 if (jsCapability != nullptr) {
116 auto capabilityPtr = PrinterCapabilityHelper::BuildFromJs(env, jsCapability);
117 if (capabilityPtr == nullptr) {
118 PRINT_HILOGE("Failed to build printer info object from js");
119 return nullptr;
120 }
121 nativeObj->SetCapability(*capabilityPtr);
122 }
123
124 auto jsPrinterMake = NapiPrintUtils::GetNamedProperty(env, jsValue, PARAM_INFO_PRINTER_MAKE);
125 if (jsPrinterMake != nullptr) {
126 nativeObj->SetPrinterMake(NapiPrintUtils::GetStringPropertyUtf8(env, jsValue, PARAM_INFO_PRINTER_MAKE));
127 }
128
129 auto jsUri = NapiPrintUtils::GetNamedProperty(env, jsValue, PARAM_INFO_URI);
130 if (jsUri != nullptr) {
131 nativeObj->SetUri(NapiPrintUtils::GetStringPropertyUtf8(env, jsValue, PARAM_INFO_URI));
132 }
133
134 auto jsOption = NapiPrintUtils::GetNamedProperty(env, jsValue, PARAM_JOB_OPTION);
135 if (jsOption != nullptr) {
136 nativeObj->SetOption(NapiPrintUtils::GetStringPropertyUtf8(env, jsValue, PARAM_JOB_OPTION));
137 }
138 return nativeObj;
139 }
140
ValidateProperty(napi_env env,napi_value object)141 bool PrinterInfoHelper::ValidateProperty(napi_env env, napi_value object)
142 {
143 std::map<std::string, PrintParamStatus> propertyList = {
144 {PARAM_INFO_PRINTERID, PRINT_PARAM_NOT_SET},
145 {PARAM_INFO_PRINTERNAME, PRINT_PARAM_NOT_SET},
146 {PARAM_INFO_PRINTERSTATE, PRINT_PARAM_OPT},
147 {PARAM_INFO_PRINTERICON, PRINT_PARAM_OPT},
148 {PARAM_INFO_DESCRIPTION, PRINT_PARAM_OPT},
149 {PARAM_INFO_CAPABILITY, PRINT_PARAM_OPT},
150 {PARAM_JOB_OPTION, PRINT_PARAM_OPT},
151 {PARAM_INFO_PRINTER_MAKE, PRINT_PARAM_OPT},
152 {PARAM_INFO_URI, PRINT_PARAM_OPT},
153 {PARAM_INFO_PRINTER_STATUS, PRINT_PARAM_OPT},
154 };
155
156 auto names = NapiPrintUtils::GetPropertyNames(env, object);
157 return NapiPrintUtils::VerifyProperty(names, propertyList);
158 }
159 } // namespace OHOS::Print
160