• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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_manager_service.h"
17 
18 #include <thread>
19 
20 #include <ability_manager_client.h>
21 #include <cinttypes>
22 #include <chrono>
23 #include <hisysevent.h>
24 #include <hitrace_meter.h>
25 #include <ipc_skeleton.h>
26 #include <parameters.h>
27 #include <rs_iwindow_animation_controller.h>
28 #include <system_ability_definition.h>
29 #include <sstream>
30 #include "xcollie/watchdog.h"
31 
32 #include "color_parser.h"
33 #include "display_manager_service_inner.h"
34 #include "dm_common.h"
35 #include "drag_controller.h"
36 #include "memory_guard.h"
37 #include "minimize_app.h"
38 #include "permission.h"
39 #include "remote_animation.h"
40 #include "singleton_container.h"
41 #include "starting_window.h"
42 #include "ui/rs_ui_director.h"
43 #include "window_helper.h"
44 #include "window_inner_manager.h"
45 #include "window_layout_policy.h"
46 #include "window_manager_agent_controller.h"
47 #include "window_manager_hilog.h"
48 #include "wm_common.h"
49 #include "wm_math.h"
50 
51 namespace OHOS {
52 namespace Rosen {
53 namespace {
54     constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WindowManagerService"};
55 }
56 WM_IMPLEMENT_SINGLE_INSTANCE(WindowManagerService)
57 
58 const bool REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(&SingletonContainer::Get<WindowManagerService>());
59 
WindowManagerService()60 WindowManagerService::WindowManagerService() : SystemAbility(WINDOW_MANAGER_SERVICE_ID, true),
61     rsInterface_(RSInterfaces::GetInstance()),
62     windowShowPerformReport_(new PerformReporter("SHOW_WINDOW_TIME", {20, 35, 50}))
63 {
64     windowRoot_ = new WindowRoot(
__anona0e6452c0202(Event event, const sptr<IRemoteObject>& remoteObject) 65         [this](Event event, const sptr<IRemoteObject>& remoteObject) { OnWindowEvent(event, remoteObject); });
66     inputWindowMonitor_ = new InputWindowMonitor(windowRoot_);
67     windowController_ = new WindowController(windowRoot_, inputWindowMonitor_);
68     dragController_ = new DragController(windowRoot_);
69     windowDumper_ = new WindowDumper(windowRoot_);
70     freezeDisplayController_ = new FreezeController();
71     windowCommonEvent_ = std::make_shared<WindowCommonEvent>();
72     startingOpen_ = system::GetParameter("persist.window.sw.enabled", "1") == "1"; // startingWin default enabled
73     runner_ = AppExecFwk::EventRunner::Create(name_);
74     handler_ = std::make_shared<AppExecFwk::EventHandler>(runner_);
75     snapshotController_ = new SnapshotController(windowRoot_, handler_);
76     int ret = HiviewDFX::Watchdog::GetInstance().AddThread(name_, handler_);
77     if (ret != 0) {
78         WLOGFE("Add watchdog thread failed");
79     }
__anona0e6452c0302() 80     handler_->PostTask([]() { MemoryGuard cacheGuard; }, AppExecFwk::EventQueue::Priority::IMMEDIATE);
81     // init RSUIDirector, it will handle animation callback
82     rsUiDirector_ = RSUIDirector::Create();
__anona0e6452c0402(const std::function<void()>& task) 83     rsUiDirector_->SetUITaskRunner([this](const std::function<void()>& task) { PostAsyncTask(task); });
84     rsUiDirector_->Init(false);
85 }
86 
OnStart()87 void WindowManagerService::OnStart()
88 {
89     WLOGFI("start");
90     if (!Init()) {
91         WLOGFE("Init failed");
92         return;
93     }
94     WindowInnerManager::GetInstance().Start(system::GetParameter("persist.window.holder.enable", "0") == "1");
95     sptr<IDisplayChangeListener> listener = new DisplayChangeListener();
96     DisplayManagerServiceInner::GetInstance().RegisterDisplayChangeListener(listener);
97 
98     sptr<IWindowInfoQueriedListener> windowInfoQueriedListener = new WindowInfoQueriedListener();
99     DisplayManagerServiceInner::GetInstance().RegisterWindowInfoQueriedListener(windowInfoQueriedListener);
100 
101     PostAsyncTask([this]() {
102         sptr<IRSScreenChangeListener> rSScreenChangeListener = new IRSScreenChangeListener();
103         rSScreenChangeListener->onConnected_
104             = std::bind(&WindowManagerService::OnRSScreenConnected, this);
105         rSScreenChangeListener->onDisconnected_
106             = std::bind(&WindowManagerService::OnRSScreenDisconnected, this);
107         WLOGFI("RegisterRSScreenChangeListener");
108         DisplayManagerServiceInner::GetInstance().RegisterRSScreenChangeListener(rSScreenChangeListener);
109     });
110 
111     AddSystemAbilityListener(RENDER_SERVICE);
112     AddSystemAbilityListener(ABILITY_MGR_SERVICE_ID);
113     AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID);
114 
115     if (!Publish(this)) {
116         WLOGFE("Publish failed");
117     }
118     WLOGFI("end");
119 }
120 
PostAsyncTask(Task task)121 void WindowManagerService::PostAsyncTask(Task task)
122 {
123     if (handler_) {
124         bool ret = handler_->PostTask(task, AppExecFwk::EventQueue::Priority::IMMEDIATE);
125         if (!ret) {
126             WLOGFE("EventHandler PostTask Failed");
127         }
128     }
129 }
130 
PostVoidSyncTask(Task task)131 void WindowManagerService::PostVoidSyncTask(Task task)
132 {
133     if (handler_) {
134         bool ret = handler_->PostSyncTask(task, AppExecFwk::EventQueue::Priority::IMMEDIATE);
135         if (!ret) {
136             WLOGFE("EventHandler PostVoidSyncTask Failed");
137         }
138     }
139 }
140 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)141 void WindowManagerService::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
142 {
143     WLOGFI("systemAbilityId: %{public}d, start", systemAbilityId);
144     switch (systemAbilityId) {
145         case RENDER_SERVICE:
146             WLOGFI("RENDER_SERVICE");
147             InitWithRanderServiceAdded();
148             break;
149         case ABILITY_MGR_SERVICE_ID:
150             WLOGFI("ABILITY_MGR_SERVICE_ID");
151             InitWithAbilityManagerServiceAdded();
152             break;
153         case COMMON_EVENT_SERVICE_ID:
154             WLOGFI("COMMON_EVENT_SERVICE_ID");
155             windowCommonEvent_->SubscriberEvent();
156             break;
157         default:
158             WLOGFW("unhandled sysabilityId: %{public}d", systemAbilityId);
159             break;
160     }
161     WLOGFI("systemAbilityId: %{public}d, end", systemAbilityId);
162 }
163 
OnAccountSwitched(int accountId)164 void WindowManagerService::OnAccountSwitched(int accountId)
165 {
166     PostAsyncTask([this, accountId]() {
167         windowRoot_->RemoveSingleUserWindowNodes(accountId);
168     });
169     WLOGFI("called");
170 }
171 
WindowVisibilityChangeCallback(std::shared_ptr<RSOcclusionData> occlusionData)172 void WindowManagerService::WindowVisibilityChangeCallback(std::shared_ptr<RSOcclusionData> occlusionData)
173 {
174     WLOGFD("NotifyWindowVisibilityChange: enter");
175     std::weak_ptr<RSOcclusionData> weak(occlusionData);
176     PostVoidSyncTask([this, weak]() {
177         auto weakOcclusionData = weak.lock();
178         if (weakOcclusionData == nullptr) {
179             WLOGFE("weak occlusionData is nullptr");
180             return;
181         }
182         windowRoot_->NotifyWindowVisibilityChange(weakOcclusionData);
183     });
184 }
185 
InitWithRanderServiceAdded()186 void WindowManagerService::InitWithRanderServiceAdded()
187 {
188     auto windowVisibilityChangeCb = std::bind(&WindowManagerService::WindowVisibilityChangeCallback, this,
189         std::placeholders::_1);
190     WLOGFI("RegisterWindowVisibilityChangeCallback");
191     if (rsInterface_.RegisterOcclusionChangeCallback(windowVisibilityChangeCb) != WM_OK) {
192         WLOGFE("RegisterWindowVisibilityChangeCallback failed");
193     }
194     RenderModeChangeCallback renderModeChangeCb
195         = std::bind(&WindowManagerService::OnRenderModeChanged, this, std::placeholders::_1);
196     WLOGFI("SetRenderModeChangeCallback");
197     if (rsInterface_.SetRenderModeChangeCallback(renderModeChangeCb) != WM_OK) {
198         WLOGFE("SetRenderModeChangeCallback failed");
199     }
200 
201     if (windowRoot_->GetMaxUniRenderAppWindowNumber() <= 0) {
202         rsInterface_.UpdateRenderMode(false);
203     }
204 }
205 
InitWithAbilityManagerServiceAdded()206 void WindowManagerService::InitWithAbilityManagerServiceAdded()
207 {
208     if (snapshotController_ == nullptr) {
209         snapshotController_ = new SnapshotController(windowRoot_, handler_);
210     }
211     WLOGFI("RegisterSnapshotHandler");
212     if (AAFwk::AbilityManagerClient::GetInstance()->RegisterSnapshotHandler(snapshotController_) != ERR_OK) {
213         WLOGFE("RegisterSnapshotHandler failed");
214     }
215 
216     if (wmsHandler_ == nullptr) {
217         wmsHandler_ = new WindowManagerServiceHandler();
218     }
219     WLOGFI("RegisterWindowManagerServiceHandler");
220     if (AAFwk::AbilityManagerClient::GetInstance()->RegisterWindowManagerServiceHandler(wmsHandler_) != ERR_OK) {
221         WLOGFE("RegisterWindowManagerServiceHandler failed");
222     }
223 }
224 
NotifyWindowTransition(sptr<AAFwk::AbilityTransitionInfo> from,sptr<AAFwk::AbilityTransitionInfo> to)225 void WindowManagerServiceHandler::NotifyWindowTransition(
226     sptr<AAFwk::AbilityTransitionInfo> from, sptr<AAFwk::AbilityTransitionInfo> to)
227 {
228     sptr<WindowTransitionInfo> fromInfo = nullptr;
229     sptr<WindowTransitionInfo> toInfo = nullptr;
230     if (from) { // if exists, transition to window transition info
231         fromInfo = new WindowTransitionInfo(from);
232     }
233     if (to) {
234         toInfo = new WindowTransitionInfo(to);
235     }
236     WindowManagerService::GetInstance().NotifyWindowTransition(fromInfo, toInfo, false);
237 }
238 
GetFocusWindow(sptr<IRemoteObject> & abilityToken)239 int32_t WindowManagerServiceHandler::GetFocusWindow(sptr<IRemoteObject>& abilityToken)
240 {
241     return static_cast<int32_t>(WindowManagerService::GetInstance().GetFocusWindowInfo(abilityToken));
242 }
243 
StartingWindow(sptr<AAFwk::AbilityTransitionInfo> info,std::shared_ptr<Media::PixelMap> pixelMap)244 void WindowManagerServiceHandler::StartingWindow(
245     sptr<AAFwk::AbilityTransitionInfo> info, std::shared_ptr<Media::PixelMap> pixelMap)
246 {
247     sptr<WindowTransitionInfo> windowInfo = new WindowTransitionInfo(info);
248     WLOGFI("hot start is called");
249     WindowManagerService::GetInstance().StartingWindow(windowInfo, pixelMap, false);
250 }
251 
StartingWindow(sptr<AAFwk::AbilityTransitionInfo> info,std::shared_ptr<Media::PixelMap> pixelMap,uint32_t bgColor)252 void WindowManagerServiceHandler::StartingWindow(
253     sptr<AAFwk::AbilityTransitionInfo> info, std::shared_ptr<Media::PixelMap> pixelMap, uint32_t bgColor)
254 {
255     sptr<WindowTransitionInfo> windowInfo = new WindowTransitionInfo(info);
256     WLOGFI("cold start is called");
257     WindowManagerService::GetInstance().StartingWindow(windowInfo, pixelMap, true, bgColor);
258 }
259 
CancelStartingWindow(sptr<IRemoteObject> abilityToken)260 void WindowManagerServiceHandler::CancelStartingWindow(sptr<IRemoteObject> abilityToken)
261 {
262     WLOGFI("WindowManagerServiceHandler CancelStartingWindow!");
263     WindowManagerService::GetInstance().CancelStartingWindow(abilityToken);
264 }
265 
Init()266 bool WindowManagerService::Init()
267 {
268     WLOGFI("Init start");
269     if (WindowManagerConfig::LoadConfigXml()) {
270         if (WindowManagerConfig::GetConfig().IsMap()) {
271             WindowManagerConfig::DumpConfig(*WindowManagerConfig::GetConfig().mapValue_);
272         }
273         ConfigureWindowManagerService();
274     }
275     WLOGFI("Init success");
276     return true;
277 }
278 
Dump(int fd,const std::vector<std::u16string> & args)279 int WindowManagerService::Dump(int fd, const std::vector<std::u16string>& args)
280 {
281     if (windowDumper_ == nullptr) {
282         windowDumper_ = new WindowDumper(windowRoot_);
283     }
284 
285     return PostSyncTask([this, fd, &args]() {
286         return static_cast<int>(windowDumper_->Dump(fd, args));
287     });
288 }
289 
ConfigureWindowManagerService()290 void WindowManagerService::ConfigureWindowManagerService()
291 {
292     const auto& config = WindowManagerConfig::GetConfig();
293     WindowManagerConfig::ConfigItem item = config["decor"].GetProp("enable");
294     if (item.IsBool()) {
295         systemConfig_.isSystemDecorEnable_ = item.boolValue_;
296     }
297     item = config["minimizeByOther"].GetProp("enable");
298     if (item.IsBool()) {
299         MinimizeApp::SetMinimizedByOtherConfig(item.boolValue_);
300     }
301     item = config["stretchable"].GetProp("enable");
302     if (item.IsBool()) {
303         systemConfig_.isStretchable_ = item.boolValue_;
304     }
305     item = config["defaultWindowMode"];
306     if (item.IsInts()) {
307         auto numbers = *item.intsValue_;
308         if (numbers.size() == 1 &&
309             (numbers[0] == static_cast<int32_t>(WindowMode::WINDOW_MODE_FULLSCREEN) ||
310              numbers[0] == static_cast<int32_t>(WindowMode::WINDOW_MODE_FLOATING))) {
311             systemConfig_.defaultWindowMode_ = static_cast<WindowMode>(static_cast<uint32_t>(numbers[0]));
312             StartingWindow::SetDefaultWindowMode(systemConfig_.defaultWindowMode_);
313         }
314     }
315     item = config["remoteAnimation"].GetProp("enable");
316     if (item.IsBool()) {
317         RemoteAnimation::isRemoteAnimationEnable_ = item.boolValue_;
318     }
319     item = config["maxAppWindowNumber"];
320     if (item.IsInts()) {
321         auto numbers = *item.intsValue_;
322         if (numbers.size() == 1 && numbers[0] > 0) {
323             windowRoot_->SetMaxAppWindowNumber(static_cast<uint32_t>(numbers[0]));
324         }
325     }
326     item = config["maxUniRenderAppWindowNumber"];
327     if (item.IsInts()) {
328         auto numbers = *item.intsValue_;
329         if (numbers.size() == 1 && numbers[0] > 0) {
330             windowRoot_->SetMaxUniRenderAppWindowNumber(static_cast<uint32_t>(numbers[0]));
331         }
332     }
333     item = config["modeChangeHotZones"];
334     if (item.IsInts()) {
335         ConfigHotZones(*item.intsValue_);
336     }
337     item = config["splitRatios"];
338     if (item.IsFloats()) {
339         windowRoot_->SetSplitRatios(*item.floatsValue_);
340     }
341     item = config["exitSplitRatios"];
342     if (item.IsFloats()) {
343         windowRoot_->SetExitSplitRatios(*item.floatsValue_);
344     }
345     item = config["windowAnimation"];
346     if (item.IsMap()) {
347         ConfigWindowAnimation(item);
348     }
349     item = config["keyboardAnimation"];
350     if (item.IsMap()) {
351         ConfigKeyboardAnimation(item);
352     }
353     item = config["windowEffect"];
354     if (item.IsMap()) {
355         ConfigWindowEffect(item);
356     }
357     item = config["floatingBottomPosY"];
358     if (item.IsInts()) {
359         auto numbers = *item.intsValue_;
360         if (numbers.size() == 1 && numbers[0] > 0) {
361             WindowLayoutPolicy::SetCascadeRectBottomPosYLimit(static_cast<uint32_t>(numbers[0]));
362         }
363     }
364 }
365 
ConfigHotZones(const std::vector<int> & numbers)366 void WindowManagerService::ConfigHotZones(const std::vector<int>& numbers)
367 {
368     if (numbers.size() == 3) { // 3 hot zones
369         hotZonesConfig_.fullscreenRange_ = static_cast<uint32_t>(numbers[0]); // 0 fullscreen
370         hotZonesConfig_.primaryRange_ = static_cast<uint32_t>(numbers[1]);    // 1 primary
371         hotZonesConfig_.secondaryRange_ = static_cast<uint32_t>(numbers[2]);  // 2 secondary
372         hotZonesConfig_.isModeChangeHotZoneConfigured_ = true;
373     }
374 }
375 
ConfigWindowAnimation(const WindowManagerConfig::ConfigItem & animeConfig)376 void WindowManagerService::ConfigWindowAnimation(const WindowManagerConfig::ConfigItem& animeConfig)
377 {
378     auto& windowAnimationConfig = WindowNodeContainer::GetAnimationConfigRef().windowAnimationConfig_;
379     WindowManagerConfig::ConfigItem item = animeConfig["timing"];
380     if (item.IsMap() && item.mapValue_->count("curve")) {
381         windowAnimationConfig.animationTiming_.timingCurve_ = CreateCurve(item["curve"]);
382     }
383     item = animeConfig["timing"]["duration"];
384     if (item.IsInts()) {
385         auto numbers = *item.intsValue_;
386         if (numbers.size() == 1) { // duration
387             windowAnimationConfig.animationTiming_.timingProtocol_ =
388                 RSAnimationTimingProtocol(numbers[0]);
389         }
390     }
391     item = animeConfig["scale"];
392     if (item.IsFloats()) {
393         auto numbers = *item.floatsValue_;
394         if (numbers.size() == 1) { // 1 xy scale
395             windowAnimationConfig.scale_.x_ =
396             windowAnimationConfig.scale_.y_ = numbers[0]; // 0 xy scale
397         } else if (numbers.size() == 2) { // 2 x,y sclae
398             windowAnimationConfig.scale_.x_ = numbers[0]; // 0 x scale
399             windowAnimationConfig.scale_.y_ = numbers[1]; // 1 y scale
400         } else if (numbers.size() == 3) { // 3 x,y,z scale
401             windowAnimationConfig.scale_ = Vector3f(&numbers[0]);
402         }
403     }
404     item = animeConfig["rotation"];
405     if (item.IsFloats() && item.floatsValue_->size() == 4) { // 4 (axix,angle)
406         windowAnimationConfig.rotation_ = Vector4f(item.floatsValue_->data());
407     }
408     item = animeConfig["translate"];
409     if (item.IsFloats()) {
410         auto numbers = *item.floatsValue_;
411         if (numbers.size() == 2) { // 2 translate xy
412             windowAnimationConfig.translate_.x_ = numbers[0]; // 0 translate x
413             windowAnimationConfig.translate_.y_ = numbers[1]; // 1 translate y
414         } else if (numbers.size() == 3) { // 3 translate xyz
415             windowAnimationConfig.translate_.x_ = numbers[0]; // 0 translate x
416             windowAnimationConfig.translate_.y_ = numbers[1]; // 1 translate y
417             windowAnimationConfig.translate_.z_ = numbers[2]; // 2 translate z
418         }
419     }
420     item = animeConfig["opacity"];
421     if (item.IsFloats()) {
422         auto numbers = *item.floatsValue_;
423         numbers.size() == 1 ? (windowAnimationConfig.opacity_ = numbers[0]) : float();
424     }
425 }
426 
ConfigKeyboardAnimation(const WindowManagerConfig::ConfigItem & animeConfig)427 void WindowManagerService::ConfigKeyboardAnimation(const WindowManagerConfig::ConfigItem& animeConfig)
428 {
429     auto& animationConfig = WindowNodeContainer::GetAnimationConfigRef();
430     WindowManagerConfig::ConfigItem item = animeConfig["timing"];
431     if (item.IsMap() && item.mapValue_->count("curve")) {
432         animationConfig.keyboardAnimationConfig_.curve_ = CreateCurve(item["curve"]);
433     }
434     item = animeConfig["timing"]["durationIn"];
435     if (item.IsInts()) {
436         auto numbers = *item.intsValue_;
437         if (numbers.size() == 1) { // duration
438             animationConfig.keyboardAnimationConfig_.durationIn_ =
439                 RSAnimationTimingProtocol(numbers[0]);
440         }
441     }
442     item = animeConfig["timing"]["durationOut"];
443     if (item.IsInts()) {
444         auto numbers = *item.intsValue_;
445         if (numbers.size() == 1) { // duration
446             animationConfig.keyboardAnimationConfig_.durationOut_ =
447                 RSAnimationTimingProtocol(numbers[0]);
448         }
449     }
450 }
451 
ConfigAppWindowCornerRadius(const WindowManagerConfig::ConfigItem & item,float & out)452 bool WindowManagerService::ConfigAppWindowCornerRadius(const WindowManagerConfig::ConfigItem& item, float& out)
453 {
454     std::map<std::string, float> stringToCornerRadius = {
455         {"off", 0.0f}, {"defaultCornerRadiusXS", 4.0f}, {"defaultCornerRadiusS", 8.0f},
456         {"defaultCornerRadiusM", 12.0f}, {"defaultCornerRadiusL", 16.0f}, {"defaultCornerRadiusXL", 24.0f}
457     };
458 
459     if (item.IsString()) {
460         auto value = item.stringValue_;
461         if (stringToCornerRadius.find(value) != stringToCornerRadius.end()) {
462             out = stringToCornerRadius[value];
463             return true;
464         }
465     }
466     return false;
467 }
468 
ConfigAppWindowShadow(const WindowManagerConfig::ConfigItem & shadowConfig,WindowShadowParameters & outShadow)469 bool WindowManagerService::ConfigAppWindowShadow(const WindowManagerConfig::ConfigItem& shadowConfig,
470     WindowShadowParameters& outShadow)
471 {
472     WindowManagerConfig::ConfigItem item = shadowConfig["elevation"];
473     if (item.IsFloats()) {
474         auto elevation = *item.floatsValue_;
475         if (elevation.size() != 1 || MathHelper::LessNotEqual(elevation[0], 0.0)) {
476             return false;
477         }
478         outShadow.elevation_ = elevation[0];
479     }
480 
481     item = shadowConfig["color"];
482     if (item.IsString()) {
483         auto color = item.stringValue_;
484         uint32_t colorValue;
485         if (!ColorParser::Parse(color, colorValue)) {
486             return false;
487         }
488         outShadow.color_ = color;
489     }
490 
491     item = shadowConfig["offsetX"];
492     if (item.IsFloats()) {
493         auto offsetX = *item.floatsValue_;
494         if (offsetX.size() != 1) {
495             return false;
496         }
497         outShadow.offsetX_ = offsetX[0];
498     }
499 
500     item = shadowConfig["offsetY"];
501     if (item.IsFloats()) {
502         auto offsetY = *item.floatsValue_;
503         if (offsetY.size() != 1) {
504             return false;
505         }
506         outShadow.offsetY_ = offsetY[0];
507     }
508 
509     item = shadowConfig["alpha"];
510     if (item.IsFloats()) {
511         auto alpha = *item.floatsValue_;
512         if (alpha.size() != 1 || (alpha.size() == 1 &&
513             MathHelper::LessNotEqual(alpha[0], 0.0) && MathHelper::GreatNotEqual(alpha[0], 1.0))) {
514             return false;
515         }
516         outShadow.alpha_ = alpha[0];
517     }
518     return true;
519 }
520 
ConfigWindowEffect(const WindowManagerConfig::ConfigItem & effectConfig)521 void WindowManagerService::ConfigWindowEffect(const WindowManagerConfig::ConfigItem& effectConfig)
522 {
523     AppWindowEffectConfig config;
524 
525     // config corner radius
526     WindowManagerConfig::ConfigItem item = effectConfig["appWindows"]["cornerRadius"];
527     if (item.IsMap()) {
528         if (ConfigAppWindowCornerRadius(item["fullScreen"], config.fullScreenCornerRadius_) &&
529             ConfigAppWindowCornerRadius(item["split"], config.splitCornerRadius_) &&
530             ConfigAppWindowCornerRadius(item["float"], config.floatCornerRadius_)) {
531             systemConfig_.effectConfig_ = config;
532         }
533     }
534 
535     // config shadow
536     item = effectConfig["appWindows"]["shadow"]["focused"];
537     if (item.IsMap()) {
538         if (ConfigAppWindowShadow(item, config.focusedShadow_)) {
539             systemConfig_.effectConfig_.focusedShadow_ = config.focusedShadow_;
540         }
541     }
542 
543     item = effectConfig["appWindows"]["shadow"]["unfocused"];
544     if (item.IsMap()) {
545         if (ConfigAppWindowShadow(item, config.unfocusedShadow_)) {
546             systemConfig_.effectConfig_.unfocusedShadow_ = config.unfocusedShadow_;
547         }
548     }
549 }
550 
CreateCurve(const WindowManagerConfig::ConfigItem & curveConfig)551 RSAnimationTimingCurve WindowManagerService::CreateCurve(const WindowManagerConfig::ConfigItem& curveConfig)
552 {
553     const auto& nameItem = curveConfig.GetProp("name");
554     if (nameItem.IsString()) {
555         std::string name = nameItem.stringValue_;
556         if (name == "easeOut") {
557             return RSAnimationTimingCurve::EASE_OUT;
558         } else if (name == "ease") {
559             return RSAnimationTimingCurve::EASE;
560         } else if (name == "easeIn") {
561             return RSAnimationTimingCurve::EASE_IN;
562         } else if (name == "easeInOut") {
563             return RSAnimationTimingCurve::EASE_IN_OUT;
564         } else if (name == "default") {
565             return RSAnimationTimingCurve::DEFAULT;
566         } else if (name == "linear") {
567             return RSAnimationTimingCurve::LINEAR;
568         } else if (name == "spring") {
569             return RSAnimationTimingCurve::SPRING;
570         } else if (name == "interactiveSpring") {
571             return RSAnimationTimingCurve::INTERACTIVE_SPRING;
572         } else if (name == "cubic" && curveConfig.IsFloats() &&
573             curveConfig.floatsValue_->size() == 4) { // 4 curve parameter
574             const auto& numbers = *curveConfig.floatsValue_;
575             return RSAnimationTimingCurve::CreateCubicCurve(numbers[0], // 0 ctrlX1
576                 numbers[1], // 1 ctrlY1
577                 numbers[2], // 2 ctrlX2
578                 numbers[3]); // 3 ctrlY2
579         }
580     }
581     return RSAnimationTimingCurve::EASE_OUT;
582 }
583 
OnStop()584 void WindowManagerService::OnStop()
585 {
586     windowCommonEvent_->UnSubscriberEvent();
587     WindowInnerManager::GetInstance().Stop();
588     WLOGFI("ready to stop service.");
589 }
590 
NotifyWindowTransition(sptr<WindowTransitionInfo> & fromInfo,sptr<WindowTransitionInfo> & toInfo,bool isFromClient)591 WMError WindowManagerService::NotifyWindowTransition(
592     sptr<WindowTransitionInfo>& fromInfo, sptr<WindowTransitionInfo>& toInfo, bool isFromClient)
593 {
594     if (!isFromClient) {
595         WLOGFI("NotifyWindowTransition asynchronously.");
596         PostAsyncTask([this, fromInfo, toInfo]() mutable {
597             return windowController_->NotifyWindowTransition(fromInfo, toInfo);
598         });
599         return WMError::WM_OK;
600     } else {
601         WLOGFI("NotifyWindowTransition synchronously.");
602         return PostSyncTask([this, &fromInfo, &toInfo]() {
603             return windowController_->NotifyWindowTransition(fromInfo, toInfo);
604         });
605     }
606 }
607 
GetFocusWindowInfo(sptr<IRemoteObject> & abilityToken)608 WMError WindowManagerService::GetFocusWindowInfo(sptr<IRemoteObject>& abilityToken)
609 {
610     return PostSyncTask([this, &abilityToken]() {
611         return windowController_->GetFocusWindowInfo(abilityToken);
612     });
613 }
614 
StartingWindow(sptr<WindowTransitionInfo> info,std::shared_ptr<Media::PixelMap> pixelMap,bool isColdStart,uint32_t bkgColor)615 void WindowManagerService::StartingWindow(sptr<WindowTransitionInfo> info, std::shared_ptr<Media::PixelMap> pixelMap,
616     bool isColdStart, uint32_t bkgColor)
617 {
618     if (!startingOpen_) {
619         WLOGFI("startingWindow not open!");
620         return;
621     }
622     PostAsyncTask([this, info, pixelMap, isColdStart, bkgColor]() {
623         windowController_->StartingWindow(info, pixelMap, bkgColor, isColdStart);
624     });
625 }
626 
CancelStartingWindow(sptr<IRemoteObject> abilityToken)627 void WindowManagerService::CancelStartingWindow(sptr<IRemoteObject> abilityToken)
628 {
629     WLOGFI("begin CancelStartingWindow!");
630     if (!startingOpen_) {
631         WLOGFI("startingWindow not open!");
632         return;
633     }
634     PostAsyncTask([this, abilityToken]() {
635         windowController_->CancelStartingWindow(abilityToken);
636     });
637 }
638 
CheckAnimationPermission(const sptr<WindowProperty> & property) const639 bool WindowManagerService::CheckAnimationPermission(const sptr<WindowProperty>& property) const
640 {
641     if (property->GetAnimationFlag() == static_cast<uint32_t>(WindowAnimation::CUSTOM)
642         && !Permission::IsSystemCalling()) {
643         WLOGFD("check animation permission failed");
644         return false;
645     }
646     return true;
647 }
648 
CheckSystemWindowPermission(const sptr<WindowProperty> & property) const649 bool WindowManagerService::CheckSystemWindowPermission(const sptr<WindowProperty>& property) const
650 {
651     WindowType type = property->GetWindowType();
652     if (!WindowHelper::IsSystemWindow(type)) {
653         // type is not system
654         return true;
655     }
656     if (type == WindowType::WINDOW_TYPE_DRAGGING_EFFECT || type == WindowType::WINDOW_TYPE_SYSTEM_ALARM_WINDOW ||
657         type == WindowType::WINDOW_TYPE_TOAST) {
658         // some system types counld be created by normal app
659         return true;
660     }
661     if (type == WindowType::WINDOW_TYPE_FLOAT &&
662         Permission::CheckCallingPermission("ohos.permission.SYSTEM_FLOAT_WINDOW")) {
663         // WINDOW_TYPE_FLOAT counld be created by normal app with the corresponding permission
664         WLOGFD("check create permission success, normal app create float window with request permission.");
665         return true;
666     }
667     if (Permission::IsSystemCalling()) {
668         WLOGFD("check create permission success, create with system calling.");
669         return true;
670     }
671     WLOGFD("check system window permission failed.");
672     return false;
673 }
674 
CreateWindow(sptr<IWindow> & window,sptr<WindowProperty> & property,const std::shared_ptr<RSSurfaceNode> & surfaceNode,uint32_t & windowId,sptr<IRemoteObject> token)675 WMError WindowManagerService::CreateWindow(sptr<IWindow>& window, sptr<WindowProperty>& property,
676     const std::shared_ptr<RSSurfaceNode>& surfaceNode, uint32_t& windowId, sptr<IRemoteObject> token)
677 {
678     if (!window || property == nullptr || surfaceNode == nullptr || !window->AsObject()) {
679         WLOGFE("window is invalid");
680         return WMError::WM_ERROR_NULLPTR;
681     }
682     if (!CheckSystemWindowPermission(property)) {
683         WLOGFE("create system window permission denied!");
684         return WMError::WM_ERROR_INVALID_PERMISSION;
685     }
686     int pid = IPCSkeleton::GetCallingPid();
687     int uid = IPCSkeleton::GetCallingUid();
688     WMError ret = PostSyncTask([this, pid, uid, &window, &property, &surfaceNode, &windowId, &token]() {
689         HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "wms:CreateWindow(%u)", windowId);
690         return windowController_->CreateWindow(window, property, surfaceNode, windowId, token, pid, uid);
691     });
692     accessTokenIdMaps_.insert(std::pair(windowId, IPCSkeleton::GetCallingTokenID()));
693     return ret;
694 }
695 
AddWindow(sptr<WindowProperty> & property)696 WMError WindowManagerService::AddWindow(sptr<WindowProperty>& property)
697 {
698     if (property == nullptr) {
699         WLOGFE("property is nullptr");
700         return WMError::WM_ERROR_NULLPTR;
701     }
702     if (!CheckSystemWindowPermission(property) || !CheckAnimationPermission(property)) {
703         WLOGFE("add window permission denied!");
704         return WMError::WM_ERROR_INVALID_PERMISSION;
705     }
706     return PostSyncTask([this, &property]() {
707         windowShowPerformReport_->start();
708         Rect rect = property->GetRequestRect();
709         uint32_t windowId = property->GetWindowId();
710         WLOGFI("[WMS] Add: %{public}5d %{public}4d %{public}4d %{public}4d [%{public}4d %{public}4d " \
711             "%{public}4d %{public}4d]", windowId, property->GetWindowType(), property->GetWindowMode(),
712             property->GetWindowFlags(), rect.posX_, rect.posY_, rect.width_, rect.height_);
713         HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "wms:AddWindow(%u)", windowId);
714         WMError res = windowController_->AddWindowNode(property);
715         if (property->GetWindowType() == WindowType::WINDOW_TYPE_DRAGGING_EFFECT) {
716             dragController_->StartDrag(windowId);
717         }
718         if (res == WMError::WM_OK) {
719             windowShowPerformReport_->end();
720         }
721         return res;
722     });
723 }
724 
RemoveWindow(uint32_t windowId)725 WMError WindowManagerService::RemoveWindow(uint32_t windowId)
726 {
727     return PostSyncTask([this, windowId]() {
728         WLOGFI("[WMS] Remove: %{public}u", windowId);
729         HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "wms:RemoveWindow(%u)", windowId);
730         WindowInnerManager::GetInstance().NotifyWindowRemovedOrDestroyed(windowId);
731         WMError res = windowController_->RecoverInputEventToClient(windowId);
732         if (res != WMError::WM_OK) {
733             return res;
734         }
735         return windowController_->RemoveWindowNode(windowId);
736     });
737 }
738 
DestroyWindow(uint32_t windowId,bool onlySelf)739 WMError WindowManagerService::DestroyWindow(uint32_t windowId, bool onlySelf)
740 {
741     if (!accessTokenIdMaps_.isExistAndRemove(windowId, IPCSkeleton::GetCallingTokenID())) {
742         WLOGFI("Operation rejected");
743         return WMError::WM_ERROR_INVALID_OPERATION;
744     }
745     return PostSyncTask([this, windowId, onlySelf]() {
746         auto node = windowRoot_->GetWindowNode(windowId);
747         if (node == nullptr) {
748             return WMError::WM_ERROR_NULLPTR;
749         }
750         node->stateMachine_.SetDestroyTaskParam(onlySelf);
751         auto func = [this, windowId]() {
752             WLOGFI("[WMS] Destroy: %{public}u", windowId);
753             HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "wms:DestroyWindow(%u)", windowId);
754             WindowInnerManager::GetInstance().NotifyWindowRemovedOrDestroyed(windowId);
755             auto node = windowRoot_->GetWindowNode(windowId);
756             if (node == nullptr) {
757                 return WMError::WM_OK;
758             }
759             if (node->GetWindowType() == WindowType::WINDOW_TYPE_DRAGGING_EFFECT) {
760                 dragController_->FinishDrag(windowId);
761             }
762             return windowController_->DestroyWindow(windowId, node->stateMachine_.GetDestroyTaskParam());
763         };
764         if (RemoteAnimation::IsRemoteAnimationEnabledAndFirst(node->GetDisplayId()) &&
765             node->stateMachine_.IsRemoteAnimationPlaying()) {
766             WLOGFI("SetDestroyTask id:%{public}u", node->GetWindowId());
767             node->stateMachine_.SetDestroyTask(func);
768             return WMError::WM_OK;
769         }
770         WLOGFI("DestroyWindow windowId: %{public}u, name:%{public}s state: %{public}u",
771             node->GetWindowId(), node->GetWindowName().c_str(),
772             static_cast<uint32_t>(node->stateMachine_.GetCurrentState()));
773         return func();
774     });
775 }
776 
RequestFocus(uint32_t windowId)777 WMError WindowManagerService::RequestFocus(uint32_t windowId)
778 {
779     return PostSyncTask([this, windowId]() {
780         WLOGFI("[WMS] RequestFocus: %{public}u", windowId);
781         return windowController_->RequestFocus(windowId);
782     });
783 }
784 
GetAvoidAreaByType(uint32_t windowId,AvoidAreaType avoidAreaType)785 AvoidArea WindowManagerService::GetAvoidAreaByType(uint32_t windowId, AvoidAreaType avoidAreaType)
786 {
787     return PostSyncTask([this, windowId, avoidAreaType]() {
788         WLOGFI("[WMS] GetAvoidAreaByType: %{public}u, Type: %{public}u", windowId,
789             static_cast<uint32_t>(avoidAreaType));
790         return windowController_->GetAvoidAreaByType(windowId, avoidAreaType);
791     });
792 }
793 
RegisterWindowManagerAgent(WindowManagerAgentType type,const sptr<IWindowManagerAgent> & windowManagerAgent)794 bool WindowManagerService::RegisterWindowManagerAgent(WindowManagerAgentType type,
795     const sptr<IWindowManagerAgent>& windowManagerAgent)
796 {
797     if (!Permission::IsSystemCalling()) {
798         WLOGFE("register windowManager agent permission denied!");
799         return false;
800     }
801     if ((windowManagerAgent == nullptr) || (windowManagerAgent->AsObject() == nullptr)) {
802         WLOGFE("windowManagerAgent is null");
803         return false;
804     }
805     return PostSyncTask([this, &windowManagerAgent, type]() {
806         bool ret = WindowManagerAgentController::GetInstance().RegisterWindowManagerAgent(windowManagerAgent, type);
807         if (type == WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_SYSTEM_BAR) { // if system bar, notify once
808             windowController_->NotifySystemBarTints();
809         }
810         return ret;
811     });
812 }
813 
UnregisterWindowManagerAgent(WindowManagerAgentType type,const sptr<IWindowManagerAgent> & windowManagerAgent)814 bool WindowManagerService::UnregisterWindowManagerAgent(WindowManagerAgentType type,
815     const sptr<IWindowManagerAgent>& windowManagerAgent)
816 {
817     if (!Permission::IsSystemCalling()) {
818         WLOGFE("unregister windowManager agent permission denied!");
819         return false;
820     }
821     if ((windowManagerAgent == nullptr) || (windowManagerAgent->AsObject() == nullptr)) {
822         WLOGFE("windowManagerAgent is null");
823         return false;
824     }
825     return PostSyncTask([this, &windowManagerAgent, type]() {
826         return WindowManagerAgentController::GetInstance().UnregisterWindowManagerAgent(windowManagerAgent, type);
827     });
828 }
829 
SetWindowAnimationController(const sptr<RSIWindowAnimationController> & controller)830 WMError WindowManagerService::SetWindowAnimationController(const sptr<RSIWindowAnimationController>& controller)
831 {
832     if (!Permission::IsSystemCalling()) {
833         WLOGFE("set window animation controller permission denied!");
834         return WMError::WM_ERROR_INVALID_PERMISSION;
835     }
836     if (controller == nullptr) {
837         WLOGFE("RSWindowAnimation: Failed to set window animation controller, controller is null!");
838         return WMError::WM_ERROR_NULLPTR;
839     }
840 
841     sptr<AgentDeathRecipient> deathRecipient = new AgentDeathRecipient(
842         [this](sptr<IRemoteObject>& remoteObject) {
843             PostVoidSyncTask([&remoteObject]() {
844                 RemoteAnimation::OnRemoteDie(remoteObject);
845             });
846         }
847     );
848     controller->AsObject()->AddDeathRecipient(deathRecipient);
849     RemoteAnimation::SetWindowControllerAndRoot(windowController_, windowRoot_);
850     RemoteAnimation::SetMainTaskHandler(handler_);
851     return PostSyncTask([this, &controller]() {
852         WMError ret = windowController_->SetWindowAnimationController(controller);
853         RemoteAnimation::SetAnimationFirst(system::GetParameter("persist.window.af.enabled", "1") == "1");
854         return ret;
855     });
856 }
857 
OnWindowEvent(Event event,const sptr<IRemoteObject> & remoteObject)858 void WindowManagerService::OnWindowEvent(Event event, const sptr<IRemoteObject>& remoteObject)
859 {
860     if (event == Event::REMOTE_DIED) {
861         PostVoidSyncTask([this, &remoteObject]() {
862             uint32_t windowId = windowRoot_->GetWindowIdByObject(remoteObject);
863             auto node = windowRoot_->GetWindowNode(windowId);
864             if (node == nullptr) {
865                 WLOGFD("window node is nullptr, REMOTE_DIED no need to destroy");
866                 return;
867             }
868             WLOGI("window %{public}u received REMOTE_DIED", windowId);
869             node->stateMachine_.SetDestroyTaskParam(true);
870             auto func = [this, windowId]() {
871                 auto node = windowRoot_->GetWindowNode(windowId);
872                 if (node == nullptr) {
873                     WLOGFD("window node is nullptr");
874                     return;
875                 }
876                 if (node->GetWindowType() == WindowType::WINDOW_TYPE_DRAGGING_EFFECT) {
877                     dragController_->FinishDrag(windowId);
878                 }
879                 WindowInnerManager::GetInstance().NotifyWindowRemovedOrDestroyed(windowId);
880                 windowController_->DestroyWindow(windowId, node->stateMachine_.GetDestroyTaskParam());
881             };
882 
883             if (node->GetWindowType() == WindowType::WINDOW_TYPE_DESKTOP) {
884                 RemoteAnimation::OnRemoteDie(remoteObject);
885             }
886             if (RemoteAnimation::IsRemoteAnimationEnabledAndFirst(node->GetDisplayId()) &&
887                 node->stateMachine_.IsRemoteAnimationPlaying()) {
888                 WLOGFI("set destroy task windowId:%{public}u", node->GetWindowId());
889                 node->stateMachine_.SetDestroyTask(func);
890                 handler_->PostTask(func, "destroyTimeOutTask", 6000); // 6000 is time out 6s
891                 return;
892             }
893             func();
894         });
895     }
896 }
897 
NotifyDisplayStateChange(DisplayId defaultDisplayId,sptr<DisplayInfo> displayInfo,const std::map<DisplayId,sptr<DisplayInfo>> & displayInfoMap,DisplayStateChangeType type)898 void WindowManagerService::NotifyDisplayStateChange(DisplayId defaultDisplayId, sptr<DisplayInfo> displayInfo,
899     const std::map<DisplayId, sptr<DisplayInfo>>& displayInfoMap, DisplayStateChangeType type)
900 {
901     HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "wms:NotifyDisplayStateChange(%u)", type);
902     DisplayId displayId = (displayInfo == nullptr) ? DISPLAY_ID_INVALID : displayInfo->GetDisplayId();
903     if (type == DisplayStateChangeType::FREEZE) {
904         freezeDisplayController_->FreezeDisplay(displayId);
905     } else if (type == DisplayStateChangeType::UNFREEZE) {
906         freezeDisplayController_->UnfreezeDisplay(displayId);
907         /*
908          * Set 'InnerInputManager Listener' to MMI, ensure that the listener
909          * for move/drag won't be replaced by freeze-display-window
910          */
911         WindowInnerManager::GetInstance().SetInputEventConsumer();
912     } else {
913         PostAsyncTask([this, defaultDisplayId, displayInfo, displayInfoMap, type]() mutable {
914             windowController_->NotifyDisplayStateChange(defaultDisplayId, displayInfo, displayInfoMap, type);
915         });
916     }
917 }
918 
OnDisplayStateChange(DisplayId defaultDisplayId,sptr<DisplayInfo> displayInfo,const std::map<DisplayId,sptr<DisplayInfo>> & displayInfoMap,DisplayStateChangeType type)919 void DisplayChangeListener::OnDisplayStateChange(DisplayId defaultDisplayId, sptr<DisplayInfo> displayInfo,
920     const std::map<DisplayId, sptr<DisplayInfo>>& displayInfoMap, DisplayStateChangeType type)
921 {
922     WindowManagerService::GetInstance().NotifyDisplayStateChange(defaultDisplayId, displayInfo, displayInfoMap, type);
923 }
924 
OnScreenshot(DisplayId displayId)925 void DisplayChangeListener::OnScreenshot(DisplayId displayId)
926 {
927     WindowManagerService::GetInstance().OnScreenshot(displayId);
928 }
929 
NotifyServerReadyToMoveOrDrag(uint32_t windowId,sptr<WindowProperty> & windowProperty,sptr<MoveDragProperty> & moveDragProperty)930 void WindowManagerService::NotifyServerReadyToMoveOrDrag(uint32_t windowId, sptr<WindowProperty>& windowProperty,
931     sptr<MoveDragProperty>& moveDragProperty)
932 {
933     if (windowProperty == nullptr || moveDragProperty == nullptr) {
934         WLOGFE("windowProperty or moveDragProperty is invalid");
935         return;
936     }
937 
938     PostAsyncTask([this, windowId, windowProperty, moveDragProperty]() mutable {
939         if (moveDragProperty->startDragFlag_ || moveDragProperty->startMoveFlag_) {
940             bool res = WindowInnerManager::GetInstance().NotifyServerReadyToMoveOrDrag(windowId,
941                 windowProperty, moveDragProperty);
942             if (!res) {
943                 WLOGFE("invalid operation");
944                 return;
945             }
946             windowController_->InterceptInputEventToServer(windowId);
947         }
948         windowController_->NotifyServerReadyToMoveOrDrag(windowId, moveDragProperty);
949     });
950 }
951 
ProcessPointDown(uint32_t windowId,bool isPointDown)952 void WindowManagerService::ProcessPointDown(uint32_t windowId, bool isPointDown)
953 {
954     PostAsyncTask([this, windowId, isPointDown]() {
955         windowController_->ProcessPointDown(windowId, isPointDown);
956     });
957 }
958 
ProcessPointUp(uint32_t windowId)959 void WindowManagerService::ProcessPointUp(uint32_t windowId)
960 {
961     PostAsyncTask([this, windowId]() {
962         WindowInnerManager::GetInstance().NotifyWindowEndUpMovingOrDragging(windowId);
963         windowController_->RecoverInputEventToClient(windowId);
964         windowController_->ProcessPointUp(windowId);
965     });
966 }
967 
NotifyWindowClientPointUp(uint32_t windowId,const std::shared_ptr<MMI::PointerEvent> & pointerEvent)968 void WindowManagerService::NotifyWindowClientPointUp(uint32_t windowId,
969     const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
970 {
971     PostAsyncTask([this, windowId, pointerEvent]() mutable {
972         windowController_->NotifyWindowClientPointUp(windowId, pointerEvent);
973     });
974 }
975 
MinimizeAllAppWindows(DisplayId displayId)976 void WindowManagerService::MinimizeAllAppWindows(DisplayId displayId)
977 {
978     if (!Permission::IsSystemCalling()) {
979         WLOGFE("minimize all appWindows permission denied!");
980         return;
981     }
982     PostAsyncTask([this, displayId]() {
983         HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "wms:MinimizeAllAppWindows(%" PRIu64")", displayId);
984         WLOGFI("displayId %{public}" PRIu64"", displayId);
985         windowController_->MinimizeAllAppWindows(displayId);
986     });
987 }
988 
ToggleShownStateForAllAppWindows()989 WMError WindowManagerService::ToggleShownStateForAllAppWindows()
990 {
991     if (!Permission::IsSystemCalling()) {
992         WLOGFE("toggle shown state for all appwindows permission denied!");
993         return WMError::WM_ERROR_INVALID_PERMISSION;
994     }
995     PostAsyncTask([this]() {
996         HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "wms:ToggleShownStateForAllAppWindows");
997         return windowController_->ToggleShownStateForAllAppWindows();
998     });
999     return WMError::WM_OK;
1000 }
1001 
GetTopWindowId(uint32_t mainWinId,uint32_t & topWinId)1002 WMError WindowManagerService::GetTopWindowId(uint32_t mainWinId, uint32_t& topWinId)
1003 {
1004     return PostSyncTask([this, &topWinId, mainWinId]() {
1005         return windowController_->GetTopWindowId(mainWinId, topWinId);
1006     });
1007 }
1008 
SetWindowLayoutMode(WindowLayoutMode mode)1009 WMError WindowManagerService::SetWindowLayoutMode(WindowLayoutMode mode)
1010 {
1011     if (!Permission::IsSystemCalling()) {
1012         WLOGFE("set window layout mode permission denied!");
1013         return WMError::WM_ERROR_INVALID_PERMISSION;
1014     }
1015     return PostSyncTask([this, mode]() {
1016         WLOGFI("layoutMode: %{public}u", mode);
1017         HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "wms:SetWindowLayoutMode");
1018         return windowController_->SetWindowLayoutMode(mode);
1019     });
1020 }
1021 
UpdateProperty(sptr<WindowProperty> & windowProperty,PropertyChangeAction action,bool isAsyncTask)1022 WMError WindowManagerService::UpdateProperty(sptr<WindowProperty>& windowProperty, PropertyChangeAction action,
1023     bool isAsyncTask)
1024 {
1025     if (windowProperty == nullptr) {
1026         WLOGFE("windowProperty is nullptr");
1027         return WMError::WM_ERROR_NULLPTR;
1028     }
1029     if ((windowProperty->GetWindowFlags() == static_cast<uint32_t>(WindowFlag::WINDOW_FLAG_FORBID_SPLIT_MOVE) ||
1030         action == PropertyChangeAction::ACTION_UPDATE_TRANSFORM_PROPERTY) &&
1031         !Permission::IsSystemCalling()) {
1032         WLOGFE("SetForbidSplitMove or SetShowWhenLocked or SetTranform or SetTurnScreenOn permission denied!");
1033         return WMError::WM_ERROR_INVALID_PERMISSION;
1034     }
1035 
1036     if (action == PropertyChangeAction::ACTION_UPDATE_TRANSFORM_PROPERTY) {
1037         return PostSyncTask([this, windowProperty, action]() mutable {
1038             windowController_->UpdateProperty(windowProperty, action);
1039             return WMError::WM_OK;
1040         });
1041     }
1042 
1043     if (isAsyncTask || action == PropertyChangeAction::ACTION_UPDATE_RECT) {
1044         PostAsyncTask([this, windowProperty, action]() mutable {
1045             HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "wms:UpdateProperty");
1046             WMError res = windowController_->UpdateProperty(windowProperty, action);
1047             if (action == PropertyChangeAction::ACTION_UPDATE_RECT && res == WMError::WM_OK &&
1048                 windowProperty->GetWindowSizeChangeReason() == WindowSizeChangeReason::MOVE) {
1049                 dragController_->UpdateDragInfo(windowProperty->GetWindowId());
1050             }
1051         });
1052         return WMError::WM_OK;
1053     }
1054 
1055     return PostSyncTask([this, &windowProperty, action]() {
1056         HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "wms:UpdateProperty");
1057         WMError res = windowController_->UpdateProperty(windowProperty, action);
1058         if (action == PropertyChangeAction::ACTION_UPDATE_RECT && res == WMError::WM_OK &&
1059             windowProperty->GetWindowSizeChangeReason() == WindowSizeChangeReason::MOVE) {
1060             dragController_->UpdateDragInfo(windowProperty->GetWindowId());
1061         }
1062         return res;
1063     });
1064 }
1065 
GetAccessibilityWindowInfo(std::vector<sptr<AccessibilityWindowInfo>> & infos)1066 WMError WindowManagerService::GetAccessibilityWindowInfo(std::vector<sptr<AccessibilityWindowInfo>>& infos)
1067 {
1068     if (!Permission::IsSystemServiceCalling()) {
1069         WLOGFE("get accessibility window info permission denied!");
1070         return WMError::WM_ERROR_INVALID_PERMISSION;
1071     }
1072     return PostSyncTask([this, &infos]() {
1073         return windowController_->GetAccessibilityWindowInfo(infos);
1074     });
1075 }
1076 
GetVisibilityWindowInfo(std::vector<sptr<WindowVisibilityInfo>> & infos)1077 WMError WindowManagerService::GetVisibilityWindowInfo(std::vector<sptr<WindowVisibilityInfo>>& infos)
1078 {
1079     return PostSyncTask([this, &infos]() {
1080         return windowController_->GetVisibilityWindowInfo(infos);
1081     });
1082 }
1083 
GetSnapshot(int32_t windowId)1084 std::shared_ptr<Media::PixelMap> WindowManagerService::GetSnapshot(int32_t windowId)
1085 {
1086     return nullptr;
1087 }
1088 
NotifyDumpInfoResult(const std::vector<std::string> & info)1089 void WindowManagerService::NotifyDumpInfoResult(const std::vector<std::string>& info)
1090 {
1091     if (windowDumper_) {
1092         windowDumper_->dumpInfoFuture_.SetValue(info);
1093     }
1094 }
1095 
GetSystemConfig(SystemConfig & systemConfig)1096 WMError WindowManagerService::GetSystemConfig(SystemConfig& systemConfig)
1097 {
1098     systemConfig = systemConfig_;
1099     return WMError::WM_OK;
1100 }
1101 
GetModeChangeHotZones(DisplayId displayId,ModeChangeHotZones & hotZones)1102 WMError WindowManagerService::GetModeChangeHotZones(DisplayId displayId, ModeChangeHotZones& hotZones)
1103 {
1104     if (!hotZonesConfig_.isModeChangeHotZoneConfigured_) {
1105         return WMError::WM_DO_NOTHING;
1106     }
1107 
1108     return windowController_->GetModeChangeHotZones(displayId, hotZones, hotZonesConfig_);
1109 }
1110 
MinimizeWindowsByLauncher(std::vector<uint32_t> windowIds,bool isAnimated,sptr<RSIWindowAnimationFinishedCallback> & finishCallback)1111 void WindowManagerService::MinimizeWindowsByLauncher(std::vector<uint32_t> windowIds, bool isAnimated,
1112     sptr<RSIWindowAnimationFinishedCallback>& finishCallback)
1113 {
1114     if (!Permission::IsSystemCalling()) {
1115         WLOGFE("minimize windows by launcher permission denied!");
1116         return;
1117     }
1118     PostVoidSyncTask([this, windowIds, isAnimated, &finishCallback]() mutable {
1119         windowController_->MinimizeWindowsByLauncher(windowIds, isAnimated, finishCallback);
1120     });
1121 }
1122 
UpdateAvoidAreaListener(uint32_t windowId,bool haveAvoidAreaListener)1123 WMError WindowManagerService::UpdateAvoidAreaListener(uint32_t windowId, bool haveAvoidAreaListener)
1124 {
1125     return PostSyncTask([this, windowId, haveAvoidAreaListener]() {
1126         sptr<WindowNode> node = windowRoot_->GetWindowNode(windowId);
1127         if (node == nullptr) {
1128             WLOGFE("get window node failed. win %{public}u", windowId);
1129             return WMError::WM_DO_NOTHING;
1130         }
1131         sptr<WindowNodeContainer> container = windowRoot_->GetWindowNodeContainer(node->GetDisplayId());
1132         if (container == nullptr) {
1133             WLOGFE("get container failed. win %{public}u display %{public}" PRIu64"", windowId, node->GetDisplayId());
1134             return WMError::WM_DO_NOTHING;
1135         }
1136         container->UpdateAvoidAreaListener(node, haveAvoidAreaListener);
1137         return WMError::WM_OK;
1138     });
1139 }
1140 
SetAnchorAndScale(int32_t x,int32_t y,float scale)1141 void WindowManagerService::SetAnchorAndScale(int32_t x, int32_t y, float scale)
1142 {
1143     PostAsyncTask([this, x, y, scale]() {
1144         windowController_->SetAnchorAndScale(x, y, scale);
1145     });
1146 }
1147 
SetAnchorOffset(int32_t deltaX,int32_t deltaY)1148 void WindowManagerService::SetAnchorOffset(int32_t deltaX, int32_t deltaY)
1149 {
1150     PostAsyncTask([this, deltaX, deltaY]() {
1151         windowController_->SetAnchorOffset(deltaX, deltaY);
1152     });
1153 }
1154 
OffWindowZoom()1155 void WindowManagerService::OffWindowZoom()
1156 {
1157     PostAsyncTask([this]() {
1158         windowController_->OffWindowZoom();
1159     });
1160 }
1161 
UpdateRsTree(uint32_t windowId,bool isAdd)1162 WMError WindowManagerService::UpdateRsTree(uint32_t windowId, bool isAdd)
1163 {
1164     return PostSyncTask([this, windowId, isAdd]() {
1165         return windowRoot_->UpdateRsTree(windowId, isAdd);
1166     });
1167 }
1168 
OnScreenshot(DisplayId displayId)1169 void WindowManagerService::OnScreenshot(DisplayId displayId)
1170 {
1171     PostAsyncTask([this, displayId]() {
1172         windowController_->OnScreenshot(displayId);
1173     });
1174 }
1175 
BindDialogTarget(uint32_t & windowId,sptr<IRemoteObject> targetToken)1176 WMError WindowManagerService::BindDialogTarget(uint32_t& windowId, sptr<IRemoteObject> targetToken)
1177 {
1178     if (!Permission::IsSystemCalling()) {
1179         WLOGFE("bind dialog target permission denied!");
1180         return WMError::WM_ERROR_INVALID_PERMISSION;
1181     }
1182     return PostSyncTask([this, &windowId, targetToken]() {
1183         return windowController_->BindDialogTarget(windowId, targetToken);
1184     });
1185 }
1186 
HasPrivateWindow(DisplayId displayId,bool & hasPrivateWindow)1187 void WindowManagerService::HasPrivateWindow(DisplayId displayId, bool& hasPrivateWindow)
1188 {
1189     PostVoidSyncTask([this, displayId, &hasPrivateWindow]() mutable {
1190         hasPrivateWindow = windowRoot_->HasPrivateWindow(displayId);
1191     });
1192     WLOGFI("called %{public}u", hasPrivateWindow);
1193 }
1194 
HasPrivateWindow(DisplayId displayId,bool & hasPrivateWindow)1195 void WindowInfoQueriedListener::HasPrivateWindow(DisplayId displayId, bool& hasPrivateWindow)
1196 {
1197     WLOGFI("called");
1198     WindowManagerService::GetInstance().HasPrivateWindow(displayId, hasPrivateWindow);
1199 }
1200 
OnRSScreenConnected()1201 void WindowManagerService::OnRSScreenConnected()
1202 {
1203     PostAsyncTask([this]() {
1204         windowRoot_->SwitchRenderModeIfNeeded();
1205     });
1206 }
1207 
OnRSScreenDisconnected()1208 void WindowManagerService::OnRSScreenDisconnected()
1209 {
1210     PostAsyncTask([this]() {
1211         windowRoot_->SwitchRenderModeIfNeeded();
1212     });
1213 }
1214 
OnRenderModeChanged(bool isUniRender)1215 void WindowManagerService::OnRenderModeChanged(bool isUniRender)
1216 {
1217     PostAsyncTask([this, isUniRender]() {
1218         windowRoot_->OnRenderModeChanged(isUniRender);
1219     });
1220 }
1221 } // namespace Rosen
1222 } // namespace OHOS
1223