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(const RefPtr<TaskExecutor> & taskExecutor)30 explicit StorageImpl(const RefPtr<TaskExecutor>& taskExecutor) : Storage(taskExecutor) 31 { 32 if (!Container::Current()) { 33 LOGE("StorageImpl: Container empty"); 34 return; 35 } 36 fileName_ = Container::Current()->GetFilesDataPath() + "/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_; 60 std::string fileName_; 61 }; 62 63 class StorageProxyImpl : public StorageInterface { 64 RefPtr<Storage> GetStorage(const RefPtr<TaskExecutor>& taskExecutor) const override; 65 }; 66 } // namespace OHOS::Ace 67 #endif // FOUNDATION_ACE_ACE_ENGINE_ADAPTER_OHOS_CAPABILITY_PERFERENCE_STORAGE_IMPL_H