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 "session/host/include/scene_persistent_storage.h"
17
18 #include <filesystem>
19
20 namespace OHOS {
21 namespace Rosen {
22
23 std::string ScenePersistentStorage::saveDir_;
24 std::map<ScenePersistentStorageType, std::string> ScenePersistentStorage::storagePath_;
25
HasKey(const std::string & key,ScenePersistentStorageType storageType)26 bool ScenePersistentStorage::HasKey(const std::string& key, ScenePersistentStorageType storageType)
27 {
28 bool res = false;
29 auto pref = GetPreference(storageType);
30 if (!pref) {
31 WLOGE("[ScenePersistentStorage] Preferences is nullptr");
32 return res;
33 }
34 res = pref->HasKey(key);
35 WLOGD("[ScenePersistentStorage] %{public}s %{public}s", (res ? "Has persisted key:" : "Don't have persisted key:"),
36 key.c_str());
37 return res;
38 }
39
Delete(const std::string & key,ScenePersistentStorageType storageType)40 void ScenePersistentStorage::Delete(const std::string& key, ScenePersistentStorageType storageType)
41 {
42 auto pref = GetPreference(storageType);
43 if (!pref) {
44 WLOGE("[ScenePersistentStorage] Preferences is nullptr");
45 return;
46 }
47 pref->Delete(key);
48 pref->Flush();
49 WLOGD("[ScenePersistentStorage] Delete key %{public}s", key.c_str());
50 }
51
GetPreference(ScenePersistentStorageType storageType)52 std::shared_ptr<PersistentPerference> ScenePersistentStorage::GetPreference(ScenePersistentStorageType storageType)
53 {
54 auto iter = storagePath_.find(storageType);
55 if (iter == storagePath_.end()) {
56 return nullptr;
57 }
58 auto fileName = storagePath_[storageType];
59 int errCode;
60 auto pref = NativePreferences::PreferencesHelper::GetPreferences(fileName, errCode);
61 WLOGD("[ScenePersistentStorage] GetPreference file: %{public}s, errCode: %{public}d", fileName.c_str(), errCode);
62 return pref;
63 }
64
InitDir(std::string dir)65 void ScenePersistentStorage::InitDir(std::string dir)
66 {
67 saveDir_ = dir;
68 std::filesystem::path fileDir {saveDir_};
69 if (!std::filesystem::exists(fileDir)) {
70 std::filesystem::create_directories(fileDir);
71 std::filesystem::permissions(fileDir, std::filesystem::perms::owner_read | std::filesystem::perms::owner_write |
72 std::filesystem::perms::group_read | std::filesystem::perms::group_write);
73 }
74 storagePath_ = {
75 { ScenePersistentStorageType::ASPECT_RATIO, saveDir_ + "/session_window_aspect_ratio" },
76 { ScenePersistentStorageType::MAXIMIZE_STATE, saveDir_ + "/session_window_maximize_state" },
77 };
78 }
79
80 } // namespace Rosen
81 } // namespace OHOS
82