• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 DISTRIBUTED_OBJECTSTORE_IMPL_H
17 #define DISTRIBUTED_OBJECTSTORE_IMPL_H
18 
19 #include <shared_mutex>
20 
21 #include "distributed_objectstore.h"
22 namespace OHOS::ObjectStore {
23 class WatcherProxy;
24 enum SyncStatus : int8_t {
25     SYNC_START,
26     SYNCING,
27     SYNC_SUCCESS,
28     SYNC_FAIL,
29 };
30 class DistributedObjectStoreImpl : public DistributedObjectStore {
31 public:
32     DistributedObjectStoreImpl(FlatObjectStore *flatObjectStore);
33     ~DistributedObjectStoreImpl() override;
34     uint32_t Get(const std::string &sessionId, DistributedObject **object) override;
35     DistributedObject *CreateObject(const std::string &sessionId) override;
36     DistributedObject *CreateObject(const std::string &sessionId, uint32_t &status) override;
37     uint32_t DeleteObject(const std::string &sessionId) override;
38     uint32_t Watch(DistributedObject *object, std::shared_ptr<ObjectWatcher> watcher) override;
39     uint32_t UnWatch(DistributedObject *object) override;
40     uint32_t SetStatusNotifier(std::shared_ptr<StatusNotifier> notifier) override;
41     uint32_t SetProgressNotifier(std::shared_ptr<ProgressNotifier> notifier) override;
42     void NotifyCachedStatus(const std::string &sessionId) override;
43     void NotifyProgressStatus(const std::string &sessionId) override;
44 
45 private:
46     DistributedObject *CacheObject(const std::string &sessionId, FlatObjectStore *flatObjectStore);
47     void RemoveCacheObject(const std::string &sessionId);
48     FlatObjectStore *flatObjectStore_ = nullptr;
49     std::map<DistributedObject *, std::shared_ptr<WatcherProxy>> watchers_;
50     std::shared_mutex dataMutex_ {};
51     std::vector<DistributedObject *> objects_ {};
52     std::mutex watchersLock_;
53 };
54 class StatusNotifierProxy : public StatusWatcher {
55 public:
56     virtual ~StatusNotifierProxy();
57     StatusNotifierProxy(const std::shared_ptr<StatusNotifier> &notifier);
58     void OnChanged(
59         const std::string &sessionId, const std::string &networkId, const std::string &onlineStatus) override;
60 
61 private:
62     std::shared_ptr<StatusNotifier> notifier;
63 };
64 
65 class ProgressNotifierProxy : public ProgressWatcher {
66 public:
67     virtual ~ProgressNotifierProxy();
68     ProgressNotifierProxy(const std::shared_ptr<ProgressNotifier> &notifier);
69     void OnChanged(const std::string &sessionId, int32_t progress) override;
70 
71 private:
72     std::shared_ptr<ProgressNotifier> notifier;
73 };
74 
75 class WatcherProxy : public FlatObjectWatcher {
76 public:
77     using AssetChangeCallback = std::function<void(const std::string& sessionId,
78         const std::string &assetKey, std::shared_ptr<ObjectWatcher> watcher)>;
79 
80     WatcherProxy(const std::shared_ptr<ObjectWatcher> objectWatcher, const std::string &sessionId);
81     void OnChanged(
82         const std::string &sessionId, const std::vector<std::string> &changedData, bool enableTransfer) override;
83     void SetAssetChangeCallBack(const AssetChangeCallback &assetChangeCallback);
84 private:
85     bool FindChangedAssetKey(const std::string &changedKey, std::string &assetKey);
86     std::shared_ptr<ObjectWatcher> objectWatcher_;
87     AssetChangeCallback assetChangeCallback_;
88 };
89 } // namespace OHOS::ObjectStore
90 #endif // DISTRIBUTED_OBJECTSTORE_IMPL_H
91