1 /*
2 * Copyright (c) 2021-2024 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 "app_state_callback_proxy.h"
17
18 #include "configuration.h"
19 #include "ipc_types.h"
20
21 #include "hilog_tag_wrapper.h"
22
23 namespace OHOS {
24 namespace AppExecFwk {
AppStateCallbackProxy(const sptr<IRemoteObject> & impl)25 AppStateCallbackProxy::AppStateCallbackProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IAppStateCallback>(impl)
26 {}
27
WriteInterfaceToken(MessageParcel & data)28 bool AppStateCallbackProxy::WriteInterfaceToken(MessageParcel &data)
29 {
30 if (!data.WriteInterfaceToken(AppStateCallbackProxy::GetDescriptor())) {
31 TAG_LOGE(AAFwkTag::APPMGR, "write interface token failed");
32 return false;
33 }
34 return true;
35 }
36
OnAbilityRequestDone(const sptr<IRemoteObject> & token,const AbilityState state)37 void AppStateCallbackProxy::OnAbilityRequestDone(const sptr<IRemoteObject> &token, const AbilityState state)
38 {
39 TAG_LOGD(AAFwkTag::APPMGR, "begin");
40 MessageParcel data;
41 MessageParcel reply;
42 MessageOption option(MessageOption::TF_ASYNC);
43 if (!WriteInterfaceToken(data)) {
44 return;
45 }
46
47 if (token) {
48 if (!data.WriteBool(true) || !data.WriteRemoteObject(token.GetRefPtr())) {
49 TAG_LOGE(AAFwkTag::APPMGR, "Failed to write flag and token");
50 return;
51 }
52 } else {
53 if (!data.WriteBool(false)) {
54 TAG_LOGE(AAFwkTag::APPMGR, "Failed to write flag");
55 return;
56 }
57 }
58
59 int32_t abilityState = static_cast<int32_t>(state);
60 data.WriteInt32(abilityState);
61 int32_t ret = SendTransactCmd(
62 static_cast<uint32_t>(IAppStateCallback::Message::TRANSACT_ON_ABILITY_REQUEST_DONE), data, reply, option);
63 if (ret != NO_ERROR) {
64 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
65 }
66 TAG_LOGD(AAFwkTag::APPMGR, "end");
67 }
68
OnAppStateChanged(const AppProcessData & appProcessData)69 void AppStateCallbackProxy::OnAppStateChanged(const AppProcessData &appProcessData)
70 {
71 TAG_LOGD(AAFwkTag::APPMGR, "begin");
72 MessageParcel data;
73 MessageParcel reply;
74 MessageOption option(MessageOption::TF_ASYNC);
75 if (!WriteInterfaceToken(data)) {
76 return;
77 }
78 data.WriteParcelable(&appProcessData);
79 int32_t ret = SendTransactCmd(
80 static_cast<uint32_t>(IAppStateCallback::Message::TRANSACT_ON_APP_STATE_CHANGED), data, reply, option);
81 if (ret != NO_ERROR) {
82 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
83 }
84 TAG_LOGD(AAFwkTag::APPMGR, "end");
85 }
86
NotifyConfigurationChange(const AppExecFwk::Configuration & config,int32_t userId)87 void AppStateCallbackProxy::NotifyConfigurationChange(const AppExecFwk::Configuration &config, int32_t userId)
88 {
89 MessageParcel data;
90 MessageParcel reply;
91 MessageOption option(MessageOption::TF_ASYNC);
92 if (!WriteInterfaceToken(data)) {
93 TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
94 return;
95 }
96 if (!data.WriteParcelable(&config)) {
97 TAG_LOGE(AAFwkTag::APPMGR, "Write config failed.");
98 return;
99 }
100 if (!data.WriteInt32(userId)) {
101 TAG_LOGE(AAFwkTag::APPMGR, "Write usr failed.");
102 return;
103 }
104 auto error = SendTransactCmd(
105 static_cast<uint32_t>(IAppStateCallback::Message::TRANSACT_ON_NOTIFY_CONFIG_CHANGE), data, reply, option);
106 if (error != NO_ERROR) {
107 TAG_LOGE(AAFwkTag::APPMGR, "Send config error: %{public}d", error);
108 }
109 }
110
NotifyStartResidentProcess(std::vector<AppExecFwk::BundleInfo> & bundleInfos)111 void AppStateCallbackProxy::NotifyStartResidentProcess(std::vector<AppExecFwk::BundleInfo> &bundleInfos)
112 {
113 MessageParcel data;
114 MessageParcel reply;
115 MessageOption option(MessageOption::TF_ASYNC);
116 if (!WriteInterfaceToken(data)) {
117 TAG_LOGE(AAFwkTag::APPMGR, "WriteInterfaceToken failed");
118 return;
119 }
120
121 if (!data.WriteInt32(bundleInfos.size())) {
122 TAG_LOGE(AAFwkTag::APPMGR, "write bundle info size failed.");
123 return;
124 }
125
126 for (auto &bundleInfo : bundleInfos) {
127 if (!data.WriteParcelable(&bundleInfo)) {
128 TAG_LOGE(AAFwkTag::APPMGR, "write bundle info failed");
129 return;
130 }
131 }
132 auto ret = SendTransactCmd(
133 static_cast<uint32_t>(IAppStateCallback::Message::TRANSACT_ON_NOTIFY_START_RESIDENT_PROCESS),
134 data, reply, option);
135 if (ret != NO_ERROR) {
136 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
137 }
138 }
139
NotifyStartKeepAliveProcess(std::vector<AppExecFwk::BundleInfo> & bundleInfos)140 void AppStateCallbackProxy::NotifyStartKeepAliveProcess(std::vector<AppExecFwk::BundleInfo> &bundleInfos)
141 {
142 MessageParcel data;
143 MessageParcel reply;
144 MessageOption option(MessageOption::TF_ASYNC);
145 if (!WriteInterfaceToken(data)) {
146 TAG_LOGE(AAFwkTag::APPMGR, "WriteInterfaceToken failed");
147 return;
148 }
149
150 if (!data.WriteInt32(bundleInfos.size())) {
151 TAG_LOGE(AAFwkTag::APPMGR, "write bundle info size failed.");
152 return;
153 }
154
155 for (auto &bundleInfo : bundleInfos) {
156 if (!data.WriteParcelable(&bundleInfo)) {
157 TAG_LOGE(AAFwkTag::APPMGR, "write bundle info failed");
158 return;
159 }
160 }
161 auto ret = SendTransactCmd(
162 static_cast<uint32_t>(IAppStateCallback::Message::TRANSACT_ON_NOTIFY_START_KEEP_ALIVE_PROCESS),
163 data, reply, option);
164 if (ret != NO_ERROR) {
165 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
166 }
167 }
168
OnAppRemoteDied(const std::vector<sptr<IRemoteObject>> & abilityTokens)169 void AppStateCallbackProxy::OnAppRemoteDied(const std::vector<sptr<IRemoteObject>> &abilityTokens)
170 {
171 MessageParcel data;
172 MessageParcel reply;
173 MessageOption option(MessageOption::TF_ASYNC);
174 if (!WriteInterfaceToken(data)) {
175 TAG_LOGE(AAFwkTag::APPMGR, "WriteInterfaceToken failed");
176 return;
177 }
178
179 if (!data.WriteInt32(abilityTokens.size())) {
180 TAG_LOGE(AAFwkTag::APPMGR, "write token size failed.");
181 return;
182 }
183
184 for (auto &token : abilityTokens) {
185 if (!data.WriteRemoteObject(token.GetRefPtr())) {
186 TAG_LOGE(AAFwkTag::APPMGR, "write token failed");
187 return;
188 }
189 }
190 auto ret = SendTransactCmd(
191 static_cast<uint32_t>(IAppStateCallback::Message::TRANSACT_ON_APP_REMOTE_DIED),
192 data, reply, option);
193 if (ret != NO_ERROR) {
194 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
195 }
196 }
197
OnStartProcessFailed(const std::vector<sptr<IRemoteObject>> & abilityTokens)198 void AppStateCallbackProxy::OnStartProcessFailed(const std::vector<sptr<IRemoteObject>> &abilityTokens)
199 {
200 MessageParcel data;
201 MessageParcel reply;
202 MessageOption option(MessageOption::TF_ASYNC);
203 if (!WriteInterfaceToken(data)) {
204 TAG_LOGE(AAFwkTag::APPMGR, "WriteInterfaceToken failed");
205 return;
206 }
207
208 if (!data.WriteInt32(abilityTokens.size())) {
209 TAG_LOGE(AAFwkTag::APPMGR, "write token size failed.");
210 return;
211 }
212 for (auto &token : abilityTokens) {
213 if (!data.WriteRemoteObject(token.GetRefPtr())) {
214 TAG_LOGE(AAFwkTag::APPMGR, "write token failed");
215 return;
216 }
217 }
218 auto ret = SendTransactCmd(
219 static_cast<uint32_t>(IAppStateCallback::Message::TRANSACT_ON_START_PROCESS_FAILED),
220 data, reply, option);
221 if (ret != NO_ERROR) {
222 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
223 }
224 }
225
OnCacheExitInfo(uint32_t accessTokenId,const RunningProcessInfo & exitInfo,const std::string & bundleName,const std::vector<std::string> & abilityNames,const std::vector<std::string> & uiExtensionNames)226 void AppStateCallbackProxy::OnCacheExitInfo(uint32_t accessTokenId, const RunningProcessInfo &exitInfo,
227 const std::string &bundleName, const std::vector<std::string> &abilityNames,
228 const std::vector<std::string> &uiExtensionNames)
229 {
230 MessageParcel data;
231 MessageParcel reply;
232 MessageOption option(MessageOption::TF_ASYNC);
233 if (!WriteInterfaceToken(data)) {
234 TAG_LOGE(AAFwkTag::APPMGR, "WriteInterfaceToken failed");
235 return;
236 }
237 if (!data.WriteUint32(accessTokenId)) {
238 TAG_LOGE(AAFwkTag::APPMGR, "accessTokenId write failed");
239 return;
240 }
241 if (!data.WriteParcelable(&exitInfo)) {
242 TAG_LOGE(AAFwkTag::APPMGR, "exitInfo write failed");
243 return;
244 }
245 if (!data.WriteString(bundleName)) {
246 TAG_LOGE(AAFwkTag::APPMGR, "bundleName write failed");
247 return;
248 }
249 if (!data.WriteStringVector(abilityNames)) {
250 TAG_LOGE(AAFwkTag::APPMGR, "abilityNames write failed");
251 return;
252 }
253 if (!data.WriteStringVector(uiExtensionNames)) {
254 TAG_LOGE(AAFwkTag::APPMGR, "uiExtensionNames write failed");
255 return;
256 }
257 auto ret = SendTransactCmd(
258 static_cast<uint32_t>(IAppStateCallback::Message::TRANSACT_ON_CACHE_EXIT_INFO),
259 data, reply, option);
260 if (ret != NO_ERROR) {
261 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
262 }
263
264 }
265
NotifyAppPreCache(int32_t pid,int32_t userId)266 void AppStateCallbackProxy::NotifyAppPreCache(int32_t pid, int32_t userId)
267 {
268 MessageParcel data;
269 MessageParcel reply;
270 MessageOption option(MessageOption::TF_ASYNC);
271 if (!WriteInterfaceToken(data)) {
272 TAG_LOGE(AAFwkTag::APPMGR, "WriteInterfaceToken failed");
273 return;
274 }
275
276 if (!data.WriteInt32(pid)) {
277 TAG_LOGE(AAFwkTag::APPMGR, "write pid failed.");
278 return;
279 }
280
281 if (!data.WriteInt32(userId)) {
282 TAG_LOGE(AAFwkTag::APPMGR, "write userId failed.");
283 return;
284 }
285
286 auto ret = SendTransactCmd(
287 static_cast<uint32_t>(IAppStateCallback::Message::TRANSACT_ON_APP_PRE_CACHE),
288 data, reply, option);
289 if (ret != NO_ERROR) {
290 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
291 }
292 }
293
SendTransactCmd(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)294 int32_t AppStateCallbackProxy::SendTransactCmd(uint32_t code, MessageParcel &data,
295 MessageParcel &reply, MessageOption &option)
296 {
297 sptr<IRemoteObject> remote = Remote();
298 if (remote == nullptr) {
299 TAG_LOGE(AAFwkTag::APPMGR, "Remote is nullptr.");
300 return ERR_NULL_OBJECT;
301 }
302
303 auto ret = remote->SendRequest(code, data, reply, option);
304 if (ret != NO_ERROR) {
305 TAG_LOGE(AAFwkTag::APPMGR, "Send request failed with error code: %{public}d", ret);
306 return ret;
307 }
308 return ret;
309 }
310 } // namespace AppExecFwk
311 } // namespace OHOS
312