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
OnAppRemoteDied(const std::vector<sptr<IRemoteObject>> & abilityTokens)140 void AppStateCallbackProxy::OnAppRemoteDied(const std::vector<sptr<IRemoteObject>> &abilityTokens)
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(abilityTokens.size())) {
151 TAG_LOGE(AAFwkTag::APPMGR, "write token size failed.");
152 return;
153 }
154
155 for (auto &token : abilityTokens) {
156 if (!data.WriteRemoteObject(token.GetRefPtr())) {
157 TAG_LOGE(AAFwkTag::APPMGR, "write token failed");
158 return;
159 }
160 }
161 auto ret = SendTransactCmd(
162 static_cast<uint32_t>(IAppStateCallback::Message::TRANSACT_ON_APP_REMOTE_DIED),
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
NotifyAppPreCache(int32_t pid,int32_t userId)169 void AppStateCallbackProxy::NotifyAppPreCache(int32_t pid, int32_t userId)
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(pid)) {
180 TAG_LOGE(AAFwkTag::APPMGR, "write pid failed.");
181 return;
182 }
183
184 if (!data.WriteInt32(userId)) {
185 TAG_LOGE(AAFwkTag::APPMGR, "write userId failed.");
186 return;
187 }
188
189 auto ret = SendTransactCmd(
190 static_cast<uint32_t>(IAppStateCallback::Message::TRANSACT_ON_APP_PRE_CACHE),
191 data, reply, option);
192 if (ret != NO_ERROR) {
193 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
194 }
195 }
196
SendTransactCmd(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)197 int32_t AppStateCallbackProxy::SendTransactCmd(uint32_t code, MessageParcel &data,
198 MessageParcel &reply, MessageOption &option)
199 {
200 sptr<IRemoteObject> remote = Remote();
201 if (remote == nullptr) {
202 TAG_LOGE(AAFwkTag::APPMGR, "Remote is nullptr.");
203 return ERR_NULL_OBJECT;
204 }
205
206 auto ret = remote->SendRequest(code, data, reply, option);
207 if (ret != NO_ERROR) {
208 TAG_LOGE(AAFwkTag::APPMGR, "Send request failed with error code: %{public}d", ret);
209 return ret;
210 }
211 return ret;
212 }
213 } // namespace AppExecFwk
214 } // namespace OHOS
215