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_stage_impl.h"
17 #include "permission.h"
18 #include "window_impl.h"
19 #include "window_manager_hilog.h"
20 #include "window_utils.h"
21 
22 namespace OHOS {
23 namespace Rosen {
CreateCjSubWindowArrayObject(std::vector<sptr<Window>> & vec,RetStruct & ret)24 void CreateCjSubWindowArrayObject(std::vector<sptr<Window>> &vec, RetStruct &ret)
25 {
26     int64_t *windowList = static_cast<int64_t *>(malloc(sizeof(int64_t) * vec.size()));
27     if (windowList == nullptr) {
28         ret.code = static_cast<int32_t>(WmErrorCode::WM_ERROR_SYSTEM_ABNORMALLY);
29         return;
30     }
31     ret.data = windowList;
32     ret.len = 0;
33     for (auto& window : vec) {
34         if (window == nullptr) {
35             continue;
36         }
37         sptr<CJWindowImpl> windowImpl = CreateCjWindowObject(window);
38         if (windowImpl == nullptr) {
39             TLOGE(WmsLogTag::WMS_DIALOG, "[createCjSubWindowArrayObject] windowImpl is nullptr");
40             continue;
41         }
42         windowList[ret.len] = windowImpl->GetID();
43         ret.len += 1;
44     }
45     return;
46 }
47 
GetMainWindow(int64_t & windowId)48 int32_t CJWindowStageImpl::GetMainWindow(int64_t &windowId)
49 {
50     auto weakScene = windowScene_.lock();
51     if (weakScene == nullptr) {
52         TLOGE(WmsLogTag::WMS_DIALOG, "[getMainWindow] Window scene is nullptr");
53         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
54     }
55     auto window = weakScene->GetMainWindow();
56     if (window == nullptr) {
57         TLOGE(WmsLogTag::WMS_DIALOG, "[getMainWindow] Window is null");
58         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
59     }
60     auto windowImpl = CreateCjWindowObject(window);
61     if (windowImpl == nullptr) {
62         TLOGE(WmsLogTag::WMS_DIALOG, "[getMainWindow] windowImpl is null");
63         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
64     }
65     windowId = windowImpl->GetID();
66     return static_cast<int32_t>(WmErrorCode::WM_OK);
67 }
68 
CreateSubWindow(std::string name,int64_t & windowId)69 int32_t CJWindowStageImpl::CreateSubWindow(std::string name, int64_t &windowId)
70 {
71     auto weakScene = windowScene_.lock();
72     if (weakScene == nullptr) {
73         TLOGE(WmsLogTag::WMS_DIALOG, "[createSubWindow] Window scene is nullptr");
74         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
75     }
76     sptr<Rosen::WindowOption> windowOption = new(std::nothrow) Rosen::WindowOption();
77     if (windowOption == nullptr) {
78         TLOGE(WmsLogTag::WMS_DIALOG, "[createSubWindow] Create option failed");
79         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
80     }
81     windowOption->SetWindowType(Rosen::WindowType::WINDOW_TYPE_APP_SUB_WINDOW);
82     windowOption->SetWindowMode(Rosen::WindowMode::WINDOW_MODE_FLOATING);
83     auto window = weakScene->CreateWindow(name, windowOption);
84     if (window == nullptr) {
85         TLOGE(WmsLogTag::WMS_DIALOG, "[createSubWindow] Create window failed");
86         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
87     }
88     sptr<CJWindowImpl> windowImpl = CreateCjWindowObject(window);
89     if (windowImpl == nullptr) {
90         TLOGE(WmsLogTag::WMS_DIALOG, "[createSubWindow] windowImpl is null");
91         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
92     }
93     windowId = windowImpl->GetID();
94     TLOGI(WmsLogTag::WMS_DIALOG, "[createSubWindow] Create sub window %{public}s end", name.c_str());
95     return static_cast<int32_t>(WmErrorCode::WM_OK);
96 }
97 
GetSubWindow()98 RetStruct CJWindowStageImpl::GetSubWindow()
99 {
100     RetStruct ret = {.code = static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY), .len = 0, .data = nullptr};
101     auto weakScene = windowScene_.lock();
102     if (weakScene == nullptr) {
103         TLOGE(WmsLogTag::WMS_DIALOG, "[getSubWindow] Window scene is nullptr");
104         ret.code = static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
105         return ret;
106     }
107     auto subWindowVec = weakScene->GetSubWindow();
108     CreateCjSubWindowArrayObject(subWindowVec, ret);
109     return ret;
110 }
111 
OnLoadContent(const std::string & contexUrl,const std::string & storageJson,bool isLoadedByName)112 int32_t CJWindowStageImpl::OnLoadContent(const std::string &contexUrl,
113     const std::string &storageJson, bool isLoadedByName)
114 {
115     auto weakScene = windowScene_.lock();
116     if (weakScene == nullptr) {
117         TLOGE(WmsLogTag::WMS_DIALOG, "[WindowStage] WindowScene is null");
118         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STAGE_ABNORMALLY);
119     }
120     auto window = weakScene->GetMainWindow();
121     if (window == nullptr) {
122         TLOGE(WmsLogTag::WMS_DIALOG, "[WindowStage] Window is null");
123         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
124     }
125     WMError ret;
126     if (isLoadedByName) {
127         ret = window->SetUIContentByName(contexUrl, nullptr, nullptr);
128     } else {
129         ret = window->NapiSetUIContent(contexUrl, nullptr, nullptr);
130     }
131     TLOGI(WmsLogTag::WMS_DIALOG,
132         "[WindowStage] LoadContent [%{public}u, %{public}s] load content end, ret = %{public}d",
133         window->GetWindowId(), window->GetWindowName().c_str(), ret);
134     return static_cast<int32_t>(ret);
135 }
136 
DisableWindowDecor()137 int32_t CJWindowStageImpl::DisableWindowDecor()
138 {
139     auto weakScene = windowScene_.lock();
140     if (weakScene == nullptr) {
141         TLOGE(WmsLogTag::WMS_DIALOG, "[WindowStage] WindowScene is null");
142         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STAGE_ABNORMALLY);
143     }
144     auto naWindow = weakScene->GetMainWindow();
145     if (naWindow == nullptr) {
146         TLOGE(WmsLogTag::WMS_DIALOG, "[WindowStage] Window is null");
147         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
148     }
149     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(naWindow->DisableAppWindowDecor());
150     if (ret != WmErrorCode::WM_OK) {
151         TLOGE(WmsLogTag::WMS_DIALOG, "[WindowStage] Window [%{public}u, %{public}s] disable app window decor end",
152             naWindow->GetWindowId(), naWindow->GetWindowName().c_str());
153         return static_cast<int32_t>(ret);
154     }
155     return static_cast<int32_t>(WmErrorCode::WM_OK);
156 }
157 
158 /** @note @window.hierarchy */
SetShowOnLockScreen(bool showOnLockScreen)159 int32_t CJWindowStageImpl::SetShowOnLockScreen(bool showOnLockScreen)
160 {
161     if (!Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
162         TLOGE(WmsLogTag::WMS_DIALOG, "Set show on lock screen permission denied!");
163         return static_cast<int32_t>(WmErrorCode::WM_ERROR_NOT_SYSTEM_APP);
164     }
165     auto weakScene = windowScene_.lock();
166     if (weakScene == nullptr || weakScene->GetMainWindow() == nullptr) {
167         TLOGE(WmsLogTag::WMS_DIALOG, "[WindowStage] WindowScene is null or window is null");
168         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
169     }
170     auto window = weakScene->GetMainWindow();
171     WmErrorCode ret;
172     if (showOnLockScreen) {
173         ret = WM_JS_TO_ERROR_CODE_MAP.at(
174             window->AddWindowFlag(WindowFlag::WINDOW_FLAG_SHOW_WHEN_LOCKED));
175     } else {
176         ret = WM_JS_TO_ERROR_CODE_MAP.at(
177             window->RemoveWindowFlag(WindowFlag::WINDOW_FLAG_SHOW_WHEN_LOCKED));
178     }
179     TLOGI(WmsLogTag::WMS_DIALOG,
180         "[WindowStage] Window [%{public}u, %{public}s] SetShowOnLockScreen %{public}u, ret = %{public}u",
181         window->GetWindowId(), window->GetWindowName().c_str(), showOnLockScreen, ret);
182     return static_cast<int32_t>(ret);
183 }
184 
CreateCJWindowStage(std::shared_ptr<WindowScene> windowScene)185 std::shared_ptr<CJWindowStageImpl> CJWindowStageImpl::CreateCJWindowStage(
186     std::shared_ptr<WindowScene> windowScene)
187 {
188     auto ptr = FFIData::Create<CJWindowStageImpl>(windowScene);
189     if (ptr == nullptr) {
190         return nullptr;
191     }
192     auto windowStagePtr = std::shared_ptr<CJWindowStageImpl>(ptr.GetRefPtr());
193     return windowStagePtr;
194 }
195 
196 extern "C" {
OHOS_CreateCJWindowStage(const std::shared_ptr<WindowScene> & windowScene)197 FFI_EXPORT int64_t OHOS_CreateCJWindowStage(const std::shared_ptr<WindowScene>& windowScene)
198 {
199     auto ptr = OHOS::FFI::FFIData::Create<OHOS::Rosen::CJWindowStageImpl>(windowScene);
200     if (ptr == nullptr) {
201         return 0;
202     }
203     return ptr->GetID();
204 }
205 }
206 }
207 }
208