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(sptr<IRemoteObject> token)198 void AppStateCallbackProxy::OnStartProcessFailed(sptr<IRemoteObject> token)
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.WriteRemoteObject(token.GetRefPtr())) {
209 TAG_LOGE(AAFwkTag::APPMGR, "write token failed");
210 return;
211 }
212 auto ret = SendTransactCmd(
213 static_cast<uint32_t>(IAppStateCallback::Message::TRANSACT_ON_START_PROCESS_FAILED),
214 data, reply, option);
215 if (ret != NO_ERROR) {
216 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
217 }
218 }
219
OnCacheExitInfo(uint32_t accessTokenId,const AAFwk::LastExitDetailInfo & exitInfo,const std::string & bundleName,const std::vector<std::string> & abilityNames,const std::vector<std::string> & uiExtensionNames)220 void AppStateCallbackProxy::OnCacheExitInfo(uint32_t accessTokenId, const AAFwk::LastExitDetailInfo &exitInfo,
221 const std::string &bundleName, const std::vector<std::string> &abilityNames,
222 const std::vector<std::string> &uiExtensionNames)
223 {
224 MessageParcel data;
225 MessageParcel reply;
226 MessageOption option(MessageOption::TF_ASYNC);
227 if (!WriteInterfaceToken(data)) {
228 TAG_LOGE(AAFwkTag::APPMGR, "WriteInterfaceToken failed");
229 return;
230 }
231 if (!data.WriteUint32(accessTokenId)) {
232 TAG_LOGE(AAFwkTag::APPMGR, "accessTokenId write failed");
233 return;
234 }
235 if (!data.WriteParcelable(&exitInfo)) {
236 TAG_LOGE(AAFwkTag::APPMGR, "exitInfo write failed");
237 return;
238 }
239 if (!data.WriteString(bundleName)) {
240 TAG_LOGE(AAFwkTag::APPMGR, "bundleName write failed");
241 return;
242 }
243 if (!data.WriteStringVector(abilityNames)) {
244 TAG_LOGE(AAFwkTag::APPMGR, "abilityNames write failed");
245 return;
246 }
247 if (!data.WriteStringVector(uiExtensionNames)) {
248 TAG_LOGE(AAFwkTag::APPMGR, "uiExtensionNames write failed");
249 return;
250 }
251 auto ret = SendTransactCmd(
252 static_cast<uint32_t>(IAppStateCallback::Message::TRANSACT_ON_CACHE_EXIT_INFO),
253 data, reply, option);
254 if (ret != NO_ERROR) {
255 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
256 }
257 }
258
NotifyAppPreCache(int32_t pid,int32_t userId)259 void AppStateCallbackProxy::NotifyAppPreCache(int32_t pid, int32_t userId)
260 {
261 MessageParcel data;
262 MessageParcel reply;
263 MessageOption option(MessageOption::TF_ASYNC);
264 if (!WriteInterfaceToken(data)) {
265 TAG_LOGE(AAFwkTag::APPMGR, "WriteInterfaceToken failed");
266 return;
267 }
268
269 if (!data.WriteInt32(pid)) {
270 TAG_LOGE(AAFwkTag::APPMGR, "write pid failed.");
271 return;
272 }
273
274 if (!data.WriteInt32(userId)) {
275 TAG_LOGE(AAFwkTag::APPMGR, "write userId failed.");
276 return;
277 }
278
279 auto ret = SendTransactCmd(
280 static_cast<uint32_t>(IAppStateCallback::Message::TRANSACT_ON_APP_PRE_CACHE),
281 data, reply, option);
282 if (ret != NO_ERROR) {
283 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
284 }
285 }
286
SendTransactCmd(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)287 int32_t AppStateCallbackProxy::SendTransactCmd(uint32_t code, MessageParcel &data,
288 MessageParcel &reply, MessageOption &option)
289 {
290 sptr<IRemoteObject> remote = Remote();
291 if (remote == nullptr) {
292 TAG_LOGE(AAFwkTag::APPMGR, "Remote is nullptr.");
293 return ERR_NULL_OBJECT;
294 }
295
296 auto ret = remote->SendRequest(code, data, reply, option);
297 if (ret != NO_ERROR) {
298 TAG_LOGE(AAFwkTag::APPMGR, "Send request failed with error code: %{public}d", ret);
299 return ret;
300 }
301 return ret;
302 }
303 } // namespace AppExecFwk
304 } // namespace OHOS
305