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_CLIENT_H 17 #define OHOS_ABILITY_RUNTIME_DATAOBS_MGR_CLIENT_H 18 19 #include <mutex> 20 21 #include "concurrent_map.h" 22 #include "data_ability_observer_interface.h" 23 #include "dataobs_mgr_errors.h" 24 #include "dataobs_mgr_interface.h" 25 #include "uri.h" 26 27 #include "iremote_object.h" 28 29 namespace OHOS { 30 namespace AAFwk { 31 /** 32 * @class DataObsMgrClient 33 * DataObsMgrClient is used to access dataobs manager services. 34 */ 35 class DataObsMgrClient { 36 public: 37 DataObsMgrClient(); 38 virtual ~DataObsMgrClient(); 39 static std::shared_ptr<DataObsMgrClient> GetInstance(); 40 41 /** 42 * Registers an observer to DataObsMgr specified by the given Uri. 43 * 44 * @param uri, Indicates the path of the data to operate. 45 * @param dataObserver, Indicates the IDataAbilityObserver object. 46 * 47 * @return Returns ERR_OK on success, others on failure. 48 */ 49 ErrCode RegisterObserver(const Uri &uri, sptr<IDataAbilityObserver> dataObserver); 50 51 /** 52 * Deregisters an observer used for DataObsMgr specified by the given Uri. 53 * 54 * @param uri, Indicates the path of the data to operate. 55 * @param dataObserver, Indicates the IDataAbilityObserver object. 56 * 57 * @return Returns ERR_OK on success, others on failure. 58 */ 59 ErrCode UnregisterObserver(const Uri &uri, sptr<IDataAbilityObserver> dataObserver); 60 61 /** 62 * Notifies the registered observers of a change to the data resource specified by Uri. 63 * 64 * @param uri, Indicates the path of the data to operate. 65 * 66 * @return Returns ERR_OK on success, others on failure. 67 */ 68 ErrCode NotifyChange(const Uri &uri); 69 70 /** 71 * Registers an observer to DataObsMgr specified by the given Uri. 72 * 73 * @param uri, Indicates the path of the data to operate. 74 * @param dataObserver, Indicates the IDataAbilityObserver object. 75 * 76 * @return Returns SUCCESS on success, others on failure. 77 */ 78 Status RegisterObserverExt(const Uri &uri, sptr<IDataAbilityObserver> dataObserver, bool isDescendants); 79 80 /** 81 * Deregisters an observer used for DataObsMgr specified by the given Uri. 82 * 83 * @param uri, Indicates the path of the data to operate. 84 * @param dataObserver, Indicates the IDataAbilityObserver object. 85 * 86 * @return Returns SUCCESS on success, others on failure. 87 */ 88 Status UnregisterObserverExt(const Uri &uri, sptr<IDataAbilityObserver> dataObserver); 89 90 /** 91 * Deregisters observers used for DataObsMgr specified. 92 * 93 * @param dataObserver, Indicates the IDataAbilityObserver object. 94 * 95 * @return Returns SUCCESS on success, others on failure. 96 */ 97 Status UnregisterObserverExt(sptr<IDataAbilityObserver> dataObserver); 98 99 /** 100 * Notifies the registered observers of a change to the data resource specified by Uris. 101 * 102 * @param changeInfo Indicates the info of the data to operate. 103 * 104 * @return Returns SUCCESS on success, others on failure. 105 */ 106 Status NotifyChangeExt(const ChangeInfo &changeInfo); 107 108 private: 109 class SystemAbilityStatusChangeListener; 110 /** 111 * Connect dataobs manager service. 112 * 113 * @return Returns SUCCESS on success, others on failure. 114 */ 115 std::pair<Status, sptr<IDataObsMgr>> GetObsMgr(); 116 117 void ResetService(); 118 void OnRemoteDied(); 119 void ReRegister(); 120 121 class ServiceDeathRecipient : public IRemoteObject::DeathRecipient { 122 public: ServiceDeathRecipient(std::weak_ptr<DataObsMgrClient> owner)123 explicit ServiceDeathRecipient(std::weak_ptr<DataObsMgrClient> owner) : owner_(owner) {} OnRemoteDied(const wptr<IRemoteObject> & object)124 void OnRemoteDied(const wptr<IRemoteObject> &object) override 125 { 126 auto serviceClient = owner_.lock(); 127 if (serviceClient != nullptr) { 128 serviceClient->OnRemoteDied(); 129 } 130 } 131 132 private: 133 std::weak_ptr<DataObsMgrClient> owner_; 134 }; 135 136 static constexpr int RESUB_INTERVAL = 2; 137 static std::mutex mutex_; 138 static std::shared_ptr<DataObsMgrClient> instance_; 139 sptr<IDataObsMgr> dataObsManger_; 140 ConcurrentMap<sptr<IDataAbilityObserver>, std::list<Uri>> observers_; 141 142 struct Param { ParamParam143 Param(const Uri &uri, bool isDescendants) : uri(uri), isDescendants(isDescendants){}; 144 Uri uri; 145 bool isDescendants; 146 }; 147 ConcurrentMap<sptr<IDataAbilityObserver>, std::list<Param>> observerExts_; 148 sptr<SystemAbilityStatusChangeListener> callback_; 149 }; 150 } // namespace AAFwk 151 } // namespace OHOS 152 #endif // OHOS_ABILITY_RUNTIME_DATAOBS_MGR_CLIENT_H 153