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)35 static sptr<Window> CreateWindowWithSession(sptr<WindowOption>& option,
36 const std::shared_ptr<OHOS::AbilityRuntime::Context>& context, WMError& errCode,
37 sptr<ISession> iSession = nullptr)
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);
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 = 0;
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->RemoveWindowFlag(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 sptr<WindowImpl> windowImpl = new(std::nothrow) WindowImpl(option);
96 if (windowImpl == nullptr) {
97 WLOGFE("malloc windowImpl failed");
98 return nullptr;
99 }
100 WMError error = windowImpl->Create(option->GetParentId(), context);
101 if (error != WMError::WM_OK) {
102 errCode = error;
103 return nullptr;
104 }
105 return windowImpl;
106 }
107
Create(sptr<WindowOption> & option,const std::shared_ptr<OHOS::AbilityRuntime::Context> & context,const sptr<IRemoteObject> & iSession,WMError & errCode)108 sptr<Window> Window::Create(sptr<WindowOption>& option, const std::shared_ptr<OHOS::AbilityRuntime::Context>& context,
109 const sptr<IRemoteObject>& iSession, WMError& errCode)
110 {
111 // create from ability mgr service
112 if (!iSession || !option) {
113 WLOGFE("host window session is nullptr: %{public}u or option is null: %{public}u",
114 iSession == nullptr, option == nullptr);
115 return nullptr;
116 }
117 if (option->GetWindowName().empty()) {
118 WLOGFE("window name in option is empty");
119 return nullptr;
120 }
121 WindowType type = option->GetWindowType();
122 if (!(WindowHelper::IsAppWindow(type) || WindowHelper::IsUIExtensionWindow(type)
123 || WindowHelper::IsAppComponentWindow(type))) {
124 WLOGFE("window type is invalid %{public}d", type);
125 return nullptr;
126 }
127 return CreateWindowWithSession(option, context, errCode, iface_cast<Rosen::ISession>(iSession));
128 }
129
Find(const std::string & windowName)130 sptr<Window> Window::Find(const std::string& windowName)
131 {
132 if (SceneBoardJudgement::IsSceneBoardEnabled()) {
133 return WindowSessionImpl::Find(windowName);
134 } else {
135 return WindowImpl::Find(windowName);
136 }
137 }
138
GetTopWindowWithContext(const std::shared_ptr<AbilityRuntime::Context> & context)139 sptr<Window> Window::GetTopWindowWithContext(const std::shared_ptr<AbilityRuntime::Context>& context)
140 {
141 if (SceneBoardJudgement::IsSceneBoardEnabled()) {
142 return WindowSceneSessionImpl::GetTopWindowWithContext(context);
143 } else {
144 return WindowImpl::GetTopWindowWithContext(context);
145 }
146 }
147
GetTopWindowWithId(uint32_t mainWinId)148 sptr<Window> Window::GetTopWindowWithId(uint32_t mainWinId)
149 {
150 if (SceneBoardJudgement::IsSceneBoardEnabled()) {
151 return WindowSceneSessionImpl::GetTopWindowWithId(mainWinId);
152 } else {
153 return WindowImpl::GetTopWindowWithId(mainWinId);
154 }
155 }
156
GetSubWindow(uint32_t parentId)157 std::vector<sptr<Window>> Window::GetSubWindow(uint32_t parentId)
158 {
159 if (SceneBoardJudgement::IsSceneBoardEnabled()) {
160 return WindowSessionImpl::GetSubWindow(parentId);
161 } else {
162 return WindowImpl::GetSubWindow(parentId);
163 }
164 }
165
UpdateConfigurationForAll(const std::shared_ptr<AppExecFwk::Configuration> & configuration)166 void Window::UpdateConfigurationForAll(const std::shared_ptr<AppExecFwk::Configuration>& configuration)
167 {
168 if (SceneBoardJudgement::IsSceneBoardEnabled()) {
169 WindowSceneSessionImpl::UpdateConfigurationForAll(configuration);
170 RootScene::UpdateConfigurationForAll(configuration);
171 } else {
172 WindowImpl::UpdateConfigurationForAll(configuration);
173 }
174 }
175 } // namespace Rosen
176 } // namespace OHOS
177