1 /*
2 * Copyright (c) 2023 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 "root_scene.h"
17
18 #include <bundlemgr/launcher_service.h>
19 #include <event_handler.h>
20 #include <input_manager.h>
21 #include <iremote_stub.h>
22 #include <ui_content.h>
23 #include <viewport_config.h>
24
25 #include "app_mgr_client.h"
26 #include "input_transfer_station.h"
27 #include "singleton.h"
28 #include "singleton_container.h"
29
30 #include "anr_manager.h"
31 #include "intention_event_manager.h"
32 #include "vsync_station.h"
33 #include "window_manager_hilog.h"
34 #include "window_rate_manager.h"
35
36 namespace OHOS {
37 namespace Rosen {
38 namespace {
39 constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, HILOG_DOMAIN_WINDOW, "RootScene" };
40 const std::string INPUT_AND_VSYNC_THREAD = "InputAndVsyncThread";
41
42 class BundleStatusCallback : public IRemoteStub<AppExecFwk::IBundleStatusCallback> {
43 public:
BundleStatusCallback(RootScene * rootScene)44 explicit BundleStatusCallback(RootScene* rootScene) : rootScene_(rootScene) {}
45 virtual ~BundleStatusCallback() = default;
46
OnBundleStateChanged(const uint8_t installType,const int32_t resultCode,const std::string & resultMsg,const std::string & bundleName)47 void OnBundleStateChanged(const uint8_t installType,
48 const int32_t resultCode, const std::string& resultMsg, const std::string& bundleName) override {}
49
OnBundleAdded(const std::string & bundleName,const int userId)50 void OnBundleAdded(const std::string& bundleName, const int userId) override
51 {
52 rootScene_->OnBundleUpdated(bundleName);
53 }
54
OnBundleUpdated(const std::string & bundleName,const int userId)55 void OnBundleUpdated(const std::string& bundleName, const int userId) override
56 {
57 rootScene_->OnBundleUpdated(bundleName);
58 }
59
OnBundleRemoved(const std::string & bundleName,const int userId)60 void OnBundleRemoved(const std::string& bundleName, const int userId) override {}
61
62 private:
63 RootScene* rootScene_;
64 };
65 } // namespace
66
67 sptr<RootScene> RootScene::staticRootScene_;
68
RootScene()69 RootScene::RootScene()
70 {
71 launcherService_ = new AppExecFwk::LauncherService();
72 if (!launcherService_->RegisterCallback(new BundleStatusCallback(this))) {
73 WLOGFE("Failed to register bundle status callback.");
74 }
75 }
76
~RootScene()77 RootScene::~RootScene()
78 {
79 uiContent_ = nullptr;
80 }
81
LoadContent(const std::string & contentUrl,napi_env env,napi_value storage,AbilityRuntime::Context * context)82 void RootScene::LoadContent(const std::string& contentUrl, napi_env env, napi_value storage,
83 AbilityRuntime::Context* context)
84 {
85 if (context == nullptr) {
86 WLOGFE("context is nullptr!");
87 return;
88 }
89 uiContent_ = Ace::UIContent::Create(context, reinterpret_cast<NativeEngine*>(env));
90 if (uiContent_ == nullptr) {
91 WLOGFE("uiContent_ is nullptr!");
92 return;
93 }
94
95 uiContent_->Initialize(this, contentUrl, storage);
96 uiContent_->Foreground();
97 uiContent_->SetFrameLayoutFinishCallback(std::move(frameLayoutFinishCb_));
98 RegisterInputEventListener();
99 DelayedSingleton<ANRManager>::GetInstance()->Init();
100 DelayedSingleton<ANRManager>::GetInstance()->SetAnrObserver(([](int32_t pid) {
101 WLOGFD("Receive anr notice enter");
102 AppExecFwk::AppFaultDataBySA faultData;
103 faultData.faultType = AppExecFwk::FaultDataType::APP_FREEZE;
104 faultData.pid = pid;
105 faultData.errorObject.name = AppExecFwk::AppFreezeType::APP_INPUT_BLOCK;
106 faultData.errorObject.message = "User input does not respond normally, report by sceneBoard.";
107 faultData.errorObject.stack = "";
108 if (int32_t ret = DelayedSingleton<AppExecFwk::AppMgrClient>::GetInstance()->NotifyAppFaultBySA(faultData);
109 ret != 0) {
110 WLOGFE("NotifyAppFaultBySA failed, pid:%{public}d, errcode:%{public}d", pid, ret);
111 }
112 WLOGFD("Receive anr notice leave");
113 }));
114 DelayedSingleton<ANRManager>::GetInstance()->SetAppInfoGetter(
115 [](int32_t pid, std::string& bundleName, int32_t uid) {
116 int32_t ret = DelayedSingleton<AppExecFwk::AppMgrClient>::GetInstance()->GetBundleNameByPid(
117 pid, bundleName, uid);
118 if (ret != 0) {
119 WLOGFE("GetBundleNameByPid failed, pid:%{public}d, errcode:%{public}d", pid, ret);
120 }
121 });
122 }
123
UpdateViewportConfig(const Rect & rect,WindowSizeChangeReason reason)124 void RootScene::UpdateViewportConfig(const Rect& rect, WindowSizeChangeReason reason)
125 {
126 if (uiContent_ == nullptr) {
127 WLOGFE("uiContent_ is nullptr!");
128 return;
129 }
130 Ace::ViewportConfig config;
131 config.SetSize(rect.width_, rect.height_);
132 config.SetPosition(rect.posX_, rect.posY_);
133 config.SetDensity(density_);
134 uiContent_->UpdateViewportConfig(config, reason);
135 }
136
UpdateConfiguration(const std::shared_ptr<AppExecFwk::Configuration> & configuration)137 void RootScene::UpdateConfiguration(const std::shared_ptr<AppExecFwk::Configuration>& configuration)
138 {
139 if (uiContent_) {
140 WLOGFD("notify root scene ace");
141 uiContent_->UpdateConfiguration(configuration);
142 }
143 }
144
UpdateConfigurationForAll(const std::shared_ptr<AppExecFwk::Configuration> & configuration)145 void RootScene::UpdateConfigurationForAll(const std::shared_ptr<AppExecFwk::Configuration>& configuration)
146 {
147 WLOGD("notify root scene ace for all");
148 if (staticRootScene_) {
149 staticRootScene_->UpdateConfiguration(configuration);
150 }
151 }
152
RegisterInputEventListener()153 void RootScene::RegisterInputEventListener()
154 {
155 auto mainEventRunner = AppExecFwk::EventRunner::GetMainEventRunner();
156 if (mainEventRunner) {
157 WLOGFD("MainEventRunner is available");
158 eventHandler_ = std::make_shared<AppExecFwk::EventHandler>(mainEventRunner);
159 } else {
160 WLOGFD("MainEventRunner is not available");
161 eventHandler_ = AppExecFwk::EventHandler::Current();
162 if (!eventHandler_) {
163 eventHandler_ =
164 std::make_shared<AppExecFwk::EventHandler>(AppExecFwk::EventRunner::Create(INPUT_AND_VSYNC_THREAD));
165 }
166 VsyncStation::GetInstance().SetIsMainHandlerAvailable(false);
167 VsyncStation::GetInstance().SetVsyncEventHandler(eventHandler_);
168 }
169 if (!(DelayedSingleton<IntentionEventManager>::GetInstance()->EnableInputEventListener(
170 uiContent_.get(), eventHandler_))) {
171 WLOGFE("EnableInputEventListener fail");
172 }
173 InputTransferStation::GetInstance().MarkRegisterToMMI();
174 }
175
RequestVsync(const std::shared_ptr<VsyncCallback> & vsyncCallback)176 void RootScene::RequestVsync(const std::shared_ptr<VsyncCallback>& vsyncCallback)
177 {
178 std::lock_guard<std::mutex> lock(mutex_);
179 VsyncStation::GetInstance().RequestVsync(vsyncCallback);
180 }
181
GetVSyncPeriod()182 int64_t RootScene::GetVSyncPeriod()
183 {
184 std::lock_guard<std::mutex> lock(mutex_);
185 return VsyncStation::GetInstance().GetVSyncPeriod();
186 }
187
FlushFrameRate(uint32_t rate)188 void RootScene::FlushFrameRate(uint32_t rate)
189 {
190 std::lock_guard<std::mutex> lock(mutex_);
191 WindowRateManager::GetInstance().FlushFrameRateForRootWindow(rate);
192 }
193
OnBundleUpdated(const std::string & bundleName)194 void RootScene::OnBundleUpdated(const std::string& bundleName)
195 {
196 WLOGFD("bundle %{public}s updated", bundleName.c_str());
197 if (uiContent_) {
198 uiContent_->UpdateResource();
199 }
200 }
201
SetFrameLayoutFinishCallback(std::function<void ()> && callback)202 void RootScene::SetFrameLayoutFinishCallback(std::function<void()>&& callback)
203 {
204 frameLayoutFinishCb_ = callback;
205 if (uiContent_) {
206 uiContent_->SetFrameLayoutFinishCallback(std::move(frameLayoutFinishCb_));
207 }
208 WLOGFI("[WMSLayout] SetFrameLayoutFinishCallback end");
209 }
210 } // namespace Rosen
211 } // namespace OHOS
212