• 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 "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     auto status = distributedDataMgr_->RegisterClientDeathObserver(appId, remoteObject);
112     if (status != SUCCESS) {
113         LOG_ERROR("RegisterClientDeathObserver failed");
114         return ERR_EXIST;
115     }
116     return SUCCESS;
117 }
118 
ObjectStoreDataServiceProxy(const sptr<IRemoteObject> & impl)119 ObjectStoreDataServiceProxy::ObjectStoreDataServiceProxy(const sptr<IRemoteObject> &impl)
120     : IRemoteProxy<DistributedObject::IKvStoreDataService>(impl)
121 {
122     LOG_INFO("init data service proxy.");
123 }
124 
GetFeatureInterface(const std::string & name)125 sptr<IRemoteObject> ObjectStoreDataServiceProxy::GetFeatureInterface(const std::string &name)
126 {
127     MessageParcel data;
128     if (!data.WriteInterfaceToken(ObjectStoreDataServiceProxy::GetDescriptor())) {
129         LOG_ERROR("write descriptor failed");
130         return nullptr;
131     }
132 
133     if (!ITypesUtil::Marshal(data, name)) {
134         LOG_ERROR("write name failed");
135         return nullptr;
136     }
137 
138     MessageParcel reply;
139     MessageOption mo { MessageOption::TF_SYNC };
140     sptr<IRemoteObject> remote = Remote();
141     if (remote == nullptr) {
142         LOG_ERROR("SendRequest remote is nullptr.");
143         return nullptr;
144     }
145     int32_t error =
146         remote->SendRequest(static_cast<uint32_t>(KvStoreCode::GET_FEATURE_INTERFACE), data, reply, mo);
147     if (error != 0) {
148         LOG_ERROR("SendRequest returned %{public}d", error);
149         return nullptr;
150     }
151 
152     sptr<IRemoteObject> remoteObject;
153     if (!ITypesUtil::Unmarshal(reply, remoteObject)) {
154         LOG_ERROR("remote object is nullptr");
155         return nullptr;
156     }
157     return remoteObject;
158 }
159 
RegisterClientDeathObserver(const std::string & appId,sptr<IRemoteObject> observer)160 uint32_t ObjectStoreDataServiceProxy::RegisterClientDeathObserver(
161     const std::string &appId, sptr<IRemoteObject> observer)
162 {
163     MessageParcel data;
164     MessageParcel reply;
165     if (!data.WriteInterfaceToken(ObjectStoreDataServiceProxy::GetDescriptor())) {
166         LOG_ERROR("write descriptor failed");
167         return ERR_IPC;
168     }
169     if (observer == nullptr) {
170         return ERR_INVALID_ARGS;
171     }
172     if (!ITypesUtil::Marshal(data, appId, observer)) {
173         LOG_ERROR("remote observer fail");
174         return ERR_IPC;
175     }
176 
177     MessageOption mo { MessageOption::TF_SYNC };
178     sptr<IRemoteObject> remoteObject = Remote();
179     if (remoteObject == nullptr) {
180         LOG_ERROR("SendRequest remoteObject is nullptr.");
181         return ERR_IPC;
182     }
183     int32_t error =
184         remoteObject->SendRequest(static_cast<uint32_t>(KvStoreCode::REGISTERCLIENTDEATHOBSERVER), data, reply, mo);
185     if (error != 0) {
186         LOG_WARN("failed during IPC. errCode %d", error);
187         return ERR_IPC;
188     }
189     return static_cast<uint32_t>(reply.ReadInt32());
190 }
191 }