• 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 "udmf_service_client.h"
17 
18 #include "iservice_registry.h"
19 #include "system_ability_definition.h"
20 
21 #include "logger.h"
22 
23 namespace OHOS {
24 namespace UDMF {
25 std::shared_ptr<UdmfServiceClient> UdmfServiceClient::instance_;
26 std::mutex UdmfServiceClient::mutex_;
27 sptr<DistributedKv::IKvStoreDataService> UdmfServiceClient::kvDataServiceProxy_;
28 
UdmfServiceClient(const sptr<UdmfServiceProxy> & proxy)29 UdmfServiceClient::UdmfServiceClient(const sptr<UdmfServiceProxy> &proxy) : udmfProxy_(proxy)
30 {
31     LOG_INFO(UDMF_SERVICE, "construct");
32 }
33 
GetInstance()34 std::shared_ptr<UdmfServiceClient> UdmfServiceClient::GetInstance()
35 {
36     std::lock_guard<decltype(mutex_)> lockGuard(mutex_);
37     if (instance_ != nullptr) {
38         return instance_;
39     }
40     sptr<DistributedKv::IKvStoreDataService> ability = GetDistributedKvDataService();
41     if (ability == nullptr) {
42         return nullptr;
43     }
44     sptr<IRemoteObject> service = ability->GetFeatureInterface("udmf");
45     if (service == nullptr) {
46         return nullptr;
47     }
48     sptr<UdmfServiceProxy> proxy = iface_cast<UdmfServiceProxy>(service);
49     if (proxy == nullptr) {
50         return nullptr;
51     }
52     instance_ = std::make_shared<UdmfServiceClient>(proxy);
53     return instance_;
54 }
55 
GetDistributedKvDataService()56 sptr<DistributedKv::IKvStoreDataService> UdmfServiceClient::GetDistributedKvDataService()
57 {
58     if (kvDataServiceProxy_ != nullptr) {
59         return kvDataServiceProxy_;
60     }
61     LOG_INFO(UDMF_SERVICE, "create remote proxy.");
62     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
63     if (samgr == nullptr) {
64         LOG_ERROR(UDMF_SERVICE, "get samgr fail.");
65         return nullptr;
66     }
67 
68     auto remote = samgr->CheckSystemAbility(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID);
69     kvDataServiceProxy_ = iface_cast<DistributedKv::IKvStoreDataService>(remote);
70     if (kvDataServiceProxy_ == nullptr) {
71         LOG_ERROR(UDMF_SERVICE, "initialize proxy failed.");
72         return nullptr;
73     }
74     auto deathRecipientPtr = new (std::nothrow)ServiceDeathRecipient();
75     if (deathRecipientPtr == nullptr) {
76         return nullptr;
77     }
78     if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(deathRecipientPtr))) {
79         LOG_ERROR(UDMF_SERVICE, "Add death recipient fail!");
80     }
81     return kvDataServiceProxy_;
82 }
83 
ServiceDeathRecipient()84 UdmfServiceClient::ServiceDeathRecipient::ServiceDeathRecipient()
85 {
86     LOG_INFO(UDMF_SERVICE, "Construct!");
87 }
88 
~ServiceDeathRecipient()89 UdmfServiceClient::ServiceDeathRecipient::~ServiceDeathRecipient()
90 {
91     LOG_INFO(UDMF_SERVICE, "Destruct!");
92 }
93 
OnRemoteDied(const wptr<IRemoteObject> & remote)94 void UdmfServiceClient::ServiceDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
95 {
96     LOG_WARN(UDMF_SERVICE, "DistributedDataService die!");
97     std::lock_guard<decltype(mutex_)> lockGuard(mutex_);
98     kvDataServiceProxy_ = nullptr;
99     instance_ = nullptr;
100 }
101 
SetData(CustomOption & option,UnifiedData & unifiedData,std::string & key)102 int32_t UdmfServiceClient::SetData(CustomOption &option, UnifiedData &unifiedData, std::string &key)
103 {
104     LOG_INFO(UDMF_SERVICE, "start");
105     return udmfProxy_->SetData(option, unifiedData, key);
106 }
107 
GetData(const QueryOption & query,UnifiedData & unifiedData)108 int32_t UdmfServiceClient::GetData(const QueryOption &query, UnifiedData &unifiedData)
109 {
110     LOG_INFO(UDMF_SERVICE, "start");
111     return udmfProxy_->GetData(query, unifiedData);
112 }
113 
GetBatchData(const QueryOption & query,std::vector<UnifiedData> & unifiedDataSet)114 int32_t UdmfServiceClient::GetBatchData(const QueryOption &query, std::vector<UnifiedData> &unifiedDataSet)
115 {
116     LOG_INFO(UDMF_SERVICE, "start");
117     return udmfProxy_->GetBatchData(query, unifiedDataSet);
118 }
119 
UpdateData(const QueryOption & query,UnifiedData & unifiedData)120 int32_t UdmfServiceClient::UpdateData(const QueryOption &query, UnifiedData &unifiedData)
121 {
122     LOG_INFO(UDMF_SERVICE, "start");
123     return udmfProxy_->UpdateData(query, unifiedData);
124 }
125 
DeleteData(const QueryOption & query,std::vector<UnifiedData> & unifiedDataSet)126 int32_t UdmfServiceClient::DeleteData(const QueryOption &query, std::vector<UnifiedData> &unifiedDataSet)
127 {
128     LOG_INFO(UDMF_SERVICE, "start");
129     return udmfProxy_->DeleteData(query, unifiedDataSet);
130 }
131 
GetSummary(const QueryOption & query,Summary & summary)132 int32_t UdmfServiceClient::GetSummary(const QueryOption &query, Summary &summary)
133 {
134     LOG_INFO(UDMF_SERVICE, "start");
135     return udmfProxy_->GetSummary(query, summary);
136 }
137 
AddPrivilege(const QueryOption & query,Privilege & privilege)138 int32_t UdmfServiceClient::AddPrivilege(const QueryOption &query, Privilege &privilege)
139 {
140     LOG_INFO(UDMF_SERVICE, "start");
141     return udmfProxy_->AddPrivilege(query, privilege);
142 }
143 
Sync(const QueryOption & query,const std::vector<std::string> & devices)144 int32_t UdmfServiceClient::Sync(const QueryOption &query, const std::vector<std::string> &devices)
145 {
146     LOG_INFO(UDMF_SERVICE, "start");
147     return udmfProxy_->Sync(query, devices);
148 }
149 } // namespace UDMF
150 } // namespace OHOS