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 "storage_impl.h"
17 #include "application_context.h"
18
19 namespace OHOS::Ace {
StorageImpl(int areaMode)20 StorageImpl::StorageImpl(int areaMode) : Storage()
21 {
22 std::string fileName = "";
23 // areaMode >= 0 means using global path
24 if (areaMode >= 0) {
25 auto appContext = OHOS::AbilityRuntime::Context::GetApplicationContext();
26 if (appContext != nullptr) {
27 auto defaultAreaMode = appContext->GetArea();
28 appContext->SwitchArea(areaMode);
29 fileName = appContext->GetFilesDir();
30 appContext->SwitchArea(defaultAreaMode);
31 } else {
32 LOGE("appContext get failed in StorageImpl.");
33 }
34 } else {
35 // areaMode < 0 means using module path
36 fileName = AceApplicationInfo::GetInstance().GetDataFileDirPath();
37 }
38 if (fileName.empty()) {
39 LOGE("Cannot get storage date file path.");
40 }
41 fileName_ = fileName + "/persistent_storage";
42 };
43
GetPreference(const std::string & fileName)44 std::shared_ptr<NativePreferences::Preferences> StorageImpl::GetPreference(const std::string& fileName)
45 {
46 auto it = preferences_.find(fileName);
47 if (it != preferences_.end()) {
48 return it->second;
49 }
50 auto pref = NativePreferences::PreferencesHelper::GetPreferences(fileName, errCode_);
51 preferences_.insert(std::make_pair(fileName, pref));
52 return pref;
53 }
54
SetString(const std::string & key,const std::string & value)55 void StorageImpl::SetString(const std::string& key, const std::string& value)
56 {
57 std::shared_ptr<NativePreferences::Preferences> pref = GetPreference(fileName_);
58 CHECK_NULL_VOID(pref);
59 pref->PutString(key, value);
60 pref->Flush();
61 }
62
GetString(const std::string & key)63 std::string StorageImpl::GetString(const std::string& key)
64 {
65 std::shared_ptr<NativePreferences::Preferences> pref = GetPreference(fileName_);
66 CHECK_NULL_RETURN(pref, "");
67 return pref->GetString(key, "");
68 }
69
Clear()70 void StorageImpl::Clear()
71 {
72 std::shared_ptr<NativePreferences::Preferences> pref = GetPreference(fileName_);
73 CHECK_NULL_VOID(pref);
74 pref->Clear();
75 NativePreferences::PreferencesHelper::DeletePreferences(fileName_);
76 preferences_.erase(fileName_);
77 }
78
Delete(const std::string & key)79 void StorageImpl::Delete(const std::string& key)
80 {
81 std::shared_ptr<NativePreferences::Preferences> pref = GetPreference(fileName_);
82 CHECK_NULL_VOID(pref);
83 pref->Delete(key);
84 pref->FlushSync();
85 }
86
GetStorage(int areaMode) const87 RefPtr<Storage> StorageProxyImpl::GetStorage(int areaMode) const
88 {
89 return AceType::MakeRefPtr<StorageImpl>(areaMode);
90 }
91 } // namespace OHOS::Ace