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 <memory>
17 #include <string>
18
19 #include "datashare_delegate.h"
20 #include "datashare_predicates.h"
21 #include "datashare_result_set.h"
22 #include "datashare_values_bucket.h"
23 #include "iservice_registry.h"
24 #include "pasteboard_error.h"
25 #include "pasteboard_hilog.h"
26 #include "third_party/vixl/src/utils-vixl.h"
27
28 namespace OHOS::MiscServices {
29 const constexpr char *SETTING_COLUMN_KEYWORD = "KEYWORD";
30 const constexpr char *SETTING_COLUMN_VALUE = "VALUE";
31 const constexpr char *SETTING_URI_PROXY = "datashare:///com.ohos.settingsdata/entry/settingsdata/"
32 "SETTINGSDATA?Proxy=true";
33 constexpr const char *SETTINGS_DATA_EXT_URI = "datashare:///com.ohos.settingsdata.DataAbility";
34 constexpr const int32_t PASTEBOARD_SA_ID = 3701;
35
36 std::mutex DataShareDelegate::mutex_;
37 sptr<IRemoteObject> DataShareDelegate::remoteObj_ = nullptr;
38 DataShareDelegate *DataShareDelegate::instance_ = nullptr;
GetInstance()39 DataShareDelegate &DataShareDelegate::GetInstance()
40 {
41 if (instance_ == nullptr) {
42 std::lock_guard<std::mutex> lock(mutex_);
43 if (instance_ == nullptr) {
44 instance_ = new DataShareDelegate();
45 Initialize();
46 }
47 }
48 return *instance_;
49 }
50
Initialize()51 void DataShareDelegate::Initialize()
52 {
53 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
54 if (samgr == nullptr) {
55 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "get sa manager return nullptr");
56 return;
57 }
58 auto remoteObj = samgr->GetSystemAbility(PASTEBOARD_SA_ID);
59 if (remoteObj == nullptr) {
60 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "get system ability failed, id=%{public}d", PASTEBOARD_SA_ID);
61 return;
62 }
63 remoteObj_ = remoteObj;
64 }
65
CreateDataShareHelper()66 std::shared_ptr<DataShare::DataShareHelper> DataShareDelegate::CreateDataShareHelper()
67 {
68 auto [ret, helper] = DataShare::DataShareHelper::Create(remoteObj_, SETTING_URI_PROXY, SETTINGS_DATA_EXT_URI);
69 if (ret != 0) {
70 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "create helper failed ret %{public}d", ret);
71 return nullptr;
72 }
73 return helper;
74 }
75
ReleaseDataShareHelper(std::shared_ptr<DataShare::DataShareHelper> helper)76 bool DataShareDelegate::ReleaseDataShareHelper(std::shared_ptr<DataShare::DataShareHelper> helper)
77 {
78 if (!helper->Release()) {
79 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "release helper fail");
80 return false;
81 }
82 return true;
83 }
84
GetValue(const std::string &key, std::string &value)85 int32_t DataShareDelegate::GetValue(const std::string &key, std::string &value)
86 {
87 auto helper = CreateDataShareHelper();
88 if (helper == nullptr) {
89 return static_cast<int32_t>(PasteboardError::CREATE_DATASHARE_SERVICE_ERROR);
90 }
91 std::vector<std::string> columns = { SETTING_COLUMN_VALUE };
92 DataShare::DataSharePredicates predicates;
93 predicates.EqualTo(SETTING_COLUMN_KEYWORD, key);
94 Uri uri = MakeUri(key);
95 auto resultSet = helper->Query(uri, predicates, columns);
96 ReleaseDataShareHelper(helper);
97 if (resultSet == nullptr) {
98 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Query failed key=%{public}s", key.c_str());
99 return static_cast<int32_t>(PasteboardError::INVALID_RETURN_VALUE_ERROR);
100 }
101 int32_t count;
102 resultSet->GetRowCount(count);
103 if (count == 0) {
104 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "no value, key=%{public}s, count=%{public}d", key.c_str(), count);
105 resultSet->Close();
106 return static_cast<int32_t>(PasteboardError::QUERY_SETTING_NO_DATA_ERROR);
107 }
108 int32_t index = 0;
109 resultSet->GoToRow(index);
110 int32_t ret = resultSet->GetString(index, value);
111 if (ret != DataShare::E_OK) {
112 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "get value failed, ret=%{public}d", ret);
113 resultSet->Close();
114 return ret;
115 }
116 resultSet->Close();
117 return static_cast<int32_t>(PasteboardError::E_OK);
118 }
119
MakeUri(const std::string &key)120 Uri DataShareDelegate::MakeUri(const std::string &key)
121 {
122 Uri uri(std::string(SETTING_URI_PROXY) + "&key=" + key);
123 return uri;
124 }
125
RegisterObserver(const std::string &key, sptr<AAFwk::IDataAbilityObserver> observer)126 int32_t DataShareDelegate::RegisterObserver(const std::string &key, sptr<AAFwk::IDataAbilityObserver> observer)
127 {
128 auto uri = MakeUri(key);
129 auto helper = CreateDataShareHelper();
130 if (helper == nullptr) {
131 return ERR_NO_INIT;
132 }
133 helper->RegisterObserver(uri, observer);
134 ReleaseDataShareHelper(helper);
135 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "register observer %{public}s", uri.ToString().c_str());
136 return ERR_OK;
137 }
138
UnregisterObserver(const std::string &key, sptr<AAFwk::IDataAbilityObserver> observer)139 int32_t DataShareDelegate::UnregisterObserver(const std::string &key, sptr<AAFwk::IDataAbilityObserver> observer)
140 {
141 auto uri = MakeUri(key);
142 auto helper = CreateDataShareHelper();
143 if (helper == nullptr) {
144 return ERR_NO_INIT;
145 }
146 helper->UnregisterObserver(uri, observer);
147 ReleaseDataShareHelper(helper);
148 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "unregister observer %{public}s", uri.ToString().c_str());
149 return ERR_OK;
150 }
151 } // namespace OHOS::MiscServices