• 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 FOUNDATION_ACE_ADAPTER_OHOS_CAPABILITY_DISTRIBUTED_STORAGE_DISTRIBUTED_STORAGE_H
17 #define FOUNDATION_ACE_ADAPTER_OHOS_CAPABILITY_DISTRIBUTED_STORAGE_DISTRIBUTED_STORAGE_H
18 
19 #include <map>
20 
21 #include "core/common/storage/storage.h"
22 
23 namespace OHOS::ObjectStore {
24 class DistributedObject;
25 class ObjectWatcher;
26 } // namespace OHOS::ObjectStore
27 
28 namespace OHOS::Ace {
29 
30 using OnDataChangeCallback = std::function<void(const std::string&, const std::vector<std::string>&)>;
31 using ObjectStatusNotifyCallback = std::function<void(const std::string&, const std::string&)>;
32 
33 class DistributedObjectPtr final {
34 public:
35     DistributedObjectPtr(const std::string& sessionId, OnDataChangeCallback&& onChange);
36     ~DistributedObjectPtr();
37 
38     OHOS::ObjectStore::DistributedObject* GetRawPtr();
IsInvalid()39     bool IsInvalid() const
40     {
41         return invalid_;
42     }
43 
44 private:
45     std::string sessionId_;
46     OHOS::ObjectStore::DistributedObject* object_ = nullptr;
47     std::shared_ptr<OHOS::ObjectStore::ObjectWatcher> watcher_;
48     bool invalid_ = true;
49 };
50 
51 class DistributedStorage final : public Storage {
52     DECLARE_ACE_TYPE(DistributedStorage, Storage);
53 
54 public:
DistributedStorage(const std::string & sessionId,const RefPtr<TaskExecutor> & taskExecutor)55     explicit DistributedStorage(const std::string& sessionId, const RefPtr<TaskExecutor>& taskExecutor)
56         : Storage(taskExecutor), sessionId_(sessionId)
57     {}
58     ~DistributedStorage() override = default;
59 
60     bool Init(std::function<void(const std::string&)>&& notifier);
61 
62     void SetString(const std::string& key, const std::string& value) override;
63     std::string GetString(const std::string& key) override;
64     void SetDouble(const std::string& key, const double value) override;
65     bool GetDouble(const std::string& key, double& value) override;
66     void SetBoolean(const std::string& key, const bool value) override;
67     bool GetBoolean(const std::string& key, bool& value) override;
68     DataType GetDataType(const std::string& key) override;
Clear()69     void Clear() override {}
Delete(const std::string & key)70     void Delete(const std::string& key) override {}
71     void NotifyStatus(const std::string& status);
72 
73     static void AddStorage(const std::string& sessionId, RefPtr<DistributedStorage> storage);
74     static void DeleteStorage(const std::string& sessionId);
75     static void OnStatusNotify(const std::string& sessionId, const std::string& status);
76 
77 private:
78     std::unique_ptr<DistributedObjectPtr> objectPtr_;
79     std::string sessionId_;
80     std::function<void(const std::string&)> notifyCallback_;
81 
82     static std::map<std::string, RefPtr<DistributedStorage>> storageMap_;
83 };
84 
85 } // namespace OHOS::Ace
86 
87 #endif // FOUNDATION_ACE_ADAPTER_OHOS_CAPABILITY_DISTRIBUTED_STORAGE_DISTRIBUTED_STORAGE_H