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