• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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_PROXY_DATA_SUBSCRIBER_MANAGER_H
17 #define DATASHARESERVICE_PROXY_DATA_SUBSCRIBER_MANAGER_H
18 
19 #include "concurrent_map.h"
20 #include "data_proxy_observer.h"
21 #include "dataproxy_handle_common.h"
22 
23 namespace OHOS::DataShare {
24 struct ProxyDataKey {
25     ProxyDataKey(const std::string &uri, const std::string &bundleName);
26     bool operator<(const ProxyDataKey &rhs) const;
27     bool operator>(const ProxyDataKey &rhs) const;
28     bool operator<=(const ProxyDataKey &rhs) const;
29     bool operator>=(const ProxyDataKey &rhs) const;
30     bool operator==(const ProxyDataKey &rhs) const;
31     bool operator!=(const ProxyDataKey &rhs) const;
32     std::string uri;
33     std::string bundleName;
34 };
35 
36 class ProxyDataSubscriberManager {
37 public:
38     static ProxyDataSubscriberManager &GetInstance();
39     DataProxyErrorCode Add(const ProxyDataKey &key, const sptr<IProxyDataObserver> &observer,
40         const std::string &bundleName, const std::string &callerAppIdentifier, const int32_t &userId);
41     DataProxyErrorCode Delete(const ProxyDataKey &key, const int32_t &userId);
42     void Emit(const std::vector<ProxyDataKey> &keys, const std::map<DataShareObserver::ChangeType,
43         std::vector<DataShareProxyData>> &datas, const int32_t &userId);
44     void Clear();
45 
46 private:
47 struct ObserverNode {
48     ObserverNode(const sptr<IProxyDataObserver> &observer, const uint32_t &callerTokenId,
49         const std::string &bundleName, const std::string &callerAppIdentifier, const int32_t &userId = 0);
50         sptr<IProxyDataObserver> observer;
51         uint32_t callerTokenId;
52         std::string bundleName;
53         std::string callerAppIdentifier;
54         int32_t userId = 0;
55     };
56 
57     bool CheckAllowList(const std::vector<std::string> &allowList,
58         const std::string &callerAppIdentifier);
59     ProxyDataSubscriberManager() = default;
60     ConcurrentMap<ProxyDataKey, std::vector<ObserverNode>> ProxyDataCache_;
61 };
62 } // namespace OHOS::DataShare
63 #endif // DATASHARESERVICE_PROXY_DATA_SUBSCRIBER_MANAGER_H
64