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 "application_state_observer_proxy.h"
17
18 #include "hilog_tag_wrapper.h"
19 #include "ipc_types.h"
20
21
22 namespace OHOS {
23 namespace AppExecFwk {
24 namespace {
25 const int32_t ERR_INVALID_STUB = 32;
26 }
ApplicationStateObserverProxy(const sptr<IRemoteObject> & impl)27 ApplicationStateObserverProxy::ApplicationStateObserverProxy(
28 const sptr<IRemoteObject> &impl) : IRemoteProxy<IApplicationStateObserver>(impl)
29 {}
30
WriteInterfaceToken(MessageParcel & data)31 bool ApplicationStateObserverProxy::WriteInterfaceToken(MessageParcel &data)
32 {
33 if (!data.WriteInterfaceToken(ApplicationStateObserverProxy::GetDescriptor())) {
34 TAG_LOGE(AAFwkTag::APPMGR, "write interface token failed");
35 return false;
36 }
37 return true;
38 }
39
OnForegroundApplicationChanged(const AppStateData & appStateData)40 void ApplicationStateObserverProxy::OnForegroundApplicationChanged(const AppStateData &appStateData)
41 {
42 MessageParcel data;
43 MessageParcel reply;
44 MessageOption option(MessageOption::TF_ASYNC);
45 if (!WriteInterfaceToken(data)) {
46 return;
47 }
48 data.WriteParcelable(&appStateData);
49 int32_t ret = SendTransactCmd(
50 static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_FOREGROUND_APPLICATION_CHANGED),
51 data, reply, option);
52 if (ret != NO_ERROR && ret != ERR_INVALID_STUB) {
53 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d, bundleName: %{public}s.",
54 ret, appStateData.bundleName.c_str());
55 }
56 }
57
OnAbilityStateChanged(const AbilityStateData & abilityStateData)58 void ApplicationStateObserverProxy::OnAbilityStateChanged(const AbilityStateData &abilityStateData)
59 {
60 MessageParcel data;
61 MessageParcel reply;
62 MessageOption option(MessageOption::TF_ASYNC);
63 if (!WriteInterfaceToken(data)) {
64 return;
65 }
66 data.WriteParcelable(&abilityStateData);
67 int32_t ret = SendTransactCmd(
68 static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_ABILITY_STATE_CHANGED),
69 data, reply, option);
70 if (ret != NO_ERROR && ret != ERR_INVALID_STUB) {
71 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is wrong, error code: %{public}d, bundleName: %{public}s.",
72 ret, abilityStateData.bundleName.c_str());
73 }
74 }
75
OnExtensionStateChanged(const AbilityStateData & abilityStateData)76 void ApplicationStateObserverProxy::OnExtensionStateChanged(const AbilityStateData &abilityStateData)
77 {
78 MessageParcel data;
79 MessageParcel reply;
80 MessageOption option(MessageOption::TF_ASYNC);
81 if (!WriteInterfaceToken(data)) {
82 return;
83 }
84 data.WriteParcelable(&abilityStateData);
85 int32_t ret = SendTransactCmd(
86 static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_EXTENSION_STATE_CHANGED),
87 data, reply, option);
88 if (ret != NO_ERROR && ret != ERR_INVALID_STUB) {
89 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is wrong, error code: %{public}d, bundleName:%{public}s.",
90 ret, abilityStateData.bundleName.c_str());
91 }
92 }
93
OnProcessCreated(const ProcessData & processData)94 void ApplicationStateObserverProxy::OnProcessCreated(const ProcessData &processData)
95 {
96 MessageParcel data;
97 MessageParcel reply;
98 MessageOption option(MessageOption::TF_ASYNC);
99 if (!WriteInterfaceToken(data)) {
100 return;
101 }
102 data.WriteParcelable(&processData);
103 int32_t ret = SendTransactCmd(
104 static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_PROCESS_CREATED),
105 data, reply, option);
106 if (ret != NO_ERROR && ret != ERR_INVALID_STUB) {
107 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is wrong, error code: %{public}d, bundleName:%{public}s.",
108 ret, processData.bundleName.c_str());
109 }
110 }
111
OnProcessReused(const ProcessData & processData)112 void ApplicationStateObserverProxy::OnProcessReused(const ProcessData &processData)
113 {
114 TAG_LOGD(AAFwkTag::APPMGR, "start");
115 MessageParcel data;
116 MessageParcel reply;
117 MessageOption option(MessageOption::TF_ASYNC);
118 if (!WriteInterfaceToken(data)) {
119 return;
120 }
121 data.WriteParcelable(&processData);
122 int32_t ret = SendTransactCmd(
123 static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_PROCESS_REUSED),
124 data, reply, option);
125 if (ret != NO_ERROR && ret != ERR_INVALID_STUB) {
126 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is wrong, error code: %{public}d, bundleName:%{public}s.",
127 ret, processData.bundleName.c_str());
128 }
129 }
130
OnProcessStateChanged(const ProcessData & processData)131 void ApplicationStateObserverProxy::OnProcessStateChanged(const ProcessData &processData)
132 {
133 MessageParcel data;
134 MessageParcel reply;
135 MessageOption option(MessageOption::TF_ASYNC);
136 if (!WriteInterfaceToken(data)) {
137 return;
138 }
139 data.WriteParcelable(&processData);
140 int32_t ret = SendTransactCmd(
141 static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_PROCESS_STATE_CHANGED),
142 data, reply, option);
143 if (ret != NO_ERROR && ret != ERR_INVALID_STUB) {
144 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is wrong, error code: %{public}d, bundleName:%{public}s.",
145 ret, processData.bundleName.c_str());
146 }
147 TAG_LOGD(AAFwkTag::APPMGR, "end");
148 }
149
OnProcessDied(const ProcessData & processData)150 void ApplicationStateObserverProxy::OnProcessDied(const ProcessData &processData)
151 {
152 MessageParcel data;
153 MessageParcel reply;
154 MessageOption option(MessageOption::TF_ASYNC);
155 if (!WriteInterfaceToken(data)) {
156 return;
157 }
158 data.WriteParcelable(&processData);
159 int32_t ret = SendTransactCmd(
160 static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_PROCESS_DIED),
161 data, reply, option);
162 if (ret != NO_ERROR && ret != ERR_INVALID_STUB) {
163 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is wrong, error code: %{public}d, bundleName:%{public}s.",
164 ret, processData.bundleName.c_str());
165 }
166 }
167
OnApplicationStateChanged(const AppStateData & appStateData)168 void ApplicationStateObserverProxy::OnApplicationStateChanged(const AppStateData &appStateData)
169 {
170 MessageParcel data;
171 MessageParcel reply;
172 MessageOption option(MessageOption::TF_ASYNC);
173 if (!WriteInterfaceToken(data)) {
174 TAG_LOGE(AAFwkTag::APPMGR, "WriteInterfaceToken failed");
175 return;
176 }
177 data.WriteParcelable(&appStateData);
178 int32_t ret = SendTransactCmd(
179 static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_APPLICATION_STATE_CHANGED),
180 data, reply, option);
181 if (ret != NO_ERROR && ret != ERR_INVALID_STUB) {
182 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d, bundleName: %{public}s.",
183 ret, appStateData.bundleName.c_str());
184 }
185 }
186
OnAppStateChanged(const AppStateData & appStateData)187 void ApplicationStateObserverProxy::OnAppStateChanged(const AppStateData &appStateData)
188 {
189 MessageParcel data;
190 MessageParcel reply;
191 MessageOption option(MessageOption::TF_ASYNC);
192 if (!WriteInterfaceToken(data)) {
193 TAG_LOGE(AAFwkTag::APPMGR, "WriteInterfaceToken failed");
194 return;
195 }
196 data.WriteParcelable(&appStateData);
197 int32_t ret = SendTransactCmd(
198 static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_APP_STATE_CHANGED),
199 data, reply, option);
200 if (ret != NO_ERROR && ret != ERR_INVALID_STUB) {
201 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d, , bundleName: %{public}s",
202 ret, appStateData.bundleName.c_str());
203 }
204 }
205
OnAppStarted(const AppStateData & appStateData)206 void ApplicationStateObserverProxy::OnAppStarted(const AppStateData &appStateData)
207 {
208 MessageParcel data;
209 MessageParcel reply;
210 MessageOption option(MessageOption::TF_ASYNC);
211 if (!WriteInterfaceToken(data)) {
212 TAG_LOGE(AAFwkTag::APPMGR, "WriteInterfaceToken failed");
213 return;
214 }
215 data.WriteParcelable(&appStateData);
216 int32_t ret = SendTransactCmd(
217 static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_APP_STARTED),
218 data, reply, option);
219 if (ret != NO_ERROR && ret != ERR_INVALID_STUB) {
220 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d, bundleName: %{public}s.",
221 ret, appStateData.bundleName.c_str());
222 }
223 }
224
OnAppStopped(const AppStateData & appStateData)225 void ApplicationStateObserverProxy::OnAppStopped(const AppStateData &appStateData)
226 {
227 MessageParcel data;
228 MessageParcel reply;
229 MessageOption option(MessageOption::TF_ASYNC);
230 if (!WriteInterfaceToken(data)) {
231 TAG_LOGE(AAFwkTag::APPMGR, "OnAppStopped, WriteInterfaceToken failed");
232 return;
233 }
234 data.WriteParcelable(&appStateData);
235 int32_t ret = SendTransactCmd(
236 static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_APP_STOPPED),
237 data, reply, option);
238 if (ret != NO_ERROR && ret != ERR_INVALID_STUB) {
239 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d, bundleName: %{public}s.",
240 ret, appStateData.bundleName.c_str());
241 }
242 }
243
OnPageShow(const PageStateData & pageStateData)244 void ApplicationStateObserverProxy::OnPageShow(const PageStateData &pageStateData)
245 {
246 MessageParcel data;
247 MessageParcel reply;
248 MessageOption option(MessageOption::TF_ASYNC);
249 if (!WriteInterfaceToken(data)) {
250 TAG_LOGE(AAFwkTag::APPMGR, "WriteInterfaceToken failed");
251 return;
252 }
253 data.WriteParcelable(&pageStateData);
254 int32_t ret = SendTransactCmd(
255 static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_PAGE_SHOW),
256 data, reply, option);
257 if (ret != NO_ERROR && ret != ERR_INVALID_STUB) {
258 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d, bundleName: %{public}s",
259 ret, pageStateData.bundleName.c_str());
260 }
261 }
262
OnPageHide(const PageStateData & pageStateData)263 void ApplicationStateObserverProxy::OnPageHide(const PageStateData &pageStateData)
264 {
265 MessageParcel data;
266 MessageParcel reply;
267 MessageOption option(MessageOption::TF_ASYNC);
268 if (!WriteInterfaceToken(data)) {
269 TAG_LOGE(AAFwkTag::APPMGR, "WriteInterfaceToken failed");
270 return;
271 }
272 data.WriteParcelable(&pageStateData);
273 int32_t ret = SendTransactCmd(
274 static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_PAGE_HIDE),
275 data, reply, option);
276 if (ret != NO_ERROR && ret != ERR_INVALID_STUB) {
277 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d, bundleName: %{public}s",
278 ret, pageStateData.bundleName.c_str());
279 }
280 }
281
OnAppCacheStateChanged(const AppStateData & appStateData)282 void ApplicationStateObserverProxy::OnAppCacheStateChanged(const AppStateData &appStateData)
283 {
284 MessageParcel data;
285 MessageParcel reply;
286 MessageOption option(MessageOption::TF_ASYNC);
287 if (!WriteInterfaceToken(data)) {
288 TAG_LOGE(AAFwkTag::APPMGR, "WriteInterfaceToken failed");
289 return;
290 }
291 data.WriteParcelable(&appStateData);
292 int32_t ret = SendTransactCmd(
293 static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_APP_CACHE_STATE_CHANGED),
294 data, reply, option);
295 if (ret != NO_ERROR && ret != ERR_INVALID_STUB) {
296 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d, bundleName: %{public}s.",
297 ret, appStateData.bundleName.c_str());
298 }
299 }
300
SendTransactCmd(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)301 int32_t ApplicationStateObserverProxy::SendTransactCmd(uint32_t code, MessageParcel &data,
302 MessageParcel &reply, MessageOption &option)
303 {
304 sptr<IRemoteObject> remote = Remote();
305 if (remote == nullptr) {
306 TAG_LOGE(AAFwkTag::APPMGR, "Remote is nullptr.");
307 return ERR_NULL_OBJECT;
308 }
309
310 return remote->SendRequest(code, data, reply, option);
311 }
312 } // namespace AppExecFwk
313 } // namespace OHOS
314