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 RDB_SUBSCRIBER_MANAGER_H 17 #define RDB_SUBSCRIBER_MANAGER_H 18 19 #include <memory> 20 21 #include "callbacks_manager.h" 22 #include "concurrent_map.h" 23 #include "data_proxy_observer.h" 24 #include "data_proxy_observer_stub.h" 25 #include "datashare_template.h" 26 #include "idatashare.h" 27 #include "iremote_stub.h" 28 #include "data_share_service_proxy.h" 29 30 namespace OHOS { 31 namespace DataShare { 32 struct RdbObserverMapKey { 33 std::string uri_; 34 std::string clearUri_; 35 TemplateId templateId_; RdbObserverMapKeyRdbObserverMapKey36 RdbObserverMapKey(const std::string &uri, const TemplateId &templateId) : uri_(uri), templateId_(templateId) 37 { 38 auto pos = uri_.find_first_of('?'); 39 if (pos != std::string::npos) { 40 clearUri_ = uri_.substr(0, pos); 41 } else { 42 clearUri_ = uri_; 43 } 44 } 45 bool operator==(const RdbObserverMapKey &node) const 46 { 47 return clearUri_ == node.clearUri_ && templateId_ == node.templateId_; 48 } 49 bool operator!=(const RdbObserverMapKey &node) const 50 { 51 return !(node == *this); 52 } 53 bool operator<(const RdbObserverMapKey &node) const 54 { 55 if (clearUri_ != node.clearUri_) { 56 return clearUri_ < node.clearUri_; 57 } 58 return templateId_ < node.templateId_; 59 } stringRdbObserverMapKey60 operator std::string() const 61 { 62 return uri_; 63 } 64 }; 65 66 class RdbObserver { 67 public: 68 RdbObserver(const RdbCallback &callback); 69 void OnChange(const RdbChangeNode &changeNode); 70 bool operator==(const RdbObserver &rhs) const; 71 bool operator!=(const RdbObserver &rhs) const; 72 73 private: 74 RdbCallback callback_; 75 }; 76 77 class RdbSubscriberManager : public CallbacksManager<RdbObserverMapKey, RdbObserver> { 78 public: 79 using Key = RdbObserverMapKey; 80 using Observer = RdbObserver; 81 using BaseCallbacks = CallbacksManager<RdbObserverMapKey, RdbObserver>; 82 static RdbSubscriberManager &GetInstance(); 83 84 std::vector<OperationResult> AddObservers(void *subscriber, std::shared_ptr<DataShareServiceProxy> proxy, 85 const std::vector<std::string> &uris, const TemplateId &templateId, const RdbCallback &callback); 86 std::vector<OperationResult> DelObservers(void *subscriber, std::shared_ptr<DataShareServiceProxy> proxy, 87 const std::vector<std::string> &uris, const TemplateId &templateId); 88 std::vector<OperationResult> DelObservers(void *subscriber, std::shared_ptr<DataShareServiceProxy> proxy); 89 std::vector<OperationResult> EnableObservers(void *subscriber, std::shared_ptr<DataShareServiceProxy> proxy, 90 const std::vector<std::string> &uris, const TemplateId &templateId); 91 std::vector<OperationResult> DisableObservers(void *subscriber, std::shared_ptr<DataShareServiceProxy> proxy, 92 const std::vector<std::string> &uris, const TemplateId &templateId); 93 void RecoverObservers(std::shared_ptr<DataShareServiceProxy> proxy); 94 void Emit(const RdbChangeNode &changeNode); 95 96 private: 97 void Emit(const std::vector<Key> &keys, const std::shared_ptr<Observer> &observer); 98 void EmitOnEnable(std::map<Key, std::vector<ObserverNodeOnEnabled>> &obsMap); 99 RdbSubscriberManager(); 100 sptr<RdbObserverStub> serviceCallback_; 101 ConcurrentMap<Key, RdbChangeNode> lastChangeNodeMap_; 102 }; 103 } // namespace DataShare 104 } // namespace OHOS 105 #endif // RDB_SUBSCRIBER_MANAGER_H 106