• 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 #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";
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 = "option";
30 
MakeJsObject(napi_env env,const PrinterInfo & info)31 napi_value PrinterInfoHelper::MakeJsObject(napi_env env, const PrinterInfo &info)
32 {
33     napi_value jsObj = nullptr;
34     PRINT_CALL(env, napi_create_object(env, &jsObj));
35     NapiPrintUtils::SetStringPropertyUtf8(env, jsObj, PARAM_INFO_PRINTERID, info.GetPrinterId());
36     NapiPrintUtils::SetStringPropertyUtf8(env, jsObj, PARAM_INFO_PRINTERNAME, info.GetPrinterName());
37     NapiPrintUtils::SetUint32Property(env, jsObj, PARAM_INFO_PRINTERSTATE, info.GetPrinterState());
38 
39     if (info.GetPrinterIcon() != PRINT_INVALID_ID) {
40         NapiPrintUtils::SetUint32Property(env, jsObj, PARAM_INFO_PRINTERICON, info.GetPrinterIcon());
41     }
42 
43     if (info.GetDescription() != "") {
44         NapiPrintUtils::SetStringPropertyUtf8(env, jsObj, PARAM_INFO_DESCRIPTION, info.GetDescription());
45     }
46 
47     if (info.HasCapability()) {
48         PrinterCapability cap;
49         info.GetCapability(cap);
50         napi_value jsCapability = PrinterCapabilityHelper::MakeJsObject(env, cap);
51         PRINT_CALL(env, napi_set_named_property(env, jsObj, PARAM_INFO_CAPABILITY, jsCapability));
52     }
53 
54     if (info.HasOption()) {
55         NapiPrintUtils::SetStringPropertyUtf8(env, jsObj, PARAM_JOB_OPTION, info.GetOption());
56     }
57     return jsObj;
58 }
59 
BuildFromJs(napi_env env,napi_value jsValue)60 std::shared_ptr<PrinterInfo> PrinterInfoHelper::BuildFromJs(napi_env env, napi_value jsValue)
61 {
62     auto nativeObj = std::make_shared<PrinterInfo>();
63     if (nativeObj == nullptr) {
64         PRINT_HILOGE("Failed to create printer info object");
65         return nullptr;
66     }
67 
68     if (!ValidateProperty(env, jsValue)) {
69         PRINT_HILOGE("Invalid property of printer info");
70         return nullptr;
71     }
72 
73     std::string printerId = NapiPrintUtils::GetStringPropertyUtf8(env, jsValue, PARAM_INFO_PRINTERID);
74     std::string printerName = NapiPrintUtils::GetStringPropertyUtf8(env, jsValue, PARAM_INFO_PRINTERNAME);
75     if (printerId == "" || printerName == "") {
76         PRINT_HILOGE("Invalid printer id[%{private}s] or printer name[%{private}s]",
77             printerId.c_str(), printerName.c_str());
78         return nullptr;
79     }
80     nativeObj->SetPrinterId(printerId);
81     nativeObj->SetPrinterName(printerName);
82 
83     uint32_t printerState = NapiPrintUtils::GetUint32Property(env, jsValue, PARAM_INFO_PRINTERSTATE);
84     if (printerState >= PRINTER_UNKNOWN) {
85         PRINT_HILOGE("Invalid printer state");
86         return nullptr;
87     }
88     nativeObj->SetPrinterState(printerState);
89 
90     auto jsIcon = NapiPrintUtils::GetNamedProperty(env, jsValue, PARAM_INFO_PRINTERICON);
91     if (jsIcon != nullptr) {
92         nativeObj->SetPrinterIcon(NapiPrintUtils::GetUint32Property(env, jsValue, PARAM_INFO_PRINTERICON));
93     }
94 
95     auto jsDesc = NapiPrintUtils::GetNamedProperty(env, jsValue, PARAM_INFO_DESCRIPTION);
96     if (jsDesc != nullptr) {
97         nativeObj->SetDescription(NapiPrintUtils::GetStringPropertyUtf8(env, jsValue, PARAM_INFO_DESCRIPTION));
98     }
99 
100     auto jsCapability = NapiPrintUtils::GetNamedProperty(env, jsValue, PARAM_INFO_CAPABILITY);
101     if (jsCapability != nullptr) {
102         auto capabilityPtr = PrinterCapabilityHelper::BuildFromJs(env, jsCapability);
103         if (capabilityPtr == nullptr) {
104             PRINT_HILOGE("Failed to build printer info object from js");
105             return nullptr;
106         }
107         nativeObj->SetCapability(*capabilityPtr);
108     }
109 
110     auto jsOption = NapiPrintUtils::GetNamedProperty(env, jsValue, PARAM_JOB_OPTION);
111     if (jsOption != nullptr) {
112         nativeObj->SetOption(NapiPrintUtils::GetStringPropertyUtf8(env, jsValue, PARAM_JOB_OPTION));
113     }
114     return nativeObj;
115 }
116 
ValidateProperty(napi_env env,napi_value object)117 bool PrinterInfoHelper::ValidateProperty(napi_env env, napi_value object)
118 {
119     std::map<std::string, PrintParamStatus> propertyList = {
120         {PARAM_INFO_PRINTERID, PRINT_PARAM_NOT_SET},
121         {PARAM_INFO_PRINTERNAME, PRINT_PARAM_NOT_SET},
122         {PARAM_INFO_PRINTERSTATE, PRINT_PARAM_NOT_SET},
123         {PARAM_INFO_PRINTERICON, PRINT_PARAM_OPT},
124         {PARAM_INFO_DESCRIPTION, PRINT_PARAM_OPT},
125         {PARAM_INFO_CAPABILITY, PRINT_PARAM_OPT},
126         {PARAM_JOB_OPTION, PRINT_PARAM_OPT},
127     };
128 
129     auto names = NapiPrintUtils::GetPropertyNames(env, object);
130     for (auto name : names) {
131         if (propertyList.find(name) == propertyList.end()) {
132             PRINT_HILOGE("Invalid property: %{public}s", name.c_str());
133             return false;
134         }
135         propertyList[name] = PRINT_PARAM_SET;
136     }
137 
138     for (auto propertypItem : propertyList) {
139         if (propertypItem.second == PRINT_PARAM_NOT_SET) {
140             PRINT_HILOGE("Missing Property: %{public}s", propertypItem.first.c_str());
141             return false;
142         }
143     }
144     return true;
145 }
146 }  // namespace OHOS::Print
147