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