1 /*
2  * Copyright (c) 2023 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 #define LOG_TAG "CloudManager"
16 #include "cloud_manager.h"
17 
18 #include "cloud_service_proxy.h"
19 #include "icloud_service.h"
20 #include "iservice_registry.h"
21 #include "itypes_util.h"
22 #include "logger.h"
23 #include "system_ability_definition.h"
24 
25 namespace OHOS::CloudData {
26 using namespace OHOS::Rdb;
27 using namespace OHOS::DistributedRdb::RelationalStore;
28 
29 class DataMgrService : public IRemoteProxy<CloudData::IKvStoreDataService> {
30 public:
31     explicit DataMgrService(const sptr<IRemoteObject> &impl);
32     ~DataMgrService() = default;
33     sptr<IRemoteObject> GetFeatureInterface(const std::string &name) override;
34 };
35 
36 class CloudDeath : public IRemoteObject::DeathRecipient {
37 public:
CloudDeath(std::function<void ()> action)38     explicit CloudDeath(std::function<void()> action) : action_(std::move(action)){};
OnRemoteDied(const wptr<IRemoteObject> & object)39     void OnRemoteDied(const wptr<IRemoteObject> &object) override
40     {
41         if (action_) {
42             action_();
43         }
44     }
45 
46 private:
47     std::function<void()> action_;
48 };
49 
GetInstance()50 CloudManager &CloudManager::GetInstance()
51 {
52     static CloudManager instance;
53     return instance;
54 }
55 
GetCloudService()56 std::pair<int32_t, std::shared_ptr<CloudService>> CloudManager::GetCloudService()
57 {
58     std::lock_guard<decltype(mutex_)> lg(mutex_);
59     if (cloudService_ != nullptr) {
60         return std::make_pair(CloudService::Status::SUCCESS, cloudService_);
61     }
62 
63     auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
64     if (saMgr == nullptr) {
65         LOG_ERROR("get system ability manager failed.");
66         return std::make_pair(CloudService::Status::SERVER_UNAVAILABLE, nullptr);
67     }
68     auto dataMgrObject = saMgr->CheckSystemAbility(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID);
69     if (dataMgrObject == nullptr) {
70         LOG_ERROR("get distributed data manager failed.");
71         return std::make_pair(CloudService::Status::SERVER_UNAVAILABLE, nullptr);
72     }
73 
74     sptr<DataMgrService> dataMgr = new (std::nothrow) DataMgrService(dataMgrObject);
75     if (dataMgr == nullptr) {
76         LOG_ERROR("new CloudDataServiceProxy failed.");
77         return std::make_pair(CloudService::Status::SERVER_UNAVAILABLE, nullptr);
78     }
79 
80     auto cloudObject = dataMgr->GetFeatureInterface(CloudService::SERVICE_NAME);
81     if (cloudObject == nullptr) {
82         LOG_ERROR("get cloud service failed.");
83         return std::make_pair(CloudService::Status::FEATURE_UNAVAILABLE, nullptr);
84     }
85 
86     cloudObject->AddDeathRecipient(new CloudDeath([this]() {
87         std::lock_guard<decltype(mutex_)> lg(mutex_);
88         cloudService_ = nullptr;
89     }));
90 
91     sptr<CloudServiceProxy> proxy = new (std::nothrow) CloudServiceProxy(cloudObject);
92     if (proxy == nullptr) {
93         return std::make_pair(CloudService::Status::FEATURE_UNAVAILABLE, nullptr);
94     }
95 
96     cloudService_ = std::shared_ptr<CloudService>(proxy.GetRefPtr(), [holder = proxy](const auto *) {});
97     if (cloudService_ == nullptr) {
98         return std::make_pair(CloudService::Status::FEATURE_UNAVAILABLE, nullptr);
99     }
100     return std::make_pair(CloudService::Status::SUCCESS, cloudService_);
101 }
102 
DataMgrService(const sptr<IRemoteObject> & impl)103 DataMgrService::DataMgrService(const sptr<IRemoteObject> &impl) : IRemoteProxy<CloudData::IKvStoreDataService>(impl)
104 {
105 }
106 
GetFeatureInterface(const std::string & name)107 sptr<IRemoteObject> DataMgrService::GetFeatureInterface(const std::string &name)
108 {
109     LOG_INFO("%s", name.c_str());
110     MessageParcel data;
111     if (!data.WriteInterfaceToken(DataMgrService::GetDescriptor())) {
112         LOG_ERROR("write descriptor failed.");
113         return nullptr;
114     }
115 
116     if (!ITypesUtil::Marshal(data, name)) {
117         LOG_ERROR("write descriptor failed.");
118         return nullptr;
119     }
120 
121     MessageParcel reply;
122     MessageOption mo{ MessageOption::TF_SYNC };
123     int32_t error = Remote()->SendRequest(
124         static_cast<uint32_t>(CloudKvStoreInterfaceCode::GET_FEATURE_INTERFACE), data, reply, mo);
125     if (error != 0) {
126         LOG_ERROR("SendRequest returned %{public}d", error);
127         return nullptr;
128     }
129 
130     sptr<IRemoteObject> remoteObject;
131     if (!ITypesUtil::Unmarshal(reply, remoteObject)) {
132         LOG_ERROR("remote object is nullptr.");
133         return nullptr;
134     }
135     return remoteObject;
136 }
137 } // namespace OHOS::CloudData