• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 enum class ExtensionEventFlag : int32_t {
25     EVENT_NONE = 0x00000000,
26     EVENT_PAN_GESTURE_LEFT = 0x00000001,
27     EVENT_PAN_GESTURE_RIGHT = 0x00000002,
28     EVENT_PAN_GESTURE_UP = 0x00000004,
29     EVENT_PAN_GESTURE_DOWN = 0x00000008,
30     EVENT_CLICK = 0x00000100,
31     EVENT_LONG_PRESS = 0x00000200,
32 };
33 }
34 
Finalizer(napi_env env,void * data,void * hint)35 void ExtensionManager::Finalizer(napi_env env, void* data, void* hint)
36 {
37     TLOGD(WmsLogTag::WMS_UIEXT, "in");
38     std::unique_ptr<ExtensionManager>(static_cast<ExtensionManager*>(data));
39 }
40 
SetNamedProperty(napi_env env,napi_value obj,const std::string & name,uint32_t value)41 static napi_status SetNamedProperty(napi_env env, napi_value obj, const std::string& name, uint32_t value)
42 {
43     TLOGD(WmsLogTag::WMS_UIEXT, "in");
44     napi_value property = nullptr;
45     napi_status status = napi_create_uint32(env, value, &property);
46     if (status != napi_ok) {
47         TLOGE(WmsLogTag::WMS_UIEXT, "Failed to call napi_create_uint32");
48         return status;
49     }
50     status = napi_set_named_property(env, obj, name.c_str(), property);
51     if (status != napi_ok) {
52         TLOGE(WmsLogTag::WMS_UIEXT, "Failed to call napi_set_named_property");
53         return status;
54     }
55     return status;
56 }
57 
ExportRectChangeReasonType(napi_env env)58 static napi_value ExportRectChangeReasonType(napi_env env)
59 {
60     TLOGD(WmsLogTag::WMS_UIEXT, "in");
61     napi_value result = nullptr;
62     napi_create_object(env, &result);
63     if (result == nullptr) {
64         TLOGE(WmsLogTag::WMS_UIEXT, "Failed to create object");
65         return nullptr;
66     }
67 
68     static_cast<void>(SetNamedProperty(env, result, "HOST_WINDOW_RECT_CHANGE", HOST_WINDOW_RECT_CHANGE));
69 
70     napi_object_freeze(env, result);
71     return result;
72 }
73 
ExportEventFlag(napi_env env)74 static napi_value ExportEventFlag(napi_env env)
75 {
76     TLOGD(WmsLogTag::WMS_UIEXT, "in");
77     napi_value result = nullptr;
78     napi_create_object(env, &result);
79     if (result == nullptr) {
80         TLOGE(WmsLogTag::WMS_UIEXT, "Failed to create object");
81         return nullptr;
82     }
83 
84     static_cast<void>(SetNamedProperty(env, result, "EVENT_NONE",
85         static_cast<int32_t>(ExtensionEventFlag::EVENT_NONE)));
86     static_cast<void>(SetNamedProperty(env, result, "EVENT_PAN_GESTURE_LEFT",
87         static_cast<int32_t>(ExtensionEventFlag::EVENT_PAN_GESTURE_LEFT)));
88     static_cast<void>(SetNamedProperty(env, result, "EVENT_PAN_GESTURE_RIGHT",
89         static_cast<int32_t>(ExtensionEventFlag::EVENT_PAN_GESTURE_RIGHT)));
90     static_cast<void>(SetNamedProperty(env, result, "EVENT_PAN_GESTURE_UP",
91         static_cast<int32_t>(ExtensionEventFlag::EVENT_PAN_GESTURE_UP)));
92     static_cast<void>(SetNamedProperty(env, result, "EVENT_PAN_GESTURE_DOWN",
93         static_cast<int32_t>(ExtensionEventFlag::EVENT_PAN_GESTURE_DOWN)));
94     static_cast<void>(SetNamedProperty(env, result, "EVENT_CLICK",
95         static_cast<int32_t>(ExtensionEventFlag::EVENT_CLICK)));
96     static_cast<void>(SetNamedProperty(env, result, "EVENT_LONG_PRESS",
97         static_cast<int32_t>(ExtensionEventFlag::EVENT_LONG_PRESS)));
98 
99     napi_object_freeze(env, result);
100     return result;
101 }
102 
ExtensionModuleInit(napi_env env,napi_value exportObj)103 napi_value ExtensionModuleInit(napi_env env, napi_value exportObj)
104 {
105     TLOGD(WmsLogTag::WMS_UIEXT, "in");
106     if (env == nullptr || exportObj == nullptr) {
107         TLOGE(WmsLogTag::WMS_UIEXT, "env or exportObj is nullptr");
108         return nullptr;
109     }
110     std::unique_ptr<ExtensionManager> extensionManager = std::make_unique<ExtensionManager>();
111     napi_wrap(env, exportObj, extensionManager.release(), ExtensionManager::Finalizer, nullptr, nullptr);
112 
113     napi_set_named_property(env, exportObj, "RectChangeReason", ExportRectChangeReasonType(env));
114     napi_set_named_property(env, exportObj, "EventFlag", ExportEventFlag(env));
115 
116     napi_value result = nullptr;
117     napi_get_undefined(env, &result);
118     return result;
119 }
120 } // namespace Rosen
121 } // namespace OHOS