• 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_RDB_SUBSCRIBER_MANAGER_H
17 #define DATASHARESERVICE_RDB_SUBSCRIBER_MANAGER_H
18 
19 #include <list>
20 #include <string>
21 
22 #include "concurrent_map.h"
23 #include "context.h"
24 #include "data_provider_config.h"
25 #include "data_proxy_observer.h"
26 #include "data_share_db_config.h"
27 #include "datashare_template.h"
28 #include "executor_pool.h"
29 
30 namespace OHOS::DataShare {
31 struct Key {
32     Key(const std::string &uri, int64_t subscriberId, const std::string &bundleName);
33     bool operator==(const Key &rhs) const;
34     bool operator!=(const Key &rhs) const;
35     bool operator<(const Key &rhs) const;
36     bool operator>(const Key &rhs) const;
37     bool operator<=(const Key &rhs) const;
38     bool operator>=(const Key &rhs) const;
39     const std::string uri;
40     const int64_t subscriberId;
41     const std::string bundleName;
42 };
43 class TemplateManager {
44 public:
45     static TemplateManager &GetInstance();
46     int32_t Add(const Key &key, int32_t userId, const Template &tpl);
47     int32_t Delete(const Key &key, int32_t userId);
48     bool Get(const Key &key, int32_t userId, Template &tpl);
49 
50 private:
51     TemplateManager();
52     friend class RdbSubscriberManager;
53 };
54 
55 class RdbSubscriberManager {
56 public:
57     static RdbSubscriberManager &GetInstance();
58     int Add(const Key &key, const sptr<IDataProxyRdbObserver> observer, std::shared_ptr<Context> context,
59         std::shared_ptr<ExecutorPool> executorPool);
60     int Delete(const Key &key, uint32_t firstCallerTokenId);
61     void Delete(uint32_t callerTokenId, uint32_t callerPid);
62     int Disable(const Key &key, uint32_t firstCallerTokenId);
63     int Enable(const Key &key, std::shared_ptr<Context> context);
64     void Emit(const std::string &uri, int64_t subscriberId, const std::string &bundleName,
65         std::shared_ptr<Context> context);
66     void Emit(const std::string &uri, std::shared_ptr<Context> context);
67     void Emit(const std::string &uri, int32_t userId, DistributedData::StoreMetaData &metaData);
68     void EmitByKey(const Key &key, int32_t userId, const DistributedData::StoreMetaData &metaData);
69     DistributedData::StoreMetaData GenMetaDataFromContext(const std::shared_ptr<Context> context);
70     std::vector<Key> GetKeysByUri(const std::string &uri);
71     void Clear();
72 
73 private:
74     struct ObserverNode {
75         ObserverNode(const sptr<IDataProxyRdbObserver> &observer, uint32_t firstCallerTokenId,
76             uint32_t callerTokenId = 0, uint32_t callerPid = 0, int32_t userId = 0);
77         sptr<IDataProxyRdbObserver> observer;
78         uint32_t firstCallerTokenId;
79         uint32_t callerTokenId;
80         uint32_t callerPid;
81         bool enabled = true;
82         bool isNotifyOnEnabled = false;
83         int32_t userId = 0;
84     };
85 
86     RdbSubscriberManager() = default;
87     ConcurrentMap<Key, std::vector<ObserverNode>> rdbCache_;
88     int Notify(const Key &key, int32_t userId,
89         const std::vector<ObserverNode> &val, const DistributedData::StoreMetaData &metaData);
90     int GetEnableObserverCount(const Key &key);
91     void SetObserverNotifyOnEnabled(std::vector<ObserverNode> &nodes);
92 };
93 } // namespace OHOS::DataShare
94 #endif // DATASHARESERVICE_RDB_SUBSCRIBER_MANAGER_H
95