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