• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "oh_preferences_option.h"
17 
18 #include "log_print.h"
19 #include "oh_convertor.h"
20 #include "oh_preferences_impl.h"
21 #include "oh_preferences_err_code.h"
22 #include "preferences_helper.h"
23 
24 using namespace OHOS::PreferencesNdk;
25 
SetFileName(const std::string & str)26 int OH_PreferencesOption::SetFileName(const std::string &str)
27 {
28     std::unique_lock<std::shared_mutex> writeLock(opMutex_);
29     if (str.empty()) {
30         LOG_ERROR("Set file path failed, str is empty");
31         return OH_Preferences_ErrCode::PREFERENCES_ERROR_INVALID_PARAM;
32     }
33     fileName = str;
34     return OH_Preferences_ErrCode::PREFERENCES_OK;
35 }
36 
SetBundleName(const std::string & str)37 void OH_PreferencesOption::SetBundleName(const std::string &str)
38 {
39     std::unique_lock<std::shared_mutex> writeLock(opMutex_);
40     bundleName = str;
41 }
42 
SetDataGroupId(const std::string & str)43 void OH_PreferencesOption::SetDataGroupId(const std::string &str)
44 {
45     std::unique_lock<std::shared_mutex> writeLock(opMutex_);
46     dataGroupId = str;
47 }
48 
SetStorageType(const Preferences_StorageType & type)49 void OH_PreferencesOption::SetStorageType(const Preferences_StorageType &type)
50 {
51     std::unique_lock<std::shared_mutex> writeLock(opMutex_);
52     storageType = type;
53 }
54 
GetStorageType()55 Preferences_StorageType OH_PreferencesOption::GetStorageType()
56 {
57     std::shared_lock<std::shared_mutex> readLock(opMutex_);
58     return storageType;
59 }
60 
GetFileName()61 std::string OH_PreferencesOption::GetFileName()
62 {
63     std::shared_lock<std::shared_mutex> readLock(opMutex_);
64     return fileName;
65 }
66 
GetBundleName()67 std::string OH_PreferencesOption::GetBundleName()
68 {
69     std::shared_lock<std::shared_mutex> readLock(opMutex_);
70     return bundleName;
71 }
72 
GetDataGroupId()73 std::string OH_PreferencesOption::GetDataGroupId()
74 {
75     std::shared_lock<std::shared_mutex> readLock(opMutex_);
76     return dataGroupId;
77 }
78 
OH_PreferencesOption_Create(void)79 OH_PreferencesOption* OH_PreferencesOption_Create(void)
80 {
81     OH_PreferencesOption* option = new (std::nothrow) OH_PreferencesOption();
82     if (option == nullptr) {
83         LOG_ERROR("new option object failed");
84         return nullptr;
85     }
86     option->cid = PreferencesNdkStructId::PREFERENCES_OH_OPTION_CID;
87     if (!OHOS::NativePreferences::PreferencesHelper::IsStorageTypeSupported(
88         OHConvertor::NdkStorageTypeToNative(Preferences_StorageType::PREFERENCES_STORAGE_GSKV))) {
89         option->SetStorageType(Preferences_StorageType::PREFERENCES_STORAGE_XML);
90     }
91     return option;
92 }
93 
OH_PreferencesOption_SetFileName(OH_PreferencesOption * option,const char * fileName)94 int OH_PreferencesOption_SetFileName(OH_PreferencesOption *option, const char *fileName)
95 {
96     if (option == nullptr || fileName == nullptr ||
97         !NDKPreferencesUtils::PreferencesStructValidCheck(
98             option->cid, PreferencesNdkStructId::PREFERENCES_OH_OPTION_CID)) {
99         LOG_ERROR("set option's file path failed, option is null: %{public}d, fileName is null: %{public}d, "
100             "err: %{public}d", (option == nullptr), (fileName == nullptr),
101             OH_Preferences_ErrCode::PREFERENCES_ERROR_INVALID_PARAM);
102         return OH_Preferences_ErrCode::PREFERENCES_ERROR_INVALID_PARAM;
103     }
104     return option->SetFileName(std::string(fileName));
105 }
106 
OH_PreferencesOption_SetBundleName(OH_PreferencesOption * option,const char * bundleName)107 int OH_PreferencesOption_SetBundleName(OH_PreferencesOption *option, const char *bundleName)
108 {
109     if (option == nullptr || bundleName == nullptr ||
110         !NDKPreferencesUtils::PreferencesStructValidCheck(
111             option->cid, PreferencesNdkStructId::PREFERENCES_OH_OPTION_CID)) {
112         LOG_ERROR("set option's bundleName failed, option is null: %{public}d, "
113             "bundleName is null: %{public}d, errCode: %{public}d", (option == nullptr),
114             (bundleName == nullptr), OH_Preferences_ErrCode::PREFERENCES_ERROR_INVALID_PARAM);
115         return OH_Preferences_ErrCode::PREFERENCES_ERROR_INVALID_PARAM;
116     }
117     option->SetBundleName(std::string(bundleName));
118     return OH_Preferences_ErrCode::PREFERENCES_OK;
119 }
120 
OH_PreferencesOption_SetDataGroupId(OH_PreferencesOption * option,const char * dataGroupId)121 int OH_PreferencesOption_SetDataGroupId(OH_PreferencesOption *option, const char *dataGroupId)
122 {
123     if (option == nullptr || dataGroupId == nullptr ||
124         !NDKPreferencesUtils::PreferencesStructValidCheck(
125             option->cid, PreferencesNdkStructId::PREFERENCES_OH_OPTION_CID)) {
126         LOG_ERROR("set option's dataGroupId failed, option is null: %{public}d, "
127             "dataGroupId is null: %{public}d, errCode: %{public}d", (option == nullptr),
128             (dataGroupId == nullptr), OH_Preferences_ErrCode::PREFERENCES_ERROR_INVALID_PARAM);
129         return OH_Preferences_ErrCode::PREFERENCES_ERROR_INVALID_PARAM;
130     }
131     option->SetDataGroupId(std::string(dataGroupId));
132     return OH_Preferences_ErrCode::PREFERENCES_OK;
133 }
134 
OH_PreferencesOption_SetStorageType(OH_PreferencesOption * option,Preferences_StorageType type)135 int OH_PreferencesOption_SetStorageType(OH_PreferencesOption *option, Preferences_StorageType type)
136 {
137     if (option == nullptr || !NDKPreferencesUtils::PreferencesStructValidCheck(
138         option->cid, PreferencesNdkStructId::PREFERENCES_OH_OPTION_CID)) {
139         LOG_ERROR("set option's storage type failed, option is null: %{public}d", (option == nullptr));
140         return OH_Preferences_ErrCode::PREFERENCES_ERROR_INVALID_PARAM;
141     }
142     if (type < Preferences_StorageType::PREFERENCES_STORAGE_XML ||
143         type > Preferences_StorageType::PREFERENCES_STORAGE_GSKV) {
144         LOG_ERROR("set option's storage type failed, type invalid: %{public}d", static_cast<int>(type));
145         return OH_Preferences_ErrCode::PREFERENCES_ERROR_INVALID_PARAM;
146     }
147 
148     option->SetStorageType(type);
149     return OH_Preferences_ErrCode::PREFERENCES_OK;
150 }
151 
OH_PreferencesOption_Destroy(OH_PreferencesOption * option)152 int OH_PreferencesOption_Destroy(OH_PreferencesOption* option)
153 {
154     if (option == nullptr ||
155         !NDKPreferencesUtils::PreferencesStructValidCheck(
156             option->cid, PreferencesNdkStructId::PREFERENCES_OH_OPTION_CID)) {
157         LOG_ERROR("destroy option failed, option is null: %{public}d, errCode: %{public}d",
158             (option == nullptr), OH_Preferences_ErrCode::PREFERENCES_ERROR_INVALID_PARAM);
159         return OH_Preferences_ErrCode::PREFERENCES_ERROR_INVALID_PARAM;
160     }
161     delete option;
162     return OH_Preferences_ErrCode::PREFERENCES_OK;
163 }
164