• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,
30         int64_t subscriberId);
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     bool operator!=(const PublishedDataKey &rhs) const;
37     std::string key;
38     std::string bundleName;
39     int64_t subscriberId;
40 };
41 
42 class PublishedDataSubscriberManager {
43 public:
44     static PublishedDataSubscriberManager &GetInstance();
45     int Add(const PublishedDataKey &key, const sptr<IDataProxyPublishedDataObserver> observer,
46         uint32_t firstCallerTokenId, int32_t userId);
47     int Delete(const PublishedDataKey &key, uint32_t firstCallerTokenId);
48     void Delete(uint32_t callerTokenId, uint32_t callerPid);
49     int Disable(const PublishedDataKey &key, uint32_t firstCallerTokenId);
50     int Enable(const PublishedDataKey &key, uint32_t firstCallerTokenId);
51     void Emit(const std::vector<PublishedDataKey> &keys, int32_t userId, const std::string &ownerBundleName,
52         const sptr<IDataProxyPublishedDataObserver> observer = nullptr);
53     void Clear();
54     int GetCount(const PublishedDataKey &key);
55 
56     bool IsNotifyOnEnabled(const PublishedDataKey &key, uint32_t callerTokenId);
57     void SetObserversNotifiedOnEnabled(const std::vector<PublishedDataKey> &keys);
58 
59 private:
60     struct ObserverNode {
61         ObserverNode(const sptr<IDataProxyPublishedDataObserver> &observer, uint32_t firstCallerTokenId,
62             uint32_t callerTokenId = 0, uint32_t callerPid = 0, int32_t userId = 0);
63         sptr<IDataProxyPublishedDataObserver> observer;
64         uint32_t firstCallerTokenId;
65         uint32_t callerTokenId;
66         uint32_t callerPid;
67         bool enabled = true;
68         bool isNotifyOnEnabled = false;
69         int32_t userId = 0;
70     };
71 
72     PublishedDataSubscriberManager() = default;
73     void PutInto(std::map<sptr<IDataProxyPublishedDataObserver>, std::vector<PublishedDataKey>> &,
74         const std::vector<ObserverNode> &, const PublishedDataKey &, const sptr<IDataProxyPublishedDataObserver>,
75         int32_t userId);
76     ConcurrentMap<PublishedDataKey, std::vector<ObserverNode>> publishedDataCache_;
77 };
78 } // namespace OHOS::DataShare
79 #endif // DATASHARESERVICE_PUBLISHED_DATA_SUBSCRIBER_MANAGER_H
80