1 /*
2  * Copyright (c) 2021 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_DATAOBS_MGR_INNER_H
17 #define OHOS_ABILITY_RUNTIME_DATAOBS_MGR_INNER_H
18 
19 #include <atomic>
20 #include <list>
21 #include <map>
22 #include <memory>
23 #include <mutex>
24 #include <string>
25 #include "cpp/mutex.h"
26 
27 #include "data_ability_observer_interface.h"
28 
29 namespace OHOS {
30 namespace AAFwk {
31 class DataObsMgrInner : public std::enable_shared_from_this<DataObsMgrInner> {
32 public:
33     using ObsMapType = std::map<std::string, std::list<sptr<IDataAbilityObserver>>>;
34     using ObsRecipientMapType = std::map<sptr<IRemoteObject>, sptr<IRemoteObject::DeathRecipient>>;
35 
36     DataObsMgrInner();
37     virtual ~DataObsMgrInner();
38 
39     int HandleRegisterObserver(const Uri &uri, sptr<IDataAbilityObserver> dataObserver);
40     int HandleUnregisterObserver(const Uri &uri, sptr<IDataAbilityObserver> dataObserver);
41     int HandleNotifyChange(const Uri &uri);
42     void OnCallBackDied(const wptr<IRemoteObject> &remote);
43 
44 private:
45     void AddObsDeathRecipient(sptr<IDataAbilityObserver> dataObserver);
46     void RemoveObsDeathRecipient(sptr<IRemoteObject> dataObserver);
47     void RemoveObs(sptr<IRemoteObject> dataObserver);
48     bool HaveRegistered(sptr<IDataAbilityObserver> dataObserver);
49 
50     static constexpr uint32_t OBS_NUM_MAX = 50;
51     ffrt::mutex innerMutex_;
52     ObsMapType observers_;
53     ObsRecipientMapType obsRecipient_;
54 };
55 }  // namespace AAFwk
56 }  // namespace OHOS
57 #endif  // OHOS_ABILITY_RUNTIME_DATAOBS_MGR_INNER_H
58