1 /*
2 * Copyright (c) 2024-2025 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 "setting_observer_manager.h"
17
18 #include "datashare_helper.h"
19 #include "datashare_result_set.h"
20 #include "ipc_skeleton.h"
21 #include "iservice_registry.h"
22 #include "hiview_logger.h"
23 #include "rdb_helper.h"
24 #include "rdb_store.h"
25 #include "system_ability_definition.h"
26 #include "uri.h"
27
28 namespace OHOS {
29 namespace HiviewDFX {
30 DEFINE_LOG_TAG("HiView-SettingObserverManager");
31 namespace {
32 constexpr char SETTINGS_DATA_BASE_URI[] =
33 "datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true";
34
CreateDataShareHelper()35 std::shared_ptr<DataShare::DataShareHelper> CreateDataShareHelper()
36 {
37 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
38 if (samgr == nullptr) {
39 HIVIEW_LOGE("SAMGR is nullptr");
40 return nullptr;
41 }
42 sptr<IRemoteObject> remoteObj = samgr->GetSystemAbility(DFX_SYS_EVENT_SERVICE_ABILITY_ID);
43 if (remoteObj == nullptr) {
44 HIVIEW_LOGE("UE SA ability is nullptr");
45 return nullptr;
46 }
47 return DataShare::DataShareHelper::Creator(remoteObj, SETTINGS_DATA_BASE_URI);
48 }
49 }
50
OnChange()51 void SettingObserver::OnChange()
52 {
53 if (callback_ != nullptr) {
54 callback_(paramKey_);
55 }
56 }
57
SettingObserverManager()58 SettingObserverManager::SettingObserverManager()
59 {
60 }
61
~SettingObserverManager()62 SettingObserverManager::~SettingObserverManager()
63 {
64 }
65
RegisterObserver(const std::string & paramKey,SettingObserver::ObserverCallback callback)66 bool SettingObserverManager::RegisterObserver(const std::string& paramKey, SettingObserver::ObserverCallback callback)
67 {
68 auto observer = GetSettingObserver(paramKey);
69 if (observer != nullptr) {
70 HIVIEW_LOGI("observer has been registered with key %{public}s", paramKey.c_str());
71 UnregisterObserver(paramKey);
72 }
73 auto helper = CreateDataShareHelper();
74 if (helper == nullptr) {
75 HIVIEW_LOGE("DataShareHelper is null with key %{public}s", paramKey.c_str());
76 return false;
77 }
78 Uri uri = AssembleUri(paramKey);
79 observer = new SettingObserver(paramKey, callback);
80 helper->RegisterObserver(uri, observer);
81 helper->Release();
82 HIVIEW_LOGI("succeed to register observer with key %{public}s", paramKey.c_str());
83 std::lock_guard<std::mutex> observerGurad(observersMutex_);
84 observers_[paramKey] = observer;
85 return true;
86 }
87
UnregisterObserver(const std::string & paramKey)88 bool SettingObserverManager::UnregisterObserver(const std::string& paramKey)
89 {
90 auto observer = GetSettingObserver(paramKey);
91 if (observer != nullptr) {
92 HIVIEW_LOGI("observer not found with key %{public}s", paramKey.c_str());
93 return true;
94 }
95 auto helper = CreateDataShareHelper();
96 if (helper == nullptr) {
97 HIVIEW_LOGE("DataShareHelper is null with key %{public}s", paramKey.c_str());
98 return false;
99 }
100 Uri uri = AssembleUri(paramKey);
101 helper->UnregisterObserver(uri, observer);
102 helper->Release();
103 HIVIEW_LOGI("succeed to unregister observer with key %{public}s", paramKey.c_str());
104 std::lock_guard<std::mutex> observerGurad(observersMutex_);
105 observers_.erase(paramKey);
106 return true;
107 }
108
GetStringValue(const std::string & paramKey,const std::string & defaultVal)109 std::string SettingObserverManager::GetStringValue(const std::string& paramKey, const std::string& defaultVal)
110 {
111 auto helper = CreateDataShareHelper();
112 if (helper == nullptr) {
113 HIVIEW_LOGE("DataShareHelper is null with key %{public}s", paramKey.c_str());
114 return defaultVal;
115 }
116 DataShare::DataSharePredicates predicates;
117 predicates.EqualTo("KEYWORD", paramKey);
118 std::vector<std::string> columns = { "VALUE" };
119 Uri uri = AssembleUri(paramKey);
120 auto resultSet = helper->Query(uri, predicates, columns);
121 if (resultSet == nullptr) {
122 HIVIEW_LOGE("result set is null with key %{public}s", paramKey.c_str());
123 helper->Release();
124 return defaultVal;
125 }
126 int32_t rowCount = 0;
127 resultSet->GetRowCount(rowCount);
128 if (rowCount == 0) {
129 HIVIEW_LOGE("count of result set is zero with key %{public}s", paramKey.c_str());
130 resultSet->Close();
131 helper->Release();
132 return defaultVal;
133 }
134 resultSet->GoToRow(0);
135 std::string valueResult;
136 auto ret = resultSet->GetString(0, valueResult); // get first column for setting value
137 if (ret != NativeRdb::E_OK) {
138 HIVIEW_LOGE("no result found with key %{public}s, ret is %{public}d", paramKey.c_str(), ret);
139 resultSet->Close();
140 helper->Release();
141 return defaultVal;
142 }
143 resultSet->Close();
144 helper->Release();
145 HIVIEW_LOGI("setting value is %{public}s, ret is %{public}s", valueResult.c_str(), paramKey.c_str());
146 return valueResult;
147 }
148
AssembleUri(const std::string & paramKey)149 Uri SettingObserverManager::AssembleUri(const std::string& paramKey)
150 {
151 return Uri(std::string(SETTINGS_DATA_BASE_URI) + "&key=" + paramKey);
152 }
153
GetSettingObserver(const std::string & paramKey)154 sptr<SettingObserver> SettingObserverManager::GetSettingObserver(const std::string& paramKey)
155 {
156 std::lock_guard<std::mutex> observerGurad(observersMutex_);
157 auto iter = observers_.find(paramKey);
158 if (iter != observers_.end()) {
159 return iter->second;
160 }
161 return nullptr;
162 }
163 } // HiviewDFX
164 } // OHOS
165
166