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 #include "base/utils/utils.h"
17 #include "storage_impl.h"
18
19 namespace OHOS::Ace {
GetPreference(const std::string & fileName)20 std::shared_ptr<NativePreferences::Preferences> StorageImpl::GetPreference(const std::string& fileName)
21 {
22 return NativePreferences::PreferencesHelper::GetPreferences(fileName, errCode_);
23 }
24
SetString(const std::string & key,const std::string & value)25 void StorageImpl::SetString(const std::string& key, const std::string& value)
26 {
27 std::shared_ptr<NativePreferences::Preferences> pref = GetPreference(fileName_);
28 CHECK_NULL_VOID(pref);
29 LOGI("Set preference with key %{public}s, value %{public}s", key.c_str(), value.c_str());
30 pref->PutString(key, value);
31 pref->Flush();
32 }
33
GetString(const std::string & key)34 std::string StorageImpl::GetString(const std::string& key)
35 {
36 std::shared_ptr<NativePreferences::Preferences> pref = GetPreference(fileName_);
37 CHECK_NULL_RETURN(pref, "");
38 LOGI("Get preference with key %{public}s", key.c_str());
39 return pref->GetString(key, "");
40 }
41
Clear()42 void StorageImpl::Clear()
43 {
44 std::shared_ptr<NativePreferences::Preferences> pref = GetPreference(fileName_);
45 CHECK_NULL_VOID(pref);
46 pref->Clear();
47 LOGI("StorageImpl: Clear preferences");
48 NativePreferences::PreferencesHelper::DeletePreferences(fileName_);
49 }
50
Delete(const std::string & key)51 void StorageImpl::Delete(const std::string& key)
52 {
53 std::shared_ptr<NativePreferences::Preferences> pref = GetPreference(fileName_);
54 CHECK_NULL_VOID(pref);
55 LOGI("StorageImpl: Delete preference with key %{public}s", key.c_str());
56 pref->Delete(key);
57 pref->FlushSync();
58 }
59
GetStorage(const RefPtr<TaskExecutor> & taskExecutor) const60 RefPtr<Storage> StorageProxyImpl::GetStorage(const RefPtr<TaskExecutor>& taskExecutor) const
61 {
62 return AceType::MakeRefPtr<StorageImpl>(taskExecutor);
63 }
64 } // namespace OHOS::Ace