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 #include "anr_manager.h"
22
23 namespace OHOS::Rosen {
24 namespace {
25 constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, HILOG_DOMAIN_WINDOW, "ExtensionSession" };
26 } // namespace
27
ExtensionSession(const SessionInfo & info)28 ExtensionSession::ExtensionSession(const SessionInfo& info) : Session(info)
29 {
30 WLOGFD("Create extension session, bundleName: %{public}s, moduleName: %{public}s, abilityName: %{public}s.",
31 info.bundleName_.c_str(), info.moduleName_.c_str(), info.abilityName_.c_str());
32 GeneratePersistentId(true, info.persistentId_);
33 }
34
Connect(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)35 WSError ExtensionSession::Connect(
36 const sptr<ISessionStage>& sessionStage, const sptr<IWindowEventChannel>& eventChannel,
37 const std::shared_ptr<RSSurfaceNode>& surfaceNode, SystemSessionConfig& systemConfig,
38 sptr<WindowSessionProperty> property, sptr<IRemoteObject> token, int32_t pid, int32_t uid)
39 {
40 // Get pid and uid before posting task.
41 pid = pid == -1 ? IPCSkeleton::GetCallingRealPid() : pid;
42 uid = uid == -1 ? IPCSkeleton::GetCallingUid() : uid;
43 auto task = [weakThis = wptr(this), sessionStage, eventChannel, surfaceNode, &systemConfig, property, token, pid, uid]() {
44 auto session = weakThis.promote();
45 if (!session) {
46 WLOGFE("session is null");
47 return WSError::WS_ERROR_DESTROYED_OBJECT;
48 }
49 return session->Session::Connect(
50 sessionStage, eventChannel, surfaceNode, systemConfig, property, token, pid, uid);
51 };
52 return PostSyncTask(task, "Connect");
53 }
54
TransferAbilityResult(uint32_t resultCode,const AAFwk::Want & want)55 WSError ExtensionSession::TransferAbilityResult(uint32_t resultCode, const AAFwk::Want& want)
56 {
57 if (extSessionEventCallback_ != nullptr &&
58 extSessionEventCallback_->transferAbilityResultFunc_ != nullptr) {
59 extSessionEventCallback_->transferAbilityResultFunc_(resultCode, want);
60 }
61 return WSError::WS_OK;
62 }
63
TransferExtensionData(const AAFwk::WantParams & wantParams)64 WSError ExtensionSession::TransferExtensionData(const AAFwk::WantParams& wantParams)
65 {
66 if (extSessionEventCallback_ != nullptr &&
67 extSessionEventCallback_->transferExtensionDataFunc_ != nullptr) {
68 extSessionEventCallback_->transferExtensionDataFunc_(wantParams);
69 }
70 return WSError::WS_OK;
71 }
72
TransferComponentData(const AAFwk::WantParams & wantParams)73 WSError ExtensionSession::TransferComponentData(const AAFwk::WantParams& wantParams)
74 {
75 if (!IsSessionValid()) {
76 return WSError::WS_ERROR_INVALID_SESSION;
77 }
78 sessionStage_->NotifyTransferComponentData(wantParams);
79 return WSError::WS_OK;
80 }
81
TransferComponentDataSync(const AAFwk::WantParams & wantParams,AAFwk::WantParams & reWantParams)82 WSErrorCode ExtensionSession::TransferComponentDataSync(const AAFwk::WantParams& wantParams,
83 AAFwk::WantParams& reWantParams)
84 {
85 if (!IsSessionValid()) {
86 return WSErrorCode::WS_ERROR_TRANSFER_DATA_FAILED;
87 }
88 return sessionStage_->NotifyTransferComponentDataSync(wantParams, reWantParams);
89 }
90
NotifyRemoteReady()91 void ExtensionSession::NotifyRemoteReady()
92 {
93 if (extSessionEventCallback_ != nullptr &&
94 extSessionEventCallback_->notifyRemoteReadyFunc_ != nullptr) {
95 extSessionEventCallback_->notifyRemoteReadyFunc_();
96 }
97 }
98
NotifySyncOn()99 void ExtensionSession::NotifySyncOn()
100 {
101 if (extSessionEventCallback_ != nullptr &&
102 extSessionEventCallback_->notifySyncOnFunc_ != nullptr) {
103 extSessionEventCallback_->notifySyncOnFunc_();
104 }
105 }
106
NotifyAsyncOn()107 void ExtensionSession::NotifyAsyncOn()
108 {
109 if (extSessionEventCallback_ != nullptr &&
110 extSessionEventCallback_->notifyAsyncOnFunc_ != nullptr) {
111 extSessionEventCallback_->notifyAsyncOnFunc_();
112 }
113 }
114
TriggerBindModalUIExtension()115 void ExtensionSession::TriggerBindModalUIExtension()
116 {
117 if (isFirstTriggerBindModal_ && extSessionEventCallback_ != nullptr &&
118 extSessionEventCallback_->notifyBindModalFunc_ != nullptr) {
119 WLOGFD("Start calling bind modal func.");
120 extSessionEventCallback_->notifyBindModalFunc_();
121 isFirstTriggerBindModal_ = false;
122 }
123 }
124
RegisterExtensionSessionEventCallback(const sptr<ExtensionSessionEventCallback> & extSessionEventCallback)125 void ExtensionSession::RegisterExtensionSessionEventCallback(
126 const sptr<ExtensionSessionEventCallback>& extSessionEventCallback)
127 {
128 extSessionEventCallback_ = extSessionEventCallback;
129 }
130
GetExtensionSessionEventCallback()131 sptr<ExtensionSession::ExtensionSessionEventCallback> ExtensionSession::GetExtensionSessionEventCallback()
132 {
133 if (extSessionEventCallback_ == nullptr) {
134 extSessionEventCallback_ = new(std::nothrow) ExtensionSessionEventCallback();
135 }
136
137 return extSessionEventCallback_;
138 }
139
TransferAccessibilityEvent(const Accessibility::AccessibilityEventInfo & info,int64_t uiExtensionIdLevel)140 WSError ExtensionSession::TransferAccessibilityEvent(const Accessibility::AccessibilityEventInfo& info,
141 int64_t uiExtensionIdLevel)
142 {
143 NotifyTransferAccessibilityEvent(info, uiExtensionIdLevel);
144 return WSError::WS_OK;
145 }
146
UpdateAvoidArea(const sptr<AvoidArea> & avoidArea,AvoidAreaType type)147 WSError ExtensionSession::UpdateAvoidArea(const sptr<AvoidArea>& avoidArea, AvoidAreaType type)
148 {
149 if (!IsSessionValid()) {
150 return WSError::WS_ERROR_INVALID_SESSION;
151 }
152 return sessionStage_->UpdateAvoidArea(avoidArea, type);
153 }
154
GetAvoidAreaByType(AvoidAreaType type)155 AvoidArea ExtensionSession::GetAvoidAreaByType(AvoidAreaType type)
156 {
157 Rosen::AvoidArea avoidArea;
158 if (extSessionEventCallback_ != nullptr && extSessionEventCallback_->notifyGetAvoidAreaByTypeFunc_ != nullptr) {
159 avoidArea = extSessionEventCallback_->notifyGetAvoidAreaByTypeFunc_(type);
160 }
161 return avoidArea;
162 }
163
Background()164 WSError ExtensionSession::Background()
165 {
166 SessionState state = GetSessionState();
167 WLOGFI("[WMSLife] Background ExtensionSession, id: %{public}d, state: %{public}" PRIu32"", GetPersistentId(),
168 static_cast<uint32_t>(state));
169 if (state == SessionState::STATE_ACTIVE && GetWindowType() == WindowType::WINDOW_TYPE_UI_EXTENSION) {
170 UpdateSessionState(SessionState::STATE_INACTIVE);
171 state = SessionState::STATE_INACTIVE;
172 isActive_ = false;
173 }
174 if (state != SessionState::STATE_INACTIVE) {
175 WLOGFW("[WMSLife] Background state invalid! state:%{public}u", state);
176 return WSError::WS_ERROR_INVALID_SESSION;
177 }
178 UpdateSessionState(SessionState::STATE_BACKGROUND);
179 NotifyBackground();
180 DelayedSingleton<ANRManager>::GetInstance()->OnBackground(persistentId_);
181 return WSError::WS_OK;
182 }
183 } // namespace OHOS::Rosen
184