1 /*
2  * Copyright (c) 2021 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 #ifndef OHOS_ABILITY_RUNTIME_APPLICATION_STATE_OBSERVER_STUB_H
17 #define OHOS_ABILITY_RUNTIME_APPLICATION_STATE_OBSERVER_STUB_H
18 
19 #include <map>
20 #include <mutex>
21 
22 #include "iremote_stub.h"
23 #include "nocopyable.h"
24 #include "string_ex.h"
25 #include "app_mgr_constants.h"
26 #include "iapplication_state_observer.h"
27 
28 namespace OHOS {
29 namespace AppExecFwk {
30 class ApplicationStateObserverStub : public IRemoteStub<IApplicationStateObserver> {
31 public:
32     ApplicationStateObserverStub() = default;
33     virtual ~ApplicationStateObserverStub() = default;
34 
35     virtual int OnRemoteRequest(
36         uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override;
37 
38     /**
39      * Application foreground state changed callback.
40      *
41      * @param appStateData Application Process data.
42      */
43     virtual void OnForegroundApplicationChanged(const AppStateData &appStateData) override;
44 
45     /**
46      * Will be called when the ability state changes.
47      *
48      * @param abilityStateData Ability state data.
49      */
50     virtual void OnAbilityStateChanged(const AbilityStateData &abilityStateData) override;
51 
52     /**
53      * Will be called when the extension state changes.
54      *
55      * @param abilityStateData Extension state data.
56      */
57     virtual void OnExtensionStateChanged(const AbilityStateData &abilityStateData) override;
58 
59     /**
60      * Will be called when the process start.
61      *
62      * @param processData Process data.
63      */
64     virtual void OnProcessCreated(const ProcessData &processData) override;
65 
66     /**
67      * Will be called when the process state change.
68      *
69      * @param processData Process data.
70      */
71     virtual void OnProcessStateChanged(const ProcessData &processData) override;
72 
73     /**
74      * Will be called when the process die.
75      *
76      * @param processData Process data.
77      */
78     virtual void OnProcessDied(const ProcessData &processData) override;
79 
80     /**
81      * Application state changed callback.
82      * Only observe APP_STATE_CREATE and APP_STATE_TERMINATED
83      *
84      * @param appStateData Application state data.
85      */
86     virtual void OnApplicationStateChanged(const AppStateData &appStateData) override;
87 
88     /**
89      * Application state changed callback.
90      * Only observe APP_STATE_FOREGROUND and APP_STATE_BACKGROUND
91      *
92      * @param appStateData Application state data.
93      */
94     virtual void OnAppStateChanged(const AppStateData &appStateData) override;
95 
96     virtual void OnProcessReused(const ProcessData &processData) override;
97 
98     /**
99      * Will be called when the application start.
100      *
101      * @param appStateData Application state data.
102      */
103     virtual void OnAppStarted(const AppStateData &appStateData) override;
104 
105     /**
106      * Will be called when the application stop.
107      *
108      * @param appStateData Application state data.
109      */
110     virtual void OnAppStopped(const AppStateData &appStateData) override;
111 
112     /**
113      * Will be called when page show.
114      *
115      * @param pageStateData Page state data.
116      */
117     virtual void OnPageShow(const PageStateData &pageStateData) override;
118 
119     /**
120      * Will be called whe page hide.
121      *
122      * @param pageStateData Page state data.
123      */
124     virtual void OnPageHide(const PageStateData &pageStateData) override;
125 
126     /**
127      * Will be called when application cache state change.
128      *
129      * @param appStateData Application state data.
130      */
131     virtual void OnAppCacheStateChanged(const AppStateData &appStateData) override;
132 
133 private:
134     int32_t HandleOnForegroundApplicationChanged(MessageParcel &data, MessageParcel &reply);
135 
136     int32_t HandleOnAbilityStateChanged(MessageParcel &data, MessageParcel &reply);
137 
138     int32_t HandleOnExtensionStateChanged(MessageParcel &data, MessageParcel &reply);
139 
140     int32_t HandleOnProcessCreated(MessageParcel &data, MessageParcel &reply);
141 
142     int32_t HandleOnProcessStateChanged(MessageParcel &data, MessageParcel &reply);
143 
144     int32_t HandleOnProcessDied(MessageParcel &data, MessageParcel &reply);
145 
146     int32_t HandleOnApplicationStateChanged(MessageParcel &data, MessageParcel &reply);
147 
148     int32_t HandleOnAppStateChanged(MessageParcel &data, MessageParcel &reply);
149 
150     int32_t HandleOnProcessReused(MessageParcel &data, MessageParcel &reply);
151 
152     int32_t HandleOnAppStarted(MessageParcel &data, MessageParcel &reply);
153 
154     int32_t HandleOnAppStopped(MessageParcel &data, MessageParcel &reply);
155 
156     int32_t HandleOnPageShow(MessageParcel &data, MessageParcel &reply);
157 
158     int32_t HandleOnPageHide(MessageParcel &data, MessageParcel &reply);
159 
160     int32_t HandleOnAppCacheStateChanged(MessageParcel &data, MessageParcel &reply);
161 
162     static std::mutex callbackMutex_;
163 
164     DISALLOW_COPY_AND_MOVE(ApplicationStateObserverStub);
165 };
166 
167 /**
168  * @class ApplicationStateObserverRecipient
169  * ApplicationStateObserverRecipient notices IRemoteBroker died.
170  */
171 class ApplicationStateObserverRecipient : public IRemoteObject::DeathRecipient {
172 public:
173     using RemoteDiedHandler = std::function<void(const wptr<IRemoteObject> &)>;
174     explicit ApplicationStateObserverRecipient(RemoteDiedHandler handler);
175     virtual ~ApplicationStateObserverRecipient();
176     virtual void OnRemoteDied(const wptr<IRemoteObject> &remote);
177 
178 private:
179     RemoteDiedHandler handler_;
180 };
181 }  // namespace AppExecFwk
182 }  // namespace OHOS
183 #endif  // OHOS_ABILITY_RUNTIME_APPLICATION_STATE_OBSERVER_STUB_H
184