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