1 /*
2 * Copyright (c) 2023 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 #define LOG_TAG "ImageNapi"
16 #include "image_napi.h"
17
18 #include "file_napi.h"
19 #include "image.h"
20 #include "unified_record_napi.h"
21
22 namespace OHOS {
23 namespace UDMF {
Constructor(napi_env env)24 napi_value ImageNapi::Constructor(napi_env env)
25 {
26 LOG_DEBUG(UDMF_KITS_NAPI, "ImageNapi");
27 napi_property_descriptor properties[] = {
28 /* Image extends UnifiedRecord */
29 DECLARE_NAPI_FUNCTION("getType", UnifiedRecordNapi::GetType),
30 DECLARE_NAPI_FUNCTION("getValue", UnifiedRecordNapi::GetValue),
31 DECLARE_NAPI_FUNCTION("addEntry", UnifiedRecordNapi::AddEntry),
32 DECLARE_NAPI_FUNCTION("getEntry", UnifiedRecordNapi::GetEntry),
33 DECLARE_NAPI_FUNCTION("getEntries", UnifiedRecordNapi::GetEntries),
34 DECLARE_NAPI_FUNCTION("getTypes", UnifiedRecordNapi::GetTypes),
35 /* Image extends File */
36 DECLARE_NAPI_GETTER_SETTER("details", FileNapi::GetDetails, FileNapi::SetDetails),
37 DECLARE_NAPI_GETTER_SETTER("uri", FileNapi::GetUri, FileNapi::SetUri),
38 /* Image properties */
39 DECLARE_NAPI_GETTER_SETTER("imageUri", GetImageUri, SetImageUri),
40 };
41 size_t count = sizeof(properties) / sizeof(properties[0]);
42 return NapiDataUtils::DefineClass(env, "Image", properties, count, ImageNapi::New);
43 }
44
New(napi_env env,napi_callback_info info)45 napi_value ImageNapi::New(napi_env env, napi_callback_info info)
46 {
47 LOG_DEBUG(UDMF_KITS_NAPI, "ImageNapi");
48 auto ctxt = std::make_shared<ContextBase>();
49 ctxt->GetCbInfoSync(env, info);
50 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
51
52 auto *image = new (std::nothrow) ImageNapi();
53 ASSERT_ERR(ctxt->env, image != nullptr, Status::E_ERROR, "no memory for image!");
54 image->value_ = std::make_shared<Image>();
55 ASSERT_CALL(env, napi_wrap(env, ctxt->self, image, Destructor, nullptr, nullptr), image);
56 return ctxt->self;
57 }
58
NewInstance(napi_env env,std::shared_ptr<UnifiedRecord> in,napi_value & out)59 void ImageNapi::NewInstance(napi_env env, std::shared_ptr<UnifiedRecord> in, napi_value &out)
60 {
61 LOG_DEBUG(UDMF_KITS_NAPI, "ImageNapi");
62 ASSERT_CALL_VOID(env, napi_new_instance(env, Constructor(env), 0, nullptr, &out));
63 auto *image = new (std::nothrow) ImageNapi();
64 ASSERT_ERR_VOID(env, image != nullptr, Status::E_ERROR, "no memory for image!");
65 image->value_ = std::static_pointer_cast<Image>(in);
66 ASSERT_CALL_DELETE(env, napi_wrap(env, out, image, Destructor, nullptr, nullptr), image);
67 }
68
Destructor(napi_env env,void * data,void * hint)69 void ImageNapi::Destructor(napi_env env, void *data, void *hint)
70 {
71 LOG_DEBUG(UDMF_KITS_NAPI, "ImageNapi finalize.");
72 auto *image = static_cast<ImageNapi *>(data);
73 ASSERT_VOID(image != nullptr, "finalize null!");
74 delete image;
75 }
76
GetImage(napi_env env,napi_callback_info info,std::shared_ptr<ContextBase> ctxt)77 ImageNapi *ImageNapi::GetImage(napi_env env, napi_callback_info info, std::shared_ptr<ContextBase> ctxt)
78 {
79 LOG_DEBUG(UDMF_KITS_NAPI, "ImageNapi");
80 ctxt->GetCbInfoSync(env, info);
81 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
82 return static_cast<ImageNapi *>(ctxt->native);
83 }
84
GetImageUri(napi_env env,napi_callback_info info)85 napi_value ImageNapi::GetImageUri(napi_env env, napi_callback_info info)
86 {
87 LOG_DEBUG(UDMF_KITS_NAPI, "ImageNapi");
88 auto ctxt = std::make_shared<ContextBase>();
89 auto image = GetImage(env, info, ctxt);
90 ASSERT_ERR(
91 ctxt->env, (image != nullptr && image->value_ != nullptr), Status::E_ERROR, "invalid object!");
92 ctxt->status = NapiDataUtils::SetValue(env, image->value_->GetUri(), ctxt->output);
93 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, "set image uri failed!");
94 return ctxt->output;
95 }
96
SetImageUri(napi_env env,napi_callback_info info)97 napi_value ImageNapi::SetImageUri(napi_env env, napi_callback_info info)
98 {
99 LOG_DEBUG(UDMF_KITS_NAPI, "ImageNapi");
100 auto ctxt = std::make_shared<ContextBase>();
101 std::string uri;
102 auto input = [env, ctxt, &uri](size_t argc, napi_value *argv) {
103 ASSERT_BUSINESS_ERR(ctxt, argc >= 1,
104 Status::E_INVALID_PARAMETERS, "Parameter error: Mandatory parameters are left unspecified");
105 ctxt->status = NapiDataUtils::GetValue(env, argv[0], uri);
106 ASSERT_BUSINESS_ERR(ctxt, ctxt->status == napi_ok,
107 Status::E_INVALID_PARAMETERS, "Parameter error: parameter imageUri type must be string");
108 };
109 ctxt->GetCbInfoSync(env, info, input);
110 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
111 auto image = static_cast<ImageNapi *>(ctxt->native);
112 ASSERT_ERR(
113 ctxt->env, (image != nullptr && image->value_ != nullptr), Status::E_ERROR, "invalid object!");
114 image->value_->SetUri(uri);
115 return nullptr;
116 }
117 } // namespace UDMF
118 } // namespace OHOS