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