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 #ifndef FOUNDATION_ACE_ACE_ENGINE_ADAPTER_OHOS_CAPABILITY_PERFERENCE_STORAGE_IMPL_H
16 #define FOUNDATION_ACE_ACE_ENGINE_ADAPTER_OHOS_CAPABILITY_PERFERENCE_STORAGE_IMPL_H
17 
18 #include "base/thread/task_executor.h"
19 #include "preferences.h"
20 #include "preferences_helper.h"
21 #include "base/utils/string_utils.h"
22 #include "core/common/storage/storage_interface.h"
23 #include "core/common/container.h"
24 
25 namespace OHOS::Ace {
26 class StorageImpl : public Storage {
27     DECLARE_ACE_TYPE(StorageImpl, Storage);
28 
29 public:
StorageImpl()30     StorageImpl() : Storage()
31     {
32         auto fileName = AceApplicationInfo::GetInstance().GetDataFileDirPath();
33         if (fileName.empty()) {
34             LOGW("Cannot get storage date file path.");
35         }
36         fileName_ = fileName + "/persistent_storage";
37     };
38     ~StorageImpl() override = default;
39 
40     void SetString(const std::string& key, const std::string& value) override;
41     std::string GetString(const std::string& key) override;
SetDouble(const std::string & key,const double value)42     void SetDouble(const std::string& key, const double value) override
43     {}
GetDouble(const std::string & key,double & value)44     bool GetDouble(const std::string& key, double& value) override
45     {
46         return false;
47     }
SetBoolean(const std::string & key,const bool value)48     void SetBoolean(const std::string& key, const bool value) override
49     {}
GetBoolean(const std::string & key,bool & value)50     bool GetBoolean(const std::string& key, bool& value) override
51     {
52         return false;
53     }
54     void Clear() override;
55     void Delete(const std::string& key) override;
56 
57 private:
58     std::shared_ptr<NativePreferences::Preferences> GetPreference(const std::string& fileName);
59     int errCode_ = 0;
60     std::string fileName_;
61     std::unordered_map<std::string, std::shared_ptr<NativePreferences::Preferences>> preferences_;
62 };
63 
64 class StorageProxyImpl : public StorageInterface {
65     RefPtr<Storage> GetStorage() const override;
66 };
67 } // namespace OHOS::Ace
68 #endif // FOUNDATION_ACE_ACE_ENGINE_ADAPTER_OHOS_CAPABILITY_PERFERENCE_STORAGE_IMPL_H