1 /*
2 * Copyright (c) 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 "singleton_container.h"
17 #include "picture_in_picture_manager.h"
18 #include "web_picture_in_picture_controller.h"
19 #include "window_manager_hilog.h"
20 #include "window_scene_session_impl.h"
21
22 namespace OHOS {
23 namespace Rosen {
24 namespace {
25 const std::string WEB_PIP_CONTENT_PATH = "/system/etc/window/resources/web_pip_content.abc";
26 }
27
WebPictureInPictureController(const PiPConfig & config)28 WebPictureInPictureController::WebPictureInPictureController(const PiPConfig& config)
29 {
30 pipOption_ = sptr<PipOption>::MakeSptr();
31 pipOption_->SetPipTemplate(config.pipTemplateType);
32 pipOption_->SetContentSize(config.width, config.height);
33 pipOption_->SetControlGroup(config.controlGroup);
34 mainWindowId_ = config.mainWindowId;
35 mainWindow_ = WindowSceneSessionImpl::GetMainWindowWithId(config.mainWindowId);
36 env_ = config.env;
37 curState_ = PiPWindowState::STATE_UNDEFINED;
38 isWeb_ = true;
39 weakRef_ = this;
40 }
41
CreatePictureInPictureWindow(StartPipType startType)42 WMError WebPictureInPictureController::CreatePictureInPictureWindow(StartPipType startType)
43 {
44 if (pipOption_ == nullptr || mainWindow_ == nullptr) {
45 TLOGE(WmsLogTag::WMS_PIP, "pipOption or mainWindow is nullptr");
46 return WMError::WM_ERROR_PIP_CREATE_FAILED;
47 }
48 TLOGI(WmsLogTag::WMS_PIP, "mainWindow:%{public}u, mainWindowState:%{public}u",
49 mainWindowId_, mainWindow_->GetWindowState());
50 mainWindowLifeCycleListener_ = sptr<PictureInPictureControllerBase::WindowLifeCycleListener>::MakeSptr();
51 mainWindow_->RegisterLifeCycleListener(mainWindowLifeCycleListener_);
52 if (mainWindow_->GetWindowState() != WindowState::STATE_SHOWN) {
53 TLOGE(WmsLogTag::WMS_PIP, "mainWindow is not shown. create failed.");
54 return WMError::WM_ERROR_PIP_CREATE_FAILED;
55 }
56 UpdateWinRectByComponent();
57 auto windowOption = sptr<WindowOption>::MakeSptr();
58 windowOption->SetWindowName(PIP_WINDOW_NAME);
59 windowOption->SetWindowType(WindowType::WINDOW_TYPE_PIP);
60 windowOption->SetWindowMode(WindowMode::WINDOW_MODE_PIP);
61 windowOption->SetWindowRect(windowRect_);
62 windowOption->SetKeepScreenOn(true);
63 windowOption->SetTouchable(false);
64 WMError errCode = WMError::WM_OK;
65 PiPTemplateInfo pipTemplateInfo;
66 pipOption_->GetPiPTemplateInfo(pipTemplateInfo);
67 auto context = mainWindow_->GetContext();
68 SingletonContainer::Get<PiPReporter>().SetCurrentPackageName(context->GetApplicationInfo()->name);
69 sptr<Window> window = Window::CreatePiP(windowOption, pipTemplateInfo, context, errCode);
70 if (window == nullptr || errCode != WMError::WM_OK) {
71 TLOGW(WmsLogTag::WMS_PIP, "Window create failed, reason: %{public}d", errCode);
72 return WMError::WM_ERROR_PIP_CREATE_FAILED;
73 }
74 window_ = window;
75 window_->UpdatePiPRect(windowRect_, WindowSizeChangeReason::PIP_START);
76 PictureInPictureManager::PutPipControllerInfo(window_->GetWindowId(), this);
77 return WMError::WM_OK;
78 }
79
StartPictureInPicture(StartPipType startType)80 WMError WebPictureInPictureController::StartPictureInPicture(StartPipType startType)
81 {
82 TLOGI(WmsLogTag::WMS_PIP, "called");
83 if (pipOption_ == nullptr) {
84 TLOGE(WmsLogTag::WMS_PIP, "pipOption is null");
85 return WMError::WM_ERROR_PIP_CREATE_FAILED;
86 }
87 if (curState_ == PiPWindowState::STATE_STARTING || curState_ == PiPWindowState::STATE_STARTED) {
88 TLOGW(WmsLogTag::WMS_PIP, "pipWindow is starting, state: %{public}u, id: %{public}u, mainWindow: %{public}u",
89 curState_, (window_ == nullptr) ? INVALID_WINDOW_ID : window_->GetWindowId(), mainWindowId_);
90 SingletonContainer::Get<PiPReporter>().ReportPiPStartWindow(static_cast<int32_t>(startType),
91 pipOption_->GetPipTemplate(), PipConst::FAILED, "Pip window is starting");
92 return WMError::WM_ERROR_PIP_REPEAT_OPERATION;
93 }
94 curState_ = PiPWindowState::STATE_STARTING;
95 PictureInPictureManager::DoClose(true, true);
96 return StartPictureInPictureInner(startType);
97 }
98
SetUIContent() const99 void WebPictureInPictureController::SetUIContent() const
100 {
101 napi_value storage = nullptr;
102 napi_ref storageRef = pipOption_->GetStorageRef();
103 if (storageRef != nullptr) {
104 napi_get_reference_value(env_, storageRef, &storage);
105 TLOGI(WmsLogTag::WMS_PIP, "startPiP with localStorage");
106 }
107 window_->SetUIContentByAbc(WEB_PIP_CONTENT_PATH, env_, storage, nullptr);
108 }
109
UpdateContentSize(int32_t width,int32_t height)110 void WebPictureInPictureController::UpdateContentSize(int32_t width, int32_t height)
111 {
112 if (width <= 0 || height <= 0) {
113 TLOGE(WmsLogTag::WMS_PIP, "invalid size");
114 return;
115 }
116 if (pipOption_ == nullptr) {
117 TLOGE(WmsLogTag::WMS_PIP, "pipOption is nullptr");
118 return;
119 }
120 pipOption_->SetContentSize(static_cast<uint32_t>(width), static_cast<uint32_t>(height));
121 if (curState_ != PiPWindowState::STATE_STARTED) {
122 TLOGD(WmsLogTag::WMS_PIP, "UpdateContentSize is disabled when state: %{public}u", curState_);
123 return;
124 }
125 if (window_ == nullptr) {
126 TLOGE(WmsLogTag::WMS_PIP, "pipWindow not exist");
127 return;
128 }
129 TLOGI(WmsLogTag::WMS_PIP, "UpdateContentSize window: %{public}u width:%{public}u height:%{public}u",
130 window_->GetWindowId(), width, height);
131 Rect rect = {0, 0, width, height};
132 window_->UpdatePiPRect(rect, WindowSizeChangeReason::PIP_RATIO_CHANGE);
133 SingletonContainer::Get<PiPReporter>().ReportPiPRatio(width, height);
134 }
135
SetPipInitialSurfaceRect(int32_t positionX,int32_t positionY,uint32_t width,uint32_t height)136 void WebPictureInPictureController::SetPipInitialSurfaceRect(int32_t positionX, int32_t positionY,
137 uint32_t width, uint32_t height)
138 {
139 initPositionX_ = positionX;
140 initPositionY_ = positionY;
141 initWidth_ = width;
142 initHeight_ = height;
143 }
144
RestorePictureInPictureWindow()145 void WebPictureInPictureController::RestorePictureInPictureWindow()
146 {
147 StopPictureInPicture(true, StopPipType::NULL_STOP, false);
148 SingletonContainer::Get<PiPReporter>().ReportPiPRestore();
149 TLOGI(WmsLogTag::WMS_PIP, "restore pip main window finished");
150 }
151
UpdateWinRectByComponent()152 void WebPictureInPictureController::UpdateWinRectByComponent()
153 {
154 uint32_t contentWidth = 0;
155 uint32_t contentHeight = 0;
156 pipOption_->GetContentSize(contentWidth, contentHeight);
157 windowRect_.width_ = contentWidth;
158 windowRect_.height_ = contentHeight;
159 if (initWidth_ > 0 && initHeight_ > 0) {
160 windowRect_.width_ = initWidth_;
161 windowRect_.height_ = initHeight_;
162 }
163 windowRect_.posX_ = initPositionX_;
164 windowRect_.posY_ = initPositionY_;
165 TLOGD(WmsLogTag::WMS_PIP, "position width: %{public}u, height: %{public}u, posX: %{public}d, posY: %{public}d",
166 windowRect_.width_, windowRect_.height_, windowRect_.posX_, windowRect_.posY_);
167 }
168
UpdatePiPSourceRect() const169 void WebPictureInPictureController::UpdatePiPSourceRect() const
170 {
171 Rect rect = { 0, 0, 0, 0 };
172 TLOGI(WmsLogTag::WMS_PIP, "result rect: [%{public}d, %{public}d, %{public}u, %{public}u]",
173 rect.posX_, rect.posY_, rect.width_, rect.height_);
174 window_->UpdatePiPRect(rect, WindowSizeChangeReason::PIP_RESTORE);
175 }
176
SetXComponentController(std::shared_ptr<XComponentController> xComponentController)177 WMError WebPictureInPictureController::SetXComponentController(
178 std::shared_ptr<XComponentController> xComponentController)
179 {
180 TLOGI(WmsLogTag::WMS_PIP, "called");
181 pipXComponentController_ = xComponentController;
182 if (window_ == nullptr) {
183 TLOGE(WmsLogTag::WMS_PIP, "window is nullptr when set XComponentController");
184 return WMError::WM_ERROR_PIP_STATE_ABNORMALLY;
185 }
186 OnPictureInPictureStart();
187 return WMError::WM_OK;
188 }
189
GetWebRequestId()190 uint8_t WebPictureInPictureController::GetWebRequestId()
191 {
192 uint8_t res = webRequestId_;
193 if (webRequestId_ == UINT8_MAX) {
194 webRequestId_ = 0;
195 } else {
196 webRequestId_++;
197 }
198 return res;
199 }
200 } // namespace Rosen
201 } // namespace OHOS
202