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 #include "print_resolution_helper.h"
16 #include "print_constant.h"
17 #include "napi_print_utils.h"
18 #include "print_log.h"
19 
20 namespace OHOS::Print {
21 static constexpr const char *PARAM_RESOLUTION_ID = "id";
22 static constexpr const char *PARAM_RESOLUTION_HORIZONTALDPI = "horizontalDpi";
23 static constexpr const char *PARAM_RESOLUTION_VERTICALDPI = "verticalDpi";
MakeJsObject(napi_env env,const PrintResolution & resolution)24 napi_value PrintResolutionHelper::MakeJsObject(napi_env env, const PrintResolution &resolution)
25 {
26     napi_value jsObj = nullptr;
27     PRINT_CALL(env, napi_create_object(env, &jsObj));
28     NapiPrintUtils::SetStringPropertyUtf8(env, jsObj, PARAM_RESOLUTION_ID, resolution.GetId());
29     NapiPrintUtils::SetUint32Property(env, jsObj, PARAM_RESOLUTION_HORIZONTALDPI, resolution.GetHorizontalDpi());
30     NapiPrintUtils::SetUint32Property(env, jsObj, PARAM_RESOLUTION_VERTICALDPI, resolution.GetVerticalDpi());
31     return jsObj;
32 }
33 
BuildFromJs(napi_env env,napi_value jsValue)34 std::shared_ptr<PrintResolution> PrintResolutionHelper::BuildFromJs(napi_env env, napi_value jsValue)
35 {
36     std::shared_ptr<PrintResolution> nativeObj = nullptr;
37     if (ValidateProperty(env, jsValue)) {
38         nativeObj = std::make_shared<PrintResolution>();
39         std::string id = NapiPrintUtils::GetStringPropertyUtf8(env, jsValue, PARAM_RESOLUTION_ID);
40         uint32_t horizontalDpi = NapiPrintUtils::GetUint32Property(env, jsValue, PARAM_RESOLUTION_HORIZONTALDPI);
41         uint32_t verticalDpi = NapiPrintUtils::GetUint32Property(env, jsValue, PARAM_RESOLUTION_VERTICALDPI);
42         if (id == "") {
43             PRINT_HILOGE("Invalid resolution id");
44             return nullptr;
45         }
46         if (nativeObj == nullptr) {
47             PRINT_HILOGE("nativeObj is nullptr");
48             return nullptr;
49         }
50         nativeObj->SetId(id);
51         nativeObj->SetHorizontalDpi(horizontalDpi);
52         nativeObj->SetVerticalDpi(verticalDpi);
53     }
54     return nativeObj;
55 }
56 
ValidateProperty(napi_env env,napi_value object)57 bool PrintResolutionHelper::ValidateProperty(napi_env env, napi_value object)
58 {
59     std::map<std::string, PrintParamStatus> propertyList = {
60         {PARAM_RESOLUTION_ID, PRINT_PARAM_NOT_SET},
61         {PARAM_RESOLUTION_HORIZONTALDPI, PRINT_PARAM_NOT_SET},
62         {PARAM_RESOLUTION_VERTICALDPI, PRINT_PARAM_NOT_SET},
63     };
64 
65     auto names = NapiPrintUtils::GetPropertyNames(env, object);
66     return NapiPrintUtils::VerifyProperty(names, propertyList);
67 }
68 }  // namespace OHOS::Print
69