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 <vector> 23 24 #include "app_running_record.h" 25 #include "app_state_data.h" 26 #include "iapp_state_callback.h" 27 #include "iapplication_state_observer.h" 28 #include "permission_constants.h" 29 #include "permission_verification.h" 30 #include "singleton.h" 31 #include "uri_permission_manager_client.h" 32 33 namespace OHOS { 34 namespace AppExecFwk { 35 class AppStateObserverManager : public std::enable_shared_from_this<AppStateObserverManager> { 36 DECLARE_DELAYED_SINGLETON(AppStateObserverManager) 37 public: 38 void Init(); 39 int32_t RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer, 40 const std::vector<std::string> &bundleNameList = {}); 41 int32_t UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer); 42 void StateChangedNotifyObserver(const AbilityStateData abilityStateData, bool isAbility); 43 void OnAppStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord, const ApplicationState state, 44 bool needNotifyApp); 45 void OnProcessCreated(const std::shared_ptr<AppRunningRecord> &appRecord); 46 void OnProcessStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord); 47 void OnRenderProcessCreated(const std::shared_ptr<RenderRecord> &RenderRecord); 48 void OnProcessDied(const std::shared_ptr<AppRunningRecord> &appRecord); 49 void OnRenderProcessDied(const std::shared_ptr<RenderRecord> &renderRecord); 50 void OnProcessReused(const std::shared_ptr<AppRunningRecord> &appRecord); 51 private: 52 void HandleAppStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord, const ApplicationState state, 53 bool needNotifyApp); 54 void HandleStateChangedNotifyObserver(const AbilityStateData abilityStateData, bool isAbility); 55 void HandleOnAppProcessCreated(const std::shared_ptr<AppRunningRecord> &appRecord); 56 void HandleOnRenderProcessCreated(const std::shared_ptr<RenderRecord> &RenderRecord); 57 void HandleOnAppProcessDied(const std::shared_ptr<AppRunningRecord> &appRecord); 58 void HandleOnRenderProcessDied(const std::shared_ptr<RenderRecord> &RenderRecord); 59 bool ObserverExist(const sptr<IApplicationStateObserver> &observer); 60 void AddObserverDeathRecipient(const sptr<IApplicationStateObserver> &observer); 61 void RemoveObserverDeathRecipient(const sptr<IApplicationStateObserver> &observer); 62 ProcessData WrapProcessData(const std::shared_ptr<AppRunningRecord> &appRecord); 63 ProcessData WrapRenderProcessData(const std::shared_ptr<RenderRecord> &renderRecord); 64 void OnObserverDied(const wptr<IRemoteObject> &remote); 65 AppStateData WrapAppStateData(const std::shared_ptr<AppRunningRecord> &appRecord, 66 const ApplicationState state); 67 void HandleOnProcessCreated(const ProcessData &data); 68 void HandleOnProcessStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord); 69 void HandleOnProcessDied(const ProcessData &data); 70 void HandleOnProcessResued(const std::shared_ptr<AppRunningRecord> &appRecord); 71 72 private: 73 std::shared_ptr<AppExecFwk::EventHandler> handler_; 74 std::recursive_mutex observerLock_; 75 std::map<sptr<IRemoteObject>, sptr<IRemoteObject::DeathRecipient>> recipientMap_; 76 std::map<sptr<IApplicationStateObserver>, std::vector<std::string>> appStateObserverMap_; 77 }; 78 } // namespace AppExecFwk 79 } // namespace OHOS 80 #endif // OHOS_ABILITY_RUNTIME_APP_STATE_OBSERVER_MANAGER_H 81