• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 "js_pip_utils.h"
17 
18 #include <string>
19 #include "wm_common.h"
20 #include "window_manager_hilog.h"
21 
22 namespace OHOS {
23 namespace Rosen {
NapiGetUndefined(napi_env env)24 napi_value NapiGetUndefined(napi_env env)
25 {
26     napi_value result = nullptr;
27     napi_get_undefined(env, &result);
28     return result;
29 }
30 
NapiThrowInvalidParam(napi_env env,std::string msg)31 napi_value NapiThrowInvalidParam(napi_env env, std::string msg)
32 {
33     napi_throw(env, AbilityRuntime::CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM), msg));
34     return NapiGetUndefined(env);
35 }
36 
NapiIsCallable(napi_env env,napi_value value)37 bool NapiIsCallable(napi_env env, napi_value value)
38 {
39     bool result = false;
40     napi_is_callable(env, value, &result);
41     return result;
42 }
43 
GetType(napi_env env,napi_value value)44 napi_valuetype GetType(napi_env env, napi_value value)
45 {
46     napi_valuetype res = napi_undefined;
47     napi_typeof(env, value, &res);
48     return res;
49 }
50 
SetNamedProperty(napi_env env,napi_value & obj,const std::string & name,int32_t value)51 static napi_status SetNamedProperty(napi_env env, napi_value& obj, const std::string& name, int32_t value)
52 {
53     napi_value property = nullptr;
54     napi_status status = napi_create_int32(env, value, &property);
55     if (status != napi_ok) {
56         return status;
57     }
58     status = napi_set_named_property(env, obj, name.c_str(), property);
59     if (status != napi_ok) {
60         return status;
61     }
62     return status;
63 }
64 
ExportPictureInPictureTemplateType(napi_env env)65 static napi_value ExportPictureInPictureTemplateType(napi_env env)
66 {
67     napi_value result = nullptr;
68     napi_create_object(env, &result);
69     if (result == nullptr) {
70         TLOGE(WmsLogTag::WMS_PIP, "Failed to get object");
71         return nullptr;
72     }
73     (void)SetNamedProperty(env, result, "VIDEO_PLAY", static_cast<int32_t>(PiPTemplateType::VIDEO_PLAY));
74     (void)SetNamedProperty(env, result, "VIDEO_CALL", static_cast<int32_t>(PiPTemplateType::VIDEO_CALL));
75     (void)SetNamedProperty(env, result, "VIDEO_MEETING", static_cast<int32_t>(PiPTemplateType::VIDEO_MEETING));
76     (void)SetNamedProperty(env, result, "VIDEO_LIVE", static_cast<int32_t>(PiPTemplateType::VIDEO_LIVE));
77     napi_object_freeze(env, result);
78     return result;
79 }
80 
ExportPictureInPictureState(napi_env env)81 static napi_value ExportPictureInPictureState(napi_env env)
82 {
83     napi_value result = nullptr;
84     napi_create_object(env, &result);
85     if (result == nullptr) {
86         TLOGE(WmsLogTag::WMS_PIP, "Failed to get object");
87         return nullptr;
88     }
89     (void)SetNamedProperty(env, result, "ABOUT_TO_START", static_cast<int32_t>(PiPState::ABOUT_TO_START));
90     (void)SetNamedProperty(env, result, "STARTED", static_cast<int32_t>(PiPState::STARTED));
91     (void)SetNamedProperty(env, result, "ABOUT_TO_STOP", static_cast<int32_t>(PiPState::ABOUT_TO_STOP));
92     (void)SetNamedProperty(env, result, "STOPPED", static_cast<int32_t>(PiPState::STOPPED));
93     (void)SetNamedProperty(env, result, "ABOUT_TO_RESTORE", static_cast<int32_t>(PiPState::ABOUT_TO_RESTORE));
94     (void)SetNamedProperty(env, result, "ERROR", static_cast<int32_t>(PiPState::ERROR));
95     napi_object_freeze(env, result);
96     return result;
97 }
98 
ExportVideoPlayControlGroup(napi_env env)99 static napi_value ExportVideoPlayControlGroup(napi_env env)
100 {
101     napi_value result = nullptr;
102     napi_create_object(env, &result);
103     if (result == nullptr) {
104         TLOGE(WmsLogTag::WMS_PIP, "Failed to get object");
105         return nullptr;
106     }
107     (void)SetNamedProperty(env, result, "VIDEO_PREVIOUS_NEXT",
108         static_cast<uint32_t>(PiPControlGroup::VIDEO_PREVIOUS_NEXT));
109     (void)SetNamedProperty(env, result, "FAST_FORWARD_BACKWARD",
110         static_cast<uint32_t>(PiPControlGroup::FAST_FORWARD_BACKWARD));
111     napi_object_freeze(env, result);
112     return result;
113 }
114 
ExportVideoCallControlGroup(napi_env env)115 static napi_value ExportVideoCallControlGroup(napi_env env)
116 {
117     napi_value result = nullptr;
118     napi_create_object(env, &result);
119     if (result == nullptr) {
120         TLOGE(WmsLogTag::WMS_PIP, "Failed to get object");
121         return nullptr;
122     }
123     (void)SetNamedProperty(env, result, "MICROPHONE_SWITCH",
124         static_cast<uint32_t>(PiPControlGroup::VIDEO_CALL_MICROPHONE_SWITCH));
125     (void)SetNamedProperty(env, result, "HANG_UP_BUTTON",
126         static_cast<uint32_t>(PiPControlGroup::VIDEO_CALL_HANG_UP_BUTTON));
127     (void)SetNamedProperty(env, result, "CAMERA_SWITCH",
128         static_cast<uint32_t>(PiPControlGroup::VIDEO_CALL_CAMERA_SWITCH));
129     (void)SetNamedProperty(env, result, "MUTE_SWITCH",
130         static_cast<uint32_t>(PiPControlGroup::VIDEO_CALL_MUTE_SWITCH));
131     napi_object_freeze(env, result);
132     return result;
133 }
134 
ExportVideoMeetingControlGroup(napi_env env)135 static napi_value ExportVideoMeetingControlGroup(napi_env env)
136 {
137     napi_value result = nullptr;
138     napi_create_object(env, &result);
139     if (result == nullptr) {
140         TLOGE(WmsLogTag::WMS_PIP, "Failed to get object");
141         return nullptr;
142     }
143     (void)SetNamedProperty(env, result, "HANG_UP_BUTTON",
144         static_cast<uint32_t>(PiPControlGroup::VIDEO_MEETING_HANG_UP_BUTTON));
145     (void)SetNamedProperty(env, result, "CAMERA_SWITCH",
146         static_cast<uint32_t>(PiPControlGroup::VIDEO_MEETING_CAMERA_SWITCH));
147     (void)SetNamedProperty(env, result, "MUTE_SWITCH",
148         static_cast<uint32_t>(PiPControlGroup::VIDEO_MEETING_MUTE_SWITCH));
149     (void)SetNamedProperty(env, result, "MICROPHONE_SWITCH",
150         static_cast<uint32_t>(PiPControlGroup::VIDEO_MEETING_MICROPHONE_SWITCH));
151     napi_object_freeze(env, result);
152     return result;
153 }
154 
ExportVideoLiveControlGroup(napi_env env)155 static napi_value ExportVideoLiveControlGroup(napi_env env)
156 {
157     napi_value result = nullptr;
158     napi_create_object(env, &result);
159     if (result == nullptr) {
160         TLOGE(WmsLogTag::WMS_PIP, "Failed to get object");
161         return nullptr;
162     }
163     (void)SetNamedProperty(env, result, "VIDEO_PLAY_PAUSE",
164         static_cast<uint32_t>(PiPControlGroup::VIDEO_PLAY_PAUSE));
165     (void)SetNamedProperty(env, result, "MUTE_SWITCH",
166         static_cast<uint32_t>(PiPControlGroup::VIDEO_LIVE_MUTE_SWITCH));
167     napi_object_freeze(env, result);
168     return result;
169 }
170 
ExportControlStatus(napi_env env)171 static napi_value ExportControlStatus(napi_env env)
172 {
173     napi_value result = nullptr;
174     napi_create_object(env, &result);
175     if (result == nullptr) {
176         TLOGE(WmsLogTag::WMS_PIP, "Failed to get object");
177         return nullptr;
178     }
179     (void)SetNamedProperty(env, result, "PLAY",
180         static_cast<int32_t>(PiPControlStatus::PLAY));
181     (void)SetNamedProperty(env, result, "PAUSE",
182         static_cast<int32_t>(PiPControlStatus::PAUSE));
183     (void)SetNamedProperty(env, result, "OPEN",
184         static_cast<int32_t>(PiPControlStatus::OPEN));
185     (void)SetNamedProperty(env, result, "CLOSE",
186         static_cast<int32_t>(PiPControlStatus::CLOSE));
187     (void)SetNamedProperty(env, result, "ENABLED",
188         static_cast<int32_t>(PiPControlStatus::ENABLED));
189     (void)SetNamedProperty(env, result, "DISABLED",
190         static_cast<int32_t>(PiPControlStatus::DISABLED));
191     napi_object_freeze(env, result);
192     return result;
193 }
194 
ExportControlType(napi_env env)195 static napi_value ExportControlType(napi_env env)
196 {
197     napi_value result = nullptr;
198     napi_create_object(env, &result);
199     if (result == nullptr) {
200         TLOGE(WmsLogTag::WMS_PIP, "Failed to get object");
201         return nullptr;
202     }
203     (void)SetNamedProperty(env, result, "VIDEO_PLAY_PAUSE",
204         static_cast<uint32_t>(PiPControlType::VIDEO_PLAY_PAUSE));
205     (void)SetNamedProperty(env, result, "VIDEO_PREVIOUS",
206         static_cast<uint32_t>(PiPControlType::VIDEO_PREVIOUS));
207     (void)SetNamedProperty(env, result, "VIDEO_NEXT",
208         static_cast<uint32_t>(PiPControlType::VIDEO_NEXT));
209     (void)SetNamedProperty(env, result, "FAST_FORWARD",
210         static_cast<uint32_t>(PiPControlType::FAST_FORWARD));
211     (void)SetNamedProperty(env, result, "FAST_BACKWARD",
212         static_cast<uint32_t>(PiPControlType::FAST_BACKWARD));
213     (void)SetNamedProperty(env, result, "HANG_UP_BUTTON",
214         static_cast<uint32_t>(PiPControlType::HANG_UP_BUTTON));
215     (void)SetNamedProperty(env, result, "MICROPHONE_SWITCH",
216         static_cast<uint32_t>(PiPControlType::MICROPHONE_SWITCH));
217     (void)SetNamedProperty(env, result, "CAMERA_SWITCH",
218         static_cast<uint32_t>(PiPControlType::CAMERA_SWITCH));
219     (void)SetNamedProperty(env, result, "MUTE_SWITCH",
220         static_cast<uint32_t>(PiPControlType::MUTE_SWITCH));
221     napi_object_freeze(env, result);
222     return result;
223 }
224 
InitEnums(napi_env env,napi_value exports)225 napi_status InitEnums(napi_env env, napi_value exports)
226 {
227     const napi_property_descriptor properties[] = {
228         DECLARE_NAPI_PROPERTY("PiPTemplateType", ExportPictureInPictureTemplateType(env)),
229         DECLARE_NAPI_PROPERTY("PiPState", ExportPictureInPictureState(env)),
230         DECLARE_NAPI_PROPERTY("VideoPlayControlGroup", ExportVideoPlayControlGroup(env)),
231         DECLARE_NAPI_PROPERTY("VideoCallControlGroup", ExportVideoCallControlGroup(env)),
232         DECLARE_NAPI_PROPERTY("VideoMeetingControlGroup", ExportVideoMeetingControlGroup(env)),
233         DECLARE_NAPI_PROPERTY("PiPControlType", ExportControlType(env)),
234         DECLARE_NAPI_PROPERTY("PiPControlStatus", ExportControlStatus(env)),
235         DECLARE_NAPI_PROPERTY("VideoLiveControlGroup", ExportVideoLiveControlGroup(env)),
236     };
237     size_t count = sizeof(properties) / sizeof(napi_property_descriptor);
238     return napi_define_properties(env, exports, count, properties);
239 }
240 } // Rosen
241 } // OHOS