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