• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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_scene.h"
17 
18 #include <ability_context.h>
19 #include <configuration.h>
20 
21 #include "perform_reporter.h"
22 #include "singleton_container.h"
23 #include "static_call.h"
24 #include "window_manager_hilog.h"
25 
26 namespace OHOS {
27 namespace Rosen {
28 namespace {
29 const std::string MAIN_WINDOW_ID = "main window";
30 std::atomic<uint32_t> g_count { 0 };
31 
GenerateMainWindowName(const std::shared_ptr<AbilityRuntime::Context> & context)32 std::string GenerateMainWindowName(const std::shared_ptr<AbilityRuntime::Context>& context)
33 {
34     if (context == nullptr) {
35         return MAIN_WINDOW_ID + std::to_string(g_count++);
36     } else {
37         std::string windowName = context->GetBundleName() + std::to_string(g_count++);
38         std::size_t pos = windowName.find_last_of('.');
39         return (pos == std::string::npos) ? windowName : windowName.substr(pos + 1); // skip '.'
40     }
41 }
42 }
43 
~WindowScene()44 WindowScene::~WindowScene()
45 {
46     TLOGI(WmsLogTag::WMS_MAIN, "winId %{public}u destructor!", mainWindowId_);
47 }
48 
OnLastStrongRef(const void *)49 void WindowScene::OnLastStrongRef(const void *)
50 {
51     if (auto mainWindow = GetMainWindow()) {
52         mainWindow->Destroy();
53     }
54 }
55 
Init(DisplayId displayId,const std::shared_ptr<AbilityRuntime::Context> & context,sptr<IWindowLifeCycle> & listener,sptr<WindowOption> option)56 WMError WindowScene::Init(DisplayId displayId, const std::shared_ptr<AbilityRuntime::Context>& context,
57     sptr<IWindowLifeCycle>& listener, sptr<WindowOption> option)
58 {
59     TLOGI(WmsLogTag::WMS_MAIN, "WindowScene init with normal option!");
60     if (option == nullptr) {
61         option = sptr<WindowOption>::MakeSptr();
62     }
63     option->SetDisplayId(displayId);
64     option->SetWindowTag(WindowTag::MAIN_WINDOW);
65     if (context != nullptr) {
66         option->SetBundleName(context->GetBundleName());
67     }
68     auto mainWindow = SingletonContainer::Get<StaticCall>().CreateWindow(
69         GenerateMainWindowName(context), option, context);
70     if (mainWindow == nullptr) {
71         TLOGE(WmsLogTag::WMS_MAIN, "mainWindow is null after create Window!");
72         return WMError::WM_ERROR_NULLPTR;
73     }
74     {
75         std::lock_guard<std::mutex> lock(mainWindowMutex_);
76         mainWindow_ = mainWindow;
77     }
78     mainWindowId_ = mainWindow->GetWindowId();
79     mainWindow->RegisterLifeCycleListener(listener);
80 
81     // report when application startup request window
82     SingletonContainer::Get<WindowInfoReporter>()
83         .ReportStartWindow(option->GetBundleName(), mainWindow->GetWindowName());
84     return WMError::WM_OK;
85 }
86 
Init(DisplayId displayId,const std::shared_ptr<AbilityRuntime::Context> & context,sptr<IWindowLifeCycle> & listener,sptr<WindowOption> option,const sptr<IRemoteObject> & iSession,const std::string & identityToken)87 WMError WindowScene::Init(DisplayId displayId, const std::shared_ptr<AbilityRuntime::Context>& context,
88     sptr<IWindowLifeCycle>& listener, sptr<WindowOption> option, const sptr<IRemoteObject>& iSession,
89     const std::string& identityToken)
90 {
91     TLOGI(WmsLogTag::WMS_MAIN, "WindowScene with window session!");
92     if (option == nullptr || iSession == nullptr) {
93         TLOGE(WmsLogTag::WMS_MAIN, "failed with option or iSession null!");
94         return WMError::WM_ERROR_NULLPTR;
95     }
96     option->SetDisplayId(displayId);
97     option->SetWindowName(GenerateMainWindowName(context));
98     if (context != nullptr) {
99         option->SetBundleName(context->GetBundleName());
100     }
101     auto mainWindow = SingletonContainer::Get<StaticCall>()
102         .CreateWindow(option, context, iSession, identityToken);
103     if (mainWindow == nullptr) {
104         TLOGE(WmsLogTag::WMS_MAIN, "mainWindow is null after create Window!");
105         return WMError::WM_ERROR_NULLPTR;
106     }
107     {
108         std::lock_guard<std::mutex> lock(mainWindowMutex_);
109         mainWindow_ = mainWindow;
110     }
111     mainWindowId_ = mainWindow->GetWindowId();
112     mainWindow->RegisterLifeCycleListener(listener);
113 
114     // report when application startup request window
115     SingletonContainer::Get<WindowInfoReporter>()
116         .ReportStartWindow(option->GetBundleName(), mainWindow->GetWindowName());
117     return WMError::WM_OK;
118 }
119 
CreateWindow(const std::string & windowName,sptr<WindowOption> & option) const120 sptr<Window> WindowScene::CreateWindow(const std::string& windowName, sptr<WindowOption>& option) const
121 {
122     auto mainWindow = GetMainWindow();
123     if (windowName.empty() || mainWindow == nullptr || option == nullptr) {
124         TLOGE(WmsLogTag::WMS_MAIN, "new windowName: %{public}s", windowName.c_str());
125         return nullptr;
126     }
127     option->SetParentId(mainWindow->GetWindowId());
128     option->SetWindowTag(WindowTag::SUB_WINDOW);
129     TLOGD(WmsLogTag::WMS_SUB, "windowName: %{public}s, parentId: %{public}u",
130         windowName.c_str(), mainWindow->GetWindowId());
131     return SingletonContainer::Get<StaticCall>().CreateWindow(windowName, option, mainWindow->GetContext());
132 }
133 
GetMainWindow() const134 sptr<Window> WindowScene::GetMainWindow() const
135 {
136     std::lock_guard<std::mutex> lock(mainWindowMutex_);
137     return mainWindow_;
138 }
139 
GetSubWindow()140 std::vector<sptr<Window>> WindowScene::GetSubWindow()
141 {
142     auto mainWindow = GetMainWindow();
143     if (mainWindow == nullptr) {
144         TLOGE(WmsLogTag::WMS_MAIN, "failed, because main window is null");
145         return {};
146     }
147     uint32_t parentId = mainWindow->GetWindowId();
148     return SingletonContainer::Get<StaticCall>().GetSubWindow(parentId);
149 }
150 
GoForeground(uint32_t reason)151 WMError WindowScene::GoForeground(uint32_t reason)
152 {
153     TLOGI(WmsLogTag::WMS_MAIN, "reason: %{public}u", reason);
154     auto mainWindow = GetMainWindow();
155     if (mainWindow == nullptr) {
156         TLOGE(WmsLogTag::WMS_MAIN, "failed, because main window is null");
157         return WMError::WM_ERROR_NULLPTR;
158     }
159     return mainWindow->Show(reason);
160 }
161 
GoResume()162 WMError WindowScene::GoResume()
163 {
164     TLOGI(WmsLogTag::WMS_MAIN, "in");
165     auto mainWindow = GetMainWindow();
166     if (mainWindow == nullptr) {
167         TLOGE(WmsLogTag::WMS_MAIN, "failed, because main window is null");
168         return WMError::WM_ERROR_NULLPTR;
169     }
170     mainWindow->Resume();
171     return WMError::WM_OK;
172 }
173 
GoBackground(uint32_t reason)174 WMError WindowScene::GoBackground(uint32_t reason)
175 {
176     TLOGI(WmsLogTag::WMS_MAIN, "reason: %{public}u", reason);
177     auto mainWindow = GetMainWindow();
178     if (mainWindow == nullptr) {
179         TLOGE(WmsLogTag::WMS_MAIN, "failed, because main window is null");
180         return WMError::WM_ERROR_NULLPTR;
181     }
182     return mainWindow->Hide(reason);
183 }
184 
GoDestroy()185 WMError WindowScene::GoDestroy()
186 {
187     auto mainWindow = GetMainWindow();
188     if (mainWindow == nullptr) {
189         TLOGE(WmsLogTag::WMS_MAIN, "main window is null");
190         return WMError::WM_ERROR_NULLPTR;
191     }
192     WMError ret = mainWindow->Destroy();
193     if (ret != WMError::WM_OK) {
194         TLOGE(WmsLogTag::WMS_MAIN, "failed, name: %{public}s", mainWindow->GetWindowName().c_str());
195         return ret;
196     }
197 
198     std::lock_guard<std::mutex> lock(mainWindowMutex_);
199     mainWindow_ = nullptr;
200     return WMError::WM_OK;
201 }
202 
OnNewWant(const AAFwk::Want & want)203 WMError WindowScene::OnNewWant(const AAFwk::Want& want)
204 {
205     auto mainWindow = GetMainWindow();
206     if (mainWindow == nullptr) {
207         TLOGE(WmsLogTag::WMS_MAIN, "failed, because main window is null");
208         return WMError::WM_ERROR_NULLPTR;
209     }
210     mainWindow->OnNewWant(want);
211     return WMError::WM_OK;
212 }
213 
SetSystemBarProperty(WindowType type,const SystemBarProperty & property) const214 WMError WindowScene::SetSystemBarProperty(WindowType type, const SystemBarProperty& property) const
215 {
216     auto mainWindow = GetMainWindow();
217     if (mainWindow == nullptr) {
218         TLOGE(WmsLogTag::WMS_MAIN, "failed, because main window is null");
219         return WMError::WM_ERROR_NULLPTR;
220     }
221     return mainWindow->SetSystemBarProperty(type, property);
222 }
223 
RequestFocus() const224 WMError WindowScene::RequestFocus() const
225 {
226     auto mainWindow = GetMainWindow();
227     if (mainWindow == nullptr) {
228         TLOGE(WmsLogTag::WMS_MAIN, "failed, because main window is null");
229         return WMError::WM_ERROR_NULLPTR;
230     }
231     return mainWindow->RequestFocus();
232 }
233 
UpdateConfiguration(const std::shared_ptr<AppExecFwk::Configuration> & configuration)234 void WindowScene::UpdateConfiguration(const std::shared_ptr<AppExecFwk::Configuration>& configuration)
235 {
236     if (auto mainWindow = GetMainWindow()) {
237         TLOGI(WmsLogTag::WMS_MAIN, "winId: %{public}u", mainWindow->GetWindowId());
238         mainWindow->UpdateConfiguration(configuration);
239     } else {
240         TLOGE(WmsLogTag::WMS_MAIN, "failed, because main window is null");
241     }
242 }
243 
UpdateConfigurationForSpecified(const std::shared_ptr<AppExecFwk::Configuration> & configuration,const std::shared_ptr<Global::Resource::ResourceManager> & resourceManager)244 void WindowScene::UpdateConfigurationForSpecified(const std::shared_ptr<AppExecFwk::Configuration>& configuration,
245     const std::shared_ptr<Global::Resource::ResourceManager>& resourceManager)
246 {
247     if (auto mainWindow = GetMainWindow()) {
248         TLOGI(WmsLogTag::WMS_ATTRIBUTE, "winId: %{public}u", mainWindow->GetWindowId());
249         mainWindow->UpdateConfigurationForSpecified(configuration, resourceManager);
250     } else {
251         TLOGE(WmsLogTag::WMS_ATTRIBUTE, "main window is null");
252     }
253 }
254 
GetContentInfo(BackupAndRestoreType type) const255 std::string WindowScene::GetContentInfo(BackupAndRestoreType type) const
256 {
257     auto mainWindow = GetMainWindow();
258     if (mainWindow == nullptr) {
259         TLOGE(WmsLogTag::WMS_MAIN, "failed, because main window is null");
260         return "";
261     }
262     return mainWindow->GetContentInfo(type);
263 }
264 
NotifyMemoryLevel(int32_t level)265 WMError WindowScene::NotifyMemoryLevel(int32_t level)
266 {
267     auto mainWindow = GetMainWindow();
268     if (mainWindow == nullptr) {
269         TLOGE(WmsLogTag::WMS_MAIN, "failed, because main window is null");
270         return WMError::WM_ERROR_NULLPTR;
271     }
272     TLOGI(WmsLogTag::WMS_MAIN, "level: %{public}d", level);
273     return mainWindow->NotifyMemoryLevel(level);
274 }
275 
276 } // namespace Rosen
277 } // namespace OHOS
278