1 /*
2  * Copyright (c) 2024-2024 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 "extension_manager.h"
17 
18 #include "window_manager_hilog.h"
19 
20 namespace OHOS {
21 namespace Rosen {
22 namespace {
23 constexpr uint32_t HOST_WINDOW_RECT_CHANGE = 1;
24 }
25 
Finalizer(napi_env env,void * data,void * hint)26 void ExtensionManager::Finalizer(napi_env env, void* data, void* hint)
27 {
28     TLOGD(WmsLogTag::WMS_UIEXT, "in");
29     std::unique_ptr<ExtensionManager>(static_cast<ExtensionManager*>(data));
30 }
31 
SetNamedProperty(napi_env env,napi_value obj,const std::string & name,uint32_t value)32 static napi_status SetNamedProperty(napi_env env, napi_value obj, const std::string& name, uint32_t value)
33 {
34     TLOGD(WmsLogTag::WMS_UIEXT, "in");
35     napi_value property = nullptr;
36     napi_status status = napi_create_uint32(env, value, &property);
37     if (status != napi_ok) {
38         TLOGE(WmsLogTag::WMS_UIEXT, "Failed to call napi_create_uint32");
39         return status;
40     }
41     status = napi_set_named_property(env, obj, name.c_str(), property);
42     if (status != napi_ok) {
43         TLOGE(WmsLogTag::WMS_UIEXT, "Failed to call napi_set_named_property");
44         return status;
45     }
46     return status;
47 }
48 
ExportRectChangeReasonType(napi_env env)49 static napi_value ExportRectChangeReasonType(napi_env env)
50 {
51     TLOGD(WmsLogTag::WMS_UIEXT, "in");
52     napi_value result = nullptr;
53     napi_create_object(env, &result);
54     if (result == nullptr) {
55         TLOGE(WmsLogTag::WMS_UIEXT, "Failed to create object");
56         return nullptr;
57     }
58 
59     static_cast<void>(SetNamedProperty(env, result, "HOST_WINDOW_RECT_CHANGE", HOST_WINDOW_RECT_CHANGE));
60 
61     napi_object_freeze(env, result);
62     return result;
63 }
64 
ExtensionModuleInit(napi_env env,napi_value exportObj)65 napi_value ExtensionModuleInit(napi_env env, napi_value exportObj)
66 {
67     TLOGD(WmsLogTag::WMS_UIEXT, "in");
68     if (env == nullptr || exportObj == nullptr) {
69         TLOGE(WmsLogTag::WMS_UIEXT, "env or exportObj is nullptr");
70         return nullptr;
71     }
72     std::unique_ptr<ExtensionManager> extensionManager = std::make_unique<ExtensionManager>();
73     napi_wrap(env, exportObj, extensionManager.release(), ExtensionManager::Finalizer, nullptr, nullptr);
74 
75     napi_set_named_property(env, exportObj, "RectChangeReason", ExportRectChangeReasonType(env));
76 
77     napi_value result = nullptr;
78     napi_get_undefined(env, &result);
79     return result;
80 }
81 } // namespace Rosen
82 } // namespace OHOS