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 DATASHARESERVICE_PUBLISHED_DATA_SUBSCRIBER_MANAGER_H 17 #define DATASHARESERVICE_PUBLISHED_DATA_SUBSCRIBER_MANAGER_H 18 19 #include <list> 20 #include <string> 21 22 #include "concurrent_map.h" 23 #include "context.h" 24 #include "data_proxy_observer.h" 25 #include "datashare_template.h" 26 #include "executor_pool.h" 27 namespace OHOS::DataShare { 28 struct PublishedDataKey { 29 PublishedDataKey(const std::string &key, const std::string &bundleName, int64_t subscriberId); 30 bool operator<(const PublishedDataKey &rhs) const; 31 bool operator>(const PublishedDataKey &rhs) const; 32 bool operator<=(const PublishedDataKey &rhs) const; 33 bool operator>=(const PublishedDataKey &rhs) const; 34 bool operator==(const PublishedDataKey &rhs) const; 35 bool operator!=(const PublishedDataKey &rhs) const; 36 std::string key; 37 std::string bundleName; 38 int64_t subscriberId; 39 }; 40 41 class PublishedDataSubscriberManager { 42 public: 43 static PublishedDataSubscriberManager &GetInstance(); 44 int Add(const PublishedDataKey &key, const sptr<IDataProxyPublishedDataObserver> observer, 45 uint32_t firstCallerTokenId); 46 int Delete(const PublishedDataKey &key, uint32_t firstCallerTokenId); 47 void Delete(uint32_t callerTokenId, uint32_t callerPid); 48 int Disable(const PublishedDataKey &key, uint32_t firstCallerTokenId); 49 int Enable(const PublishedDataKey &key, uint32_t firstCallerTokenId); 50 void Emit(const std::vector<PublishedDataKey> &keys, int32_t userId, const std::string &ownerBundleName, 51 const sptr<IDataProxyPublishedDataObserver> observer = nullptr); 52 void Clear(); 53 int GetCount(const PublishedDataKey &key); 54 55 bool IsNotifyOnEnabled(const PublishedDataKey &key, uint32_t callerTokenId); 56 void SetObserversNotifiedOnEnabled(const std::vector<PublishedDataKey> &keys); 57 58 private: 59 struct ObserverNode { 60 ObserverNode(const sptr<IDataProxyPublishedDataObserver> &observer, uint32_t firstCallerTokenId, 61 uint32_t callerTokenId = 0, uint32_t callerPid = 0); 62 sptr<IDataProxyPublishedDataObserver> observer; 63 uint32_t firstCallerTokenId; 64 uint32_t callerTokenId; 65 uint32_t callerPid; 66 bool enabled = true; 67 bool isNotifyOnEnabled = false; 68 }; 69 70 PublishedDataSubscriberManager() = default; 71 void PutInto(std::map<sptr<IDataProxyPublishedDataObserver>, std::vector<PublishedDataKey>> &, 72 const std::vector<ObserverNode> &, const PublishedDataKey &, const sptr<IDataProxyPublishedDataObserver>); 73 ConcurrentMap<PublishedDataKey, std::vector<ObserverNode>> publishedDataCache_; 74 }; 75 } // namespace OHOS::DataShare 76 #endif // DATASHARESERVICE_PUBLISHED_DATA_SUBSCRIBER_MANAGER_H 77