• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 FOUNDATION_APPEXECFWK_INTERFACES_INNERKITS_APPEXECFWK_CORE_INCLUDE_APPMGR_APPLICATION_STATE_OBSERVER_STUB_H
17 #define FOUNDATION_APPEXECFWK_INTERFACES_INNERKITS_APPEXECFWK_CORE_INCLUDE_APPMGR_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();
33     virtual ~ApplicationStateObserverStub();
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 die.
68      *
69      * @param processData Process data.
70      */
71     virtual void OnProcessDied(const ProcessData &processData) override;
72 
73     /**
74      * Application state changed callback.
75      *
76      * @param appStateData Application state data.
77      */
78     virtual void OnApplicationStateChanged(const AppStateData &appStateData) override;
79 
80 private:
81     int32_t HandleOnForegroundApplicationChanged(MessageParcel &data, MessageParcel &reply);
82 
83     int32_t HandleOnAbilityStateChanged(MessageParcel &data, MessageParcel &reply);
84 
85     int32_t HandleOnExtensionStateChanged(MessageParcel &data, MessageParcel &reply);
86 
87     int32_t HandleOnProcessCreated(MessageParcel &data, MessageParcel &reply);
88 
89     int32_t HandleOnProcessDied(MessageParcel &data, MessageParcel &reply);
90 
91     int32_t HandleOnApplicationStateChanged(MessageParcel &data, MessageParcel &reply);
92 
93     using ApplicationStateObserverFunc = int32_t (ApplicationStateObserverStub::*)(MessageParcel &data,
94         MessageParcel &reply);
95     std::map<uint32_t, ApplicationStateObserverFunc> memberFuncMap_;
96     static std::mutex callbackMutex_;
97 
98     DISALLOW_COPY_AND_MOVE(ApplicationStateObserverStub);
99 };
100 
101 /**
102  * @class ApplicationStateObserverRecipient
103  * ApplicationStateObserverRecipient notices IRemoteBroker died.
104  */
105 class ApplicationStateObserverRecipient : public IRemoteObject::DeathRecipient {
106 public:
107     using RemoteDiedHandler = std::function<void(const wptr<IRemoteObject> &)>;
108     ApplicationStateObserverRecipient(RemoteDiedHandler handler);
109     virtual ~ApplicationStateObserverRecipient();
110     virtual void OnRemoteDied(const wptr<IRemoteObject> &remote);
111 
112 private:
113     RemoteDiedHandler handler_;
114 };
115 }  // namespace AppExecFwk
116 }  // namespace OHOS
117 #endif  // FOUNDATION_APPEXECFWK_INTERFACES_INNERKITS_APPEXECFWK_CORE_INCLUDE_APPMGR_APPLICATION_STATE_OBSERVER_STUB_H
118