1 /*
2 * Copyright (c) 2023 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 "persistent_storage.h"
17
18 namespace OHOS {
19 namespace Rosen {
20 namespace {
21 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "PersistentStorage"};
22 }
23 std::map<PersistentStorageType, std::string> PersistentStorage::storagePath_ = {
24 { PersistentStorageType::ASPECT_RATIO, "/data/service/el1/public/window/window_aspect_ratio.xml" },
25 { PersistentStorageType::MAXIMIZE_STATE, "/data/service/el1/public/window/window_maximize_state.xml" },
26 };
27
HasKey(const std::string & key,PersistentStorageType storageType)28 bool PersistentStorage::HasKey(const std::string& key, PersistentStorageType storageType)
29 {
30 bool res = false;
31 auto pref = GetPreference(storageType);
32 if (!pref) {
33 WLOGE("[PersistentStorage] Preferences is nullptr");
34 return res;
35 }
36 res = pref->HasKey(key);
37 WLOGD("[PersistentStorage] %{public}s %{public}s", key.c_str(),
38 (res ? "Has persisted key:" : "Don't have persisted key:"));
39 return res;
40 }
41
Delete(const std::string & key,PersistentStorageType storageType)42 void PersistentStorage::Delete(const std::string& key, PersistentStorageType storageType)
43 {
44 auto pref = GetPreference(storageType);
45 if (!pref) {
46 WLOGE("[PersistentStorage] Preferences is nullptr");
47 return;
48 }
49 pref->Delete(key);
50 pref->Flush();
51 WLOGD("[PersistentStorage] Delete key %{public}s", key.c_str());
52 }
53
GetPreference(PersistentStorageType storageType)54 std::shared_ptr<PersistentPerference> PersistentStorage::GetPreference(PersistentStorageType storageType)
55 {
56 auto iter = storagePath_.find(storageType);
57 if (iter == storagePath_.end()) {
58 return nullptr;
59 }
60 auto fileName = storagePath_[storageType];
61 int errCode;
62 auto pref = NativePreferences::PreferencesHelper::GetPreferences(fileName, errCode);
63 WLOGD("[PersistentStorage] GetPreference fileName: %{public}s, errCode: %{public}d", fileName.c_str(), errCode);
64 return pref;
65 }
66 } // namespace Rosen
67 } // namespace OHOS