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 "js_pip_window_listener.h"
17 
18 #include "js_pip_controller.h"
19 #include "window_manager_hilog.h"
20 #include "picture_in_picture_manager.h"
21 
22 namespace OHOS {
23 namespace Rosen {
24 using namespace AbilityRuntime;
25 
CallJsFunction(napi_env env,napi_value method,napi_value const * argv,size_t argc)26 static napi_value CallJsFunction(napi_env env, napi_value method, napi_value const * argv, size_t argc)
27 {
28     if (env == nullptr || method == nullptr) {
29         TLOGE(WmsLogTag::WMS_PIP, "env nullptr or method is nullptr");
30         return nullptr;
31     }
32     napi_value result = nullptr;
33     napi_value callResult = nullptr;
34     napi_get_undefined(env, &result);
35     napi_get_undefined(env, &callResult);
36     napi_call_function(env, result, method, argc, argv, &callResult);
37     TLOGD(WmsLogTag::WMS_PIP, "called.");
38     return callResult;
39 }
40 
~JsPiPWindowListener()41 JsPiPWindowListener::~JsPiPWindowListener()
42 {
43     TLOGI(WmsLogTag::WMS_PIP, "~JsWindowListener");
44 }
45 
GetCallbackRef() const46 std::shared_ptr<NativeReference> JsPiPWindowListener::GetCallbackRef() const
47 {
48     return jsCallBack_;
49 }
50 
OnPreparePictureInPictureStart()51 void JsPiPWindowListener::OnPreparePictureInPictureStart()
52 {
53     OnPipListenerCallback(PiPState::ABOUT_TO_START, 0);
54 }
55 
OnPictureInPictureStart()56 void JsPiPWindowListener::OnPictureInPictureStart()
57 {
58     OnPipListenerCallback(PiPState::STARTED, 0);
59 }
60 
OnPreparePictureInPictureStop()61 void JsPiPWindowListener::OnPreparePictureInPictureStop()
62 {
63     OnPipListenerCallback(PiPState::ABOUT_TO_STOP, 0);
64 }
65 
OnPictureInPictureStop()66 void JsPiPWindowListener::OnPictureInPictureStop()
67 {
68     OnPipListenerCallback(PiPState::STOPPED, 0);
69 }
70 
OnRestoreUserInterface()71 void JsPiPWindowListener::OnRestoreUserInterface()
72 {
73     OnPipListenerCallback(PiPState::ABOUT_TO_RESTORE, 0);
74 }
75 
OnPictureInPictureOperationError(int32_t errorCode)76 void JsPiPWindowListener::OnPictureInPictureOperationError(int32_t errorCode)
77 {
78     OnPipListenerCallback(PiPState::ERROR, errorCode);
79 }
80 
OnPipListenerCallback(PiPState state,int32_t errorCode)81 void JsPiPWindowListener::OnPipListenerCallback(PiPState state, int32_t errorCode)
82 {
83     TLOGI(WmsLogTag::WMS_PIP, "state: %{public}d", static_cast<int32_t>(state));
84     if (PictureInPictureManager::innerCallbackRef_ != nullptr) {
85         napi_value value[] = { CreateJsValue(env_, static_cast<uint32_t>(state))};
86         CallJsFunction(env_, PictureInPictureManager::innerCallbackRef_->GetNapiValue(), value, ArraySize(value));
87     }
88     auto napiTask = [jsCallback = jsCallBack_, state, errorCode, env = env_]() {
89         napi_value argv[] = {CreateJsValue(env, static_cast<uint32_t>(state)), CreateJsValue(env, errorCode)};
90         CallJsFunction(env, jsCallback->GetNapiValue(), argv, ArraySize(argv));
91     };
92     if (env_ != nullptr) {
93         napi_status ret = napi_send_event(env_, napiTask, napi_eprio_immediate);
94         if (ret != napi_status::napi_ok) {
95             TLOGE(WmsLogTag::WMS_PIP, "Failed to SendEvent");
96         }
97     } else {
98         TLOGE(WmsLogTag::WMS_PIP, "env is nullptr");
99     }
100 }
101 
OnActionEvent(const std::string & actionEvent,int32_t statusCode)102 void JsPiPWindowListener::OnActionEvent(const std::string& actionEvent, int32_t statusCode)
103 {
104     TLOGI(WmsLogTag::WMS_PIP, "called, actionEvent: %{public}s", actionEvent.c_str());
105     auto napiTask = [jsCallback = jsCallBack_, actionEvent, statusCode, env = env_]() {
106         napi_value argv[] = {CreateJsValue(env, actionEvent), CreateJsValue(env, statusCode)};
107         CallJsFunction(env, jsCallback->GetNapiValue(), argv, ArraySize(argv));
108     };
109     if (env_ != nullptr) {
110         napi_status ret = napi_send_event(env_, napiTask, napi_eprio_immediate);
111         if (ret != napi_status::napi_ok) {
112             TLOGE(WmsLogTag::WMS_PIP, "Failed to SendEvent");
113         }
114     } else {
115         TLOGE(WmsLogTag::WMS_PIP, "env is nullptr");
116     }
117 }
118 
OnControlEvent(PiPControlType controlType,PiPControlStatus statusCode)119 void JsPiPWindowListener::OnControlEvent(PiPControlType controlType, PiPControlStatus statusCode)
120 {
121     TLOGI(WmsLogTag::WMS_PIP, "controlType:%{public}u, statusCode:%{public}d", controlType, statusCode);
122     auto napiTask = [jsCallback = jsCallBack_, controlType, statusCode, env = env_]() {
123         napi_value propertyValue = nullptr;
124         napi_create_object(env, &propertyValue);
125         if (propertyValue == nullptr) {
126             TLOGE(WmsLogTag::WMS_PIP, "propertyValue is nullptr");
127             return;
128         }
129         napi_set_named_property(env, propertyValue, "controlType", CreateJsValue(env, controlType));
130         napi_set_named_property(env, propertyValue, "status", CreateJsValue(env, statusCode));
131         napi_value argv[] = {propertyValue};
132         CallJsFunction(env, jsCallback->GetNapiValue(), argv, ArraySize(argv));
133     };
134     if (env_ != nullptr) {
135         napi_status ret = napi_send_event(env_, napiTask, napi_eprio_immediate);
136         if (ret != napi_status::napi_ok) {
137             TLOGE(WmsLogTag::WMS_PIP, "Failed to SendEvent");
138         }
139     } else {
140         TLOGE(WmsLogTag::WMS_PIP, "env is nullptr");
141     }
142 }
143 } // namespace Rosen
144 } // namespace OHOS