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