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 "print_extension_info_helper.h"
17 #include "napi_print_utils.h"
18 #include "print_log.h"
19 
20 namespace OHOS::Print {
21 static constexpr const char *PARAM_EXTINFO_EXTENSION_ID = "extensionId";
22 static constexpr const char *PARAM_EXTINFO_VENDOR_ID = "vendorId";
23 static constexpr const char *PARAM_EXTINFO_VENDOR_NAME = "vendorName";
24 static constexpr const char *PARAM_EXTINFO_ICON = "vendorIcon";
25 static constexpr const char *PARAM_EXTINFO_VERSION = "version";
MakeJsObject(napi_env env,const PrintExtensionInfo & info)26 napi_value PrintExtensionInfoHelper::MakeJsObject(napi_env env, const PrintExtensionInfo &info)
27 {
28     napi_value jsObj = nullptr;
29 
30     napi_create_object(env, &jsObj);
31     NapiPrintUtils::SetStringPropertyUtf8(env, jsObj, PARAM_EXTINFO_EXTENSION_ID, info.GetExtensionId());
32     NapiPrintUtils::SetStringPropertyUtf8(env, jsObj, PARAM_EXTINFO_VENDOR_ID, info.GetVendorId());
33     NapiPrintUtils::SetStringPropertyUtf8(env, jsObj, PARAM_EXTINFO_VENDOR_NAME, info.GetVendorName());
34     NapiPrintUtils::SetUint32Property(env, jsObj, PARAM_EXTINFO_ICON, info.GetVendorIcon());
35     NapiPrintUtils::SetStringPropertyUtf8(env, jsObj, PARAM_EXTINFO_VERSION, info.GetVersion());
36     return jsObj;
37 }
38 
BuildFromJs(napi_env env,napi_value jsValue)39 std::shared_ptr<PrintExtensionInfo> PrintExtensionInfoHelper::BuildFromJs(napi_env env, napi_value jsValue)
40 {
41     auto nativeObj = std::make_shared<PrintExtensionInfo>();
42 
43     auto names = NapiPrintUtils::GetPropertyNames(env, jsValue);
44     for (auto name : names) {
45         PRINT_HILOGD("Property: %{public}s", name.c_str());
46     }
47 
48     std::string extensionId = NapiPrintUtils::GetStringPropertyUtf8(env, jsValue, PARAM_EXTINFO_EXTENSION_ID);
49     std::string vendorId = NapiPrintUtils::GetStringPropertyUtf8(env, jsValue, PARAM_EXTINFO_VENDOR_ID);
50     std::string vendorName = NapiPrintUtils::GetStringPropertyUtf8(env, jsValue, PARAM_EXTINFO_VENDOR_NAME);
51     uint32_t iconId = NapiPrintUtils::GetUint32Property(env, jsValue, PARAM_EXTINFO_ICON);
52     std::string version = NapiPrintUtils::GetStringPropertyUtf8(env, jsValue, PARAM_EXTINFO_VERSION);
53     nativeObj->SetExtensionId(extensionId);
54     nativeObj->SetVendorId(vendorId);
55     nativeObj->SetVendorName(vendorName);
56     nativeObj->SetVendorIcon(iconId);
57     nativeObj->SetVersion(version);
58 
59     return nativeObj;
60 }
61 }  // namespace OHOS::Print
62