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
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 LOG_INFO("init proxy");
106 }
107
GetFeatureInterface(const std::string & name)108 sptr<IRemoteObject> DataMgrService::GetFeatureInterface(const std::string &name)
109 {
110 LOG_INFO("%s", name.c_str());
111 MessageParcel data;
112 if (!data.WriteInterfaceToken(DataMgrService::GetDescriptor())) {
113 LOG_ERROR("write descriptor failed");
114 return nullptr;
115 }
116
117 if (!ITypesUtil::Marshal(data, name)) {
118 LOG_ERROR("write descriptor failed");
119 return nullptr;
120 }
121
122 MessageParcel reply;
123 MessageOption mo{ MessageOption::TF_SYNC };
124 int32_t error = Remote()->SendRequest(
125 static_cast<uint32_t>(CloudKvStoreInterfaceCode::GET_FEATURE_INTERFACE), data, reply, mo);
126 if (error != 0) {
127 LOG_ERROR("SendRequest returned %{public}d", error);
128 return nullptr;
129 }
130
131 sptr<IRemoteObject> remoteObject;
132 if (!ITypesUtil::Unmarshal(reply, remoteObject)) {
133 LOG_ERROR("remote object is nullptr");
134 return nullptr;
135 }
136 return remoteObject;
137 }
138 } // namespace OHOS::CloudData