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