1 /* 2 * Copyright (c) 2022 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_APP_STATE_OBSERVER_MANAGER_H 17 #define OHOS_ABILITY_RUNTIME_APP_STATE_OBSERVER_MANAGER_H 18 19 #include <map> 20 #include <mutex> 21 #include <string> 22 #include <unordered_map> 23 24 #include "ability_foreground_state_observer_interface.h" 25 #include "app_foreground_state_observer_interface.h" 26 #include "app_running_record.h" 27 #include "app_state_data.h" 28 #include "cpp/mutex.h" 29 #include "iapp_state_callback.h" 30 #include "iapplication_state_observer.h" 31 #include "page_state_data.h" 32 #include "permission_constants.h" 33 #include "permission_verification.h" 34 #include "singleton.h" 35 #include "task_handler_wrap.h" 36 #ifdef SUPPORT_UPMS 37 #include "uri_permission_manager_client.h" 38 #endif // SUPPORT_UPMS 39 40 namespace OHOS { 41 namespace AppExecFwk { 42 struct AppStateObserverInfo { 43 int32_t uid = 0; 44 std::vector<std::string> bundleNames; 45 }; 46 47 using AppStateObserverMap = std::map<sptr<IApplicationStateObserver>, AppStateObserverInfo>; 48 using AppForegroundStateObserverMap = std::map<sptr<IAppForegroundStateObserver>, int32_t>; 49 using AbilityForegroundObserverMap = std::map<sptr<IAbilityForegroundStateObserver>, int32_t>; 50 51 enum class ObserverType { 52 APPLICATION_STATE_OBSERVER, 53 APP_FOREGROUND_STATE_OBSERVER, 54 ABILITY_FOREGROUND_STATE_OBSERVER, 55 }; 56 class AppStateObserverManager : public std::enable_shared_from_this<AppStateObserverManager> { 57 DECLARE_DELAYED_SINGLETON(AppStateObserverManager) 58 public: 59 void Init(); 60 int32_t RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer, 61 const std::vector<std::string> &bundleNameList = {}); 62 int32_t UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer); 63 int32_t RegisterAppForegroundStateObserver(const sptr<IAppForegroundStateObserver> &observer); 64 int32_t UnregisterAppForegroundStateObserver(const sptr<IAppForegroundStateObserver> &observer); 65 int32_t RegisterAbilityForegroundStateObserver(const sptr<IAbilityForegroundStateObserver> &observer); 66 int32_t UnregisterAbilityForegroundStateObserver(const sptr<IAbilityForegroundStateObserver> &observer); 67 void StateChangedNotifyObserver( 68 const AbilityStateData abilityStateData, bool isAbility, bool isFromWindowFocusChanged); 69 void OnAppStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord, const ApplicationState state, 70 bool needNotifyApp, bool isFromWindowFocusChanged); 71 void OnAppStarted(const std::shared_ptr<AppRunningRecord> &appRecord); 72 void OnAppStopped(const std::shared_ptr<AppRunningRecord> &appRecord); 73 void OnProcessCreated(const std::shared_ptr<AppRunningRecord> &appRecord, bool isPreload); 74 void OnProcessStateChanged( 75 const std::shared_ptr<AppRunningRecord> &appRecord, bool isFromWindowFocusChanged = false); 76 void OnProcessBindingRelationChanged(const std::shared_ptr<AppRunningRecord> &appRecord, 77 const UIExtensionProcessBindInfo &bindInfo, int32_t bindingRelation); 78 void OnWindowShow(const std::shared_ptr<AppRunningRecord> &appRecord); 79 void OnWindowHidden(const std::shared_ptr<AppRunningRecord> &appRecord); 80 void OnRenderProcessCreated(const std::shared_ptr<RenderRecord> &RenderRecord, const bool isPreload); 81 #ifdef SUPPORT_CHILD_PROCESS 82 void OnChildProcessCreated(std::shared_ptr<ChildProcessRecord> childRecord); 83 #endif // SUPPORT_CHILD_PROCESS 84 void OnProcessDied(const std::shared_ptr<AppRunningRecord> &appRecord); 85 void OnRenderProcessDied(const std::shared_ptr<RenderRecord> &renderRecord); 86 #ifdef SUPPORT_CHILD_PROCESS 87 void OnChildProcessDied(std::shared_ptr<ChildProcessRecord> childRecord); 88 #endif // SUPPORT_CHILD_PROCESS 89 void OnProcessReused(const std::shared_ptr<AppRunningRecord> &appRecord); 90 void OnPageShow(const PageStateData pageStateData); 91 void OnPageHide(const PageStateData pageStateData); 92 void OnAppCacheStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord, ApplicationState state); 93 void OnKeepAliveStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord); 94 void OnProcessPreForegroundChanged(std::shared_ptr<AppRunningRecord> appRecord); 95 96 private: 97 void HandleOnWindowShow(const std::shared_ptr<AppRunningRecord> &appRecord); 98 void HandleOnWindowHidden(const std::shared_ptr<AppRunningRecord> &appRecord); 99 void HandleAppStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord, const ApplicationState state, 100 bool needNotifyApp, bool isFromWindowFocusChanged); 101 void HandleOnAppStarted(const std::shared_ptr<AppRunningRecord> &appRecord); 102 void HandleOnAppStopped(const std::shared_ptr<AppRunningRecord> &appRecord); 103 void HandleStateChangedNotifyObserver( 104 const AbilityStateData abilityStateData, bool isAbility, bool isFromWindowFocusChanged); 105 void HandleOnAppProcessCreated(const std::shared_ptr<AppRunningRecord> &appRecord, bool isPreload); 106 void HandleOnRenderProcessCreated(const std::shared_ptr<RenderRecord> &RenderRecord, const bool isPreload); 107 #ifdef SUPPORT_CHILD_PROCESS 108 void HandleOnChildProcessCreated(std::shared_ptr<ChildProcessRecord> childRecord); 109 #endif // SUPPORT_CHILD_PROCESS 110 void HandleOnAppProcessDied(const std::shared_ptr<AppRunningRecord> &appRecord); 111 void HandleOnRenderProcessDied(const std::shared_ptr<RenderRecord> &RenderRecord); 112 #ifdef SUPPORT_CHILD_PROCESS 113 void HandleOnChildProcessDied(std::shared_ptr<ChildProcessRecord> childRecord); 114 #endif // SUPPORT_CHILD_PROCESS 115 bool ObserverExist(const sptr<IRemoteBroker> &observer); 116 bool IsAppForegroundObserverExist(const sptr<IRemoteBroker> &observer); 117 bool IsAbilityForegroundObserverExist(const sptr<IRemoteBroker> &observer); 118 void AddObserverDeathRecipient(const sptr<IRemoteBroker> &observer, const ObserverType &type); 119 void RemoveObserverDeathRecipient(const sptr<IRemoteBroker> &observer); 120 AppStateObserverMap GetAppStateObserverMapCopy(); 121 AppForegroundStateObserverMap GetAppForegroundStateObserverMapCopy(); 122 AbilityForegroundObserverMap GetAbilityForegroundObserverMapCopy(); 123 ProcessData WrapProcessData( 124 const std::shared_ptr<AppRunningRecord> &appRecord, bool isFromWindowFocusChanged = false); 125 ProcessData WrapRenderProcessData(const std::shared_ptr<RenderRecord> &renderRecord); 126 ProcessBindData WrapProcessBindData(const UIExtensionProcessBindInfo &bindInfo, int32_t bindingRelation); 127 #ifdef SUPPORT_CHILD_PROCESS 128 int32_t WrapChildProcessData(ProcessData &processData, std::shared_ptr<ChildProcessRecord> childRecord); 129 #endif // SUPPORT_CHILD_PROCESS 130 void OnObserverDied(const wptr<IRemoteObject> &remote, const ObserverType &type); 131 AppStateData WrapAppStateData(const std::shared_ptr<AppRunningRecord> &appRecord, 132 const ApplicationState state, bool isFromWindowFocusChanged = false); 133 void HandleOnProcessCreated(const ProcessData &data); 134 void HandleOnProcessStateChanged( 135 const std::shared_ptr<AppRunningRecord> &appRecordm, bool isFromWindowFocusChanged = false); 136 void HandleOnProcessDied(const ProcessData &data); 137 void HandleOnProcessResued(const std::shared_ptr<AppRunningRecord> &appRecord); 138 void HandleOnPageShow(const PageStateData pageStateData); 139 void HandleOnPageHide(const PageStateData pageStateData); 140 void HandleOnAppCacheStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord, ApplicationState state); 141 void AddObserverCount(int32_t uid); 142 void DecreaseObserverCount(int32_t uid); 143 void HandleOnProcessBindingRelationChanged(const std::shared_ptr<AppRunningRecord> &appRecord, 144 const UIExtensionProcessBindInfo &bindInfo, int32_t bindingRelation); 145 void HandleOnKeepAliveStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord); 146 void HandleOnProcessPreForegroundChanged(std::shared_ptr<AppRunningRecord> appRecord); 147 148 private: 149 std::shared_ptr<AAFwk::TaskHandlerWrap> handler_; 150 AppStateObserverMap appStateObserverMap_; 151 AppForegroundStateObserverMap appForegroundStateObserverMap_; 152 AbilityForegroundObserverMap abilityForegroundObserverMap_; 153 std::map<sptr<IRemoteObject>, sptr<IRemoteObject::DeathRecipient>> recipientMap_; 154 std::unordered_map<int32_t, int32_t> observerCountMap_; // <uid, count> 155 int32_t observerAmount_ = 0; 156 ffrt::mutex observerLock_; 157 ffrt::mutex appForegroundObserverLock_; 158 ffrt::mutex abilityForegroundObserverLock_; 159 ffrt::mutex recipientMapMutex_; 160 ffrt::mutex observerCountMapMutex_; 161 }; 162 } // namespace AppExecFwk 163 } // namespace OHOS 164 #endif // OHOS_ABILITY_RUNTIME_APP_STATE_OBSERVER_MANAGER_H 165