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 "preferences_wrapper.h"
17 
18 #include "preferences.h"
19 #include "preferences_errno.h"
20 #include "preferences_helper.h"
21 #include "security_guard_log.h"
22 
23 namespace OHOS::Security::SecurityGuard {
24 namespace {
25     const std::string PATH = "/data/service/el1/public/security_guard/securityguardProperties.xml";
26 }
27 
PutInt(const std::string & key,int value)28 int32_t PreferenceWrapper::PutInt(const std::string &key, int value)
29 {
30     int32_t errCode = NativePreferences::E_ERROR;
31     std::shared_ptr<NativePreferences::Preferences> preferences =
32         NativePreferences::PreferencesHelper::GetPreferences(PATH, errCode);
33     if (preferences == nullptr || errCode != NativePreferences::E_OK) {
34         SGLOGE("get preferences error, code=%{public}d", errCode);
35         return errCode;
36     }
37     errCode = preferences->PutInt(key, value);
38     if (errCode != NativePreferences::E_OK) {
39         SGLOGE("put int error, code=%{public}d", errCode);
40         return errCode;
41     }
42     preferences->Flush();
43     return NativePreferences::E_OK;
44 }
45 
GetInt(const std::string & key,int defaultValue)46 int32_t PreferenceWrapper::GetInt(const std::string &key, int defaultValue)
47 {
48     int32_t errCode = NativePreferences::E_ERROR;
49     std::shared_ptr<NativePreferences::Preferences> preferences =
50         NativePreferences::PreferencesHelper::GetPreferences(PATH, errCode);
51     if (preferences == nullptr || errCode != NativePreferences::E_OK) {
52         SGLOGE("get preferences error, code=%{public}d", errCode);
53         return -1;
54     }
55     return preferences->GetInt(key, defaultValue);
56 }
57 }