• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025-2025 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 "web_picture_in_picture_controller_interface.h"
17 
18 #include "native_pip_window_listener.h"
19 #include "window_manager_hilog.h"
20 
21 namespace OHOS {
22 namespace Rosen {
23 
24 using namespace Ace;
25 
26 namespace {
27     constexpr uint32_t MAX_CONTROL_GROUP_NUM = 3;
28     std::shared_mutex cbSetMutex_;
29     std::shared_mutex initRectMutex_;
30     const std::set<PiPControlGroup> VIDEO_PLAY_CONTROLS {
31         PiPControlGroup::VIDEO_PREVIOUS_NEXT,
32         PiPControlGroup::FAST_FORWARD_BACKWARD,
33     };
34     const std::set<PiPControlGroup> VIDEO_CALL_CONTROLS {
35         PiPControlGroup::VIDEO_CALL_MICROPHONE_SWITCH,
36         PiPControlGroup::VIDEO_CALL_HANG_UP_BUTTON,
37         PiPControlGroup::VIDEO_CALL_CAMERA_SWITCH,
38         PiPControlGroup::VIDEO_CALL_MUTE_SWITCH,
39     };
40     const std::set<PiPControlGroup> VIDEO_MEETING_CONTROLS {
41         PiPControlGroup::VIDEO_MEETING_HANG_UP_BUTTON,
42         PiPControlGroup::VIDEO_MEETING_CAMERA_SWITCH,
43         PiPControlGroup::VIDEO_MEETING_MUTE_SWITCH,
44         PiPControlGroup::VIDEO_MEETING_MICROPHONE_SWITCH,
45     };
46     const std::set<PiPControlGroup> VIDEO_LIVE_CONTROLS {
47         PiPControlGroup::VIDEO_PLAY_PAUSE,
48         PiPControlGroup::VIDEO_LIVE_MUTE_SWITCH,
49     };
50     const std::map<PiPTemplateType, std::set<PiPControlGroup>> TEMPLATE_CONTROL_MAP {
51         {PiPTemplateType::VIDEO_PLAY, VIDEO_PLAY_CONTROLS},
52         {PiPTemplateType::VIDEO_CALL, VIDEO_CALL_CONTROLS},
53         {PiPTemplateType::VIDEO_MEETING, VIDEO_MEETING_CONTROLS},
54         {PiPTemplateType::VIDEO_LIVE, VIDEO_LIVE_CONTROLS},
55     };
56 }
57 
checkControlsRules(uint32_t pipTemplateType,const std::vector<std::uint32_t> & controlGroups)58 static int32_t checkControlsRules(uint32_t pipTemplateType, const std::vector<std::uint32_t>& controlGroups)
59 {
60     auto iter = TEMPLATE_CONTROL_MAP.find(static_cast<PiPTemplateType>(pipTemplateType));
61     if (iter == TEMPLATE_CONTROL_MAP.end()) {
62         TLOGE(WmsLogTag::WMS_PIP, "createPip param error, pipTemplateType not exists");
63         return -1;
64     }
65     auto controls = iter->second;
66     if (controlGroups.size() > MAX_CONTROL_GROUP_NUM) {
67         return -1;
68     }
69     for (auto control : controlGroups) {
70         if (controls.find(static_cast<PiPControlGroup>(control)) == controls.end()) {
71             TLOGE(WmsLogTag::WMS_PIP, "pipOption param error, controlGroup not matches, controlGroup: %{public}u",
72                 control);
73             return -1;
74         }
75     }
76     if (pipTemplateType != static_cast<uint32_t>(PiPTemplateType::VIDEO_PLAY)) {
77         return 0;
78     }
79     auto iterFirst = std::find(controlGroups.begin(), controlGroups.end(),
80         static_cast<uint32_t>(PiPControlGroup::VIDEO_PREVIOUS_NEXT));
81     auto iterSecond = std::find(controlGroups.begin(), controlGroups.end(),
82         static_cast<uint32_t>(PiPControlGroup::FAST_FORWARD_BACKWARD));
83     if (iterFirst != controlGroups.end() && iterSecond != controlGroups.end()) {
84         TLOGE(WmsLogTag::WMS_PIP, "pipOption param error, %{public}u conflicts with %{public}u in controlGroups",
85             static_cast<uint32_t>(PiPControlGroup::VIDEO_PREVIOUS_NEXT),
86             static_cast<uint32_t>(PiPControlGroup::FAST_FORWARD_BACKWARD));
87         return -1;
88     }
89     return 0;
90 }
91 
checkCreatePipParams(const PiPConfig & config)92 static int32_t checkCreatePipParams(const PiPConfig& config)
93 {
94     if (config.mainWindowId == 0) {
95         TLOGE(WmsLogTag::WMS_PIP, "mainWindowId could not be zero");
96         return -1;
97     }
98     if (config.width == 0 || config.height == 0) {
99         TLOGE(WmsLogTag::WMS_PIP, "width or height could not be zero");
100         return -1;
101     }
102     if (config.env == nullptr) {
103         TLOGE(WmsLogTag::WMS_PIP, "invalid env");
104         return -1;
105     }
106     return checkControlsRules(config.pipTemplateType, config.controlGroup);
107 }
108 
Create(const PiPConfig & pipConfig)109 WMError WebPictureInPictureControllerInterface::Create(const PiPConfig& pipConfig)
110 {
111     isPipEnabled_ = PictureInPictureControllerBase::GetPipEnabled();
112     if (!isPipEnabled_) {
113         TLOGE(WmsLogTag::WMS_PIP, "The device is not supported");
114         return WMError::WM_ERROR_DEVICE_NOT_SUPPORT;
115     }
116     int32_t ret = checkCreatePipParams(pipConfig);
117     if (ret == -1) {
118         TLOGE(WmsLogTag::WMS_PIP, "Invalid parameters when createPipController, "
119             "please check if mainWindowId/width/height is zero, "
120             "or env is nullptr, or controlGroup mismatch the corresponding pipTemplateType");
121         return WMError::WM_ERROR_INVALID_PARAM;
122     }
123     sptrWebPipController_ = sptr<WebPictureInPictureController>::MakeSptr(pipConfig);
124     return WMError::WM_OK;
125 }
126 
StartPip(uint32_t controllerId)127 WMError WebPictureInPictureControllerInterface::StartPip(uint32_t controllerId)
128 {
129     if (auto pipController = sptrWebPipController_) {
130         sptrWebPipController_->SetControllerId(controllerId);
131         auto res = sptrWebPipController_->StartPictureInPicture(StartPipType::NATIVE_START);
132         if (res == WMError::WM_OK) {
133             isStarted_ = true;
134         }
135         return res;
136     } else {
137         TLOGE(WmsLogTag::WMS_PIP, "webPipController is nullptr");
138         return WMError::WM_ERROR_PIP_INTERNAL_ERROR;
139     }
140 }
141 
StopPip()142 WMError WebPictureInPictureControllerInterface::StopPip()
143 {
144     if (auto pipController = sptrWebPipController_) {
145         auto res = pipController->StopPictureInPictureFromClient();
146         if (res == WMError::WM_OK) {
147             isStarted_ = false;
148         }
149         return res;
150     } else {
151         TLOGE(WmsLogTag::WMS_PIP, "webPipController is nullptr");
152         return WMError::WM_ERROR_PIP_INTERNAL_ERROR;
153     }
154 }
155 
UpdateContentSize(int32_t width,int32_t height)156 WMError WebPictureInPictureControllerInterface::UpdateContentSize(int32_t width, int32_t height)
157 {
158     if (width < 1 || height < 1) {
159         TLOGE(WmsLogTag::WMS_PIP, "invalid width or height");
160         return WMError::WM_ERROR_INVALID_PARAM;
161     }
162     if (auto pipController = sptrWebPipController_) {
163         pipController->UpdateContentSize(width, height);
164         return WMError::WM_OK;
165     } else {
166         TLOGE(WmsLogTag::WMS_PIP, "webPipController is nullptr");
167         return WMError::WM_ERROR_PIP_INTERNAL_ERROR;
168     }
169 }
170 
UpdatePiPControlStatus(PiPControlType controlType,PiPControlStatus status)171 WMError WebPictureInPictureControllerInterface::UpdatePiPControlStatus(PiPControlType controlType,
172     PiPControlStatus status)
173 {
174     if (auto pipController = sptrWebPipController_) {
175         pipController->UpdatePiPControlStatus(controlType, status);
176         return WMError::WM_OK;
177     } else {
178         TLOGE(WmsLogTag::WMS_PIP, "webPipController is nullptr");
179         return WMError::WM_ERROR_PIP_INTERNAL_ERROR;
180     }
181 }
182 
setPiPControlEnabled(PiPControlType controlType,bool enabled)183 WMError WebPictureInPictureControllerInterface::setPiPControlEnabled(PiPControlType controlType, bool enabled)
184 {
185     if (auto pipController = sptrWebPipController_) {
186         pipController->UpdatePiPControlStatus(controlType,
187             enabled ? PiPControlStatus::ENABLED : PiPControlStatus::DISABLED);
188         return WMError::WM_OK;
189     } else {
190         TLOGE(WmsLogTag::WMS_PIP, "webPipController is nullptr");
191         return WMError::WM_ERROR_PIP_INTERNAL_ERROR;
192     }
193 }
194 
SetPipInitialSurfaceRect(int32_t positionX,int32_t positionY,uint32_t width,uint32_t height)195 WMError WebPictureInPictureControllerInterface::SetPipInitialSurfaceRect(int32_t positionX, int32_t positionY,
196     uint32_t width, uint32_t height)
197 {
198     if (width <= 0 || height <= 0) {
199         TLOGE(WmsLogTag::WMS_PIP, "invalid initial rect");
200         return WMError::WM_ERROR_INVALID_PARAM;
201     }
202     if (auto pipController = sptrWebPipController_) {
203         std::unique_lock<std::shared_mutex> lock(initRectMutex_);
204         pipController->SetPipInitialSurfaceRect(positionX, positionY, width, height);
205         return WMError::WM_OK;
206     } else {
207         TLOGE(WmsLogTag::WMS_PIP, "webPipController is nullptr");
208         return WMError::WM_ERROR_PIP_INTERNAL_ERROR;
209     }
210 }
211 
UnsetPipInitialSurfaceRect()212 WMError WebPictureInPictureControllerInterface::UnsetPipInitialSurfaceRect()
213 {
214     if (auto pipController = sptrWebPipController_) {
215         std::unique_lock<std::shared_mutex> lock(initRectMutex_);
216         pipController->SetPipInitialSurfaceRect(0, 0, 0, 0);
217         return WMError::WM_OK;
218     } else {
219         TLOGE(WmsLogTag::WMS_PIP, "webPipController is nullptr");
220         return WMError::WM_ERROR_PIP_INTERNAL_ERROR;
221     }
222 }
223 
RegisterStartPipListener(NativePipStartPipCallback callback)224 WMError WebPictureInPictureControllerInterface::RegisterStartPipListener(NativePipStartPipCallback callback)
225 {
226     if (!isPipEnabled_) {
227         TLOGE(WmsLogTag::WMS_PIP, "The device is not supported");
228         return WMError::WM_ERROR_DEVICE_NOT_SUPPORT;
229     }
230     if (callback == nullptr) {
231         TLOGE(WmsLogTag::WMS_PIP, "callback is null");
232         return WMError::WM_ERROR_INVALID_PARAM;
233     }
234     auto listener = sptr<NativePiPWindowListener>::MakeSptr(callback);
235     return RegisterListenerWithType(ListenerType::PIP_START_CB, listener);
236 }
237 
UnregisterStartPipListener(NativePipStartPipCallback callback)238 WMError WebPictureInPictureControllerInterface::UnregisterStartPipListener(NativePipStartPipCallback callback)
239 {
240     if (!isPipEnabled_) {
241         TLOGE(WmsLogTag::WMS_PIP, "The device is not supported");
242         return WMError::WM_ERROR_DEVICE_NOT_SUPPORT;
243     }
244     if (callback == nullptr) {
245         TLOGE(WmsLogTag::WMS_PIP, "callback is null");
246         return WMError::WM_ERROR_INVALID_PARAM;
247     }
248     auto listener = sptr<NativePiPWindowListener>::MakeSptr(callback);
249     return UnregisterListenerWithType(ListenerType::PIP_START_CB, listener);
250 }
251 
252 
RegisterLifeCycleListener(NativePipLifeCycleCallback callback)253 WMError WebPictureInPictureControllerInterface::RegisterLifeCycleListener(NativePipLifeCycleCallback callback)
254 {
255     if (!isPipEnabled_) {
256         TLOGE(WmsLogTag::WMS_PIP, "The device is not supported");
257         return WMError::WM_ERROR_DEVICE_NOT_SUPPORT;
258     }
259     if (callback == nullptr) {
260         TLOGE(WmsLogTag::WMS_PIP, "callback is null");
261         return WMError::WM_ERROR_INVALID_PARAM;
262     }
263     auto listener = sptr<NativePiPWindowListener>::MakeSptr(callback);
264     return RegisterListenerWithType(ListenerType::STATE_CHANGE_CB, listener);
265 }
266 
UnregisterLifeCycleListener(NativePipLifeCycleCallback callback)267 WMError WebPictureInPictureControllerInterface::UnregisterLifeCycleListener(NativePipLifeCycleCallback callback)
268 {
269     if (!isPipEnabled_) {
270         TLOGE(WmsLogTag::WMS_PIP, "The device is not supported");
271         return WMError::WM_ERROR_DEVICE_NOT_SUPPORT;
272     }
273     if (callback == nullptr) {
274         TLOGE(WmsLogTag::WMS_PIP, "callback is null");
275         return WMError::WM_ERROR_INVALID_PARAM;
276     }
277     auto listener = sptr<NativePiPWindowListener>::MakeSptr(callback);
278     return UnregisterListenerWithType(ListenerType::STATE_CHANGE_CB, listener);
279 }
280 
RegisterControlEventListener(NativePipControlEventCallback callback)281 WMError WebPictureInPictureControllerInterface::RegisterControlEventListener(NativePipControlEventCallback callback)
282 {
283     if (!isPipEnabled_) {
284         TLOGE(WmsLogTag::WMS_PIP, "The device is not supported");
285         return WMError::WM_ERROR_DEVICE_NOT_SUPPORT;
286     }
287     if (callback == nullptr) {
288         TLOGE(WmsLogTag::WMS_PIP, "callback is null");
289         return WMError::WM_ERROR_INVALID_PARAM;
290     }
291     auto listener = sptr<NativePiPWindowListener>::MakeSptr(callback);
292     return RegisterListenerWithType(ListenerType::CONTROL_EVENT_CB, listener);
293 }
294 
UnregisterControlEventListener(NativePipControlEventCallback callback)295 WMError WebPictureInPictureControllerInterface::UnregisterControlEventListener(NativePipControlEventCallback callback)
296 {
297     if (!isPipEnabled_) {
298         TLOGE(WmsLogTag::WMS_PIP, "The device is not supported");
299         return WMError::WM_ERROR_DEVICE_NOT_SUPPORT;
300     }
301     if (callback == nullptr) {
302         TLOGE(WmsLogTag::WMS_PIP, "callback is null");
303         return WMError::WM_ERROR_INVALID_PARAM;
304     }
305     auto listener = sptr<NativePiPWindowListener>::MakeSptr(callback);
306     return UnregisterListenerWithType(ListenerType::CONTROL_EVENT_CB, listener);
307 }
308 
RegisterResizeListener(NativePipResizeCallback callback)309 WMError WebPictureInPictureControllerInterface::RegisterResizeListener(NativePipResizeCallback callback)
310 {
311     if (!isPipEnabled_) {
312         TLOGE(WmsLogTag::WMS_PIP, "The device is not supported");
313         return WMError::WM_ERROR_DEVICE_NOT_SUPPORT;
314     }
315     if (callback == nullptr) {
316         TLOGE(WmsLogTag::WMS_PIP, "callback is null");
317         return WMError::WM_ERROR_INVALID_PARAM;
318     }
319     auto listener = sptr<NativePiPWindowListener>::MakeSptr(callback);
320     return RegisterListenerWithType(ListenerType::SIZE_CHANGE_CB, listener);
321 }
322 
UnregisterResizeListener(NativePipResizeCallback callback)323 WMError WebPictureInPictureControllerInterface::UnregisterResizeListener(NativePipResizeCallback callback)
324 {
325     if (!isPipEnabled_) {
326         TLOGE(WmsLogTag::WMS_PIP, "The device is not supported");
327         return WMError::WM_ERROR_DEVICE_NOT_SUPPORT;
328     }
329     if (callback == nullptr) {
330         TLOGE(WmsLogTag::WMS_PIP, "callback is null");
331         return WMError::WM_ERROR_INVALID_PARAM;
332     }
333     auto listener = sptr<NativePiPWindowListener>::MakeSptr(callback);
334     return UnregisterListenerWithType(ListenerType::SIZE_CHANGE_CB, listener);
335 }
336 
CheckRegisterParam(const sptr<NativePiPWindowListener> & listener)337 WMError WebPictureInPictureControllerInterface::CheckRegisterParam(const sptr<NativePiPWindowListener>& listener)
338 {
339     if (sptrWebPipController_ == nullptr) {
340         TLOGE(WmsLogTag::WMS_PIP, "WebPipController is nullptr");
341         return WMError::WM_ERROR_PIP_INTERNAL_ERROR;
342     }
343     if (listener == nullptr) {
344         TLOGE(WmsLogTag::WMS_PIP, "New NativePiPWindowListener failed");
345         return WMError::WM_ERROR_PIP_STATE_ABNORMALLY;
346     }
347     return WMError::WM_OK;
348 }
349 
RegisterListenerWithType(ListenerType type,const sptr<NativePiPWindowListener> & listener)350 WMError WebPictureInPictureControllerInterface::RegisterListenerWithType(ListenerType type,
351     const sptr<NativePiPWindowListener>& listener)
352 {
353     WMError ret = WMError::WM_OK;
354     if ((ret = CheckRegisterParam(listener)) != WMError::WM_OK) {
355         return ret;
356     }
357     if (IsRegistered(type, listener)) {
358         TLOGE(WmsLogTag::WMS_PIP, "Listener already registered");
359         return WMError::WM_ERROR_INVALID_PARAM;
360     }
361     uint32_t cbMapSize = 0;
362     {
363         std::unique_lock<std::shared_mutex> lock(cbSetMutex_);
364         switch (type) {
365             case ListenerType::STATE_CHANGE_CB:
366                 ret = ProcessStateChangeRegister(listener);
367                 cbMapSize = lifeCycleCallbackSet_.size();
368                 break;
369             case ListenerType::CONTROL_EVENT_CB:
370                 ret = ProcessControlEventRegister(listener);
371                 cbMapSize = controlEventCallbackSet_.size();
372                 break;
373             case ListenerType::SIZE_CHANGE_CB:
374                 ret = ProcessSizeChangeRegister(listener);
375                 cbMapSize = resizeCallbackSet_.size();
376                 break;
377             case ListenerType::PIP_START_CB:
378                 ret = ProcessPipStartRegister(listener);
379                 cbMapSize = startPipCallbackSet_.size();
380                 break;
381             default:
382                 TLOGE(WmsLogTag::WMS_PIP, "Listener type not found");
383                 return WMError::WM_ERROR_INVALID_PARAM;
384         }
385     }
386     if (ret != WMError::WM_OK) {
387         TLOGE(WmsLogTag::WMS_PIP, "Register type %{public}u failed", type);
388         return ret;
389     }
390     TLOGI(WmsLogTag::WMS_PIP, "Register type %{public}u success! callback map size: %{public}d", type, cbMapSize);
391     return WMError::WM_OK;
392 }
393 
UnregisterListenerWithType(ListenerType type,const sptr<NativePiPWindowListener> & listener)394 WMError WebPictureInPictureControllerInterface::UnregisterListenerWithType(ListenerType type,
395     const sptr<NativePiPWindowListener>& listener)
396 {
397     WMError ret = WMError::WM_OK;
398     if ((ret = CheckRegisterParam(listener)) != WMError::WM_OK) {
399         return ret;
400     }
401     if (!IsRegistered(type, listener)) {
402         TLOGE(WmsLogTag::WMS_PIP, "Listener not registered");
403         return WMError::WM_ERROR_INVALID_PARAM;
404     }
405     uint32_t cbMapSize = 0;
406     {
407         std::unique_lock<std::shared_mutex> lock(cbSetMutex_);
408         switch (type) {
409             case ListenerType::STATE_CHANGE_CB:
410                 ret = ProcessStateChangeUnregister(listener);
411                 cbMapSize = lifeCycleCallbackSet_.size();
412                 break;
413             case ListenerType::CONTROL_EVENT_CB:
414                 ret = ProcessControlEventUnregister(listener);
415                 cbMapSize = controlEventCallbackSet_.size();
416                 break;
417             case ListenerType::SIZE_CHANGE_CB:
418                 ret = ProcessSizeChangeUnregister(listener);
419                 cbMapSize = resizeCallbackSet_.size();
420                 break;
421             case ListenerType::PIP_START_CB:
422                 ret = ProcessPipStartUnregister(listener);
423                 cbMapSize = startPipCallbackSet_.size();
424                 break;
425             default:
426                 TLOGE(WmsLogTag::WMS_PIP, "Listener type not found");
427                 return WMError::WM_ERROR_INVALID_PARAM;
428         }
429     }
430     if (ret != WMError::WM_OK) {
431         TLOGE(WmsLogTag::WMS_PIP, "Unregister type %{public}u failed", type);
432         return ret;
433     }
434     TLOGI(WmsLogTag::WMS_PIP, "Unregister type %{public}u success! callback map size: %{public}d", type, cbMapSize);
435     return WMError::WM_OK;
436 }
437 
UnregisterAllPiPLifecycle()438 WMError WebPictureInPictureControllerInterface::UnregisterAllPiPLifecycle()
439 {
440     return UnregisterAll(ListenerType::STATE_CHANGE_CB);
441 }
442 
UnregisterAllPiPControlObserver()443 WMError WebPictureInPictureControllerInterface::UnregisterAllPiPControlObserver()
444 {
445     return UnregisterAll(ListenerType::CONTROL_EVENT_CB);
446 }
447 
UnregisterAllPiPWindowSize()448 WMError WebPictureInPictureControllerInterface::UnregisterAllPiPWindowSize()
449 {
450     return UnregisterAll(ListenerType::SIZE_CHANGE_CB);
451 }
452 
UnregisterAllPiPStart()453 WMError WebPictureInPictureControllerInterface::UnregisterAllPiPStart()
454 {
455     return UnregisterAll(ListenerType::PIP_START_CB);
456 }
457 
UnregisterAll(ListenerType type)458 WMError WebPictureInPictureControllerInterface::UnregisterAll(ListenerType type)
459 {
460     if (!isPipEnabled_) {
461         TLOGE(WmsLogTag::WMS_PIP, "The device is not supported");
462         return WMError::WM_ERROR_DEVICE_NOT_SUPPORT;
463     }
464     if (auto pipController = sptrWebPipController_) {
465         std::unique_lock<std::shared_mutex> lock(cbSetMutex_);
466         switch (type) {
467             case ListenerType::STATE_CHANGE_CB:
468                 lifeCycleCallbackSet_.clear();
469                 pipController->UnregisterAllPiPLifecycle();
470                 break;
471             case ListenerType::CONTROL_EVENT_CB:
472                 controlEventCallbackSet_.clear();
473                 pipController->UnregisterAllPiPControlObserver();
474                 break;
475             case ListenerType::SIZE_CHANGE_CB:
476                 resizeCallbackSet_.clear();
477                 pipController->UnregisterAllPiPWindowSize();
478                 break;
479             case ListenerType::PIP_START_CB:
480                 startPipCallbackSet_.clear();
481                 pipController->UnregisterAllPiPStart();
482                 break;
483             default:
484                 TLOGE(WmsLogTag::WMS_PIP, "Listener type not found");
485                 return WMError::WM_ERROR_INVALID_PARAM;
486         }
487         return WMError::WM_OK;
488     } else {
489         TLOGE(WmsLogTag::WMS_PIP, "webPipController is nullptr");
490         return WMError::WM_ERROR_PIP_INTERNAL_ERROR;
491     }
492 }
493 
IsRegistered(ListenerType type,const sptr<NativePiPWindowListener> & listener)494 bool WebPictureInPictureControllerInterface::IsRegistered(ListenerType type,
495     const sptr<NativePiPWindowListener>& listener)
496 {
497     std::shared_lock<std::shared_mutex> lock(cbSetMutex_);
498     switch (type) {
499         case ListenerType::STATE_CHANGE_CB:
500             for (const auto& it : lifeCycleCallbackSet_) {
501                 if (it->GetLifeCycleCallbackRef() == listener->GetLifeCycleCallbackRef()) {
502                     return true;
503                 }
504             }
505             break;
506         case ListenerType::CONTROL_EVENT_CB:
507             for (const auto& it : controlEventCallbackSet_) {
508                 if (it->GetControlEventCallbackRef() == listener->GetControlEventCallbackRef()) {
509                     return true;
510                 }
511             }
512             break;
513         case ListenerType::SIZE_CHANGE_CB:
514             for (const auto& it : resizeCallbackSet_) {
515                 if (it->GetResizeCallbackRef() == listener->GetResizeCallbackRef()) {
516                     return true;
517                 }
518             }
519             break;
520         case ListenerType::PIP_START_CB:
521             for (const auto& it : startPipCallbackSet_) {
522                 if (it->GetStartPipCallbackRef() == listener->GetStartPipCallbackRef()) {
523                     return true;
524                 }
525             }
526             break;
527         default:
528             break;
529     }
530     return false;
531 }
532 
ProcessStateChangeRegister(const sptr<NativePiPWindowListener> & listener)533 WMError WebPictureInPictureControllerInterface::ProcessStateChangeRegister(
534     const sptr<NativePiPWindowListener>& listener)
535 {
536     sptr<IPiPLifeCycle> thisListener(listener);
537     WMError ret = sptrWebPipController_->RegisterPiPLifecycle(thisListener);
538     if (ret != WMError::WM_OK) {
539         TLOGE(WmsLogTag::WMS_PIP, "register lifeCycleCallback failed");
540         return ret;
541     }
542     lifeCycleCallbackSet_.insert(listener);
543     return ret;
544 }
545 
ProcessStateChangeUnregister(const sptr<NativePiPWindowListener> & listener)546 WMError WebPictureInPictureControllerInterface::ProcessStateChangeUnregister(
547     const sptr<NativePiPWindowListener>& listener)
548 {
549     for (const auto& it : lifeCycleCallbackSet_) {
550         if (it->GetLifeCycleCallbackRef() != listener->GetLifeCycleCallbackRef()) {
551             continue;
552         }
553         sptr<IPiPLifeCycle> thisListener(it);
554         WMError ret = sptrWebPipController_->UnregisterPiPLifecycle(thisListener);
555         if (ret != WMError::WM_OK) {
556             TLOGE(WmsLogTag::WMS_PIP, "Unregister lifeCycle callback failed");
557             return ret;
558         }
559         lifeCycleCallbackSet_.erase(it);
560         return ret;
561     }
562     TLOGE(WmsLogTag::WMS_PIP, "lifeCycle callback not found");
563     return WMError::WM_ERROR_INVALID_PARAM;
564 }
565 
ProcessControlEventRegister(const sptr<NativePiPWindowListener> & listener)566 WMError WebPictureInPictureControllerInterface::ProcessControlEventRegister(
567     const sptr<NativePiPWindowListener>& listener)
568 {
569     sptr<IPiPControlObserver> thisListener(listener);
570     WMError ret = sptrWebPipController_->RegisterPiPControlObserver(thisListener);
571     if (ret != WMError::WM_OK) {
572         TLOGE(WmsLogTag::WMS_PIP, "register controlEventCallback failed");
573         return ret;
574     }
575     controlEventCallbackSet_.insert(listener);
576     return ret;
577 }
578 
ProcessControlEventUnregister(const sptr<NativePiPWindowListener> & listener)579 WMError WebPictureInPictureControllerInterface::ProcessControlEventUnregister(
580     const sptr<NativePiPWindowListener>& listener)
581 {
582     for (const auto& it : controlEventCallbackSet_) {
583         if (it->GetControlEventCallbackRef() != listener->GetControlEventCallbackRef()) {
584             continue;
585         }
586         sptr<IPiPControlObserver> thisListener(it);
587         WMError ret = sptrWebPipController_->UnregisterPiPControlObserver(thisListener);
588         if (ret != WMError::WM_OK) {
589             TLOGE(WmsLogTag::WMS_PIP, "Unregister controlEvent callback failed");
590             return ret;
591         }
592         controlEventCallbackSet_.erase(it);
593         return ret;
594     }
595     TLOGE(WmsLogTag::WMS_PIP, "controlEvent callback not found");
596     return WMError::WM_ERROR_INVALID_PARAM;
597 }
598 
ProcessSizeChangeRegister(const sptr<NativePiPWindowListener> & listener)599 WMError WebPictureInPictureControllerInterface::ProcessSizeChangeRegister(
600     const sptr<NativePiPWindowListener>& listener)
601 {
602     sptr<IPiPWindowSize> thisListener(listener);
603     WMError ret = sptrWebPipController_->RegisterPiPWindowSize(thisListener);
604     if (ret != WMError::WM_OK) {
605         TLOGE(WmsLogTag::WMS_PIP, "register resizeCallback failed");
606         return ret;
607     }
608     resizeCallbackSet_.insert(listener);
609     return ret;
610 }
611 
ProcessSizeChangeUnregister(const sptr<NativePiPWindowListener> & listener)612 WMError WebPictureInPictureControllerInterface::ProcessSizeChangeUnregister(
613     const sptr<NativePiPWindowListener>& listener)
614 {
615     for (const auto& it : resizeCallbackSet_) {
616         if (it->GetResizeCallbackRef() != listener->GetResizeCallbackRef()) {
617             continue;
618         }
619         sptr<IPiPWindowSize> thisListener(it);
620         WMError ret = sptrWebPipController_->UnregisterPiPWindowSize(thisListener);
621         if (ret != WMError::WM_OK) {
622             TLOGE(WmsLogTag::WMS_PIP, "Unregister resize callback failed");
623             return ret;
624         }
625         resizeCallbackSet_.erase(it);
626         return ret;
627     }
628     TLOGE(WmsLogTag::WMS_PIP, "resize callback not found");
629     return WMError::WM_ERROR_INVALID_PARAM;
630 }
631 
ProcessPipStartRegister(const sptr<NativePiPWindowListener> & listener)632 WMError WebPictureInPictureControllerInterface::ProcessPipStartRegister(
633     const sptr<NativePiPWindowListener>& listener)
634 {
635     sptr<IPiPStartObserver> thisListener(listener);
636     WMError ret =  sptrWebPipController_->RegisterPiPStart(thisListener);
637     if (ret != WMError::WM_OK) {
638         TLOGE(WmsLogTag::WMS_PIP, "register pipStartCallback failed");
639         return ret;
640     }
641     startPipCallbackSet_.insert(listener);
642     return ret;
643 }
644 
ProcessPipStartUnregister(const sptr<NativePiPWindowListener> & listener)645 WMError WebPictureInPictureControllerInterface::ProcessPipStartUnregister(
646     const sptr<NativePiPWindowListener>& listener)
647 {
648     for (const auto& it : startPipCallbackSet_) {
649         if (it->GetStartPipCallbackRef() != listener->GetStartPipCallbackRef()) {
650             continue;
651         }
652         sptr<IPiPStartObserver> thisListener(it);
653         WMError ret = sptrWebPipController_->UnregisterPiPStart(thisListener);
654         if (ret != WMError::WM_OK) {
655             TLOGE(WmsLogTag::WMS_PIP, "Unregister startPip callback failed");
656             return ret;
657         }
658         startPipCallbackSet_.erase(it);
659         return ret;
660     }
661     TLOGE(WmsLogTag::WMS_PIP, "startPip callback not found");
662     return WMError::WM_ERROR_INVALID_PARAM;
663 }
664 
665 } // namespace Rosen
666 } // namespace OHOS