1 /*
2 * Copyright (c) 2021 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 "window.h"
17
18 #include "root_scene.h"
19 #include "scene_board_judgement.h"
20 #include "session/host/include/zidl/session_interface.h"
21 #include "window_adapter.h"
22 #include "window_helper.h"
23 #include "window_impl.h"
24 #include "window_session_impl.h"
25 #include "window_scene_session_impl.h"
26 #include "window_extension_session_impl.h"
27 #include "window_manager_hilog.h"
28 #include "wm_common.h"
29 #include "floating_ball_template_info.h"
30
31 namespace OHOS {
32 namespace Rosen {
33 namespace {
34 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "Window"};
35 }
36
CreateWindowWithSession(sptr<WindowOption> & option,const std::shared_ptr<OHOS::AbilityRuntime::Context> & context,WMError & errCode,sptr<ISession> iSession=nullptr,const std::string & identityToken="",bool isModuleAbilityHookEnd=false)37 static sptr<Window> CreateWindowWithSession(sptr<WindowOption>& option,
38 const std::shared_ptr<OHOS::AbilityRuntime::Context>& context, WMError& errCode,
39 sptr<ISession> iSession = nullptr, const std::string& identityToken = "", bool isModuleAbilityHookEnd = false)
40 {
41 WLOGFD("in");
42 sptr<WindowSessionImpl> windowSessionImpl = nullptr;
43 auto sessionType = option->GetWindowSessionType();
44 if (sessionType == WindowSessionType::SCENE_SESSION) {
45 windowSessionImpl = sptr<WindowSceneSessionImpl>::MakeSptr(option);
46 } else if (sessionType == WindowSessionType::EXTENSION_SESSION) {
47 option->SetWindowType(WindowType::WINDOW_TYPE_UI_EXTENSION);
48 windowSessionImpl = sptr<WindowExtensionSessionImpl>::MakeSptr(option);
49 }
50 if (windowSessionImpl == nullptr) {
51 WLOGFE("malloc windowSessionImpl failed");
52 return nullptr;
53 }
54
55 windowSessionImpl->SetWindowType(option->GetWindowType());
56 WMError error = windowSessionImpl->Create(context, iSession, identityToken, isModuleAbilityHookEnd);
57 if (error != WMError::WM_OK) {
58 errCode = error;
59 WLOGFE("error: %{public}u", static_cast<uint32_t>(errCode));
60 return nullptr;
61 }
62 return windowSessionImpl;
63 }
64
Create(const std::string & windowName,sptr<WindowOption> & option,const std::shared_ptr<OHOS::AbilityRuntime::Context> & context,WMError & errCode)65 sptr<Window> Window::Create(const std::string& windowName, sptr<WindowOption>& option,
66 const std::shared_ptr<OHOS::AbilityRuntime::Context>& context, WMError& errCode)
67 {
68 if (windowName.empty()) {
69 WLOGFE("window name is empty");
70 return nullptr;
71 }
72 if (option == nullptr) {
73 option = sptr<WindowOption>::MakeSptr();
74 }
75 uint32_t version = 10;
76 if (context != nullptr && context->GetApplicationInfo() != nullptr) {
77 version = context->GetApplicationInfo()->apiCompatibleVersion;
78 }
79 // 10 ArkUI new framework support after API10
80 if (version < 10) {
81 option->AddWindowFlag(WindowFlag::WINDOW_FLAG_NEED_AVOID);
82 }
83 WindowType type = option->GetWindowType();
84 if (!(WindowHelper::IsAppWindow(type) || WindowHelper::IsSystemWindow(type))) {
85 WLOGFE("window type is invalid %{public}d", type);
86 return nullptr;
87 }
88 option->SetWindowName(windowName);
89 if (SceneBoardJudgement::IsSceneBoardEnabled()) {
90 return CreateWindowWithSession(option, context, errCode);
91 }
92 if (option->GetOnlySupportSceneBoard()) {
93 errCode = WMError::WM_ERROR_DEVICE_NOT_SUPPORT;
94 return nullptr;
95 }
96 sptr<WindowImpl> windowImpl = sptr<WindowImpl>::MakeSptr(option);
97 if (windowImpl == nullptr) {
98 WLOGFE("malloc windowImpl failed");
99 return nullptr;
100 }
101 WMError error = windowImpl->Create(option->GetParentId(), context);
102 if (error != WMError::WM_OK) {
103 errCode = error;
104 return nullptr;
105 }
106 return windowImpl;
107 }
108
Create(sptr<WindowOption> & option,const std::shared_ptr<OHOS::AbilityRuntime::Context> & context,const sptr<IRemoteObject> & iSession,WMError & errCode,const std::string & identityToken,bool isModuleAbilityHookEnd)109 sptr<Window> Window::Create(sptr<WindowOption>& option, const std::shared_ptr<OHOS::AbilityRuntime::Context>& context,
110 const sptr<IRemoteObject>& iSession, WMError& errCode, const std::string& identityToken,
111 bool isModuleAbilityHookEnd)
112 {
113 // create from ability mgr service
114 if (!iSession || !option) {
115 WLOGFE("host window session is nullptr: %{public}u or option is null: %{public}u",
116 iSession == nullptr, option == nullptr);
117 return nullptr;
118 }
119 if (option->GetWindowName().empty()) {
120 WLOGFE("window name in option is empty");
121 return nullptr;
122 }
123 uint32_t version = 10;
124 if ((context != nullptr) && (context->GetApplicationInfo() != nullptr)) {
125 version = context->GetApplicationInfo()->apiCompatibleVersion;
126 }
127 // 10 ArkUI new framework support after API10
128 if (version < 10) {
129 option->AddWindowFlag(WindowFlag::WINDOW_FLAG_NEED_AVOID);
130 }
131 WindowType type = option->GetWindowType();
132 if (!(WindowHelper::IsAppWindow(type) || WindowHelper::IsUIExtensionWindow(type) ||
133 WindowHelper::IsAppComponentWindow(type))) {
134 WLOGFE("window type is invalid %{public}d", type);
135 return nullptr;
136 }
137 return CreateWindowWithSession(option, context, errCode,
138 iface_cast<Rosen::ISession>(iSession), identityToken, isModuleAbilityHookEnd);
139 }
140
GetAndVerifyWindowTypeForArkUI(uint32_t parentId,const std::string & windowName,WindowType parentWindowType,WindowType & windowType)141 WMError Window::GetAndVerifyWindowTypeForArkUI(uint32_t parentId, const std::string& windowName,
142 WindowType parentWindowType, WindowType& windowType)
143 {
144 if (SceneBoardJudgement::IsSceneBoardEnabled()) {
145 return WindowSceneSessionImpl::GetAndVerifyWindowTypeForArkUI(parentId, windowName,
146 parentWindowType, windowType);
147 } else {
148 return WindowImpl::GetWindowTypeForArkUI(parentWindowType, windowType);
149 }
150 }
151
CreatePiP(sptr<WindowOption> & option,const PiPTemplateInfo & pipTemplateInfo,const std::shared_ptr<OHOS::AbilityRuntime::Context> & context,WMError & errCode)152 sptr<Window> Window::CreatePiP(sptr<WindowOption>& option, const PiPTemplateInfo& pipTemplateInfo,
153 const std::shared_ptr<OHOS::AbilityRuntime::Context>& context, WMError& errCode)
154 {
155 if (!SceneBoardJudgement::IsSceneBoardEnabled()) {
156 return nullptr;
157 }
158 if (!option) {
159 TLOGE(WmsLogTag::WMS_PIP, "option is null.");
160 return nullptr;
161 }
162 if (option->GetWindowName().empty()) {
163 TLOGE(WmsLogTag::WMS_PIP, "the window name of option is empty.");
164 return nullptr;
165 }
166 if (!WindowHelper::IsPipWindow(option->GetWindowType())) {
167 TLOGE(WmsLogTag::WMS_PIP, "window type is not pip window.");
168 return nullptr;
169 }
170 sptr<WindowSessionImpl> windowSessionImpl = sptr<WindowSceneSessionImpl>::MakeSptr(option);
171 if (windowSessionImpl == nullptr) {
172 TLOGE(WmsLogTag::WMS_PIP, "malloc windowSessionImpl failed.");
173 return nullptr;
174 }
175 windowSessionImpl->GetProperty()->SetPiPTemplateInfo(pipTemplateInfo);
176 WMError error = windowSessionImpl->Create(context, nullptr);
177 if (error != WMError::WM_OK) {
178 errCode = error;
179 TLOGW(WmsLogTag::WMS_PIP, "Create pip window, error: %{public}u", static_cast<uint32_t>(errCode));
180 return nullptr;
181 }
182 return windowSessionImpl;
183 }
184
CreateFb(sptr<WindowOption> & option,const FloatingBallTemplateBaseInfo & fbTemplateBaseInfo,const std::shared_ptr<Media::PixelMap> & icon,const std::shared_ptr<OHOS::AbilityRuntime::Context> & context,WMError & errCode)185 sptr<Window> Window::CreateFb(sptr<WindowOption>& option, const FloatingBallTemplateBaseInfo& fbTemplateBaseInfo,
186 const std::shared_ptr<Media::PixelMap>& icon, const std::shared_ptr<OHOS::AbilityRuntime::Context>& context,
187 WMError& errCode)
188 {
189 if (!SceneBoardJudgement::IsSceneBoardEnabled()) {
190 return nullptr;
191 }
192 if (!option) {
193 TLOGE(WmsLogTag::WMS_SYSTEM, "option is null.");
194 return nullptr;
195 }
196 if (option->GetWindowName().empty()) {
197 TLOGE(WmsLogTag::WMS_SYSTEM, "the window name of option is empty.");
198 return nullptr;
199 }
200 if (!WindowHelper::IsFbWindow(option->GetWindowType())) {
201 TLOGE(WmsLogTag::WMS_SYSTEM, "window type is not fb window.");
202 return nullptr;
203 }
204 sptr<WindowSessionImpl> windowSessionImpl = sptr<WindowSceneSessionImpl>::MakeSptr(option);
205 if (windowSessionImpl == nullptr) {
206 TLOGE(WmsLogTag::WMS_SYSTEM, "malloc windowSessionImpl failed.");
207 return nullptr;
208 }
209 FloatingBallTemplateInfo fbTemplateInfo = FloatingBallTemplateInfo(fbTemplateBaseInfo, icon);
210 windowSessionImpl->GetProperty()->SetFbTemplateInfo(fbTemplateInfo);
211 WMError error = windowSessionImpl->Create(context, nullptr);
212 if (error != WMError::WM_OK) {
213 errCode = error;
214 TLOGW(WmsLogTag::WMS_SYSTEM, "Create fb window, error: %{public}u", static_cast<uint32_t>(errCode));
215 return nullptr;
216 }
217 return windowSessionImpl;
218 }
219
Find(const std::string & windowName)220 sptr<Window> Window::Find(const std::string& windowName)
221 {
222 if (SceneBoardJudgement::IsSceneBoardEnabled()) {
223 return WindowSessionImpl::Find(windowName);
224 } else {
225 return WindowImpl::Find(windowName);
226 }
227 }
228
GetParentMainWindowId(uint32_t windowId)229 uint32_t Window::GetParentMainWindowId(uint32_t windowId)
230 {
231 if (SceneBoardJudgement::IsSceneBoardEnabled()) {
232 return static_cast<uint32_t>(WindowSceneSessionImpl::GetParentMainWindowId(windowId));
233 } else {
234 return INVALID_SESSION_ID;
235 }
236 }
237
GetTopWindowWithContext(const std::shared_ptr<AbilityRuntime::Context> & context)238 sptr<Window> Window::GetTopWindowWithContext(const std::shared_ptr<AbilityRuntime::Context>& context)
239 {
240 if (SceneBoardJudgement::IsSceneBoardEnabled()) {
241 return WindowSceneSessionImpl::GetTopWindowWithContext(context);
242 } else {
243 return WindowImpl::GetTopWindowWithContext(context);
244 }
245 }
246
GetTopWindowWithId(uint32_t mainWinId)247 sptr<Window> Window::GetTopWindowWithId(uint32_t mainWinId)
248 {
249 if (SceneBoardJudgement::IsSceneBoardEnabled()) {
250 return WindowSceneSessionImpl::GetTopWindowWithId(mainWinId);
251 } else {
252 return WindowImpl::GetTopWindowWithId(mainWinId);
253 }
254 }
255
GetWindowWithId(uint32_t windId)256 sptr<Window> Window::GetWindowWithId(uint32_t windId)
257 {
258 if (SceneBoardJudgement::IsSceneBoardEnabled()) {
259 return WindowSceneSessionImpl::GetWindowWithId(windId);
260 } else {
261 return WindowImpl::GetWindowWithId(windId);
262 }
263 }
264
GetMainWindowWithContext(const std::shared_ptr<AbilityRuntime::Context> & context)265 sptr<Window> Window::GetMainWindowWithContext(const std::shared_ptr<AbilityRuntime::Context>& context)
266 {
267 if (SceneBoardJudgement::IsSceneBoardEnabled()) {
268 return WindowSceneSessionImpl::GetMainWindowWithContext(context);
269 } else {
270 return nullptr;
271 }
272 }
273
GetSubWindow(uint32_t parentId)274 std::vector<sptr<Window>> Window::GetSubWindow(uint32_t parentId)
275 {
276 if (SceneBoardJudgement::IsSceneBoardEnabled()) {
277 return WindowSessionImpl::GetSubWindow(parentId);
278 } else {
279 return WindowImpl::GetSubWindow(parentId);
280 }
281 }
282
UpdateConfigurationForAll(const std::shared_ptr<AppExecFwk::Configuration> & configuration,const std::vector<std::shared_ptr<AbilityRuntime::Context>> & ignoreWindowContexts)283 void Window::UpdateConfigurationForAll(const std::shared_ptr<AppExecFwk::Configuration>& configuration,
284 const std::vector<std::shared_ptr<AbilityRuntime::Context>>& ignoreWindowContexts)
285 {
286 if (SceneBoardJudgement::IsSceneBoardEnabled()) {
287 WindowSceneSessionImpl::UpdateConfigurationForAll(configuration, ignoreWindowContexts);
288 RootScene::UpdateConfigurationForAll(configuration, ignoreWindowContexts);
289 WindowExtensionSessionImpl::UpdateConfigurationForAll(configuration, ignoreWindowContexts);
290 } else {
291 WindowImpl::UpdateConfigurationForAll(configuration, ignoreWindowContexts);
292 }
293 }
294
UpdateConfigurationSyncForAll(const std::shared_ptr<AppExecFwk::Configuration> & configuration)295 void Window::UpdateConfigurationSyncForAll(const std::shared_ptr<AppExecFwk::Configuration>& configuration)
296 {
297 if (SceneBoardJudgement::IsSceneBoardEnabled()) {
298 WindowSceneSessionImpl::UpdateConfigurationSyncForAll(configuration);
299 RootScene::UpdateConfigurationSyncForAll(configuration);
300 WindowExtensionSessionImpl::UpdateConfigurationSyncForAll(configuration);
301 } else {
302 WindowImpl::UpdateConfigurationSyncForAll(configuration);
303 }
304 }
305 } // namespace Rosen
306 } // namespace OHOS
307