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