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 <string> 22 #include <memory> 23 #include <map> 24 #include <mutex> 25 26 #include "iremote_object.h" 27 #include "refbase.h" 28 29 #include "data_ability_observer_interface.h" 30 31 #include "event_handler.h" 32 33 namespace OHOS { 34 namespace AAFwk { 35 using EventHandler = OHOS::AppExecFwk::EventHandler; 36 class DataObsMgrInner : public std::enable_shared_from_this<DataObsMgrInner> { 37 public: 38 using ObsMapType = std::map<std::string, std::list<sptr<IDataAbilityObserver>>>; 39 using ObsListType = std::list<sptr<IDataAbilityObserver>>; 40 using ObsRecipientMapType = std::map<sptr<IRemoteObject>, sptr<IRemoteObject::DeathRecipient>>; 41 42 DataObsMgrInner(); 43 virtual ~DataObsMgrInner(); 44 45 void SetHandler(const std::shared_ptr<EventHandler> &handler); 46 int HandleRegisterObserver(const Uri &uri, const sptr<IDataAbilityObserver> &dataObserver); 47 int HandleUnregisterObserver(const Uri &uri, const sptr<IDataAbilityObserver> &dataObserver); 48 int HandleNotifyChange(const Uri &uri); 49 bool CheckNeedLimmit(); 50 bool CheckRegisteFull(const Uri &uri); 51 void AtomicAddTaskCount(); 52 void AtomicSubTaskCount(); 53 void OnCallBackDied(const wptr<IRemoteObject> &remote); 54 55 private: 56 bool GetObsListFromMap(const Uri &uri, ObsListType &obslist); 57 void AddObsDeathRecipient(const sptr<IDataAbilityObserver> &dataObserver); 58 void RemoveObsDeathRecipient(const sptr<IRemoteObject> &dataObserver); 59 void HandleCallBackDiedTask(const sptr<IRemoteObject> &dataObserver); 60 void RemoveObsFromMap(const sptr<IRemoteObject> &dataObserver); 61 bool ObsExistInMap(const sptr<IDataAbilityObserver> &dataObserver); 62 63 std::atomic_int taskCount_; 64 const int taskCount_max_ = 50; 65 const unsigned int obs_max_ = 50; 66 static std::mutex innerMutex_; 67 std::shared_ptr<EventHandler> handler_ = nullptr; 68 ObsMapType obsmap_; 69 ObsRecipientMapType recipientMap_; 70 }; 71 } // namespace AAFwk 72 } // namespace OHOS 73 #endif // OHOS_ABILITY_RUNTIME_DATAOBS_MGR_INNER_H 74