1fc223305Sopenharmony_ci/* 2fc223305Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 3fc223305Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4fc223305Sopenharmony_ci * you may not use this file except in compliance with the License. 5fc223305Sopenharmony_ci * You may obtain a copy of the License at 6fc223305Sopenharmony_ci * 7fc223305Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8fc223305Sopenharmony_ci * 9fc223305Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10fc223305Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11fc223305Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fc223305Sopenharmony_ci * See the License for the specific language governing permissions and 13fc223305Sopenharmony_ci * limitations under the License. 14fc223305Sopenharmony_ci */ 15fc223305Sopenharmony_ci 16fc223305Sopenharmony_ci#include <vector> 17fc223305Sopenharmony_ci#include <map> 18fc223305Sopenharmony_ci#include <cinttypes> 19fc223305Sopenharmony_ci 20fc223305Sopenharmony_ci#include "preferences_ffi.h" 21fc223305Sopenharmony_ci#include "preferences_impl.h" 22fc223305Sopenharmony_ci#include "preferences_log.h" 23fc223305Sopenharmony_ci#include "cj_lambda.h" 24fc223305Sopenharmony_ci 25fc223305Sopenharmony_ciusing namespace OHOS::FFI; 26fc223305Sopenharmony_ciusing namespace OHOS::Preferences; 27fc223305Sopenharmony_ci 28fc223305Sopenharmony_cinamespace OHOS { 29fc223305Sopenharmony_cinamespace Preferences { 30fc223305Sopenharmony_ciextern "C" { 31fc223305Sopenharmony_ciint64_t FfiOHOSPreferencesGetPreferences(OHOS::AbilityRuntime::Context* context, const char* name, 32fc223305Sopenharmony_ci const char* dataGroupId, int32_t* errCode) 33fc223305Sopenharmony_ci{ 34fc223305Sopenharmony_ci auto nativePreferences = FFIData::Create<PreferencesImpl>(context, name, dataGroupId, errCode); 35fc223305Sopenharmony_ci if (!nativePreferences) { 36fc223305Sopenharmony_ci return 0; 37fc223305Sopenharmony_ci } 38fc223305Sopenharmony_ci return nativePreferences->GetID(); 39fc223305Sopenharmony_ci} 40fc223305Sopenharmony_ci 41fc223305Sopenharmony_ciint32_t FfiOHOSPreferencesDeletePreferences(OHOS::AbilityRuntime::Context* context, const char* name, 42fc223305Sopenharmony_ci const char* dataGroupId) 43fc223305Sopenharmony_ci{ 44fc223305Sopenharmony_ci return PreferencesImpl::DeletePreferences(context, name, dataGroupId); 45fc223305Sopenharmony_ci} 46fc223305Sopenharmony_ci 47fc223305Sopenharmony_ciint32_t FfiOHOSPreferencesRemovePreferencesFromCache(OHOS::AbilityRuntime::Context* context, const char* name, 48fc223305Sopenharmony_ci const char* dataGroupId) 49fc223305Sopenharmony_ci{ 50fc223305Sopenharmony_ci return PreferencesImpl::RemovePreferencesFromCache(context, name, dataGroupId); 51fc223305Sopenharmony_ci} 52fc223305Sopenharmony_ci 53fc223305Sopenharmony_ciValueType FfiOHOSPreferencesGet(int64_t id, const char* key, ValueType defValue) 54fc223305Sopenharmony_ci{ 55fc223305Sopenharmony_ci auto instance = FFIData::GetData<PreferencesImpl>(id); 56fc223305Sopenharmony_ci if (!instance) { 57fc223305Sopenharmony_ci LOGE("[Preferences] instance not exist. %{public}" PRId64, id); 58fc223305Sopenharmony_ci return ValueType{0}; 59fc223305Sopenharmony_ci } 60fc223305Sopenharmony_ci return instance->Get(key, defValue); 61fc223305Sopenharmony_ci} 62fc223305Sopenharmony_ci 63fc223305Sopenharmony_ciint32_t FfiOHOSPreferencesPut(int64_t id, const char* key, ValueType value) 64fc223305Sopenharmony_ci{ 65fc223305Sopenharmony_ci auto instance = FFIData::GetData<PreferencesImpl>(id); 66fc223305Sopenharmony_ci if (!instance) { 67fc223305Sopenharmony_ci LOGE("[Preferences] instance not exist. %{public}" PRId64, id); 68fc223305Sopenharmony_ci return ERR_INVALID_INSTANCE_CODE; 69fc223305Sopenharmony_ci } 70fc223305Sopenharmony_ci return instance->Put(key, value); 71fc223305Sopenharmony_ci} 72fc223305Sopenharmony_ci 73fc223305Sopenharmony_ciValueTypes FfiOHOSPreferencesGetAll(int64_t id) 74fc223305Sopenharmony_ci{ 75fc223305Sopenharmony_ci auto instance = FFIData::GetData<PreferencesImpl>(id); 76fc223305Sopenharmony_ci if (!instance) { 77fc223305Sopenharmony_ci LOGE("[Preferences] instance not exist. %{public}" PRId64, id); 78fc223305Sopenharmony_ci return ValueTypes{0}; 79fc223305Sopenharmony_ci } 80fc223305Sopenharmony_ci return instance->GetAll(); 81fc223305Sopenharmony_ci} 82fc223305Sopenharmony_ci 83fc223305Sopenharmony_civoid FfiOHOSPreferencesFlush(int64_t id) 84fc223305Sopenharmony_ci{ 85fc223305Sopenharmony_ci auto instance = FFIData::GetData<PreferencesImpl>(id); 86fc223305Sopenharmony_ci if (!instance) { 87fc223305Sopenharmony_ci LOGE("[Preferences] instance not exist. %{public}" PRId64, id); 88fc223305Sopenharmony_ci return; 89fc223305Sopenharmony_ci } 90fc223305Sopenharmony_ci instance->Flush(); 91fc223305Sopenharmony_ci return; 92fc223305Sopenharmony_ci} 93fc223305Sopenharmony_ci 94fc223305Sopenharmony_civoid FfiOHOSPreferencesClear(int64_t id) 95fc223305Sopenharmony_ci{ 96fc223305Sopenharmony_ci auto instance = FFIData::GetData<PreferencesImpl>(id); 97fc223305Sopenharmony_ci if (!instance) { 98fc223305Sopenharmony_ci LOGE("[Preferences] instance not exist. %{public}" PRId64, id); 99fc223305Sopenharmony_ci return; 100fc223305Sopenharmony_ci } 101fc223305Sopenharmony_ci instance->Clear(); 102fc223305Sopenharmony_ci return; 103fc223305Sopenharmony_ci} 104fc223305Sopenharmony_ci 105fc223305Sopenharmony_cibool FfiOHOSPreferencesHas(int64_t id, const char* key) 106fc223305Sopenharmony_ci{ 107fc223305Sopenharmony_ci auto instance = FFIData::GetData<PreferencesImpl>(id); 108fc223305Sopenharmony_ci if (!instance) { 109fc223305Sopenharmony_ci LOGE("[Preferences] instance not exist. %{public}" PRId64, id); 110fc223305Sopenharmony_ci return false; 111fc223305Sopenharmony_ci } 112fc223305Sopenharmony_ci return instance->HasKey(key); 113fc223305Sopenharmony_ci} 114fc223305Sopenharmony_ci 115fc223305Sopenharmony_ciint32_t FfiOHOSPreferencesDelete(int64_t id, const char* key) 116fc223305Sopenharmony_ci{ 117fc223305Sopenharmony_ci auto instance = FFIData::GetData<PreferencesImpl>(id); 118fc223305Sopenharmony_ci if (!instance) { 119fc223305Sopenharmony_ci LOGE("[Preferences] instance not exist. %{public}" PRId64, id); 120fc223305Sopenharmony_ci return ERR_INVALID_INSTANCE_CODE; 121fc223305Sopenharmony_ci } 122fc223305Sopenharmony_ci return instance->Delete(key); 123fc223305Sopenharmony_ci} 124fc223305Sopenharmony_ci 125fc223305Sopenharmony_ciint32_t FfiOHOSPreferencesOn(int64_t id, const char* mode, void (*callbackRef)(const char* valueRef)) 126fc223305Sopenharmony_ci{ 127fc223305Sopenharmony_ci auto instance = FFIData::GetData<PreferencesImpl>(id); 128fc223305Sopenharmony_ci if (!instance) { 129fc223305Sopenharmony_ci LOGE("[Preferences] instance not exist. %{public}" PRId64, id); 130fc223305Sopenharmony_ci return ERR_INVALID_INSTANCE_CODE; 131fc223305Sopenharmony_ci } 132fc223305Sopenharmony_ci auto onChange = [lambda = CJLambda::Create(callbackRef)](const std::string& valueRef) -> 133fc223305Sopenharmony_ci void { lambda(valueRef.c_str()); }; 134fc223305Sopenharmony_ci return instance->RegisterObserver(mode, (std::function<void(std::string)>*)(callbackRef), onChange); 135fc223305Sopenharmony_ci} 136fc223305Sopenharmony_ci 137fc223305Sopenharmony_ciint32_t FfiOHOSPreferencesOff(int64_t id, const char* mode, void (*callbackRef)(const char* value)) 138fc223305Sopenharmony_ci{ 139fc223305Sopenharmony_ci auto instance = FFIData::GetData<PreferencesImpl>(id); 140fc223305Sopenharmony_ci if (!instance) { 141fc223305Sopenharmony_ci LOGE("[Preferences] instance not exist. %{public}" PRId64, id); 142fc223305Sopenharmony_ci return ERR_INVALID_INSTANCE_CODE; 143fc223305Sopenharmony_ci } 144fc223305Sopenharmony_ci return instance->UnRegisterObserver(mode, (std::function<void(std::string)>*)(callbackRef)); 145fc223305Sopenharmony_ci} 146fc223305Sopenharmony_ci 147fc223305Sopenharmony_ciint32_t FfiOHOSPreferencesOffAll(int64_t id, const char* mode) 148fc223305Sopenharmony_ci{ 149fc223305Sopenharmony_ci auto instance = FFIData::GetData<PreferencesImpl>(id); 150fc223305Sopenharmony_ci if (!instance) { 151fc223305Sopenharmony_ci LOGE("[Preferences] instance not exist. %{public}" PRId64, id); 152fc223305Sopenharmony_ci return ERR_INVALID_INSTANCE_CODE; 153fc223305Sopenharmony_ci } 154fc223305Sopenharmony_ci return instance->UnRegisteredAllObservers(mode); 155fc223305Sopenharmony_ci} 156fc223305Sopenharmony_ci} 157fc223305Sopenharmony_ci} 158fc223305Sopenharmony_ci}