1 /*
2 * Copyright (c) 2024 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_manager_impl.h"
17 #include "ability.h"
18 #include "ability_context.h"
19 #include "accesstoken_kit.h"
20 #include "bundle_constants.h"
21 #include "display_manager.h"
22 #include "ipc_skeleton.h"
23 #include "singleton_container.h"
24 #include "window_helper.h"
25 #include "window_impl.h"
26 #include "window_manager.h"
27 #include "window_manager_hilog.h"
28 #include "window_utils.h"
29
30 namespace OHOS {
31 namespace Rosen {
32 using namespace AbilityRuntime;
33
CheckCallingPermission(std::string permission)34 bool CheckCallingPermission(std::string permission)
35 {
36 TLOGD(WmsLogTag::WMS_SUB, "Permission: %{public}s", permission.c_str());
37 if (!permission.empty() &&
38 Security::AccessToken::AccessTokenKit::VerifyAccessToken(IPCSkeleton::GetCallingTokenID(), permission)
39 != AppExecFwk::Constants::PERMISSION_GRANTED) {
40 TLOGE(WmsLogTag::WMS_SUB, "Permission %{public}s is not granted", permission.c_str());
41 return false;
42 }
43 return true;
44 }
45
CreateNewSystemWindow(OHOS::AbilityRuntime::Context * ctx,sptr<WindowOption> windowOption,int64_t * windowId)46 static int32_t CreateNewSystemWindow(OHOS::AbilityRuntime::Context* ctx,
47 sptr<WindowOption> windowOption, int64_t* windowId)
48 {
49 if (windowOption == nullptr) {
50 TLOGE(WmsLogTag::WMS_SUB, "New window option failed");
51 return static_cast<int32_t>(WmErrorCode::WM_ERROR_SYSTEM_ABNORMALLY);
52 }
53 if (ctx == nullptr) {
54 TLOGE(WmsLogTag::WMS_SUB, "Context is nullptr");
55 return static_cast<int32_t>(WmErrorCode::WM_ERROR_CONTEXT_ABNORMALLY);
56 }
57 auto sctx = ctx->shared_from_this();
58 auto context = std::weak_ptr<AbilityRuntime::Context>(sctx);
59 if (windowOption->GetWindowType() == WindowType::WINDOW_TYPE_FLOAT ||
60 windowOption->GetWindowType() == WindowType::WINDOW_TYPE_FLOAT_CAMERA) {
61 auto abilityContext = Context::ConvertTo<AbilityRuntime::AbilityContext>(context.lock());
62 if (abilityContext != nullptr) {
63 if (!CheckCallingPermission("ohos.permission.SYSTEM_FLOAT_WINDOW")) {
64 TLOGE(WmsLogTag::WMS_SUB, "TYPE_FLOAT CheckCallingPermission failed");
65 return static_cast<int32_t>(WmErrorCode::WM_ERROR_NO_PERMISSION);
66 }
67 }
68 }
69 WMError wmError = WMError::WM_OK;
70 sptr<Window> window = Window::Create(windowOption->GetWindowName(), windowOption, context.lock(), wmError);
71 WmErrorCode wmErrorCode = WM_JS_TO_ERROR_CODE_MAP.at(wmError);
72 if (window != nullptr && wmErrorCode == WmErrorCode::WM_OK) {
73 auto instance = CreateCjWindowObject(window);
74 if (!instance) {
75 return static_cast<int32_t>(WMError::WM_ERROR_NO_MEM);
76 }
77
78 *windowId = instance->GetID();
79 }
80 return static_cast<int32_t>(wmErrorCode);
81 }
82
CreateWindow(WindowParameters window)83 int32_t WindowManagerImpl::CreateWindow(WindowParameters window)
84 {
85 WindowOption option;
86 option.SetWindowName(window.name);
87 if (window.winType >= static_cast<uint32_t>(ApiWindowType::TYPE_BASE) &&
88 window.winType < static_cast<uint32_t>(ApiWindowType::TYPE_END)) {
89 option.SetWindowType(CJ_TO_NATIVE_WINDOW_TYPE_MAP.at(static_cast<ApiWindowType>(window.winType)));
90 } else {
91 option.SetWindowType(static_cast<WindowType>(window.winType));
92 }
93 if (window.displayId < 0 ||
94 SingletonContainer::Get<DisplayManager>().GetDisplayById(static_cast<uint64_t>(window.displayId)) == nullptr) {
95 TLOGE(WmsLogTag::WMS_SUB, "Failed to parse config");
96 return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM);
97 }
98 option.SetDisplayId(window.displayId);
99 option.SetParentId(window.parentId);
100 sptr<WindowOption> windowOption = new(std::nothrow) WindowOption(option);
101 if (WindowHelper::IsSystemWindow(option.GetWindowType())) {
102 TLOGI(WmsLogTag::WMS_SUB, "CreateNewSystemWindow");
103 return CreateNewSystemWindow(window.context, windowOption, window.windowId);
104 }
105 return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM);
106 }
107
SetWindowLayoutMode(uint32_t mode)108 int32_t WindowManagerImpl::SetWindowLayoutMode(uint32_t mode)
109 {
110 WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(
111 SingletonContainer::Get<WindowManager>().SetWindowLayoutMode(WindowLayoutMode(mode)));
112 return static_cast<int32_t>(ret);
113 }
114
MinimizeAll(int64_t displayId)115 int32_t WindowManagerImpl::MinimizeAll(int64_t displayId)
116 {
117 if (displayId < 0 ||
118 SingletonContainer::Get<DisplayManager>().GetDisplayById(static_cast<uint64_t>(displayId)) == nullptr) {
119 return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM);
120 }
121 WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(
122 SingletonContainer::Get<WindowManager>().MinimizeAllAppWindows(static_cast<uint64_t>(displayId)));
123 return static_cast<int32_t>(ret);
124 }
125
FindWindow(std::string name,int64_t & windowId)126 int32_t WindowManagerImpl::FindWindow(std::string name, int64_t& windowId)
127 {
128 sptr<CJWindowImpl> windowImpl = FindCjWindowObject(name);
129 if (windowImpl != nullptr) {
130 windowId = windowImpl->GetID();
131 return WINDOW_SUCCESS;
132 } else {
133 sptr<Window> window = Window::Find(name);
134 if (window == nullptr) {
135 return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
136 } else {
137 sptr<CJWindowImpl> newWindow = CreateCjWindowObject(window);
138 if (newWindow == nullptr) {
139 return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
140 }
141 windowId = newWindow->GetID();
142 return WINDOW_SUCCESS;
143 }
144 }
145 }
146
147 /** @note @window.hierarchy */
GetLastWindow(OHOS::AbilityRuntime::Context * ctx,int64_t & id)148 int32_t WindowManagerImpl::GetLastWindow(OHOS::AbilityRuntime::Context* ctx, int64_t& id)
149 {
150 sptr<Window> window = nullptr;
151 if (ctx == nullptr) {
152 TLOGE(WmsLogTag::WMS_HIERARCHY, "[WindowManager]Stage mode without context");
153 return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
154 }
155 auto sctx = ctx->shared_from_this();
156 auto context = std::weak_ptr<AbilityRuntime::Context>(sctx);
157 if (sctx == nullptr) {
158 TLOGE(WmsLogTag::WMS_HIERARCHY, "[WindowManager]Stage mode without context");
159 return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
160 }
161 window = Window::GetTopWindowWithContext(context.lock());
162 if (window == nullptr) {
163 TLOGE(WmsLogTag::WMS_HIERARCHY, "[WindowManager]Get top window failed");
164 return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
165 }
166 return FindWindow(window->GetWindowName(), id);
167 }
168
ShiftAppWindowFocus(int32_t sourceWindowId,int32_t targetWindowId)169 int32_t WindowManagerImpl::ShiftAppWindowFocus(int32_t sourceWindowId,
170 int32_t targetWindowId)
171 {
172 WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(
173 SingletonContainer::Get<WindowManager>().ShiftAppWindowFocus(sourceWindowId, targetWindowId));
174 if (ret != WmErrorCode::WM_OK) {
175 TLOGE(WmsLogTag::WMS_FOCUS, "Shift application window focus failed");
176 }
177 return static_cast<int32_t>(ret);
178 }
179 }
180 }
181