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 #ifndef OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_CLOUD_SERVICE_IMPL_H 17 #define OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_CLOUD_SERVICE_IMPL_H 18 19 #include <mutex> 20 #include <queue> 21 #include "cloud/cloud_event.h" 22 #include "cloud/cloud_info.h" 23 #include "cloud/schema_meta.h" 24 #include "cloud/subscription.h" 25 #include "cloud_service_stub.h" 26 #include "sync_manager.h" 27 namespace OHOS::CloudData { 28 class CloudServiceImpl : public CloudServiceStub { 29 public: 30 using StoreMetaData = DistributedData::StoreMetaData; 31 CloudServiceImpl(); 32 ~CloudServiceImpl() = default; 33 int32_t EnableCloud(const std::string &id, const std::map<std::string, int32_t> &switches) override; 34 int32_t DisableCloud(const std::string &id) override; 35 int32_t ChangeAppSwitch(const std::string &id, const std::string &bundleName, int32_t appSwitch) override; 36 int32_t Clean(const std::string &id, const std::map<std::string, int32_t> &actions) override; 37 int32_t NotifyDataChange(const std::string &id, const std::string &bundleName) override; 38 int32_t OnInitialize() override; 39 int32_t OnBind(const BindInfo &info) override; 40 int32_t OnUserChange(uint32_t code, const std::string &user, const std::string &account) override; 41 int32_t OnAppUninstall(const std::string &bundleName, int32_t user, int32_t index) override; 42 int32_t Online(const std::string &device) override; 43 int32_t Offline(const std::string &device) override; 44 45 private: 46 class Factory { 47 public: 48 Factory() noexcept; 49 ~Factory(); 50 private: 51 std::shared_ptr<CloudServiceImpl> product_; 52 }; 53 static Factory factory_; 54 55 using CloudInfo = DistributedData::CloudInfo; 56 using SchemaMeta = DistributedData::SchemaMeta; 57 using Event = DistributedData::Event; 58 using Subscription = DistributedData::Subscription; 59 using Handle = bool (CloudServiceImpl::*)(int32_t); 60 using Handles = std::deque<Handle>; 61 using Task = ExecutorPool::Task; 62 63 static constexpr int32_t RETRY_TIMES = 3; 64 static constexpr int32_t RETRY_INTERVAL = 60; 65 static constexpr int32_t EXPIRE_INTERVAL = 2 * 24; // 2 day 66 67 bool UpdateCloudInfo(int32_t user); 68 bool UpdateSchema(int32_t user); 69 SchemaMeta GetSchemaMeta(int32_t userId, const std::string &bundleName, int32_t instanceId); 70 CloudInfo GetCloudInfo(int32_t userId); 71 int32_t GetCloudInfo(uint32_t tokenId, const std::string &id, CloudInfo &cloudInfo); 72 int32_t GetCloudInfoFromMeta(CloudInfo &cloudInfo); 73 int32_t GetCloudInfoFromServer(CloudInfo &cloudInfo); 74 int32_t GetAppSchema(int32_t user, const std::string &bundleName, SchemaMeta &schemaMeta); 75 void GetSchema(const Event &event); 76 Task GenTask(int32_t retry, int32_t user, Handles handles = { WORK_SUB }); 77 void Execute(Task task); 78 bool DoSubscribe(int32_t user); 79 int32_t DoClean(CloudInfo &cloudInfo, const std::map<std::string, int32_t> &actions); 80 std::shared_ptr<ExecutorPool> executor_; 81 SyncManager syncManager_; 82 83 static constexpr Handle WORK_CLOUD_INFO_UPDATE = &CloudServiceImpl::UpdateCloudInfo; 84 static constexpr Handle WORK_SCHEMA_UPDATE = &CloudServiceImpl::UpdateSchema; 85 static constexpr Handle WORK_SUB = &CloudServiceImpl::DoSubscribe; 86 }; 87 } // namespace OHOS::DistributedData 88 89 #endif // OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_CLOUD_SERVICE_IMPL_H 90