• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <set>
22 #include <string>
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 #include "uri_permission_manager_client.h"
37 
38 namespace OHOS {
39 namespace AppExecFwk {
40 enum class ObserverType {
41     APPLICATION_STATE_OBSERVER,
42     APP_FOREGROUND_STATE_OBSERVER,
43     ABILITY_FOREGROUND_STATE_OBSERVER,
44 };
45 class AppStateObserverManager : public std::enable_shared_from_this<AppStateObserverManager> {
46     DECLARE_DELAYED_SINGLETON(AppStateObserverManager)
47 public:
48     void Init();
49     int32_t RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer,
50         const std::vector<std::string> &bundleNameList = {});
51     int32_t UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer);
52     int32_t RegisterAppForegroundStateObserver(const sptr<IAppForegroundStateObserver> &observer);
53     int32_t UnregisterAppForegroundStateObserver(const sptr<IAppForegroundStateObserver> &observer);
54     int32_t RegisterAbilityForegroundStateObserver(const sptr<IAbilityForegroundStateObserver> &observer);
55     int32_t UnregisterAbilityForegroundStateObserver(const sptr<IAbilityForegroundStateObserver> &observer);
56     void StateChangedNotifyObserver(
57         const AbilityStateData abilityStateData, bool isAbility, bool isFromWindowFocusChanged);
58     void OnAppStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord, const ApplicationState state,
59         bool needNotifyApp, bool isFromWindowFocusChanged);
60     void OnAppStarted(const std::shared_ptr<AppRunningRecord> &appRecord);
61     void OnAppStopped(const std::shared_ptr<AppRunningRecord> &appRecord);
62     void OnProcessCreated(const std::shared_ptr<AppRunningRecord> &appRecord);
63     void OnProcessStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord);
64     void OnRenderProcessCreated(const std::shared_ptr<RenderRecord> &RenderRecord);
65     void OnProcessDied(const std::shared_ptr<AppRunningRecord> &appRecord);
66     void OnRenderProcessDied(const std::shared_ptr<RenderRecord> &renderRecord);
67     void OnProcessReused(const std::shared_ptr<AppRunningRecord> &appRecord);
68     void OnPageShow(const PageStateData pageStateData);
69     void OnPageHide(const PageStateData pageStateData);
70 private:
71     void HandleAppStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord, const ApplicationState state,
72         bool needNotifyApp, bool isFromWindowFocusChanged);
73     void HandleOnAppStarted(const std::shared_ptr<AppRunningRecord> &appRecord);
74     void HandleOnAppStopped(const std::shared_ptr<AppRunningRecord> &appRecord);
75     void HandleStateChangedNotifyObserver(
76         const AbilityStateData abilityStateData, bool isAbility, bool isFromWindowFocusChanged);
77     void HandleOnAppProcessCreated(const std::shared_ptr<AppRunningRecord> &appRecord);
78     void HandleOnRenderProcessCreated(const std::shared_ptr<RenderRecord> &RenderRecord);
79     void HandleOnAppProcessDied(const std::shared_ptr<AppRunningRecord> &appRecord);
80     void HandleOnRenderProcessDied(const std::shared_ptr<RenderRecord> &RenderRecord);
81     bool ObserverExist(const sptr<IRemoteBroker> &observer);
82     bool IsAppForegroundObserverExist(const sptr<IRemoteBroker> &observer);
83     bool IsAbilityForegroundObserverExist(const sptr<IRemoteBroker> &observer);
84     void AddObserverDeathRecipient(const sptr<IRemoteBroker> &observer, const ObserverType &type);
85     void RemoveObserverDeathRecipient(const sptr<IRemoteBroker> &observer);
86     ProcessData WrapProcessData(const std::shared_ptr<AppRunningRecord> &appRecord);
87     ProcessData WrapRenderProcessData(const std::shared_ptr<RenderRecord> &renderRecord);
88     void OnObserverDied(const wptr<IRemoteObject> &remote, const ObserverType &type);
89     AppStateData WrapAppStateData(const std::shared_ptr<AppRunningRecord> &appRecord,
90     const ApplicationState state);
91     void HandleOnProcessCreated(const ProcessData &data);
92     void HandleOnProcessStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord);
93     void HandleOnProcessDied(const ProcessData &data);
94     void HandleOnProcessResued(const std::shared_ptr<AppRunningRecord> &appRecord);
95     void HandleOnPageShow(const PageStateData pageStateData);
96     void HandleOnPageHide(const PageStateData pageStateData);
97 
98 private:
99     std::shared_ptr<AAFwk::TaskHandlerWrap> handler_;
100     int32_t dummyCode_ = 0;
101     ffrt::mutex observerLock_;
102     std::map<sptr<IRemoteObject>, sptr<IRemoteObject::DeathRecipient>> recipientMap_;
103     std::map<sptr<IApplicationStateObserver>, std::vector<std::string>> appStateObserverMap_;
104     ffrt::mutex appForegroundObserverLock_;
105     std::set<sptr<IAppForegroundStateObserver>> appForegroundStateObserverSet_;
106     ffrt::mutex abilityforegroundObserverLock_;
107     std::set<sptr<IAbilityForegroundStateObserver>> abilityforegroundObserverSet_;
108 };
109 }  // namespace AppExecFwk
110 }  // namespace OHOS
111 #endif  // OHOS_ABILITY_RUNTIME_APP_STATE_OBSERVER_MANAGER_H
112