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