• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cstdint>
18 #include "permission.h"
19 #include "window_impl.h"
20 #include "window_manager_hilog.h"
21 #include "window_utils.h"
22 
23 namespace OHOS {
24 namespace Rosen {
CreateCjSubWindowArrayObject(std::vector<sptr<Window>> & vec,RetStruct & ret)25 void CreateCjSubWindowArrayObject(std::vector<sptr<Window>> &vec, RetStruct &ret)
26 {
27     int64_t *windowList = static_cast<int64_t *>(malloc(sizeof(int64_t) * vec.size()));
28     if (windowList == nullptr) {
29         ret.code = static_cast<int32_t>(WmErrorCode::WM_ERROR_SYSTEM_ABNORMALLY);
30         return;
31     }
32     ret.data = windowList;
33     ret.len = 0;
34     ret.code = static_cast<int32_t>(WmErrorCode::WM_OK);
35     for (auto& window : vec) {
36         if (window == nullptr) {
37             continue;
38         }
39         sptr<CJWindowImpl> windowImpl = CreateCjWindowObject(window);
40         if (windowImpl == nullptr) {
41             TLOGE(WmsLogTag::WMS_DIALOG, "windowImpl is nullptr");
42             continue;
43         }
44         windowList[ret.len] = windowImpl->GetID();
45         ret.len += 1;
46     }
47     return;
48 }
49 
GetMainWindow(int64_t & windowId)50 int32_t CJWindowStageImpl::GetMainWindow(int64_t &windowId)
51 {
52     auto weakScene = windowScene_.lock();
53     if (weakScene == nullptr) {
54         TLOGE(WmsLogTag::WMS_DIALOG, "Window scene is nullptr");
55         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
56     }
57     auto window = weakScene->GetMainWindow();
58     if (window == nullptr) {
59         TLOGE(WmsLogTag::WMS_DIALOG, "Window is null");
60         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
61     }
62     auto windowImpl = CreateCjWindowObject(window);
63     if (windowImpl == nullptr) {
64         TLOGE(WmsLogTag::WMS_DIALOG, "windowImpl is null");
65         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
66     }
67     windowId = windowImpl->GetID();
68     return static_cast<int32_t>(WmErrorCode::WM_OK);
69 }
70 
CreateSubWindow(std::string name,int64_t & windowId)71 int32_t CJWindowStageImpl::CreateSubWindow(std::string name, int64_t &windowId)
72 {
73     auto weakScene = windowScene_.lock();
74     if (weakScene == nullptr) {
75         TLOGE(WmsLogTag::WMS_DIALOG, "Window scene is nullptr");
76         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
77     }
78     sptr<Rosen::WindowOption> windowOption = new(std::nothrow) Rosen::WindowOption();
79     if (windowOption == nullptr) {
80         TLOGE(WmsLogTag::WMS_DIALOG, "Create option failed");
81         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
82     }
83     windowOption->SetWindowType(Rosen::WindowType::WINDOW_TYPE_APP_SUB_WINDOW);
84     windowOption->SetWindowMode(Rosen::WindowMode::WINDOW_MODE_FLOATING);
85     auto window = weakScene->CreateWindow(name, windowOption);
86     if (window == nullptr) {
87         TLOGE(WmsLogTag::WMS_DIALOG, "Create window failed");
88         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
89     }
90     sptr<CJWindowImpl> windowImpl = CreateCjWindowObject(window);
91     if (windowImpl == nullptr) {
92         TLOGE(WmsLogTag::WMS_DIALOG, "windowImpl is null");
93         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
94     }
95     windowId = windowImpl->GetID();
96     TLOGI(WmsLogTag::WMS_DIALOG, "Create sub window %{public}s end", name.c_str());
97     return static_cast<int32_t>(WmErrorCode::WM_OK);
98 }
99 
GetSubWindow()100 RetStruct CJWindowStageImpl::GetSubWindow()
101 {
102     RetStruct ret = {.code = static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY), .len = 0, .data = nullptr};
103     auto weakScene = windowScene_.lock();
104     if (weakScene == nullptr) {
105         TLOGE(WmsLogTag::WMS_DIALOG, "Window scene is nullptr");
106         ret.code = static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
107         return ret;
108     }
109     auto subWindowVec = weakScene->GetSubWindow();
110     CreateCjSubWindowArrayObject(subWindowVec, ret);
111     return ret;
112 }
113 
OnLoadContent(const std::string & contexUrl,const std::string & storageJson,bool isLoadedByName)114 int32_t CJWindowStageImpl::OnLoadContent(const std::string &contexUrl,
115     const std::string &storageJson, bool isLoadedByName)
116 {
117     auto weakScene = windowScene_.lock();
118     if (weakScene == nullptr) {
119         TLOGE(WmsLogTag::WMS_DIALOG, "[WindowStage] WindowScene is null");
120         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STAGE_ABNORMALLY);
121     }
122     auto window = weakScene->GetMainWindow();
123     if (window == nullptr) {
124         TLOGE(WmsLogTag::WMS_DIALOG, "[WindowStage] Window is null");
125         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
126     }
127     WMError ret;
128     if (isLoadedByName) {
129         ret = window->SetUIContentByName(contexUrl, nullptr, nullptr);
130     } else {
131         ret = window->NapiSetUIContent(contexUrl, (napi_env)nullptr, nullptr);
132     }
133     TLOGI(WmsLogTag::WMS_DIALOG,
134         "[WindowStage] [%{public}u, %{public}s] end, ret=%{public}d",
135         window->GetWindowId(), window->GetWindowName().c_str(), ret);
136     return static_cast<int32_t>(ret);
137 }
138 
DisableWindowDecor()139 int32_t CJWindowStageImpl::DisableWindowDecor()
140 {
141     auto weakScene = windowScene_.lock();
142     if (weakScene == nullptr) {
143         TLOGE(WmsLogTag::WMS_DIALOG, "[WindowStage] WindowScene is null");
144         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STAGE_ABNORMALLY);
145     }
146     auto naWindow = weakScene->GetMainWindow();
147     if (naWindow == nullptr) {
148         TLOGE(WmsLogTag::WMS_DIALOG, "[WindowStage] Window is null");
149         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
150     }
151     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(naWindow->DisableAppWindowDecor());
152     if (ret != WmErrorCode::WM_OK) {
153         TLOGE(WmsLogTag::WMS_DIALOG, "[WindowStage] Window [%{public}u, %{public}s] end",
154             naWindow->GetWindowId(), naWindow->GetWindowName().c_str());
155         return static_cast<int32_t>(ret);
156     }
157     return static_cast<int32_t>(WmErrorCode::WM_OK);
158 }
159 
160 /** @note @window.hierarchy */
SetShowOnLockScreen(bool showOnLockScreen)161 int32_t CJWindowStageImpl::SetShowOnLockScreen(bool showOnLockScreen)
162 {
163     if (!Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
164         TLOGE(WmsLogTag::WMS_HIERARCHY, "permission denied!");
165         return static_cast<int32_t>(WmErrorCode::WM_ERROR_NOT_SYSTEM_APP);
166     }
167     auto weakScene = windowScene_.lock();
168     if (weakScene == nullptr || weakScene->GetMainWindow() == nullptr) {
169         TLOGE(WmsLogTag::WMS_HIERARCHY, "[WindowStage] WindowScene is null or window is null");
170         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
171     }
172     auto window = weakScene->GetMainWindow();
173     WmErrorCode ret;
174     if (showOnLockScreen) {
175         ret = WM_JS_TO_ERROR_CODE_MAP.at(
176             window->AddWindowFlag(WindowFlag::WINDOW_FLAG_SHOW_WHEN_LOCKED));
177     } else {
178         ret = WM_JS_TO_ERROR_CODE_MAP.at(
179             window->RemoveWindowFlag(WindowFlag::WINDOW_FLAG_SHOW_WHEN_LOCKED));
180     }
181     TLOGI(WmsLogTag::WMS_HIERARCHY,
182         "[WindowStage] Window [%{public}u, %{public}s] %{public}u, ret=%{public}u",
183         window->GetWindowId(), window->GetWindowName().c_str(), showOnLockScreen, ret);
184     return static_cast<int32_t>(ret);
185 }
186 
CreateCJWindowStage(std::shared_ptr<WindowScene> windowScene)187 std::shared_ptr<CJWindowStageImpl> CJWindowStageImpl::CreateCJWindowStage(
188     std::shared_ptr<WindowScene> windowScene)
189 {
190     auto ptr = FFIData::Create<CJWindowStageImpl>(windowScene);
191     if (ptr == nullptr) {
192         return nullptr;
193     }
194     auto windowStagePtr = std::shared_ptr<CJWindowStageImpl>(ptr.GetRefPtr());
195     return windowStagePtr;
196 }
197 
SetDefaultDensityEnabled(bool enabled)198 int32_t CJWindowStageImpl::SetDefaultDensityEnabled(bool enabled)
199 {
200     auto weakScene = windowScene_.lock();
201     if (weakScene == nullptr) {
202         TLOGE(WmsLogTag::WMS_ATTRIBUTE, "[WindowStage] WindowScene is null or window is null");
203             return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
204     }
205     auto window = weakScene->GetMainWindow();
206     if (window == nullptr) {
207         TLOGE(WmsLogTag::WMS_ATTRIBUTE, "[WindowStage] Window is null");
208         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
209     }
210 
211     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(window->SetDefaultDensityEnabled(enabled));
212     return static_cast<int32_t>(ret);
213 }
214 
OnEvent(int64_t funcId)215 int32_t CJWindowStageImpl::OnEvent(int64_t funcId)
216 {
217     auto weakScene = windowScene_.lock();
218     if (weakScene == nullptr) {
219         TLOGE(WmsLogTag::DEFAULT, "[WindowStage] WindowScene is null");
220         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
221     }
222     auto window = weakScene->GetMainWindow();
223     if (window == nullptr) {
224         TLOGE(WmsLogTag::DEFAULT, "[WindowStage] Window is null");
225         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
226     }
227 
228     WmErrorCode ret = registerManager_->RegisterListener(window,
229         WINDOW_STAGE_EVENT_CB, CaseType::CASE_STAGE, funcId, 0);
230     if (ret != WmErrorCode::WM_OK) {
231         TLOGE(WmsLogTag::DEFAULT, "Register failed, window stage [%{public}u, %{public}s] type: %{public}s",
232             window->GetWindowId(), window->GetWindowName().c_str(), WINDOW_STAGE_EVENT_CB.c_str());
233     }
234     return static_cast<int32_t>(ret);
235 }
236 
OffEvent(int64_t funcId)237 int32_t CJWindowStageImpl::OffEvent(int64_t funcId)
238 {
239     auto weakScene = windowScene_.lock();
240     if (weakScene == nullptr) {
241         TLOGE(WmsLogTag::DEFAULT, "[WindowStage] WindowScene is null");
242         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
243     }
244     auto window = weakScene->GetMainWindow();
245     if (window == nullptr) {
246         TLOGE(WmsLogTag::DEFAULT, "[WindowStage] Window is null");
247         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
248     }
249 
250     WmErrorCode ret = registerManager_->UnregisterListener(window,
251         WINDOW_STAGE_EVENT_CB, CaseType::CASE_STAGE, funcId);
252     if (ret != WmErrorCode::WM_OK) {
253         TLOGE(WmsLogTag::DEFAULT, "Unregister failed, window stage [%{public}u, %{public}s] type: %{public}s",
254             window->GetWindowId(), window->GetWindowName().c_str(), WINDOW_STAGE_EVENT_CB.c_str());
255     }
256     return static_cast<int32_t>(ret);
257 }
258 
CreateSubWindowWithOptions(int64_t & windowId,const std::string & name,const std::string & title,bool decorEnabled,bool isModal)259 int32_t CJWindowStageImpl::CreateSubWindowWithOptions(int64_t &windowId,
260     const std::string& name, const std::string& title, bool decorEnabled, bool isModal)
261 {
262     auto weakScene = windowScene_.lock();
263     if (weakScene == nullptr) {
264         TLOGE(WmsLogTag::WMS_SUB, "[createSubWindowWithOptions] Window scene is nullptr");
265         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
266     }
267     sptr<Rosen::WindowOption> windowOption = new(std::nothrow) Rosen::WindowOption();
268     if (windowOption == nullptr) {
269         TLOGE(WmsLogTag::WMS_SUB, "[createSubWindowWithOptions] Create option failed");
270         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
271     }
272     if (isModal) {
273         windowOption->AddWindowFlag(WindowFlag::WINDOW_FLAG_IS_MODAL);
274     }
275     windowOption->SetSubWindowTitle(title);
276     windowOption->SetSubWindowDecorEnable(decorEnabled);
277     windowOption->SetWindowType(Rosen::WindowType::WINDOW_TYPE_APP_SUB_WINDOW);
278     windowOption->SetWindowMode(Rosen::WindowMode::WINDOW_MODE_FLOATING);
279     auto window = weakScene->CreateWindow(name, windowOption);
280     if (window == nullptr) {
281         TLOGE(WmsLogTag::WMS_SUB, "[createSubWindowWithOptions] Create window failed");
282         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
283     }
284     sptr<CJWindowImpl> windowImpl = CreateCjWindowObject(window);
285     if (windowImpl == nullptr) {
286         TLOGE(WmsLogTag::WMS_SUB, "[createSubWindowWithOptions] windowImpl is null");
287         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
288     }
289     windowId = windowImpl->GetID();
290     TLOGI(WmsLogTag::WMS_SUB, "[createSubWindowWithOptions] Create sub window %{public}s end", name.c_str());
291     return static_cast<int32_t>(WmErrorCode::WM_OK);
292 }
293 
294 extern "C" {
OHOS_CreateCJWindowStage(const std::shared_ptr<WindowScene> & windowScene)295 FFI_EXPORT int64_t OHOS_CreateCJWindowStage(const std::shared_ptr<WindowScene>& windowScene)
296 {
297     auto ptr = OHOS::FFI::FFIData::Create<OHOS::Rosen::CJWindowStageImpl>(windowScene);
298     if (ptr == nullptr) {
299         return 0;
300     }
301     return ptr->GetID();
302 }
303 }
304 }
305 }
306