• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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_FORM_FWK_FORM_OBSERVER_RECORD_H
17 #define OHOS_FORM_FWK_FORM_OBSERVER_RECORD_H
18 
19 #include <map>
20 #include <singleton.h>
21 #include <vector>
22 
23 #include "fms_log_wrapper.h"
24 #include "iremote_object.h"
25 #include "running_form_info.h"
26 
27 namespace OHOS {
28 namespace AppExecFwk {
29 /**
30  * @class FormObserverRecord
31  * Form observer record.
32  */
33 class FormObserverRecord : public DelayedRefSingleton<FormObserverRecord> {
34 public:
35     /**
36      * @brief Set value of deathRecipient_.
37      * @param callerToken Caller ability token.
38      * @param deathRecipient DeathRecipient object.
39      */
40     void SetDeathRecipient(const sptr<IRemoteObject> &callerToken,
41         const sptr<IRemoteObject::DeathRecipient> &deathRecipient);
42 
43     /**
44      * @brief set form add observer.
45      * @param bundleName BundleName of the form host.
46      * @param callerToken Caller ability token.
47      * @return Returns ERR_OK if this function is successfully called; returns others failed.
48      */
49     ErrCode SetFormAddObserver(const std::string bundleName, const sptr<IRemoteObject> &callerToken);
50 
51     /**
52      * @brief set form remove observer.
53      * @param bundleName BundleName of the form host.
54      * @param callerToken Caller ability token.
55      * @return Returns ERR_OK if this function is successfully called; returns others failed.
56      */
57     ErrCode SetFormRemoveObserver(const std::string bundleName, const sptr<IRemoteObject> &callerToken);
58 
59     /**
60      * @brief send form add message to form observer.
61      * @param bundleName BundleName of the form host.
62      */
63     void onFormAdd(const std::string bundleName, RunningFormInfo &runningFormInfo);
64 
65     /**
66      * @brief send form remove message to form observer.
67      * @param bundleName BundleName of the form host.
68      */
69     void onFormRemove(const std::string bundleName, const RunningFormInfo runningFormInfo);
70 
71     /**
72      * @brief when form observer died clean the resource.
73      * @param remote remote object.
74      */
75     void CleanResource(const wptr<IRemoteObject> &remote);
76 
77 private:
78     mutable std::mutex formAddObserverMutex_;
79     mutable std::mutex formRemoveObserverMutex_;
80     mutable std::mutex deathRecipientsMutex_;
81     std::map<std::string, std::vector<sptr<IRemoteObject>>> formAddObservers_;
82     std::map<std::string, std::vector<sptr<IRemoteObject>>> formRemoveObservers_;
83     std::map<sptr<IRemoteObject>, sptr<IRemoteObject::DeathRecipient>> deathRecipients_;
84 
85     /**
86      * @class ClientDeathRecipient
87      * notices IRemoteBroker died.
88      */
89     class ClientDeathRecipient : public IRemoteObject::DeathRecipient {
90     public:
91         /**
92          * @brief Constructor
93          */
94         ClientDeathRecipient() = default;
95         virtual ~ClientDeathRecipient() = default;
96         /**
97          * @brief handle remote object died event.
98          * @param remote remote object.
99          */
100         void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
101     };
102 };
103 }  // namespace AppExecFwk
104 }  // namespace OHOS
105 
106 #endif // OHOS_FORM_FWK_FORM_OBSERVER_RECORD_H
107