• 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 "client_adaptor.h"
17 
18 #include <thread>
19 
20 #include "logger.h"
21 #include "iservice_registry.h"
22 #include "objectstore_errors.h"
23 
24 namespace OHOS::ObjectStore {
25 sptr<OHOS::DistributedKv::IKvStoreDataService> ClientAdaptor::distributedDataMgr_ = nullptr;
26 
GetObjectService()27 sptr<OHOS::DistributedObject::IObjectService> ClientAdaptor::GetObjectService()
28 {
29     if (distributedDataMgr_ == nullptr) {
30         distributedDataMgr_ = GetDistributedDataManager();
31     }
32     if (distributedDataMgr_ == nullptr) {
33         LOG_ERROR("get distributed data manager failed");
34         return nullptr;
35     }
36 
37     auto remote = distributedDataMgr_->GetFeatureInterface("data_object");
38     if (remote == nullptr) {
39         LOG_ERROR("get object service failed");
40         return nullptr;
41     }
42     return iface_cast<DistributedObject::IObjectService>(remote);
43 }
44 
GetDistributedDataManager()45 sptr<DistributedKv::IKvStoreDataService> ClientAdaptor::GetDistributedDataManager()
46 {
47     int retry = 0;
48     while (++retry <= GET_SA_RETRY_TIMES) {
49         auto manager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
50         if (manager == nullptr) {
51             LOG_ERROR("get system ability manager failed");
52             return nullptr;
53         }
54         LOG_INFO("get distributed data manager %{public}d", retry);
55         auto remoteObject = manager->CheckSystemAbility(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID);
56         if (remoteObject == nullptr) {
57             std::this_thread::sleep_for(std::chrono::seconds(RETRY_INTERVAL));
58             continue;
59         }
60         LOG_INFO("get distributed data manager success");
61         return iface_cast<DistributedKv::IKvStoreDataService>(remoteObject);
62     }
63 
64     LOG_ERROR("get distributed data manager failed");
65     return nullptr;
66 }
67 
RegisterClientDeathListener(DistributedKv::AppId & appId,sptr<IRemoteObject> remoteObject)68 uint32_t ClientAdaptor::RegisterClientDeathListener(DistributedKv::AppId &appId, sptr<IRemoteObject> remoteObject)
69 {
70     if (distributedDataMgr_ == nullptr) {
71         distributedDataMgr_ = GetDistributedDataManager();
72     }
73     if (distributedDataMgr_ == nullptr) {
74         LOG_ERROR("get distributed data manager failed");
75         return ERR_EXIST;
76     }
77 
78     auto status = distributedDataMgr_->RegisterClientDeathObserver(appId, remoteObject);
79     if (status != SUCCESS) {
80         LOG_ERROR("RegisterClientDeathObserver failed");
81         return ERR_EXIST;
82     }
83     return SUCCESS;
84 }
85 }