• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 #include "dataobs_mgr_inner_pref.h"
16 
17 #include "data_ability_observer_stub.h"
18 #include "dataobs_mgr_errors.h"
19 #include "hilog_wrapper.h"
20 #include "common_utils.h"
21 
22 namespace OHOS {
23 namespace AAFwk {
24 
DataObsMgrInnerPref()25 DataObsMgrInnerPref::DataObsMgrInnerPref() {}
26 
~DataObsMgrInnerPref()27 DataObsMgrInnerPref::~DataObsMgrInnerPref() {}
28 
HandleRegisterObserver(const Uri & uri,sptr<IDataAbilityObserver> dataObserver)29 int DataObsMgrInnerPref::HandleRegisterObserver(const Uri &uri, sptr<IDataAbilityObserver> dataObserver)
30 {
31     std::lock_guard<std::mutex> lock(preferenceMutex_);
32 
33     auto [obsPair, flag] = observers_.try_emplace(uri.ToString(), std::list<sptr<IDataAbilityObserver>>());
34     if (!flag && obsPair->second.size() > OBS_NUM_MAX) {
35         HILOG_ERROR("The number of subscribers for this uri : %{public}s has reached the upper limit.",
36             CommonUtils::Anonymous(uri.ToString()).c_str());
37         return DATAOBS_SERVICE_OBS_LIMMIT;
38     }
39 
40     for (auto obs = obsPair->second.begin(); obs != obsPair->second.end(); obs++) {
41         if ((*obs)->AsObject() == dataObserver->AsObject()) {
42             HILOG_ERROR("the obs has registered on this uri : %{public}s",
43                 CommonUtils::Anonymous(uri.ToString()).c_str());
44             return OBS_EXIST;
45         }
46     }
47 
48     obsPair->second.push_back(dataObserver);
49 
50     AddObsDeathRecipient(dataObserver);
51     return NO_ERROR;
52 }
53 
HandleUnregisterObserver(const Uri & uri,sptr<IDataAbilityObserver> dataObserver)54 int DataObsMgrInnerPref::HandleUnregisterObserver(const Uri &uri, sptr<IDataAbilityObserver> dataObserver)
55 {
56     std::lock_guard<std::mutex> lock(preferenceMutex_);
57 
58     auto obsPair = observers_.find(uri.ToString());
59     if (obsPair == observers_.end()) {
60         HILOG_WARN("no obs on this uri : %{public}s", CommonUtils::Anonymous(uri.ToString()).c_str());
61         return NO_OBS_FOR_URI;
62     }
63 
64     HILOG_DEBUG("obs num is %{public}zu on this uri : %{public}s", obsPair->second.size(),
65         CommonUtils::Anonymous(uri.ToString()).c_str());
66     auto obs = obsPair->second.begin();
67     for (; obs != obsPair->second.end(); obs++) {
68         if ((*obs)->AsObject() == dataObserver->AsObject()) {
69             break;
70         }
71     }
72     if (obs == obsPair->second.end()) {
73         HILOG_WARN("no obs on this uri : %{public}s", CommonUtils::Anonymous(uri.ToString()).c_str());
74         return NO_OBS_FOR_URI;
75     }
76     obsPair->second.remove(*obs);
77     if (obsPair->second.empty()) {
78         observers_.erase(obsPair);
79     }
80 
81     if (!HaveRegistered(dataObserver)) {
82         RemoveObsDeathRecipient(dataObserver->AsObject());
83     }
84     return NO_ERROR;
85 }
86 
HandleNotifyChange(const Uri & uri)87 int DataObsMgrInnerPref::HandleNotifyChange(const Uri &uri)
88 {
89     std::list<sptr<IDataAbilityObserver>> obsList;
90     std::lock_guard<std::mutex> lock(preferenceMutex_);
91     {
92         std::string uriStr = uri.ToString();
93         size_t pos = uriStr.find('?');
94         if (pos == std::string::npos) {
95             HILOG_WARN("the current uri is missing the query section : %{public}s",
96                 CommonUtils::Anonymous(uriStr).c_str());
97             return INVALID_PARAM;
98         }
99         std::string observerKey = uriStr.substr(0, pos);
100         auto obsPair = observers_.find(observerKey);
101         if (obsPair == observers_.end()) {
102             HILOG_DEBUG("there is no obs on the uri : %{public}s", CommonUtils::Anonymous(uri.ToString()).c_str());
103             return NO_OBS_FOR_URI;
104         }
105         obsList = obsPair->second;
106     }
107 
108     for (auto &obs : obsList) {
109         if (obs != nullptr) {
110             obs->OnChangePreferences(const_cast<Uri &>(uri).GetQuery());
111         }
112     }
113 
114     HILOG_DEBUG("called end on the uri : %{public}s,obs num: %{public}zu",
115         CommonUtils::Anonymous(uri.ToString()).c_str(), obsList.size());
116     return NO_ERROR;
117 }
118 
AddObsDeathRecipient(sptr<IDataAbilityObserver> dataObserver)119 void DataObsMgrInnerPref::AddObsDeathRecipient(sptr<IDataAbilityObserver> dataObserver)
120 {
121     if ((dataObserver == nullptr) || dataObserver->AsObject() == nullptr) {
122         return;
123     }
124 
125     auto it = obsRecipient_.find(dataObserver->AsObject());
126     if (it != obsRecipient_.end()) {
127         HILOG_WARN("this death recipient has been added.");
128         return;
129     } else {
130         std::weak_ptr<DataObsMgrInnerPref> thisWeakPtr(shared_from_this());
131         sptr<IRemoteObject::DeathRecipient> deathRecipient =
132             new DataObsCallbackRecipient([thisWeakPtr](const wptr<IRemoteObject> &remote) {
133                 auto dataObsMgrInner = thisWeakPtr.lock();
134                 if (dataObsMgrInner) {
135                     dataObsMgrInner->OnCallBackDied(remote);
136                 }
137             });
138         if (!dataObserver->AsObject()->AddDeathRecipient(deathRecipient)) {
139             HILOG_ERROR("AddDeathRecipient failed.");
140         }
141         obsRecipient_.emplace(dataObserver->AsObject(), deathRecipient);
142     }
143 }
144 
RemoveObsDeathRecipient(sptr<IRemoteObject> dataObserver)145 void DataObsMgrInnerPref::RemoveObsDeathRecipient(sptr<IRemoteObject> dataObserver)
146 {
147     if (dataObserver == nullptr) {
148         return;
149     }
150 
151     auto it = obsRecipient_.find(dataObserver);
152     if (it != obsRecipient_.end()) {
153         it->first->RemoveDeathRecipient(it->second);
154         obsRecipient_.erase(it);
155         return;
156     }
157 }
158 
OnCallBackDied(const wptr<IRemoteObject> & remote)159 void DataObsMgrInnerPref::OnCallBackDied(const wptr<IRemoteObject> &remote)
160 {
161     auto dataObserver = remote.promote();
162     if (dataObserver == nullptr) {
163         return;
164     }
165     std::lock_guard<std::mutex> lock(preferenceMutex_);
166 
167     if (dataObserver == nullptr) {
168         HILOG_ERROR("dataObserver is nullptr.");
169         return;
170     }
171 
172     RemoveObs(dataObserver);
173 }
174 
RemoveObs(sptr<IRemoteObject> dataObserver)175 void DataObsMgrInnerPref::RemoveObs(sptr<IRemoteObject> dataObserver)
176 {
177     for (auto iter = observers_.begin(); iter != observers_.end();) {
178         auto &obsList = iter->second;
179         for (auto it = obsList.begin(); it != obsList.end(); it++) {
180             if ((*it)->AsObject() == dataObserver) {
181                 HILOG_DEBUG("Erase an observer form list.");
182                 obsList.erase(it);
183                 break;
184             }
185         }
186         if (obsList.size() == 0) {
187             iter = observers_.erase(iter);
188         } else {
189             iter++;
190         }
191     }
192     RemoveObsDeathRecipient(dataObserver);
193 }
194 
HaveRegistered(sptr<IDataAbilityObserver> dataObserver)195 bool DataObsMgrInnerPref::HaveRegistered(sptr<IDataAbilityObserver> dataObserver)
196 {
197     for (auto &[key, value] : observers_) {
198         auto obs = std::find(value.begin(), value.end(), dataObserver);
199         if (obs != value.end()) {
200             return true;
201         }
202     }
203     return false;
204 }
205 }  // namespace AAFwk
206 }  // namespace OHOS
207