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 "display_manager_adapter_lite.h"
17
18 #include <iremote_broker.h>
19 #include <iservice_registry.h>
20 #include <system_ability_definition.h>
21
22 #include "display_manager_lite.h"
23 #include "dm_common.h"
24 #include "window_manager_hilog.h"
25 #include "zidl/mock_session_manager_service_interface.h"
26
27 namespace OHOS::Rosen {
28 namespace {
29 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "DisplayManagerAdapterLite"};
30 }
WM_IMPLEMENT_SINGLE_INSTANCE(DisplayManagerAdapterLite)31 WM_IMPLEMENT_SINGLE_INSTANCE(DisplayManagerAdapterLite)
32
33
34 #define INIT_PROXY_CHECK_RETURN(ret) \
35 do { \
36 if (!InitDMSProxy()) { \
37 WLOGFE("InitDMSProxy fail"); \
38 return ret; \
39 } \
40 } while (false)
41
42 DMError BaseAdapterLite::RegisterDisplayManagerAgent(const sptr<IDisplayManagerAgent>& displayManagerAgent,
43 DisplayManagerAgentType type)
44 {
45 INIT_PROXY_CHECK_RETURN(DMError::DM_ERROR_INIT_DMS_PROXY_LOCKED);
46
47 return displayManagerServiceProxy_->RegisterDisplayManagerAgent(displayManagerAgent, type);
48 }
49
UnregisterDisplayManagerAgent(const sptr<IDisplayManagerAgent> & displayManagerAgent,DisplayManagerAgentType type)50 DMError BaseAdapterLite::UnregisterDisplayManagerAgent(const sptr<IDisplayManagerAgent>& displayManagerAgent,
51 DisplayManagerAgentType type)
52 {
53 INIT_PROXY_CHECK_RETURN(DMError::DM_ERROR_INIT_DMS_PROXY_LOCKED);
54
55 return displayManagerServiceProxy_->UnregisterDisplayManagerAgent(displayManagerAgent, type);
56 }
57
InitDMSProxy()58 bool BaseAdapterLite::InitDMSProxy()
59 {
60 std::lock_guard<std::recursive_mutex> lock(mutex_);
61 if (!isProxyValid_) {
62 sptr<ISystemAbilityManager> systemAbilityManager =
63 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
64 if (!systemAbilityManager) {
65 WLOGFE("Failed to get system ability mgr.");
66 return false;
67 }
68 sptr<IRemoteObject> mockRemoteObject = systemAbilityManager->GetSystemAbility(WINDOW_MANAGER_SERVICE_ID);
69 if (!mockRemoteObject) {
70 WLOGFI("Remote object is nullptr");
71 return false;
72 }
73 auto mockSessionManagerServiceProxy_ = iface_cast<IMockSessionManagerInterface>(mockRemoteObject);
74 if (!mockSessionManagerServiceProxy_) {
75 WLOGFW("Get mock session manager service proxy failed, nullptr");
76 return false;
77 }
78
79 sptr<IRemoteObject> displayRemoteObject = mockSessionManagerServiceProxy_->GetScreenSessionManagerLite();
80 if (!displayRemoteObject) {
81 WLOGFE("displayRemoteObject is nullptr");
82 return false;
83 }
84 displayManagerServiceProxy_ = iface_cast<IScreenSessionManagerLite>(displayRemoteObject);
85 if ((!displayManagerServiceProxy_) || (!displayManagerServiceProxy_->AsObject())) {
86 WLOGFE("Failed to get system scene session manager services");
87 return false;
88 }
89 dmsDeath_ = new (std::nothrow) DMSDeathRecipientLite(*this);
90 if (!dmsDeath_) {
91 WLOGFE("Failed to create death Recipient ptr DMSDeathRecipientLite");
92 return false;
93 }
94 sptr<IRemoteObject> remoteObject = displayManagerServiceProxy_->AsObject();
95 if (remoteObject->IsProxyObject() && !remoteObject->AddDeathRecipient(dmsDeath_)) {
96 WLOGFE("Failed to add death recipient.");
97 return false;
98 }
99 isProxyValid_ = true;
100 }
101 return true;
102 }
103
GetDefaultDisplayInfo()104 sptr<DisplayInfo> DisplayManagerAdapterLite::GetDefaultDisplayInfo()
105 {
106 INIT_PROXY_CHECK_RETURN(nullptr);
107
108 return displayManagerServiceProxy_->GetDefaultDisplayInfo();
109 }
110
IsFoldable()111 bool DisplayManagerAdapterLite::IsFoldable()
112 {
113 INIT_PROXY_CHECK_RETURN(false);
114
115 return displayManagerServiceProxy_->IsFoldable();
116 }
117
GetFoldStatus()118 FoldStatus DisplayManagerAdapterLite::GetFoldStatus()
119 {
120 INIT_PROXY_CHECK_RETURN(FoldStatus::UNKNOWN);
121
122 return displayManagerServiceProxy_->GetFoldStatus();
123 }
124
GetFoldDisplayMode()125 FoldDisplayMode DisplayManagerAdapterLite::GetFoldDisplayMode()
126 {
127 INIT_PROXY_CHECK_RETURN(FoldDisplayMode::UNKNOWN);
128
129 return displayManagerServiceProxy_->GetFoldDisplayMode();
130 }
131
GetDisplayInfo(DisplayId displayId)132 sptr<DisplayInfo> DisplayManagerAdapterLite::GetDisplayInfo(DisplayId displayId)
133 {
134 if (displayId == DISPLAY_ID_INVALID) {
135 WLOGFW("screen id is invalid");
136 return nullptr;
137 }
138 INIT_PROXY_CHECK_RETURN(nullptr);
139
140 return displayManagerServiceProxy_->GetDisplayInfoById(displayId);
141 }
142
GetCutoutInfo(DisplayId displayId)143 sptr<CutoutInfo> DisplayManagerAdapterLite::GetCutoutInfo(DisplayId displayId)
144 {
145 if (displayId == DISPLAY_ID_INVALID) {
146 WLOGFE("screen id is invalid");
147 return nullptr;
148 }
149 INIT_PROXY_CHECK_RETURN(nullptr);
150 return displayManagerServiceProxy_->GetCutoutInfo(displayId);
151 }
152
153
DMSDeathRecipientLite(BaseAdapterLite & adapter)154 DMSDeathRecipientLite::DMSDeathRecipientLite(BaseAdapterLite& adapter) : adapter_(adapter)
155 {
156 }
157
OnRemoteDied(const wptr<IRemoteObject> & wptrDeath)158 void DMSDeathRecipientLite::OnRemoteDied(const wptr<IRemoteObject>& wptrDeath)
159 {
160 if (wptrDeath == nullptr) {
161 WLOGFE("wptrDeath is nullptr");
162 return;
163 }
164
165 sptr<IRemoteObject> object = wptrDeath.promote();
166 if (!object) {
167 WLOGFE("object is nullptr");
168 return;
169 }
170 WLOGFI("dms OnRemoteDied");
171 adapter_.Clear();
172 SingletonContainer::Get<DisplayManagerLite>().OnRemoteDied();
173 return;
174 }
175
~BaseAdapterLite()176 BaseAdapterLite::~BaseAdapterLite()
177 {
178 WLOGFI("BaseAdapterLite destroy");
179 std::lock_guard<std::recursive_mutex> lock(mutex_);
180 displayManagerServiceProxy_ = nullptr;
181 }
182
Clear()183 void BaseAdapterLite::Clear()
184 {
185 WLOGFI("BaseAdapterLite Clear");
186 std::lock_guard<std::recursive_mutex> lock(mutex_);
187 if ((displayManagerServiceProxy_ != nullptr) && (displayManagerServiceProxy_->AsObject() != nullptr)) {
188 displayManagerServiceProxy_->AsObject()->RemoveDeathRecipient(dmsDeath_);
189 }
190 isProxyValid_ = false;
191 }
192 } // namespace OHOS::Rosen