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 "iservice_registry.h"
21 #include "itypes_util.h"
22 #include "logger.h"
23 #include "objectstore_errors.h"
24 
25 namespace OHOS::ObjectStore {
26 std::shared_ptr<ObjectStoreDataServiceProxy> ClientAdaptor::distributedDataMgr_ = nullptr;
27 std::mutex ClientAdaptor::mutex_;
28 
29 using KvStoreCode = OHOS::DistributedObject::ObjectStoreService::KvStoreServiceInterfaceCode;
30 
GetObjectService()31 sptr<OHOS::DistributedObject::IObjectService> ClientAdaptor::GetObjectService()
32 {
33     std::lock_guard<decltype(mutex_)> lockGuard(mutex_);
34     if (distributedDataMgr_ == nullptr) {
35         distributedDataMgr_ = GetDistributedDataManager();
36     }
37     if (distributedDataMgr_ == nullptr) {
38         LOG_ERROR("get distributed data manager failed");
39         return nullptr;
40     }
41 
42     auto remote = distributedDataMgr_->GetFeatureInterface("data_object");
43     if (remote == nullptr) {
44         LOG_ERROR("get object service failed");
45         return nullptr;
46     }
47     return iface_cast<DistributedObject::IObjectService>(remote);
48 }
49 
GetDistributedDataManager()50 std::shared_ptr<ObjectStoreDataServiceProxy> ClientAdaptor::GetDistributedDataManager()
51 {
52     int retry = 0;
53     while (++retry <= GET_SA_RETRY_TIMES) {
54         auto manager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
55         if (manager == nullptr) {
56             LOG_ERROR("get system ability manager failed");
57             return nullptr;
58         }
59         LOG_INFO("get distributed data manager %{public}d", retry);
60         auto remoteObject = manager->CheckSystemAbility(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID);
61         if (remoteObject == nullptr) {
62             std::this_thread::sleep_for(std::chrono::seconds(RETRY_INTERVAL));
63             continue;
64         }
65         LOG_INFO("get distributed data manager success");
66         sptr<ObjectStoreDataServiceProxy> proxy = new (std::nothrow)ObjectStoreDataServiceProxy(remoteObject);
67         if (proxy == nullptr) {
68             LOG_ERROR("new ObjectStoreDataServiceProxy fail.");
69             return nullptr;
70         }
71         sptr<ClientAdaptor::ServiceDeathRecipient> deathRecipientPtr = new (std::nothrow)ServiceDeathRecipient();
72         if (deathRecipientPtr == nullptr) {
73             LOG_ERROR("new deathRecipientPtr fail!");
74             return nullptr;
75         }
76         if (!remoteObject->AddDeathRecipient(deathRecipientPtr)) {
77             LOG_ERROR("Add death recipient fail!");
78         }
79         return std::shared_ptr<ObjectStoreDataServiceProxy>(proxy.GetRefPtr(), [holder = proxy](const auto *) {});
80     }
81 
82     LOG_ERROR("get distributed data manager failed");
83     return nullptr;
84 }
85 
ServiceDeathRecipient()86 ClientAdaptor::ServiceDeathRecipient::ServiceDeathRecipient()
87 {
88 }
89 
~ServiceDeathRecipient()90 ClientAdaptor::ServiceDeathRecipient::~ServiceDeathRecipient()
91 {
92 }
93 
OnRemoteDied(const wptr<IRemoteObject> & remote)94 void ClientAdaptor::ServiceDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
95 {
96     LOG_WARN("DistributedDataService die!");
97     std::lock_guard<decltype(mutex_)> lockGuard(mutex_);
98     distributedDataMgr_ = nullptr;
99 }
100 
RegisterClientDeathListener(const std::string & appId,sptr<IRemoteObject> remoteObject)101 uint32_t ClientAdaptor::RegisterClientDeathListener(const std::string &appId, sptr<IRemoteObject> remoteObject)
102 {
103     std::lock_guard<decltype(mutex_)> lockGuard(mutex_);
104     if (distributedDataMgr_ == nullptr) {
105         distributedDataMgr_ = GetDistributedDataManager();
106     }
107     if (distributedDataMgr_ == nullptr) {
108         LOG_ERROR("get distributed data manager failed");
109         return ERR_EXIST;
110     }
111 
112     auto status = distributedDataMgr_->RegisterClientDeathObserver(appId, remoteObject);
113     if (status != SUCCESS) {
114         LOG_ERROR("RegisterClientDeathObserver failed");
115         return ERR_EXIST;
116     }
117     return SUCCESS;
118 }
119 
ObjectStoreDataServiceProxy(const sptr<IRemoteObject> & impl)120 ObjectStoreDataServiceProxy::ObjectStoreDataServiceProxy(const sptr<IRemoteObject> &impl)
121     : IRemoteProxy<DistributedObject::IKvStoreDataService>(impl)
122 {
123     LOG_INFO("init data service proxy.");
124 }
125 
GetFeatureInterface(const std::string & name)126 sptr<IRemoteObject> ObjectStoreDataServiceProxy::GetFeatureInterface(const std::string &name)
127 {
128     MessageParcel data;
129     if (!data.WriteInterfaceToken(ObjectStoreDataServiceProxy::GetDescriptor())) {
130         LOG_ERROR("write descriptor failed");
131         return nullptr;
132     }
133 
134     if (!ITypesUtil::Marshal(data, name)) {
135         LOG_ERROR("write name failed");
136         return nullptr;
137     }
138 
139     MessageParcel reply;
140     MessageOption mo { MessageOption::TF_SYNC };
141     sptr<IRemoteObject> remote = Remote();
142     if (remote == nullptr) {
143         LOG_ERROR("SendRequest remote is nullptr.");
144         return nullptr;
145     }
146     int32_t error =
147         remote->SendRequest(static_cast<uint32_t>(KvStoreCode::GET_FEATURE_INTERFACE), data, reply, mo);
148     if (error != 0) {
149         LOG_ERROR("SendRequest returned %{public}d", error);
150         return nullptr;
151     }
152 
153     sptr<IRemoteObject> remoteObject;
154     if (!ITypesUtil::Unmarshal(reply, remoteObject)) {
155         LOG_ERROR("remote object is nullptr");
156         return nullptr;
157     }
158     return remoteObject;
159 }
160 
RegisterClientDeathObserver(const std::string & appId,sptr<IRemoteObject> observer)161 uint32_t ObjectStoreDataServiceProxy::RegisterClientDeathObserver(
162     const std::string &appId, sptr<IRemoteObject> observer)
163 {
164     MessageParcel data;
165     MessageParcel reply;
166     if (!data.WriteInterfaceToken(ObjectStoreDataServiceProxy::GetDescriptor())) {
167         LOG_ERROR("write descriptor failed");
168         return ERR_IPC;
169     }
170     if (observer == nullptr) {
171         return ERR_INVALID_ARGS;
172     }
173     if (!ITypesUtil::Marshal(data, appId, observer)) {
174         LOG_ERROR("remote observer fail");
175         return ERR_IPC;
176     }
177 
178     MessageOption mo { MessageOption::TF_SYNC };
179     sptr<IRemoteObject> remoteObject = Remote();
180     if (remoteObject == nullptr) {
181         LOG_ERROR("SendRequest remoteObject is nullptr.");
182         return ERR_IPC;
183     }
184     int32_t error =
185         remoteObject->SendRequest(static_cast<uint32_t>(KvStoreCode::REGISTERCLIENTDEATHOBSERVER), data, reply, mo);
186     if (error != 0) {
187         LOG_WARN("failed during IPC. errCode %d", error);
188         return ERR_IPC;
189     }
190     return static_cast<uint32_t>(reply.ReadInt32());
191 }
192 }