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_preview_attribute_helper.h"
17 #include "napi_print_utils.h"
18 #include "print_constant.h"
19 #include "print_log.h"
20 #include "print_range_helper.h"
21 
22 namespace OHOS::Print {
23 static constexpr const char *PARAM_PREATTRIBUTE_RANGE = "previewRange";
24 static constexpr const char *PARAM_PREATTRIBUTE_RESULT = "result";
25 
MakeJsObject(napi_env env,const PrintPreviewAttribute & preview)26 napi_value PrintPreviewAttributeHelper::MakeJsObject(napi_env env, const PrintPreviewAttribute &preview)
27 {
28     napi_value jsObj = nullptr;
29     PRINT_CALL(env, napi_create_object(env, &jsObj));
30 
31     if (preview.HasResult()) {
32         NapiPrintUtils::SetUint32Property(env, jsObj, PARAM_PREATTRIBUTE_RESULT, preview.GetResult());
33     }
34     PrintRange range;
35     preview.GetPreviewRange(range);
36 
37     napi_value jsPreviewRange = PrintRangeHelper::MakeJsObject(env,  range);
38     PRINT_CALL(env, napi_set_named_property(env, jsObj, PARAM_PREATTRIBUTE_RANGE, jsPreviewRange));
39     return jsObj;
40 }
41 
BuildFromJs(napi_env env,napi_value jsValue)42 std::shared_ptr<PrintPreviewAttribute> PrintPreviewAttributeHelper::BuildFromJs(napi_env env, napi_value jsValue)
43 {
44     auto nativeObj = std::make_shared<PrintPreviewAttribute>();
45 
46     if (!ValidateProperty(env, jsValue)) {
47         PRINT_HILOGE("Invalid property of print preview attribute");
48         return nullptr;
49     }
50 
51     napi_value jsPreviewRange = NapiPrintUtils::GetNamedProperty(env, jsValue, PARAM_PREATTRIBUTE_RANGE);
52     auto previewRangePtr = PrintRangeHelper::BuildFromJs(env, jsPreviewRange);
53     if (previewRangePtr == nullptr) {
54         PRINT_HILOGE("Failed to build print preview attribute object from js");
55         return nullptr;
56     }
57     nativeObj->SetPreviewRange(*previewRangePtr);
58 
59     napi_value jsResult = NapiPrintUtils::GetNamedProperty(env, jsValue, PARAM_PREATTRIBUTE_RESULT);
60     if (jsResult != nullptr) {
61         auto result = NapiPrintUtils::GetUint32FromValue(env, jsResult);
62         nativeObj->SetResult(result);
63     }
64     PRINT_HILOGE("Build Print Preview Attribute succeed");
65     return nativeObj;
66 }
67 
ValidateProperty(napi_env env,napi_value object)68 bool PrintPreviewAttributeHelper::ValidateProperty(napi_env env, napi_value object)
69 {
70     std::map<std::string, PrintParamStatus> propertyList = {
71         {PARAM_PREATTRIBUTE_RANGE, PRINT_PARAM_NOT_SET},
72         {PARAM_PREATTRIBUTE_RESULT, PRINT_PARAM_OPT},
73     };
74 
75     auto names = NapiPrintUtils::GetPropertyNames(env, object);
76     return NapiPrintUtils::VerifyProperty(names, propertyList);
77 }
78 }  // namespace OHOS::Print
79