• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     if (!data.WriteParcelable(&appStateData)) {
49         TAG_LOGE(AAFwkTag::APPMGR, "write profile failed");
50         return;
51     }
52     int32_t ret = SendTransactCmd(
53         static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_FOREGROUND_APPLICATION_CHANGED),
54         data, reply, option);
55     if (ret != NO_ERROR && ret != ERR_INVALID_STUB) {
56         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d, bundleName: %{public}s.",
57             ret, appStateData.bundleName.c_str());
58     }
59 }
60 
OnAbilityStateChanged(const AbilityStateData & abilityStateData)61 void ApplicationStateObserverProxy::OnAbilityStateChanged(const AbilityStateData &abilityStateData)
62 {
63     MessageParcel data;
64     MessageParcel reply;
65     MessageOption option(MessageOption::TF_ASYNC);
66     if (!WriteInterfaceToken(data)) {
67         return;
68     }
69     if (!data.WriteParcelable(&abilityStateData)) {
70         TAG_LOGD(AAFwkTag::APPMGR, "write profile failed");
71         return;
72     }
73     int32_t ret = SendTransactCmd(
74         static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_ABILITY_STATE_CHANGED),
75         data, reply, option);
76     if (ret != NO_ERROR && ret != ERR_INVALID_STUB) {
77         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is wrong, error code: %{public}d, bundleName: %{public}s.",
78             ret, abilityStateData.bundleName.c_str());
79     }
80 }
81 
OnExtensionStateChanged(const AbilityStateData & abilityStateData)82 void ApplicationStateObserverProxy::OnExtensionStateChanged(const AbilityStateData &abilityStateData)
83 {
84     MessageParcel data;
85     MessageParcel reply;
86     MessageOption option(MessageOption::TF_ASYNC);
87     if (!WriteInterfaceToken(data)) {
88         return;
89     }
90     if (!data.WriteParcelable(&abilityStateData)) {
91         TAG_LOGE(AAFwkTag::APPMGR, "write abilityStateData failed");
92         return;
93     }
94     int32_t ret = SendTransactCmd(
95         static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_EXTENSION_STATE_CHANGED),
96         data, reply, option);
97     if (ret != NO_ERROR && ret != ERR_INVALID_STUB) {
98         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is wrong, error code: %{public}d, bundleName:%{public}s.",
99             ret, abilityStateData.bundleName.c_str());
100     }
101 }
102 
OnProcessCreated(const ProcessData & processData)103 void ApplicationStateObserverProxy::OnProcessCreated(const ProcessData &processData)
104 {
105     MessageParcel data;
106     MessageParcel reply;
107     MessageOption option(MessageOption::TF_ASYNC);
108     if (!WriteInterfaceToken(data)) {
109         return;
110     }
111     if (!data.WriteParcelable(&processData)) {
112         TAG_LOGE(AAFwkTag::APPMGR, "write processData failed");
113         return;
114     }
115     int32_t ret = SendTransactCmd(
116         static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_PROCESS_CREATED),
117         data, reply, option);
118     if (ret != NO_ERROR && ret != ERR_INVALID_STUB) {
119         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is wrong, error code: %{public}d, bundleName:%{public}s.",
120             ret, processData.bundleName.c_str());
121     }
122 }
123 
OnProcessReused(const ProcessData & processData)124 void ApplicationStateObserverProxy::OnProcessReused(const ProcessData &processData)
125 {
126     TAG_LOGD(AAFwkTag::APPMGR, "start");
127     MessageParcel data;
128     MessageParcel reply;
129     MessageOption option(MessageOption::TF_ASYNC);
130     if (!WriteInterfaceToken(data)) {
131         return;
132     }
133     if (!data.WriteParcelable(&processData)) {
134         TAG_LOGE(AAFwkTag::APPMGR, "write processData failed");
135         return;
136     }
137 
138     int32_t ret = SendTransactCmd(
139         static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_PROCESS_REUSED),
140         data, reply, option);
141     if (ret != NO_ERROR && ret != ERR_INVALID_STUB) {
142         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is wrong, error code: %{public}d, bundleName:%{public}s.",
143             ret, processData.bundleName.c_str());
144     }
145 }
146 
OnProcessStateChanged(const ProcessData & processData)147 void ApplicationStateObserverProxy::OnProcessStateChanged(const ProcessData &processData)
148 {
149     MessageParcel data;
150     MessageParcel reply;
151     MessageOption option(MessageOption::TF_ASYNC);
152     if (!WriteInterfaceToken(data)) {
153         return;
154     }
155     if (!data.WriteParcelable(&processData)) {
156         TAG_LOGE(AAFwkTag::APPMGR, "write processData failed");
157         return;
158     }
159     int32_t ret = SendTransactCmd(
160         static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_PROCESS_STATE_CHANGED),
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     TAG_LOGD(AAFwkTag::APPMGR, "end");
167 }
168 
OnWindowShow(const ProcessData & processData)169 void ApplicationStateObserverProxy::OnWindowShow(const ProcessData &processData)
170 {
171     MessageParcel data;
172     MessageParcel reply;
173     MessageOption option(MessageOption::TF_ASYNC);
174     if (!WriteInterfaceToken(data)) {
175         return;
176     }
177     if (!data.WriteParcelable(&processData)) {
178         TAG_LOGE(AAFwkTag::APPMGR, "write processData failed");
179         return;
180     }
181     int32_t ret = SendTransactCmd(
182         static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_WINDOW_SHOW),
183         data, reply, option);
184     if (ret != NO_ERROR && ret != ERR_INVALID_STUB) {
185         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is wrong, error code: %{public}d, bundleName:%{public}s.",
186             ret, processData.bundleName.c_str());
187     }
188 }
189 
OnWindowHidden(const ProcessData & processData)190 void ApplicationStateObserverProxy::OnWindowHidden(const ProcessData &processData)
191 {
192     MessageParcel data;
193     MessageParcel reply;
194     MessageOption option(MessageOption::TF_ASYNC);
195     if (!WriteInterfaceToken(data)) {
196         return;
197     }
198     if (!data.WriteParcelable(&processData)) {
199         TAG_LOGE(AAFwkTag::APPMGR, "write processData failed");
200         return;
201     }
202     int32_t ret = SendTransactCmd(
203         static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_WINDOW_HIDDEN),
204         data, reply, option);
205     if (ret != NO_ERROR && ret != ERR_INVALID_STUB) {
206         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is wrong, error code: %{public}d, bundleName:%{public}s.",
207             ret, processData.bundleName.c_str());
208     }
209 }
210 
OnProcessDied(const ProcessData & processData)211 void ApplicationStateObserverProxy::OnProcessDied(const ProcessData &processData)
212 {
213     MessageParcel data;
214     MessageParcel reply;
215     MessageOption option(MessageOption::TF_ASYNC);
216     if (!WriteInterfaceToken(data)) {
217         return;
218     }
219     if (!data.WriteParcelable(&processData)) {
220         TAG_LOGE(AAFwkTag::APPMGR, "write processData failed");
221         return;
222     }
223     int32_t ret = SendTransactCmd(
224         static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_PROCESS_DIED),
225         data, reply, option);
226     if (ret != NO_ERROR && ret != ERR_INVALID_STUB) {
227         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is wrong, error code: %{public}d, bundleName:%{public}s.",
228             ret, processData.bundleName.c_str());
229     }
230 }
231 
OnApplicationStateChanged(const AppStateData & appStateData)232 void ApplicationStateObserverProxy::OnApplicationStateChanged(const AppStateData &appStateData)
233 {
234     MessageParcel data;
235     MessageParcel reply;
236     MessageOption option(MessageOption::TF_ASYNC);
237     if (!WriteInterfaceToken(data)) {
238         TAG_LOGE(AAFwkTag::APPMGR, "WriteInterfaceToken failed");
239         return;
240     }
241     if (!data.WriteParcelable(&appStateData)) {
242         TAG_LOGE(AAFwkTag::APPMGR, "write appStateData failed");
243         return;
244     }
245     int32_t ret = SendTransactCmd(
246         static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_APPLICATION_STATE_CHANGED),
247         data, reply, option);
248     if (ret != NO_ERROR && ret != ERR_INVALID_STUB) {
249         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d, bundleName: %{public}s.",
250             ret, appStateData.bundleName.c_str());
251     }
252 }
253 
OnAppStateChanged(const AppStateData & appStateData)254 void ApplicationStateObserverProxy::OnAppStateChanged(const AppStateData &appStateData)
255 {
256     MessageParcel data;
257     MessageParcel reply;
258     MessageOption option(MessageOption::TF_ASYNC);
259     if (!WriteInterfaceToken(data)) {
260         TAG_LOGE(AAFwkTag::APPMGR, "WriteInterfaceToken failed");
261         return;
262     }
263     if (!data.WriteParcelable(&appStateData)) {
264         TAG_LOGE(AAFwkTag::APPMGR, "write appStateData failed");
265         return;
266     }
267     int32_t ret = SendTransactCmd(
268         static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_APP_STATE_CHANGED),
269         data, reply, option);
270     if (ret != NO_ERROR && ret != ERR_INVALID_STUB) {
271         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d, , bundleName: %{public}s",
272             ret, appStateData.bundleName.c_str());
273     }
274 }
275 
OnAppStarted(const AppStateData & appStateData)276 void ApplicationStateObserverProxy::OnAppStarted(const AppStateData &appStateData)
277 {
278     MessageParcel data;
279     MessageParcel reply;
280     MessageOption option(MessageOption::TF_ASYNC);
281     if (!WriteInterfaceToken(data)) {
282         TAG_LOGE(AAFwkTag::APPMGR, "WriteInterfaceToken failed");
283         return;
284     }
285     if (!data.WriteParcelable(&appStateData)) {
286         TAG_LOGE(AAFwkTag::APPMGR, "write processData failed");
287         return;
288     }
289     int32_t ret = SendTransactCmd(
290         static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_APP_STARTED),
291         data, reply, option);
292     if (ret != NO_ERROR && ret != ERR_INVALID_STUB) {
293         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d, bundleName: %{public}s.",
294             ret, appStateData.bundleName.c_str());
295     }
296 }
297 
OnAppStopped(const AppStateData & appStateData)298 void ApplicationStateObserverProxy::OnAppStopped(const AppStateData &appStateData)
299 {
300     MessageParcel data;
301     MessageParcel reply;
302     MessageOption option(MessageOption::TF_ASYNC);
303     if (!WriteInterfaceToken(data)) {
304         TAG_LOGE(AAFwkTag::APPMGR, "OnAppStopped, WriteInterfaceToken failed");
305         return;
306     }
307     if (!data.WriteParcelable(&appStateData)) {
308         TAG_LOGE(AAFwkTag::APPMGR, "write processData failed");
309         return;
310     }
311     int32_t ret = SendTransactCmd(
312         static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_APP_STOPPED),
313         data, reply, option);
314     if (ret != NO_ERROR && ret != ERR_INVALID_STUB) {
315         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d, bundleName: %{public}s.",
316             ret, appStateData.bundleName.c_str());
317     }
318 }
319 
OnPageShow(const PageStateData & pageStateData)320 void ApplicationStateObserverProxy::OnPageShow(const PageStateData &pageStateData)
321 {
322     MessageParcel data;
323     MessageParcel reply;
324     MessageOption option(MessageOption::TF_ASYNC);
325     if (!WriteInterfaceToken(data)) {
326         TAG_LOGE(AAFwkTag::APPMGR, "WriteInterfaceToken failed");
327         return;
328     }
329     if (!data.WriteParcelable(&pageStateData)) {
330         TAG_LOGE(AAFwkTag::APPMGR, "write processData failed");
331         return;
332     }
333     int32_t ret = SendTransactCmd(
334         static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_PAGE_SHOW),
335         data, reply, option);
336     if (ret != NO_ERROR && ret != ERR_INVALID_STUB) {
337         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d, bundleName: %{public}s",
338             ret, pageStateData.bundleName.c_str());
339     }
340 }
341 
OnPageHide(const PageStateData & pageStateData)342 void ApplicationStateObserverProxy::OnPageHide(const PageStateData &pageStateData)
343 {
344     MessageParcel data;
345     MessageParcel reply;
346     MessageOption option(MessageOption::TF_ASYNC);
347     if (!WriteInterfaceToken(data)) {
348         TAG_LOGE(AAFwkTag::APPMGR, "WriteInterfaceToken failed");
349         return;
350     }
351     if (!data.WriteParcelable(&pageStateData)) {
352         TAG_LOGE(AAFwkTag::APPMGR, "write processData failed");
353         return;
354     }
355     int32_t ret = SendTransactCmd(
356         static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_PAGE_HIDE),
357         data, reply, option);
358     if (ret != NO_ERROR && ret != ERR_INVALID_STUB) {
359         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d, bundleName: %{public}s",
360             ret, pageStateData.bundleName.c_str());
361     }
362 }
363 
OnAppCacheStateChanged(const AppStateData & appStateData)364 void ApplicationStateObserverProxy::OnAppCacheStateChanged(const AppStateData &appStateData)
365 {
366     MessageParcel data;
367     MessageParcel reply;
368     MessageOption option(MessageOption::TF_ASYNC);
369     if (!WriteInterfaceToken(data)) {
370         TAG_LOGE(AAFwkTag::APPMGR, "WriteInterfaceToken failed");
371         return;
372     }
373     if (!data.WriteParcelable(&appStateData)) {
374         TAG_LOGE(AAFwkTag::APPMGR, "write processData failed");
375         return;
376     }
377     int32_t ret = SendTransactCmd(
378         static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_APP_CACHE_STATE_CHANGED),
379         data, reply, option);
380     if (ret != NO_ERROR && ret != ERR_INVALID_STUB) {
381         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d, bundleName: %{public}s.",
382             ret, appStateData.bundleName.c_str());
383     }
384 }
385 
SendTransactCmd(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)386 int32_t ApplicationStateObserverProxy::SendTransactCmd(uint32_t code, MessageParcel &data,
387     MessageParcel &reply, MessageOption &option)
388 {
389     sptr<IRemoteObject> remote = Remote();
390     if (remote == nullptr) {
391         TAG_LOGE(AAFwkTag::APPMGR, "Remote is nullptr.");
392         return ERR_NULL_OBJECT;
393     }
394 
395     return remote->SendRequest(code, data, reply, option);
396 }
397 
OnProcessBindingRelationChanged(const ProcessBindData & processBindData)398 void ApplicationStateObserverProxy::OnProcessBindingRelationChanged(const ProcessBindData &processBindData)
399 {
400     MessageParcel data;
401     MessageParcel reply;
402     MessageOption option(MessageOption::TF_ASYNC);
403     if (!WriteInterfaceToken(data)) {
404         TAG_LOGE(AAFwkTag::APPMGR, "WriteInterfaceToken failed");
405         return;
406     }
407     if (!data.WriteParcelable(&processBindData)) {
408         TAG_LOGE(AAFwkTag::APPMGR, "Write processData failed");
409         return;
410     }
411     int32_t ret = SendTransactCmd(
412         static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_PROCESS_BINDINGRELATION_CHANGED),
413         data, reply, option);
414     if (ret != NO_ERROR && ret != ERR_INVALID_STUB) {
415         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is wrong, error code: %{public}d, bundleName:%{public}s.",
416             ret, processBindData.bundleName.c_str());
417     }
418 }
419 
OnKeepAliveStateChanged(const ProcessData & processData)420 void ApplicationStateObserverProxy::OnKeepAliveStateChanged(const ProcessData &processData)
421 {
422     MessageParcel data;
423     MessageParcel reply;
424     MessageOption option(MessageOption::TF_ASYNC);
425     if (!WriteInterfaceToken(data)) {
426         TAG_LOGE(AAFwkTag::APPMGR, "OnKeepAliveStateChanged, WriteInterfaceToken failed");
427         return;
428     }
429     if (!data.WriteParcelable(&processData)) {
430         TAG_LOGE(AAFwkTag::APPMGR, "OnKeepAliveStateChanged write processData failed");
431         return;
432     }
433     int32_t ret = SendTransactCmd(
434         static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_KEEP_ALIVE_STATE_CHANGED),
435         data, reply, option);
436     if (ret != NO_ERROR && ret != ERR_INVALID_STUB) {
437         TAG_LOGW(AAFwkTag::APPMGR,
438             "OnKeepAliveStateChanged ssendRequest is wrong, error code: %{public}d, bundleName:%{public}s.", ret,
439             processData.bundleName.c_str());
440     }
441 }
442 
OnProcessPreForegroundChanged(const PreloadProcessData & preloadProcessData)443 void ApplicationStateObserverProxy::OnProcessPreForegroundChanged(const PreloadProcessData &preloadProcessData)
444 {
445     MessageParcel data;
446     MessageParcel reply;
447     MessageOption option(MessageOption::TF_ASYNC);
448     if (!WriteInterfaceToken(data)) {
449         TAG_LOGE(AAFwkTag::APPMGR, "OnProcessPreForegroundChanged, WriteInterfaceToken failed");
450         return;
451     }
452     if (!data.WriteParcelable(&preloadProcessData)) {
453         TAG_LOGE(AAFwkTag::APPMGR, "OnProcessPreForegroundChanged write preloadProcessData failed");
454         return;
455     }
456     int32_t ret = SendTransactCmd(
457         static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_PRELOAD_PROCESS_STATE_CHANGED),
458         data, reply, option);
459     if (ret != NO_ERROR && ret != ERR_INVALID_STUB) {
460         TAG_LOGW(AAFwkTag::APPMGR,
461             "OnProcessPreForegroundChanged ssendRequest is wrong, error code: %{public}d, pid:%{public}d.",
462             ret, preloadProcessData.pid);
463     }
464 }
465 }  // namespace AppExecFwk
466 }  // namespace OHOS
467