/* * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef OHOS_ABILITY_RUNTIME_APP_STATE_OBSERVER_MANAGER_H #define OHOS_ABILITY_RUNTIME_APP_STATE_OBSERVER_MANAGER_H #include #include #include #include #include "ability_foreground_state_observer_interface.h" #include "app_foreground_state_observer_interface.h" #include "app_running_record.h" #include "app_state_data.h" #include "cpp/mutex.h" #include "iapp_state_callback.h" #include "iapplication_state_observer.h" #include "page_state_data.h" #include "permission_constants.h" #include "permission_verification.h" #include "singleton.h" #include "task_handler_wrap.h" #include "uri_permission_manager_client.h" namespace OHOS { namespace AppExecFwk { using AppStateObserverMap = std::map, std::vector>; using AppForegroundStateObserverSet = std::set>; using AbilityforegroundObserverSet = std::set>; enum class ObserverType { APPLICATION_STATE_OBSERVER, APP_FOREGROUND_STATE_OBSERVER, ABILITY_FOREGROUND_STATE_OBSERVER, }; class AppStateObserverManager : public std::enable_shared_from_this { DECLARE_DELAYED_SINGLETON(AppStateObserverManager) public: void Init(); int32_t RegisterApplicationStateObserver(const sptr &observer, const std::vector &bundleNameList = {}); int32_t UnregisterApplicationStateObserver(const sptr &observer); int32_t RegisterAppForegroundStateObserver(const sptr &observer); int32_t UnregisterAppForegroundStateObserver(const sptr &observer); int32_t RegisterAbilityForegroundStateObserver(const sptr &observer); int32_t UnregisterAbilityForegroundStateObserver(const sptr &observer); void StateChangedNotifyObserver( const AbilityStateData abilityStateData, bool isAbility, bool isFromWindowFocusChanged); void OnAppStateChanged(const std::shared_ptr &appRecord, const ApplicationState state, bool needNotifyApp, bool isFromWindowFocusChanged); void OnAppStarted(const std::shared_ptr &appRecord); void OnAppStopped(const std::shared_ptr &appRecord); void OnProcessCreated(const std::shared_ptr &appRecord, bool isPreload); void OnProcessStateChanged(const std::shared_ptr &appRecord); void OnRenderProcessCreated(const std::shared_ptr &RenderRecord); void OnChildProcessCreated(std::shared_ptr childRecord); void OnProcessDied(const std::shared_ptr &appRecord); void OnRenderProcessDied(const std::shared_ptr &renderRecord); void OnChildProcessDied(std::shared_ptr childRecord); void OnProcessReused(const std::shared_ptr &appRecord); void OnPageShow(const PageStateData pageStateData); void OnPageHide(const PageStateData pageStateData); void OnAppCacheStateChanged(const std::shared_ptr &appRecord, ApplicationState state); private: void HandleAppStateChanged(const std::shared_ptr &appRecord, const ApplicationState state, bool needNotifyApp, bool isFromWindowFocusChanged); void HandleOnAppStarted(const std::shared_ptr &appRecord); void HandleOnAppStopped(const std::shared_ptr &appRecord); void HandleStateChangedNotifyObserver( const AbilityStateData abilityStateData, bool isAbility, bool isFromWindowFocusChanged); void HandleOnAppProcessCreated(const std::shared_ptr &appRecord, bool isPreload); void HandleOnRenderProcessCreated(const std::shared_ptr &RenderRecord); void HandleOnChildProcessCreated(std::shared_ptr childRecord); void HandleOnAppProcessDied(const std::shared_ptr &appRecord); void HandleOnRenderProcessDied(const std::shared_ptr &RenderRecord); void HandleOnChildProcessDied(std::shared_ptr childRecord); bool ObserverExist(const sptr &observer); bool IsAppForegroundObserverExist(const sptr &observer); bool IsAbilityForegroundObserverExist(const sptr &observer); void AddObserverDeathRecipient(const sptr &observer, const ObserverType &type); void RemoveObserverDeathRecipient(const sptr &observer); AppStateObserverMap GetAppStateObserverMapCopy(); AppForegroundStateObserverSet GetAppForegroundStateObserverSetCopy(); AbilityforegroundObserverSet GetAbilityforegroundObserverSetCopy(); ProcessData WrapProcessData(const std::shared_ptr &appRecord); ProcessData WrapRenderProcessData(const std::shared_ptr &renderRecord); int32_t WrapChildProcessData(ProcessData &processData, std::shared_ptr childRecord); void OnObserverDied(const wptr &remote, const ObserverType &type); AppStateData WrapAppStateData(const std::shared_ptr &appRecord, const ApplicationState state); void HandleOnProcessCreated(const ProcessData &data); void HandleOnProcessStateChanged(const std::shared_ptr &appRecord); void HandleOnProcessDied(const ProcessData &data); void HandleOnProcessResued(const std::shared_ptr &appRecord); void HandleOnPageShow(const PageStateData pageStateData); void HandleOnPageHide(const PageStateData pageStateData); void HandleOnAppCacheStateChanged(const std::shared_ptr &appRecord, ApplicationState state); private: std::shared_ptr handler_; int32_t dummyCode_ = 0; ffrt::mutex observerLock_; AppStateObserverMap appStateObserverMap_; ffrt::mutex appForegroundObserverLock_; AppForegroundStateObserverSet appForegroundStateObserverSet_; ffrt::mutex abilityforegroundObserverLock_; AbilityforegroundObserverSet abilityforegroundObserverSet_; ffrt::mutex recipientMapMutex_; std::map, sptr> recipientMap_; }; } // namespace AppExecFwk } // namespace OHOS #endif // OHOS_ABILITY_RUNTIME_APP_STATE_OBSERVER_MANAGER_H