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