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_helper.h"
22 #include "window_impl.h"
23 #include "window_session_impl.h"
24 #include "window_scene_session_impl.h"
25 #include "window_extension_session_impl.h"
26 #include "window_manager_hilog.h"
27 #include "wm_common.h"
28 
29 namespace OHOS {
30 namespace Rosen {
31 namespace {
32 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "Window"};
33 }
34 
CreateWindowWithSession(sptr<WindowOption>& option, const std::shared_ptr<OHOS::AbilityRuntime::Context>& context, WMError& errCode, sptr<ISession> iSession = nullptr, const std::string& identityToken = �)35 static sptr<Window> CreateWindowWithSession(sptr<WindowOption>& option,
36     const std::shared_ptr<OHOS::AbilityRuntime::Context>& context, WMError& errCode,
37     sptr<ISession> iSession = nullptr, const std::string& identityToken = "")
38 {
39     WLOGFD("in");
40     sptr<WindowSessionImpl> windowSessionImpl = nullptr;
41     auto sessionType = option->GetWindowSessionType();
42     if (sessionType == WindowSessionType::SCENE_SESSION) {
43         windowSessionImpl = sptr<WindowSceneSessionImpl>::MakeSptr(option);
44     } else if (sessionType == WindowSessionType::EXTENSION_SESSION) {
45         option->SetWindowType(WindowType::WINDOW_TYPE_UI_EXTENSION);
46         windowSessionImpl = sptr<WindowExtensionSessionImpl>::MakeSptr(option);
47     }
48     if (windowSessionImpl == nullptr) {
49         WLOGFE("malloc windowSessionImpl failed");
50         return nullptr;
51     }
52 
53     windowSessionImpl->SetWindowType(option->GetWindowType());
54     WMError error = windowSessionImpl->Create(context, iSession, identityToken);
55     if (error != WMError::WM_OK) {
56         errCode = error;
57         WLOGFE("error: %{public}u", static_cast<uint32_t>(errCode));
58         return nullptr;
59     }
60     return windowSessionImpl;
61 }
62 
Create(const std::string& windowName, sptr<WindowOption>& option, const std::shared_ptr<OHOS::AbilityRuntime::Context>& context, WMError& errCode)63 sptr<Window> Window::Create(const std::string& windowName, sptr<WindowOption>& option,
64     const std::shared_ptr<OHOS::AbilityRuntime::Context>& context, WMError& errCode)
65 {
66     if (windowName.empty()) {
67         WLOGFE("window name is empty");
68         return nullptr;
69     }
70     if (option == nullptr) {
71         option = sptr<WindowOption>::MakeSptr();
72     }
73     uint32_t version = 10;
74     if (context != nullptr && context->GetApplicationInfo() != nullptr) {
75         version = context->GetApplicationInfo()->apiCompatibleVersion;
76     }
77     // 10 ArkUI new framework support after API10
78     if (version < 10) {
79         option->AddWindowFlag(WindowFlag::WINDOW_FLAG_NEED_AVOID);
80     }
81     WindowType type = option->GetWindowType();
82     if (!(WindowHelper::IsAppWindow(type) || WindowHelper::IsSystemWindow(type))) {
83         WLOGFE("window type is invalid %{public}d", type);
84         return nullptr;
85     }
86     option->SetWindowName(windowName);
87     if (SceneBoardJudgement::IsSceneBoardEnabled()) {
88         return CreateWindowWithSession(option, context, errCode);
89     }
90     if (option->GetOnlySupportSceneBoard()) {
91         errCode = WMError::WM_ERROR_DEVICE_NOT_SUPPORT;
92         return nullptr;
93     }
94     sptr<WindowImpl> windowImpl = sptr<WindowImpl>::MakeSptr(option);
95     if (windowImpl == nullptr) {
96         WLOGFE("malloc windowImpl failed");
97         return nullptr;
98     }
99     WMError error = windowImpl->Create(option->GetParentId(), context);
100     if (error != WMError::WM_OK) {
101         errCode = error;
102         return nullptr;
103     }
104     return windowImpl;
105 }
106 
Create(sptr<WindowOption>& option, const std::shared_ptr<OHOS::AbilityRuntime::Context>& context, const sptr<IRemoteObject>& iSession, WMError& errCode, const std::string& identityToken)107 sptr<Window> Window::Create(sptr<WindowOption>& option, const std::shared_ptr<OHOS::AbilityRuntime::Context>& context,
108     const sptr<IRemoteObject>& iSession, WMError& errCode, const std::string& identityToken)
109 {
110     // create from ability mgr service
111     if (!iSession || !option) {
112         WLOGFE("host window session is nullptr: %{public}u or option is null: %{public}u",
113             iSession == nullptr, option == nullptr);
114         return nullptr;
115     }
116     if (option->GetWindowName().empty()) {
117         WLOGFE("window name in option is empty");
118         return nullptr;
119     }
120     uint32_t version = 10;
121     if ((context != nullptr) && (context->GetApplicationInfo() != nullptr)) {
122         version = context->GetApplicationInfo()->apiCompatibleVersion;
123     }
124     // 10 ArkUI new framework support after API10
125     if (version < 10) {
126         option->AddWindowFlag(WindowFlag::WINDOW_FLAG_NEED_AVOID);
127     }
128     WindowType type = option->GetWindowType();
129     if (!(WindowHelper::IsAppWindow(type) || WindowHelper::IsUIExtensionWindow(type) ||
130           WindowHelper::IsAppComponentWindow(type))) {
131         WLOGFE("window type is invalid %{public}d", type);
132         return nullptr;
133     }
134     return CreateWindowWithSession(option, context, errCode,
135         iface_cast<Rosen::ISession>(iSession), identityToken);
136 }
137 
CreatePiP(sptr<WindowOption>& option, const PiPTemplateInfo& pipTemplateInfo, const std::shared_ptr<OHOS::AbilityRuntime::Context>& context, WMError& errCode)138 sptr<Window> Window::CreatePiP(sptr<WindowOption>& option, const PiPTemplateInfo& pipTemplateInfo,
139     const std::shared_ptr<OHOS::AbilityRuntime::Context>& context, WMError& errCode)
140 {
141     if (!SceneBoardJudgement::IsSceneBoardEnabled()) {
142         return nullptr;
143     }
144     if (!option) {
145         TLOGE(WmsLogTag::WMS_PIP, "option is null.");
146         return nullptr;
147     }
148     if (option->GetWindowName().empty()) {
149         TLOGE(WmsLogTag::WMS_PIP, "the window name of option is empty.");
150         return nullptr;
151     }
152     if (!WindowHelper::IsPipWindow(option->GetWindowType())) {
153         TLOGE(WmsLogTag::WMS_PIP, "window type is not pip window.");
154         return nullptr;
155     }
156     sptr<WindowSessionImpl> windowSessionImpl = sptr<WindowSceneSessionImpl>::MakeSptr(option);
157     if (windowSessionImpl == nullptr) {
158         TLOGE(WmsLogTag::WMS_PIP, "malloc windowSessionImpl failed.");
159         return nullptr;
160     }
161     windowSessionImpl->GetProperty()->SetPiPTemplateInfo(pipTemplateInfo);
162     WMError error = windowSessionImpl->Create(context, nullptr);
163     if (error != WMError::WM_OK) {
164         errCode = error;
165         TLOGW(WmsLogTag::WMS_PIP, "Create pip window, error: %{public}u", static_cast<uint32_t>(errCode));
166         return nullptr;
167     }
168     return windowSessionImpl;
169 }
170 
Find(const std::string& windowName)171 sptr<Window> Window::Find(const std::string& windowName)
172 {
173     if (SceneBoardJudgement::IsSceneBoardEnabled()) {
174         return WindowSessionImpl::Find(windowName);
175     } else {
176         return WindowImpl::Find(windowName);
177     }
178 }
179 
GetParentMainWindowId(uint32_t windowId)180 uint32_t Window::GetParentMainWindowId(uint32_t windowId)
181 {
182     if (SceneBoardJudgement::IsSceneBoardEnabled()) {
183         return static_cast<uint32_t>(WindowSceneSessionImpl::GetParentMainWindowId(windowId));
184     } else {
185         return INVALID_SESSION_ID;
186     }
187 }
188 
GetTopWindowWithContext(const std::shared_ptr<AbilityRuntime::Context>& context)189 sptr<Window> Window::GetTopWindowWithContext(const std::shared_ptr<AbilityRuntime::Context>& context)
190 {
191     if (SceneBoardJudgement::IsSceneBoardEnabled()) {
192         return WindowSceneSessionImpl::GetTopWindowWithContext(context);
193     } else {
194         return WindowImpl::GetTopWindowWithContext(context);
195     }
196 }
197 
GetTopWindowWithId(uint32_t mainWinId)198 sptr<Window> Window::GetTopWindowWithId(uint32_t mainWinId)
199 {
200     if (SceneBoardJudgement::IsSceneBoardEnabled()) {
201         return WindowSceneSessionImpl::GetTopWindowWithId(mainWinId);
202     } else {
203         return WindowImpl::GetTopWindowWithId(mainWinId);
204     }
205 }
206 
GetWindowWithId(uint32_t windId)207 sptr<Window> Window::GetWindowWithId(uint32_t windId)
208 {
209     if (SceneBoardJudgement::IsSceneBoardEnabled()) {
210         return WindowSceneSessionImpl::GetWindowWithId(windId);
211     } else {
212         return WindowImpl::GetWindowWithId(windId);
213     }
214 }
215 
GetMainWindowWithContext(const std::shared_ptr<AbilityRuntime::Context>& context)216 sptr<Window> Window::GetMainWindowWithContext(const std::shared_ptr<AbilityRuntime::Context>& context)
217 {
218     if (SceneBoardJudgement::IsSceneBoardEnabled()) {
219         return WindowSceneSessionImpl::GetMainWindowWithContext(context);
220     } else {
221         return nullptr;
222     }
223 }
224 
GetSubWindow(uint32_t parentId)225 std::vector<sptr<Window>> Window::GetSubWindow(uint32_t parentId)
226 {
227     if (SceneBoardJudgement::IsSceneBoardEnabled()) {
228         return WindowSessionImpl::GetSubWindow(parentId);
229     } else {
230         return WindowImpl::GetSubWindow(parentId);
231     }
232 }
233 
UpdateConfigurationForAll(const std::shared_ptr<AppExecFwk::Configuration>& configuration)234 void Window::UpdateConfigurationForAll(const std::shared_ptr<AppExecFwk::Configuration>& configuration)
235 {
236     if (SceneBoardJudgement::IsSceneBoardEnabled()) {
237         WindowSceneSessionImpl::UpdateConfigurationForAll(configuration);
238         RootScene::UpdateConfigurationForAll(configuration);
239         WindowExtensionSessionImpl::UpdateConfigurationForAll(configuration);
240     } else {
241         WindowImpl::UpdateConfigurationForAll(configuration);
242     }
243 }
244 } // namespace Rosen
245 } // namespace OHOS
246