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 Set(const std::string& key, const std::string& value) override; 41 std::string Get(const std::string& key) override; 42 void Clear() override; 43 void Delete(const std::string& key) override; 44 45 private: 46 std::shared_ptr<NativePreferences::Preferences> GetPreference(const std::string& fileName); 47 int errCode_; 48 std::string fileName_; 49 }; 50 51 class StorageProxyImpl : public StorageInterface { 52 RefPtr<Storage> GetStorage(const RefPtr<TaskExecutor>& taskExecutor) const override; 53 }; 54 } // namespace OHOS::Ace 55 #endif // FOUNDATION_ACE_ACE_ENGINE_ADAPTER_OHOS_CAPABILITY_PERFERENCE_STORAGE_IMPL_H