1bc03f14fSopenharmony_ci/* 2bc03f14fSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 3bc03f14fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4bc03f14fSopenharmony_ci * you may not use this file except in compliance with the License. 5bc03f14fSopenharmony_ci * You may obtain a copy of the License at 6bc03f14fSopenharmony_ci * 7bc03f14fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8bc03f14fSopenharmony_ci * 9bc03f14fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10bc03f14fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11bc03f14fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12bc03f14fSopenharmony_ci * See the License for the specific language governing permissions and 13bc03f14fSopenharmony_ci * limitations under the License. 14bc03f14fSopenharmony_ci */ 15bc03f14fSopenharmony_ci 16bc03f14fSopenharmony_ci#include <memory> 17bc03f14fSopenharmony_ci#include <string> 18bc03f14fSopenharmony_ci 19bc03f14fSopenharmony_ci#include "datashare_delegate.h" 20bc03f14fSopenharmony_ci#include "datashare_predicates.h" 21bc03f14fSopenharmony_ci#include "datashare_result_set.h" 22bc03f14fSopenharmony_ci#include "datashare_values_bucket.h" 23bc03f14fSopenharmony_ci#include "iservice_registry.h" 24bc03f14fSopenharmony_ci#include "pasteboard_error.h" 25bc03f14fSopenharmony_ci#include "pasteboard_hilog.h" 26bc03f14fSopenharmony_ci#include "third_party/vixl/src/utils-vixl.h" 27bc03f14fSopenharmony_ci 28bc03f14fSopenharmony_cinamespace OHOS::MiscServices { 29bc03f14fSopenharmony_ciconst constexpr char *SETTING_COLUMN_KEYWORD = "KEYWORD"; 30bc03f14fSopenharmony_ciconst constexpr char *SETTING_COLUMN_VALUE = "VALUE"; 31bc03f14fSopenharmony_ciconst constexpr char *SETTING_URI_PROXY = "datashare:///com.ohos.settingsdata/entry/settingsdata/" 32bc03f14fSopenharmony_ci "SETTINGSDATA?Proxy=true"; 33bc03f14fSopenharmony_ciconstexpr const char *SETTINGS_DATA_EXT_URI = "datashare:///com.ohos.settingsdata.DataAbility"; 34bc03f14fSopenharmony_ciconstexpr const int32_t PASTEBOARD_SA_ID = 3701; 35bc03f14fSopenharmony_ci 36bc03f14fSopenharmony_cistd::mutex DataShareDelegate::mutex_; 37bc03f14fSopenharmony_cisptr<IRemoteObject> DataShareDelegate::remoteObj_ = nullptr; 38bc03f14fSopenharmony_ciDataShareDelegate *DataShareDelegate::instance_ = nullptr; 39bc03f14fSopenharmony_ciDataShareDelegate &DataShareDelegate::GetInstance() 40bc03f14fSopenharmony_ci{ 41bc03f14fSopenharmony_ci if (instance_ == nullptr) { 42bc03f14fSopenharmony_ci std::lock_guard<std::mutex> lock(mutex_); 43bc03f14fSopenharmony_ci if (instance_ == nullptr) { 44bc03f14fSopenharmony_ci instance_ = new DataShareDelegate(); 45bc03f14fSopenharmony_ci Initialize(); 46bc03f14fSopenharmony_ci } 47bc03f14fSopenharmony_ci } 48bc03f14fSopenharmony_ci return *instance_; 49bc03f14fSopenharmony_ci} 50bc03f14fSopenharmony_ci 51bc03f14fSopenharmony_civoid DataShareDelegate::Initialize() 52bc03f14fSopenharmony_ci{ 53bc03f14fSopenharmony_ci auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 54bc03f14fSopenharmony_ci if (samgr == nullptr) { 55bc03f14fSopenharmony_ci PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "get sa manager return nullptr"); 56bc03f14fSopenharmony_ci return; 57bc03f14fSopenharmony_ci } 58bc03f14fSopenharmony_ci auto remoteObj = samgr->GetSystemAbility(PASTEBOARD_SA_ID); 59bc03f14fSopenharmony_ci if (remoteObj == nullptr) { 60bc03f14fSopenharmony_ci PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "get system ability failed, id=%{public}d", PASTEBOARD_SA_ID); 61bc03f14fSopenharmony_ci return; 62bc03f14fSopenharmony_ci } 63bc03f14fSopenharmony_ci remoteObj_ = remoteObj; 64bc03f14fSopenharmony_ci} 65bc03f14fSopenharmony_ci 66bc03f14fSopenharmony_cistd::shared_ptr<DataShare::DataShareHelper> DataShareDelegate::CreateDataShareHelper() 67bc03f14fSopenharmony_ci{ 68bc03f14fSopenharmony_ci auto [ret, helper] = DataShare::DataShareHelper::Create(remoteObj_, SETTING_URI_PROXY, SETTINGS_DATA_EXT_URI); 69bc03f14fSopenharmony_ci if (ret != 0) { 70bc03f14fSopenharmony_ci PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "create helper failed ret %{public}d", ret); 71bc03f14fSopenharmony_ci return nullptr; 72bc03f14fSopenharmony_ci } 73bc03f14fSopenharmony_ci return helper; 74bc03f14fSopenharmony_ci} 75bc03f14fSopenharmony_ci 76bc03f14fSopenharmony_cibool DataShareDelegate::ReleaseDataShareHelper(std::shared_ptr<DataShare::DataShareHelper> helper) 77bc03f14fSopenharmony_ci{ 78bc03f14fSopenharmony_ci if (!helper->Release()) { 79bc03f14fSopenharmony_ci PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "release helper fail"); 80bc03f14fSopenharmony_ci return false; 81bc03f14fSopenharmony_ci } 82bc03f14fSopenharmony_ci return true; 83bc03f14fSopenharmony_ci} 84bc03f14fSopenharmony_ci 85bc03f14fSopenharmony_ciint32_t DataShareDelegate::GetValue(const std::string &key, std::string &value) 86bc03f14fSopenharmony_ci{ 87bc03f14fSopenharmony_ci auto helper = CreateDataShareHelper(); 88bc03f14fSopenharmony_ci if (helper == nullptr) { 89bc03f14fSopenharmony_ci return static_cast<int32_t>(PasteboardError::CREATE_DATASHARE_SERVICE_ERROR); 90bc03f14fSopenharmony_ci } 91bc03f14fSopenharmony_ci std::vector<std::string> columns = { SETTING_COLUMN_VALUE }; 92bc03f14fSopenharmony_ci DataShare::DataSharePredicates predicates; 93bc03f14fSopenharmony_ci predicates.EqualTo(SETTING_COLUMN_KEYWORD, key); 94bc03f14fSopenharmony_ci Uri uri = MakeUri(key); 95bc03f14fSopenharmony_ci auto resultSet = helper->Query(uri, predicates, columns); 96bc03f14fSopenharmony_ci ReleaseDataShareHelper(helper); 97bc03f14fSopenharmony_ci if (resultSet == nullptr) { 98bc03f14fSopenharmony_ci PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Query failed key=%{public}s", key.c_str()); 99bc03f14fSopenharmony_ci return static_cast<int32_t>(PasteboardError::INVALID_RETURN_VALUE_ERROR); 100bc03f14fSopenharmony_ci } 101bc03f14fSopenharmony_ci int32_t count; 102bc03f14fSopenharmony_ci resultSet->GetRowCount(count); 103bc03f14fSopenharmony_ci if (count == 0) { 104bc03f14fSopenharmony_ci PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "no value, key=%{public}s, count=%{public}d", key.c_str(), count); 105bc03f14fSopenharmony_ci resultSet->Close(); 106bc03f14fSopenharmony_ci return static_cast<int32_t>(PasteboardError::QUERY_SETTING_NO_DATA_ERROR); 107bc03f14fSopenharmony_ci } 108bc03f14fSopenharmony_ci int32_t index = 0; 109bc03f14fSopenharmony_ci resultSet->GoToRow(index); 110bc03f14fSopenharmony_ci int32_t ret = resultSet->GetString(index, value); 111bc03f14fSopenharmony_ci if (ret != DataShare::E_OK) { 112bc03f14fSopenharmony_ci PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "get value failed, ret=%{public}d", ret); 113bc03f14fSopenharmony_ci resultSet->Close(); 114bc03f14fSopenharmony_ci return ret; 115bc03f14fSopenharmony_ci } 116bc03f14fSopenharmony_ci resultSet->Close(); 117bc03f14fSopenharmony_ci return static_cast<int32_t>(PasteboardError::E_OK); 118bc03f14fSopenharmony_ci} 119bc03f14fSopenharmony_ci 120bc03f14fSopenharmony_ciUri DataShareDelegate::MakeUri(const std::string &key) 121bc03f14fSopenharmony_ci{ 122bc03f14fSopenharmony_ci Uri uri(std::string(SETTING_URI_PROXY) + "&key=" + key); 123bc03f14fSopenharmony_ci return uri; 124bc03f14fSopenharmony_ci} 125bc03f14fSopenharmony_ci 126bc03f14fSopenharmony_ciint32_t DataShareDelegate::RegisterObserver(const std::string &key, sptr<AAFwk::IDataAbilityObserver> observer) 127bc03f14fSopenharmony_ci{ 128bc03f14fSopenharmony_ci auto uri = MakeUri(key); 129bc03f14fSopenharmony_ci auto helper = CreateDataShareHelper(); 130bc03f14fSopenharmony_ci if (helper == nullptr) { 131bc03f14fSopenharmony_ci return ERR_NO_INIT; 132bc03f14fSopenharmony_ci } 133bc03f14fSopenharmony_ci helper->RegisterObserver(uri, observer); 134bc03f14fSopenharmony_ci ReleaseDataShareHelper(helper); 135bc03f14fSopenharmony_ci PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "register observer %{public}s", uri.ToString().c_str()); 136bc03f14fSopenharmony_ci return ERR_OK; 137bc03f14fSopenharmony_ci} 138bc03f14fSopenharmony_ci 139bc03f14fSopenharmony_ciint32_t DataShareDelegate::UnregisterObserver(const std::string &key, sptr<AAFwk::IDataAbilityObserver> observer) 140bc03f14fSopenharmony_ci{ 141bc03f14fSopenharmony_ci auto uri = MakeUri(key); 142bc03f14fSopenharmony_ci auto helper = CreateDataShareHelper(); 143bc03f14fSopenharmony_ci if (helper == nullptr) { 144bc03f14fSopenharmony_ci return ERR_NO_INIT; 145bc03f14fSopenharmony_ci } 146bc03f14fSopenharmony_ci helper->UnregisterObserver(uri, observer); 147bc03f14fSopenharmony_ci ReleaseDataShareHelper(helper); 148bc03f14fSopenharmony_ci PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "unregister observer %{public}s", uri.ToString().c_str()); 149bc03f14fSopenharmony_ci return ERR_OK; 150bc03f14fSopenharmony_ci} 151bc03f14fSopenharmony_ci} // namespace OHOS::MiscServices