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 if (nativeObj != nullptr) {
40 std::string id = NapiPrintUtils::GetStringPropertyUtf8(env, jsValue, PARAM_RESOLUTION_ID);
41 uint32_t horizontalDpi = NapiPrintUtils::GetUint32Property(env, jsValue, PARAM_RESOLUTION_HORIZONTALDPI);
42 uint32_t verticalDpi = NapiPrintUtils::GetUint32Property(env, jsValue, PARAM_RESOLUTION_VERTICALDPI);
43 if (id == "") {
44 PRINT_HILOGE("Invalid resolution id");
45 return nullptr;
46 }
47 nativeObj->SetId(id);
48 nativeObj->SetHorizontalDpi(horizontalDpi);
49 nativeObj->SetVerticalDpi(verticalDpi);
50 }
51 }
52 return nativeObj;
53 }
54
ValidateProperty(napi_env env,napi_value object)55 bool PrintResolutionHelper::ValidateProperty(napi_env env, napi_value object)
56 {
57 std::map<std::string, PrintParamStatus> propertyList = {
58 {PARAM_RESOLUTION_ID, PRINT_PARAM_NOT_SET},
59 {PARAM_RESOLUTION_HORIZONTALDPI, PRINT_PARAM_NOT_SET},
60 {PARAM_RESOLUTION_VERTICALDPI, PRINT_PARAM_NOT_SET},
61 };
62
63 auto names = NapiPrintUtils::GetPropertyNames(env, object);
64 for (auto name : names) {
65 if (propertyList.find(name) == propertyList.end()) {
66 PRINT_HILOGE("Invalid property: %{public}s", name.c_str());
67 return false;
68 }
69 propertyList[name] = PRINT_PARAM_SET;
70 }
71
72 for (auto propertypItem : propertyList) {
73 if (propertypItem.second == PRINT_PARAM_NOT_SET) {
74 PRINT_HILOGE("Missing Property: %{public}s", propertypItem.first.c_str());
75 return false;
76 }
77 }
78
79 return true;
80 }
81 } // namespace OHOS::Print
82