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