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(); 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 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 * 83 * @param appStateData Application state data. 84 */ 85 virtual void OnApplicationStateChanged(const AppStateData &appStateData) override; 86 87 virtual void OnAppStateChanged(const AppStateData &appStateData) override; 88 89 virtual void OnProcessReused(const ProcessData &processData) override; 90 91 /** 92 * Will be called when the application start. 93 * 94 * @param appStateData Application state data. 95 */ 96 virtual void OnAppStarted(const AppStateData &appStateData) override; 97 98 /** 99 * Will be called when the application stop. 100 * 101 * @param appStateData Application state data. 102 */ 103 virtual void OnAppStopped(const AppStateData &appStateData) override; 104 105 private: 106 int32_t HandleOnForegroundApplicationChanged(MessageParcel &data, MessageParcel &reply); 107 108 int32_t HandleOnAbilityStateChanged(MessageParcel &data, MessageParcel &reply); 109 110 int32_t HandleOnExtensionStateChanged(MessageParcel &data, MessageParcel &reply); 111 112 int32_t HandleOnProcessCreated(MessageParcel &data, MessageParcel &reply); 113 114 int32_t HandleOnProcessStateChanged(MessageParcel &data, MessageParcel &reply); 115 116 int32_t HandleOnProcessDied(MessageParcel &data, MessageParcel &reply); 117 118 int32_t HandleOnApplicationStateChanged(MessageParcel &data, MessageParcel &reply); 119 120 int32_t HandleOnAppStateChanged(MessageParcel &data, MessageParcel &reply); 121 122 int32_t HandleOnProcessReused(MessageParcel &data, MessageParcel &reply); 123 124 int32_t HandleOnAppStarted(MessageParcel &data, MessageParcel &reply); 125 126 int32_t HandleOnAppStopped(MessageParcel &data, MessageParcel &reply); 127 128 using ApplicationStateObserverFunc = int32_t (ApplicationStateObserverStub::*)(MessageParcel &data, 129 MessageParcel &reply); 130 std::map<uint32_t, ApplicationStateObserverFunc> memberFuncMap_; 131 static std::mutex callbackMutex_; 132 133 DISALLOW_COPY_AND_MOVE(ApplicationStateObserverStub); 134 }; 135 136 /** 137 * @class ApplicationStateObserverRecipient 138 * ApplicationStateObserverRecipient notices IRemoteBroker died. 139 */ 140 class ApplicationStateObserverRecipient : public IRemoteObject::DeathRecipient { 141 public: 142 using RemoteDiedHandler = std::function<void(const wptr<IRemoteObject> &)>; 143 explicit ApplicationStateObserverRecipient(RemoteDiedHandler handler); 144 virtual ~ApplicationStateObserverRecipient(); 145 virtual void OnRemoteDied(const wptr<IRemoteObject> &remote); 146 147 private: 148 RemoteDiedHandler handler_; 149 }; 150 } // namespace AppExecFwk 151 } // namespace OHOS 152 #endif // OHOS_ABILITY_RUNTIME_APPLICATION_STATE_OBSERVER_STUB_H 153