1 /*
2 * Copyright (c) 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
16 #include "data_share_manager_impl.h"
17
18 #include <thread>
19
20 #include "datashare_errno.h"
21 #include "datashare_log.h"
22 #include "datashare_radar_reporter.h"
23 #include "ikvstore_data_service.h"
24 #include "ipc_skeleton.h"
25 #include "iservice_registry.h"
26 #include "system_ability_definition.h"
27 #include "rdb_subscriber_manager.h"
28 #include "proxy_data_subscriber_manager.h"
29 #include "published_data_subscriber_manager.h"
30
31 namespace OHOS {
32 namespace DataShare {
33
34 std::mutex DataShareManagerImpl::pmutex_;
35 DataShareManagerImpl* DataShareManagerImpl::manager_ = nullptr;
36
GetInstance()37 DataShareManagerImpl* DataShareManagerImpl::GetInstance()
38 {
39 std::lock_guard<std::mutex> lock(pmutex_);
40 if (manager_ != nullptr) {
41 return manager_;
42 }
43 manager_ = new (std::nothrow)DataShareManagerImpl();
44 if (manager_ == nullptr) {
45 LOG_ERROR("DataShareManagerImpl: GetInstance failed");
46 }
47 auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
48 if (saManager == nullptr) {
49 LOG_ERROR("Failed to get saMgrProxy.");
50 return manager_;
51 }
52 sptr<DataShareClientStatusChangeStub> callback(new (std::nothrow)DataShareClientStatusChangeStub(manager_));
53 saManager->SubscribeSystemAbility(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID, callback);
54 return manager_;
55 }
56
GetDistributedDataManager()57 sptr<DataShareKvServiceProxy> DataShareManagerImpl::GetDistributedDataManager()
58 {
59 auto manager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
60 if (manager == nullptr) {
61 LOG_ERROR("get system ability manager failed");
62 return nullptr;
63 }
64 auto remoteObject = manager->CheckSystemAbility(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID);
65 if (remoteObject == nullptr) {
66 LOG_ERROR("get distributed data manager failed");
67 return nullptr;
68 }
69 sptr<DataShareKvServiceProxy> proxy = new (std::nothrow)DataShareKvServiceProxy(remoteObject);
70 if (proxy == nullptr) {
71 LOG_ERROR("new DataShareKvServiceProxy fail.");
72 return nullptr;
73 }
74 return proxy;
75 }
76
LinkToDeath(const sptr<IRemoteObject> remote)77 void DataShareManagerImpl::LinkToDeath(const sptr<IRemoteObject> remote)
78 {
79 sptr<DataShareManagerImpl::ServiceDeathRecipient> deathRecipient = new (std::nothrow)
80 DataShareManagerImpl::ServiceDeathRecipient(this);
81 if (deathRecipient == nullptr) {
82 LOG_ERROR("DataShareManagerImpl::LinkToDeath new ServiceDeathRecipient error.");
83 return;
84 }
85 if (!remote->AddDeathRecipient(deathRecipient)) {
86 LOG_ERROR("add death recipient failed");
87 }
88 LOG_DEBUG("link to death success");
89 }
90
GetDataShareServiceProxy()91 sptr<DataShareServiceProxy> DataShareManagerImpl::GetDataShareServiceProxy()
92 {
93 if (dataMgrService_ == nullptr) {
94 dataMgrService_ = GetDistributedDataManager();
95 }
96 if (dataMgrService_ == nullptr) {
97 LOG_ERROR("Get distributed data manager failed!");
98 return nullptr;
99 }
100 auto remote = dataMgrService_->GetFeatureInterface("data_share");
101 if (remote == nullptr) {
102 LOG_ERROR("Get DataShare service failed!");
103 return nullptr;
104 }
105 RegisterClientDeathObserver();
106 return iface_cast<DataShareServiceProxy>(remote);
107 }
108
RegisterClientDeathObserver()109 void DataShareManagerImpl::RegisterClientDeathObserver()
110 {
111 if (dataMgrService_ == nullptr || bundleName_.empty()) {
112 return;
113 }
114 LOG_INFO("RegisterClientDeathObserver bundleName is %{public}s", bundleName_.c_str());
115 if (clientDeathObserverPtr_ == nullptr) {
116 clientDeathObserverPtr_ = new (std::nothrow) DataShareClientDeathObserverStub();
117 }
118 if (clientDeathObserverPtr_ == nullptr) {
119 LOG_WARN("new KvStoreClientDeathObserver failed");
120 return;
121 }
122 auto status = dataMgrService_->RegisterClientDeathObserver(bundleName_, clientDeathObserverPtr_);
123 if (!status) {
124 LOG_ERROR("RegisterClientDeathObserver failed, bundleName is %{public}s", bundleName_.c_str());
125 return;
126 }
127 }
128
DataShareManagerImpl()129 DataShareManagerImpl::DataShareManagerImpl()
130 {
131 SetDeathCallback([](std::shared_ptr<DataShareServiceProxy> proxy) {
132 LOG_INFO("RecoverObs start");
133 RdbSubscriberManager::GetInstance().RecoverObservers(proxy);
134 PublishedDataSubscriberManager::GetInstance().RecoverObservers(proxy);
135 ProxyDataSubscriberManager::GetInstance().RecoverObservers(proxy);
136 });
137 }
138
~DataShareManagerImpl()139 DataShareManagerImpl::~DataShareManagerImpl()
140 {
141 }
142
GetProxy()143 std::shared_ptr<DataShareServiceProxy> DataShareManagerImpl::GetProxy()
144 {
145 std::lock_guard<std::mutex> lock(mutex_);
146 if (dataShareService_ != nullptr) {
147 return dataShareService_;
148 }
149
150 auto service = GetDataShareServiceProxy();
151 if (service == nullptr) {
152 return nullptr;
153 }
154 sptr<IDataShareService> serviceBase = service;
155 LinkToDeath(serviceBase->AsObject().GetRefPtr());
156 dataShareService_ = std::shared_ptr<DataShareServiceProxy>(
157 service.GetRefPtr(), [holder = service](const auto *) {});
158 return dataShareService_;
159 }
160
GetServiceProxy()161 std::shared_ptr<DataShareServiceProxy> DataShareManagerImpl::GetServiceProxy()
162 {
163 auto manager = DataShareManagerImpl::GetInstance();
164 if (manager == nullptr) {
165 LOG_ERROR("manager_ is nullptr");
166 return nullptr;
167 }
168 return manager->GetProxy();
169 }
170
ResetServiceHandle()171 void DataShareManagerImpl::ResetServiceHandle()
172 {
173 LOG_DEBUG("enter");
174 std::lock_guard<std::mutex> lock(mutex_);
175 dataMgrService_ = nullptr;
176 dataShareService_ = nullptr;
177 }
178
SetDeathCallback(std::function<void (std::shared_ptr<DataShareServiceProxy>)> deathCallback)179 void DataShareManagerImpl::SetDeathCallback(std::function<void(std::shared_ptr<DataShareServiceProxy>)> deathCallback)
180 {
181 deathCallback_ = deathCallback;
182 }
183
SetBundleName(const std::string & bundleName)184 void DataShareManagerImpl::SetBundleName(const std::string &bundleName)
185 {
186 bundleName_ = bundleName;
187 }
188
OnRemoteDied()189 void DataShareManagerImpl::OnRemoteDied()
190 {
191 LOG_INFO("#######datashare service has dead");
192 ResetServiceHandle();
193 }
194
SetRegisterCallback(GeneralControllerServiceImpl * ptr,std::function<void ()> registerCallback)195 void DataShareManagerImpl::SetRegisterCallback(GeneralControllerServiceImpl* ptr,
196 std::function<void()> registerCallback)
197 {
198 observers_.ComputeIfAbsent(ptr, [®isterCallback](const GeneralControllerServiceImpl*) {
199 return std::move(registerCallback);
200 });
201 }
202
RemoveRegisterCallback(GeneralControllerServiceImpl * ptr)203 void DataShareManagerImpl::RemoveRegisterCallback(GeneralControllerServiceImpl* ptr)
204 {
205 observers_.Erase(ptr);
206 }
207
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)208 void DataShareManagerImpl::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
209 {
210 if (systemAbilityId != DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID) {
211 LOG_ERROR("SystemAbilityId must be DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID, but it is %{public}d",
212 systemAbilityId);
213 return;
214 }
215 observers_.ForEach([](const auto &, auto &callback) {
216 callback();
217 return false;
218 });
219 auto serviceProxy = GetServiceProxy();
220 if (serviceProxy != nullptr) {
221 deathCallback_(serviceProxy);
222 }
223 }
224
SetCallCount(const std::string & funcName,const std::string & uri)225 bool DataShareManagerImpl::SetCallCount(const std::string &funcName, const std::string &uri)
226 {
227 return dataShareCallReporter_.Count(funcName, uri);
228 }
229 }
230 }