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_page_size_helper.h"
17 #include "napi_print_utils.h"
18 #include "print_constant.h"
19 #include "print_log.h"
20
21 namespace OHOS::Print {
22 static constexpr const char *PARAM_PAGESIZE_ID = "id";
23 static constexpr const char *PARAM_PAGESIZE_NAME = "name";
24 static constexpr const char *PARAM_PAGESIZE_WIDTH = "width";
25 static constexpr const char *PARAM_PAGESIZE_HEIGHT = "height";
MakeJsObject(napi_env env,const PrintPageSize & pageSize)26 napi_value PrintPageSizeHelper::MakeJsObject(napi_env env, const PrintPageSize &pageSize)
27 {
28 napi_value jsObj = nullptr;
29 PRINT_CALL(env, napi_create_object(env, &jsObj));
30
31 NapiPrintUtils::SetStringPropertyUtf8(env, jsObj, PARAM_PAGESIZE_ID, pageSize.GetId());
32 NapiPrintUtils::SetStringPropertyUtf8(env, jsObj, PARAM_PAGESIZE_NAME, pageSize.GetName());
33 NapiPrintUtils::SetUint32Property(env, jsObj, PARAM_PAGESIZE_WIDTH, pageSize.GetWidth());
34 NapiPrintUtils::SetUint32Property(env, jsObj, PARAM_PAGESIZE_HEIGHT, pageSize.GetHeight());
35 return jsObj;
36 }
37
BuildFromJs(napi_env env,napi_value jsValue)38 std::shared_ptr<PrintPageSize> PrintPageSizeHelper::BuildFromJs(napi_env env, napi_value jsValue)
39 {
40 auto nativeObj = std::make_shared<PrintPageSize>();
41
42 if (!ValidateProperty(env, jsValue)) {
43 PRINT_HILOGE("Invalid property of print page size");
44 return nullptr;
45 }
46
47 std::string id = NapiPrintUtils::GetStringPropertyUtf8(env, jsValue, PARAM_PAGESIZE_ID);
48 std::string name = NapiPrintUtils::GetStringPropertyUtf8(env, jsValue, PARAM_PAGESIZE_NAME);
49 uint32_t width = NapiPrintUtils::GetUint32Property(env, jsValue, PARAM_PAGESIZE_WIDTH);
50 uint32_t height = NapiPrintUtils::GetUint32Property(env, jsValue, PARAM_PAGESIZE_HEIGHT);
51
52 if (id == "") {
53 PRINT_HILOGE("Invalid resolution id");
54 return nullptr;
55 }
56
57 nativeObj->SetId(id);
58 nativeObj->SetName(name);
59 nativeObj->SetWidth(width);
60 nativeObj->SetHeight(height);
61 PRINT_HILOGI("Build Page Size succeed");
62 return nativeObj;
63 }
64
ValidateProperty(napi_env env,napi_value object)65 bool PrintPageSizeHelper::ValidateProperty(napi_env env, napi_value object)
66 {
67 std::map<std::string, PrintParamStatus> propertyList = {
68 {PARAM_PAGESIZE_ID, PRINT_PARAM_NOT_SET},
69 {PARAM_PAGESIZE_NAME, PRINT_PARAM_NOT_SET},
70 {PARAM_PAGESIZE_WIDTH, PRINT_PARAM_NOT_SET},
71 {PARAM_PAGESIZE_HEIGHT, PRINT_PARAM_NOT_SET},
72 };
73
74 auto names = NapiPrintUtils::GetPropertyNames(env, object);
75 return NapiPrintUtils::VerifyProperty(names, propertyList);
76 }
77 } // namespace OHOS::Print
78