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