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 "session/host/include/extension_session.h"
17 
18 #include "ipc_skeleton.h"
19 
20 #include "window_manager_hilog.h"
21 
22 namespace OHOS::Rosen {
23 namespace {
24 constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, HILOG_DOMAIN_WINDOW, "ExtensionSession" };
25 } // namespace
26 
SetTransferKeyEventForConsumedParams(int32_t keyEventId, bool isPreImeEvent, const std::shared_ptr<std::promise<bool>>& isConsumedPromise, const std::shared_ptr<WSError>& retCode)27 void WindowEventChannelListener::SetTransferKeyEventForConsumedParams(int32_t keyEventId, bool isPreImeEvent,
28     const std::shared_ptr<std::promise<bool>>& isConsumedPromise, const std::shared_ptr<WSError>& retCode)
29 {
30     std::lock_guard<std::mutex> lock(transferKeyEventForConsumedMutex_);
31     keyEventId_ = keyEventId;
32     isPreImeEvent_ = isPreImeEvent;
33     retCode_ = retCode;
34     isConsumedPromise_ = isConsumedPromise;
35 }
36 
ResetTransferKeyEventForConsumedParams()37 void WindowEventChannelListener::ResetTransferKeyEventForConsumedParams()
38 {
39     std::lock_guard<std::mutex> lock(transferKeyEventForConsumedMutex_);
40     retCode_ = nullptr;
41     isConsumedPromise_ = nullptr;
42 }
43 
ResetTransferKeyEventForConsumedParams(bool isConsumed, WSError retCode)44 void WindowEventChannelListener::ResetTransferKeyEventForConsumedParams(bool isConsumed, WSError retCode)
45 {
46     std::lock_guard<std::mutex> lock(transferKeyEventForConsumedMutex_);
47     if (retCode_ != nullptr) {
48         *retCode_ = retCode;
49         retCode_ = nullptr;
50     }
51     if (isConsumedPromise_ != nullptr) {
52         isConsumedPromise_->set_value(isConsumed);
53         isConsumedPromise_ = nullptr;
54     }
55 }
56 
OnTransferKeyEventForConsumed(int32_t keyEventId, bool isPreImeEvent, bool isConsumed, WSError retCode)57 void WindowEventChannelListener::OnTransferKeyEventForConsumed(int32_t keyEventId, bool isPreImeEvent, bool isConsumed,
58     WSError retCode)
59 {
60     std::lock_guard<std::mutex> lock(transferKeyEventForConsumedMutex_);
61     if (keyEventId_ != keyEventId || isPreImeEvent_ != isPreImeEvent) {
62         TLOGW(WmsLogTag::WMS_EVENT, "The event has been processed at PreIme:%{public}d id:%{public}d.",
63             isPreImeEvent, keyEventId);
64         return;
65     }
66     if (isConsumedPromise_ == nullptr || retCode_ == nullptr) {
67         TLOGW(WmsLogTag::WMS_EVENT, "Promise or ret is null at PreIme:%{public}d id:%{public}d.",
68             isPreImeEvent, keyEventId);
69         return;
70     }
71 
72     *retCode_ = retCode;
73     retCode_ = nullptr;
74     isConsumedPromise_->set_value(isConsumed);
75     isConsumedPromise_ = nullptr;
76 }
77 
OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)78 int32_t WindowEventChannelListener::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply,
79     MessageOption& option)
80 {
81     if (data.ReadInterfaceToken() != GetDescriptor()) {
82         TLOGE(WmsLogTag::WMS_EVENT, "InterfaceToken check failed");
83         return ERR_TRANSACTION_FAILED;
84     }
85 
86     auto msgId = static_cast<WindowEventChannelListenerMessage>(code);
87     switch (msgId) {
88         case WindowEventChannelListenerMessage::TRANS_ID_ON_TRANSFER_KEY_EVENT_FOR_CONSUMED_ASYNC: {
89             int32_t keyEventId = 0;
90             bool isPreImeEvent = false;
91             bool isConsumed = false;
92             int32_t intRetCode = 0;
93             if (!data.ReadInt32(keyEventId) || !data.ReadBool(isPreImeEvent) || !data.ReadBool(isConsumed) ||
94                 !data.ReadInt32(intRetCode)) {
95                 TLOGE(WmsLogTag::WMS_EVENT, "Read keyEvent info failed");
96                 return ERR_TRANSACTION_FAILED;
97             }
98             WSError retCode = static_cast<WSError>(intRetCode);
99             OnTransferKeyEventForConsumed(keyEventId, isPreImeEvent, isConsumed, retCode);
100             break;
101         }
102         default:
103             TLOGE(WmsLogTag::WMS_EVENT, "unknown transaction code %{public}d", code);
104             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
105     }
106     return ERR_NONE;
107 }
108 
OnRemoteDied(const wptr<IRemoteObject>& wptrDeath)109 void ChannelDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& wptrDeath)
110 {
111     if (wptrDeath == nullptr) {
112         TLOGE(WmsLogTag::WMS_UIEXT, "wptrDeath is null");
113         return;
114     }
115 
116     sptr<IRemoteObject> object = wptrDeath.promote();
117     if (!object) {
118         TLOGE(WmsLogTag::WMS_UIEXT, "object is null");
119         return;
120     }
121 
122     if (listener_ == nullptr) {
123         TLOGE(WmsLogTag::WMS_UIEXT, "listener_ is null");
124         return;
125     }
126     TLOGE(WmsLogTag::WMS_UIEXT, "ChannelDeathRecipient OnRemoteDied");
127     listener_->ResetTransferKeyEventForConsumedParams(false, WSError::WS_ERROR_IPC_FAILED);
128 }
129 
ExtensionSession(const SessionInfo& info)130 ExtensionSession::ExtensionSession(const SessionInfo& info) : Session(info)
131 {
132     WLOGFD("Create extension session, bundleName: %{public}s, moduleName: %{public}s, abilityName: %{public}s.",
133         info.bundleName_.c_str(), info.moduleName_.c_str(), info.abilityName_.c_str());
134     GeneratePersistentId(true, info.persistentId_);
135 }
136 
~ExtensionSession()137 ExtensionSession::~ExtensionSession()
138 {
139     TLOGI(WmsLogTag::WMS_UIEXT, "realease extension session");
140     if (windowEventChannel_ == nullptr) {
141         TLOGE(WmsLogTag::WMS_UIEXT, "window event channel is null");
142         return;
143     }
144     sptr<IRemoteObject> remoteObject = windowEventChannel_->AsObject();
145     if (remoteObject == nullptr) {
146         TLOGE(WmsLogTag::WMS_UIEXT, "remoteObject is null");
147         return;
148     }
149     remoteObject->RemoveDeathRecipient(channelDeath_);
150     channelListener_ = nullptr;
151     channelDeath_ = nullptr;
152 }
153 
ConnectInner( const sptr<ISessionStage>& sessionStage, const sptr<IWindowEventChannel>& eventChannel, const std::shared_ptr<RSSurfaceNode>& surfaceNode, SystemSessionConfig& systemConfig, sptr<WindowSessionProperty> property, sptr<IRemoteObject> token, int32_t pid, int32_t uid, const std::string& identityToken)154 WSError ExtensionSession::ConnectInner(
155     const sptr<ISessionStage>& sessionStage, const sptr<IWindowEventChannel>& eventChannel,
156     const std::shared_ptr<RSSurfaceNode>& surfaceNode, SystemSessionConfig& systemConfig,
157     sptr<WindowSessionProperty> property, sptr<IRemoteObject> token, int32_t pid, int32_t uid,
158     const std::string& identityToken)
159 {
160     if (pid == INVALID_PID || uid == INVALID_UID) {
161         TLOGE(WmsLogTag::WMS_UIEXT, "invalid pid or uid");
162         return WSError::WS_ERROR_INVALID_PARAM;
163     }
164     auto task = [weakThis = wptr(this), sessionStage, eventChannel, surfaceNode,
165         &systemConfig, property, token, pid, uid]() {
166         auto session = weakThis.promote();
167         if (!session) {
168             TLOGE(WmsLogTag::WMS_UIEXT, "session is null");
169             return WSError::WS_ERROR_DESTROYED_OBJECT;
170         }
171 
172         if (eventChannel != nullptr) {
173             sptr<IRemoteObject> remoteObject = eventChannel->AsObject();
174             if (remoteObject == nullptr) {
175                 TLOGE(WmsLogTag::WMS_UIEXT, "remoteObject is null");
176                 return WSError::WS_ERROR_DESTROYED_OBJECT;
177             }
178 
179             session->channelListener_ = sptr<WindowEventChannelListener>::MakeSptr();
180             session->channelDeath_ = sptr<ChannelDeathRecipient>::MakeSptr(session->channelListener_);
181             if (remoteObject->IsProxyObject() && !remoteObject->AddDeathRecipient(session->channelDeath_)) {
182                 TLOGE(WmsLogTag::WMS_UIEXT, "Failed to add death recipient");
183                 return WSError::WS_ERROR_INTERNAL_ERROR;
184             }
185         }
186 
187         return session->Session::ConnectInner(
188             sessionStage, eventChannel, surfaceNode, systemConfig, property, token, pid, uid);
189     };
190     return PostSyncTask(task, "ConnectInner");
191 }
192 
Connect( const sptr<ISessionStage>& sessionStage, const sptr<IWindowEventChannel>& eventChannel, const std::shared_ptr<RSSurfaceNode>& surfaceNode, SystemSessionConfig& systemConfig, sptr<WindowSessionProperty> property, sptr<IRemoteObject> token, const std::string& identityToken)193 WSError ExtensionSession::Connect(
194     const sptr<ISessionStage>& sessionStage, const sptr<IWindowEventChannel>& eventChannel,
195     const std::shared_ptr<RSSurfaceNode>& surfaceNode, SystemSessionConfig& systemConfig,
196     sptr<WindowSessionProperty> property, sptr<IRemoteObject> token,
197     const std::string& identityToken)
198 {
199     // Get pid and uid before posting task.
200     int32_t pid = IPCSkeleton::GetCallingRealPid();
201     int32_t uid = IPCSkeleton::GetCallingUid();
202     return ConnectInner(sessionStage, eventChannel, surfaceNode, systemConfig,
203         property, token, pid, uid, identityToken);
204 }
205 
TransferAbilityResult(uint32_t resultCode, const AAFwk::Want& want)206 WSError ExtensionSession::TransferAbilityResult(uint32_t resultCode, const AAFwk::Want& want)
207 {
208     if (extSessionEventCallback_ != nullptr &&
209         extSessionEventCallback_->transferAbilityResultFunc_ != nullptr) {
210         extSessionEventCallback_->transferAbilityResultFunc_(resultCode, want);
211     }
212     return WSError::WS_OK;
213 }
214 
TransferExtensionData(const AAFwk::WantParams& wantParams)215 WSError ExtensionSession::TransferExtensionData(const AAFwk::WantParams& wantParams)
216 {
217     if (extSessionEventCallback_ != nullptr &&
218         extSessionEventCallback_->transferExtensionDataFunc_ != nullptr) {
219         extSessionEventCallback_->transferExtensionDataFunc_(wantParams);
220     }
221     return WSError::WS_OK;
222 }
223 
TransferComponentData(const AAFwk::WantParams& wantParams)224 WSError ExtensionSession::TransferComponentData(const AAFwk::WantParams& wantParams)
225 {
226     if (!IsSessionValid()) {
227         return WSError::WS_ERROR_INVALID_SESSION;
228     }
229     sessionStage_->NotifyTransferComponentData(wantParams);
230     return WSError::WS_OK;
231 }
232 
TransferComponentDataSync(const AAFwk::WantParams& wantParams, AAFwk::WantParams& reWantParams)233 WSErrorCode ExtensionSession::TransferComponentDataSync(const AAFwk::WantParams& wantParams,
234                                                         AAFwk::WantParams& reWantParams)
235 {
236     if (!IsSessionValid()) {
237         return WSErrorCode::WS_ERROR_TRANSFER_DATA_FAILED;
238     }
239     return sessionStage_->NotifyTransferComponentDataSync(wantParams, reWantParams);
240 }
241 
NotifySyncOn()242 void ExtensionSession::NotifySyncOn()
243 {
244     if (extSessionEventCallback_ != nullptr &&
245         extSessionEventCallback_->notifySyncOnFunc_ != nullptr) {
246         extSessionEventCallback_->notifySyncOnFunc_();
247     }
248 }
249 
NotifyAsyncOn()250 void ExtensionSession::NotifyAsyncOn()
251 {
252     if (extSessionEventCallback_ != nullptr &&
253         extSessionEventCallback_->notifyAsyncOnFunc_ != nullptr) {
254         extSessionEventCallback_->notifyAsyncOnFunc_();
255     }
256 }
257 
NotifyDensityFollowHost(bool isFollowHost, float densityValue)258 WSError ExtensionSession::NotifyDensityFollowHost(bool isFollowHost, float densityValue)
259 {
260     if (!IsSessionValid()) {
261         return WSError::WS_ERROR_INVALID_SESSION;
262     }
263     if (!sessionStage_) {
264         TLOGE(WmsLogTag::WMS_UIEXT, "session stage is null!");
265         return WSError::WS_ERROR_NULLPTR;
266     }
267 
268     return sessionStage_->NotifyDensityFollowHost(isFollowHost, densityValue);
269 }
270 
UpdateSessionViewportConfig(const SessionViewportConfig& config)271 WSError ExtensionSession::UpdateSessionViewportConfig(const SessionViewportConfig& config)
272 {
273     if (!IsSessionValid()) {
274         TLOGE(WmsLogTag::WMS_UIEXT, "session is invalid");
275         return WSError::WS_ERROR_INVALID_SESSION;
276     }
277     if (sessionStage_ == nullptr) {
278         TLOGE(WmsLogTag::WMS_UIEXT, "sessionStage_ is null");
279         return WSError::WS_ERROR_NULLPTR;
280     }
281     TLOGI(WmsLogTag::WMS_UIEXT, "winId: %{public}d, isDensityFollowHost_:%{public}d, "
282         "displayId:%{public}" PRIu64", density:%{public}f, orientation:%{public}d.",
283         GetPersistentId(), config.isDensityFollowHost_, config.displayId_, config.density_, config.orientation_);
284     return sessionStage_->UpdateSessionViewportConfig(config);
285 }
286 
TriggerBindModalUIExtension()287 void ExtensionSession::TriggerBindModalUIExtension()
288 {
289     if (isFirstTriggerBindModal_ && extSessionEventCallback_ != nullptr &&
290         extSessionEventCallback_->notifyBindModalFunc_ != nullptr) {
291         WLOGFD("Start calling bind modal func.");
292         extSessionEventCallback_->notifyBindModalFunc_();
293         isFirstTriggerBindModal_ = false;
294     }
295 }
296 
RegisterExtensionSessionEventCallback( const sptr<ExtensionSessionEventCallback>& extSessionEventCallback)297 void ExtensionSession::RegisterExtensionSessionEventCallback(
298     const sptr<ExtensionSessionEventCallback>& extSessionEventCallback)
299 {
300     extSessionEventCallback_ = extSessionEventCallback;
301 }
302 
TransferKeyEventForConsumed(const std::shared_ptr<MMI::KeyEvent>& keyEvent, bool& isConsumed, bool& isTimeout, bool isPreImeEvent)303 WSError ExtensionSession::TransferKeyEventForConsumed(const std::shared_ptr<MMI::KeyEvent>& keyEvent, bool& isConsumed,
304     bool& isTimeout, bool isPreImeEvent)
305 {
306     if (windowEventChannel_ == nullptr) {
307         TLOGE(WmsLogTag::WMS_EVENT, "windowEventChannel_ is null");
308         return WSError::WS_ERROR_NULLPTR;
309     }
310     if (keyEvent == nullptr) {
311         TLOGE(WmsLogTag::WMS_EVENT, "KeyEvent is nullptr");
312         return WSError::WS_ERROR_NULLPTR;
313     }
314     if (channelListener_ == nullptr) {
315         TLOGE(WmsLogTag::WMS_EVENT, "Created channelListener_ is nullptr.");
316         return WSError::WS_ERROR_NULLPTR;
317     }
318     int32_t keyEventId = keyEvent->GetId();
319     TLOGI(WmsLogTag::WMS_EVENT, "In with isPreImeEvent:%{public}d, id:%{public}d", isPreImeEvent, keyEventId);
320 
321     auto isConsumedPromise = std::make_shared<std::promise<bool>>();
322     std::shared_ptr<WSError> retCode = std::make_shared<WSError>(WSError::WS_OK);
323     channelListener_->SetTransferKeyEventForConsumedParams(keyEventId, isPreImeEvent, isConsumedPromise, retCode);
324     auto ret = windowEventChannel_->TransferKeyEventForConsumedAsync(keyEvent, isPreImeEvent, channelListener_);
325     // if UiExtension was died, return transferKeyEvent before wait for timeout.
326     if (ret != WSError::WS_OK) {
327         TLOGE(WmsLogTag::WMS_EVENT, "transfer keyEvent failed with %{public}d at PreIme:%{public}d id:%{public}d.",
328             ret, isPreImeEvent, keyEventId);
329         return ret;
330     }
331 
332     // Timeout cannot exceed APP_INPUT_BLOCK
333     constexpr int64_t TRANSFER_KEY_EVENT_TIMEOUT_TIME_MS = 4000;
334     auto isConsumedFuture = isConsumedPromise->get_future().share();
335     if (isConsumedFuture.wait_for(std::chrono::milliseconds(TRANSFER_KEY_EVENT_TIMEOUT_TIME_MS)) ==
336             std::future_status::timeout) {
337         // Prevents the pointer from being used by a remote ipc after its lifetime has ended.
338         channelListener_->ResetTransferKeyEventForConsumedParams();
339         isTimeout = true;
340     } else {
341         // Prevents the pointer from being used by a death recipient after its lifetime has ended.
342         channelListener_->ResetTransferKeyEventForConsumedParams();
343         isTimeout = false;
344         isConsumed = isConsumedFuture.get();
345         ret = *retCode;
346     }
347     TLOGI(WmsLogTag::WMS_EVENT, "isConsumed:%{public}d Timeout:%{public}d ret:%{public}d at PreIme:%{public}d "
348         "id:%{public}d.", isConsumed, isTimeout, ret, isPreImeEvent, keyEventId);
349     return ret;
350 }
351 
TransferKeyEventAsync(const std::shared_ptr<MMI::KeyEvent>& keyEvent, bool isPreImeEvent)352 WSError ExtensionSession::TransferKeyEventAsync(const std::shared_ptr<MMI::KeyEvent>& keyEvent, bool isPreImeEvent)
353 {
354     if (windowEventChannel_ == nullptr) {
355         TLOGE(WmsLogTag::WMS_EVENT, "windowEventChannel_ is null");
356         return WSError::WS_ERROR_NULLPTR;
357     }
358     if (keyEvent == nullptr) {
359         TLOGE(WmsLogTag::WMS_EVENT, "KeyEvent is nullptr");
360         return WSError::WS_ERROR_NULLPTR;
361     }
362 
363     TLOGI(WmsLogTag::WMS_EVENT, "In with isPreImeEvent(%{public}d), id:%{public}d", isPreImeEvent, keyEvent->GetId());
364     channelListener_->ResetTransferKeyEventForConsumedParams();
365     auto ret = windowEventChannel_->TransferKeyEventForConsumedAsync(keyEvent, isPreImeEvent, channelListener_);
366     TLOGI(WmsLogTag::WMS_EVENT, "ret is %{public}d in id:%{public}d.", ret, keyEvent->GetId());
367     return ret;
368 }
369 
GetExtensionSessionEventCallback()370 sptr<ExtensionSession::ExtensionSessionEventCallback> ExtensionSession::GetExtensionSessionEventCallback()
371 {
372     if (extSessionEventCallback_ == nullptr) {
373         extSessionEventCallback_ = sptr<ExtensionSessionEventCallback>::MakeSptr();
374     }
375     return extSessionEventCallback_;
376 }
377 
TransferAccessibilityEvent(const Accessibility::AccessibilityEventInfo& info, int64_t uiExtensionIdLevel)378 WSError ExtensionSession::TransferAccessibilityEvent(const Accessibility::AccessibilityEventInfo& info,
379     int64_t uiExtensionIdLevel)
380 {
381     NotifyTransferAccessibilityEvent(info, uiExtensionIdLevel);
382     return WSError::WS_OK;
383 }
384 
TransferAccessibilityHoverEvent( float pointX, float pointY, int32_t sourceType, int32_t eventType, int64_t timeMs)385 WSError ExtensionSession::TransferAccessibilityHoverEvent(
386     float pointX, float pointY, int32_t sourceType, int32_t eventType, int64_t timeMs)
387 {
388     if (!windowEventChannel_) {
389         TLOGE(WmsLogTag::WMS_UIEXT, "windowEventChannel_ is null");
390         return WSError::WS_ERROR_NULLPTR;
391     }
392     return windowEventChannel_->TransferAccessibilityHoverEvent(pointX, pointY, sourceType, eventType, timeMs);
393 }
394 
TransferAccessibilityChildTreeRegister( uint32_t windowId, int32_t treeId, int64_t accessibilityId)395 WSError ExtensionSession::TransferAccessibilityChildTreeRegister(
396     uint32_t windowId, int32_t treeId, int64_t accessibilityId)
397 {
398     if (!windowEventChannel_) {
399         TLOGE(WmsLogTag::WMS_UIEXT, "windowEventChannel_ is null");
400         return WSError::WS_ERROR_NULLPTR;
401     }
402     return windowEventChannel_->TransferAccessibilityChildTreeRegister(windowId, treeId, accessibilityId);
403 }
404 
TransferAccessibilityChildTreeUnregister()405 WSError ExtensionSession::TransferAccessibilityChildTreeUnregister()
406 {
407     if (!windowEventChannel_) {
408         TLOGE(WmsLogTag::WMS_UIEXT, "windowEventChannel_ is null");
409         return WSError::WS_ERROR_NULLPTR;
410     }
411     return windowEventChannel_->TransferAccessibilityChildTreeUnregister();
412 }
413 
TransferAccessibilityDumpChildInfo( const std::vector<std::string>& params, std::vector<std::string>& info)414 WSError ExtensionSession::TransferAccessibilityDumpChildInfo(
415     const std::vector<std::string>& params, std::vector<std::string>& info)
416 {
417     if (!windowEventChannel_) {
418         TLOGE(WmsLogTag::WMS_UIEXT, "windowEventChannel_ is null");
419         return WSError::WS_ERROR_NULLPTR;
420     }
421     return windowEventChannel_->TransferAccessibilityDumpChildInfo(params, info);
422 }
423 
UpdateAvoidArea(const sptr<AvoidArea>& avoidArea, AvoidAreaType type)424 WSError ExtensionSession::UpdateAvoidArea(const sptr<AvoidArea>& avoidArea, AvoidAreaType type)
425 {
426     if (!IsSessionValid()) {
427         return WSError::WS_ERROR_INVALID_SESSION;
428     }
429     return sessionStage_->UpdateAvoidArea(avoidArea, type);
430 }
431 
GetAvoidAreaByType(AvoidAreaType type)432 AvoidArea ExtensionSession::GetAvoidAreaByType(AvoidAreaType type)
433 {
434     Rosen::AvoidArea avoidArea;
435     if (extSessionEventCallback_ != nullptr && extSessionEventCallback_->notifyGetAvoidAreaByTypeFunc_ != nullptr) {
436         avoidArea = extSessionEventCallback_->notifyGetAvoidAreaByTypeFunc_(type);
437     }
438     return avoidArea;
439 }
440 
Background(bool isFromClient, const std::string& identityToken)441 WSError ExtensionSession::Background(bool isFromClient, const std::string& identityToken)
442 {
443     SessionState state = GetSessionState();
444     TLOGI(WmsLogTag::WMS_LIFE, "Background ExtensionSession, id: %{public}d, state: %{public}" PRIu32"",
445         GetPersistentId(), static_cast<uint32_t>(state));
446     if (state == SessionState::STATE_ACTIVE && GetWindowType() == WindowType::WINDOW_TYPE_UI_EXTENSION) {
447         UpdateSessionState(SessionState::STATE_INACTIVE);
448         state = SessionState::STATE_INACTIVE;
449         isActive_ = false;
450     }
451     if (state != SessionState::STATE_INACTIVE) {
452         WLOGFW("[WMSLife] Background state invalid! state:%{public}u", state);
453         return WSError::WS_ERROR_INVALID_SESSION;
454     }
455     UpdateSessionState(SessionState::STATE_BACKGROUND);
456     NotifyBackground();
457     return WSError::WS_OK;
458 }
459 
NotifyExtensionEventAsync(uint32_t notifyEvent)460 void ExtensionSession::NotifyExtensionEventAsync(uint32_t notifyEvent)
461 {
462     TLOGI(WmsLogTag::WMS_UIEXT, "Received extension event asynchronously, notifyEvent: %{public}d", notifyEvent);
463     if (extSessionEventCallback_ != nullptr && extSessionEventCallback_->notifyExtensionEventFunc_ != nullptr) {
464         extSessionEventCallback_->notifyExtensionEventFunc_(notifyEvent);
465     }
466 }
467 
NotifyDumpInfo(const std::vector<std::string>& params, std::vector<std::string>& info)468 WSError ExtensionSession::NotifyDumpInfo(const std::vector<std::string>& params, std::vector<std::string>& info)
469 {
470     TLOGI(WmsLogTag::WMS_UIEXT, "Notify dump, persistenId: %{public}d", GetPersistentId());
471     if (!IsSessionValid()) {
472         return WSError::WS_ERROR_INVALID_SESSION;
473     }
474     if (!sessionStage_) {
475         TLOGE(WmsLogTag::WMS_UIEXT, "session stage is null");
476         return WSError::WS_ERROR_NULLPTR;
477     }
478     return sessionStage_->NotifyDumpInfo(params, info);
479 }
480 } // namespace OHOS::Rosen
481