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 private: 127 int32_t HandleOnForegroundApplicationChanged(MessageParcel &data, MessageParcel &reply); 128 129 int32_t HandleOnAbilityStateChanged(MessageParcel &data, MessageParcel &reply); 130 131 int32_t HandleOnExtensionStateChanged(MessageParcel &data, MessageParcel &reply); 132 133 int32_t HandleOnProcessCreated(MessageParcel &data, MessageParcel &reply); 134 135 int32_t HandleOnProcessStateChanged(MessageParcel &data, MessageParcel &reply); 136 137 int32_t HandleOnProcessDied(MessageParcel &data, MessageParcel &reply); 138 139 int32_t HandleOnApplicationStateChanged(MessageParcel &data, MessageParcel &reply); 140 141 int32_t HandleOnAppStateChanged(MessageParcel &data, MessageParcel &reply); 142 143 int32_t HandleOnProcessReused(MessageParcel &data, MessageParcel &reply); 144 145 int32_t HandleOnAppStarted(MessageParcel &data, MessageParcel &reply); 146 147 int32_t HandleOnAppStopped(MessageParcel &data, MessageParcel &reply); 148 149 int32_t HandleOnPageShow(MessageParcel &data, MessageParcel &reply); 150 151 int32_t HandleOnPageHide(MessageParcel &data, MessageParcel &reply); 152 153 using ApplicationStateObserverFunc = int32_t (ApplicationStateObserverStub::*)(MessageParcel &data, 154 MessageParcel &reply); 155 const std::map<uint32_t, ApplicationStateObserverFunc> memberFuncMap_ = { 156 { static_cast<uint32_t>(Message::TRANSACT_ON_FOREGROUND_APPLICATION_CHANGED), 157 &ApplicationStateObserverStub::HandleOnForegroundApplicationChanged }, 158 { static_cast<uint32_t>(Message::TRANSACT_ON_ABILITY_STATE_CHANGED), 159 &ApplicationStateObserverStub::HandleOnAbilityStateChanged }, 160 { static_cast<uint32_t>(Message::TRANSACT_ON_EXTENSION_STATE_CHANGED), 161 &ApplicationStateObserverStub::HandleOnExtensionStateChanged }, 162 { static_cast<uint32_t>(Message::TRANSACT_ON_PROCESS_CREATED), 163 &ApplicationStateObserverStub::HandleOnProcessCreated }, 164 { static_cast<uint32_t>(Message::TRANSACT_ON_PROCESS_STATE_CHANGED), 165 &ApplicationStateObserverStub::HandleOnProcessStateChanged }, 166 { static_cast<uint32_t>(Message::TRANSACT_ON_PROCESS_DIED), 167 &ApplicationStateObserverStub::HandleOnProcessDied }, 168 { static_cast<uint32_t>(Message::TRANSACT_ON_APPLICATION_STATE_CHANGED), 169 &ApplicationStateObserverStub::HandleOnApplicationStateChanged }, 170 { static_cast<uint32_t>(Message::TRANSACT_ON_APP_STATE_CHANGED), 171 &ApplicationStateObserverStub::HandleOnAppStateChanged }, 172 { static_cast<uint32_t>(Message::TRANSACT_ON_PROCESS_REUSED), 173 &ApplicationStateObserverStub::HandleOnProcessReused }, 174 { static_cast<uint32_t>(Message::TRANSACT_ON_APP_STARTED), &ApplicationStateObserverStub::HandleOnAppStarted }, 175 { static_cast<uint32_t>(Message::TRANSACT_ON_APP_STOPPED), &ApplicationStateObserverStub::HandleOnAppStopped }, 176 { static_cast<uint32_t>(Message::TRANSACT_ON_PAGE_SHOW), &ApplicationStateObserverStub::HandleOnPageShow }, 177 { static_cast<uint32_t>(Message::TRANSACT_ON_PAGE_HIDE), &ApplicationStateObserverStub::HandleOnPageHide }, 178 }; 179 static std::mutex callbackMutex_; 180 181 DISALLOW_COPY_AND_MOVE(ApplicationStateObserverStub); 182 }; 183 184 /** 185 * @class ApplicationStateObserverRecipient 186 * ApplicationStateObserverRecipient notices IRemoteBroker died. 187 */ 188 class ApplicationStateObserverRecipient : public IRemoteObject::DeathRecipient { 189 public: 190 using RemoteDiedHandler = std::function<void(const wptr<IRemoteObject> &)>; 191 explicit ApplicationStateObserverRecipient(RemoteDiedHandler handler); 192 virtual ~ApplicationStateObserverRecipient(); 193 virtual void OnRemoteDied(const wptr<IRemoteObject> &remote); 194 195 private: 196 RemoteDiedHandler handler_; 197 }; 198 } // namespace AppExecFwk 199 } // namespace OHOS 200 #endif // OHOS_ABILITY_RUNTIME_APPLICATION_STATE_OBSERVER_STUB_H 201