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