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_extra_data.h" 23 #include "cloud/cloud_info.h" 24 #include "cloud/schema_meta.h" 25 #include "cloud/subscription.h" 26 #include "cloud/sharing_center.h" 27 #include "cloud_service_stub.h" 28 #include "feature/static_acts.h" 29 #include "sync_manager.h" 30 #include "values_bucket.h" 31 namespace OHOS::CloudData { 32 class CloudServiceImpl : public CloudServiceStub { 33 public: 34 using StoreMetaData = DistributedData::StoreMetaData; 35 CloudServiceImpl(); 36 ~CloudServiceImpl() = default; 37 int32_t EnableCloud(const std::string &id, const std::map<std::string, int32_t> &switches) override; 38 int32_t DisableCloud(const std::string &id) override; 39 int32_t ChangeAppSwitch(const std::string &id, const std::string &bundleName, int32_t appSwitch) override; 40 int32_t Clean(const std::string &id, const std::map<std::string, int32_t> &actions) override; 41 int32_t NotifyDataChange(const std::string &id, const std::string &bundleName) override; 42 int32_t NotifyDataChange(const std::string& eventId, const std::string& extraData, int32_t userId) override; 43 int32_t OnInitialize() override; 44 int32_t OnBind(const BindInfo &info) override; 45 int32_t OnUserChange(uint32_t code, const std::string &user, const std::string &account) override; 46 int32_t Online(const std::string &device) override; 47 int32_t Offline(const std::string &device) override; 48 49 std::pair<int32_t, std::vector<NativeRdb::ValuesBucket>> AllocResourceAndShare(const std::string& storeId, 50 const DistributedRdb::PredicatesMemo& predicates, const std::vector<std::string>& columns, 51 const Participants& participants) override; 52 int32_t Share(const std::string &sharingRes, const Participants &participants, Results &results) override; 53 int32_t Unshare(const std::string &sharingRes, const Participants &participants, Results &results) override; 54 int32_t Exit(const std::string &sharingRes, std::pair<int32_t, std::string> &result) override; 55 int32_t ChangePrivilege( 56 const std::string &sharingRes, const Participants &participants, Results &results) override; 57 int32_t Query(const std::string &sharingRes, QueryResults &results) override; 58 int32_t QueryByInvitation(const std::string &invitation, QueryResults &results) override; 59 int32_t ConfirmInvitation(const std::string &invitation, int32_t confirmation, 60 std::tuple<int32_t, std::string, std::string> &result) override; 61 int32_t ChangeConfirmation( 62 const std::string &sharingRes, int32_t confirmation, std::pair<int32_t, std::string> &result) override; 63 64 private: 65 using StaticActs = DistributedData::StaticActs; 66 class CloudStatic : public StaticActs { 67 public: ~CloudStatic()68 ~CloudStatic() override {}; 69 int32_t OnAppUninstall(const std::string &bundleName, int32_t user, int32_t index) override; 70 }; 71 class Factory { 72 public: 73 Factory() noexcept; 74 ~Factory(); 75 private: 76 std::shared_ptr<CloudServiceImpl> product_; 77 std::shared_ptr<CloudStatic> staticActs_; 78 }; 79 static Factory factory_; 80 81 using CloudInfo = DistributedData::CloudInfo; 82 using SchemaMeta = DistributedData::SchemaMeta; 83 using Event = DistributedData::Event; 84 using CloudEvent = DistributedData::CloudEvent; 85 using Subscription = DistributedData::Subscription; 86 using Handle = bool (CloudServiceImpl::*)(int32_t); 87 using Handles = std::deque<Handle>; 88 using Task = ExecutorPool::Task; 89 using TaskId = ExecutorPool::TaskId; 90 using Duration = ExecutorPool::Duration; 91 92 struct HapInfo { 93 int32_t user; 94 int32_t instIndex; 95 std::string bundleName; 96 }; 97 98 static std::map<std::string, int32_t> ConvertAction(const std::map<std::string, int32_t> &actions); 99 static HapInfo GetHapInfo(uint32_t tokenId); 100 101 static constexpr uint64_t INVALID_SUB_TIME = 0; 102 static constexpr int32_t RETRY_TIMES = 3; 103 static constexpr int32_t RETRY_INTERVAL = 60; 104 static constexpr int32_t EXPIRE_INTERVAL = 2 * 24; // 2 day 105 static constexpr int32_t WAIT_TIME = 30; // 30 seconds 106 static constexpr int32_t DEFAULT_USER = 0; 107 static constexpr int32_t TIME_BEFORE_SUB = 12 * 60 * 60 * 1000; // 12hours, ms 108 109 bool UpdateCloudInfo(int32_t user); 110 bool UpdateSchema(int32_t user); 111 bool DoSubscribe(int32_t user); 112 bool ReleaseUserInfo(int32_t user); 113 bool DoCloudSync(int32_t user); 114 bool StopCloudSync(int32_t user); 115 116 std::pair<int32_t, CloudInfo> GetCloudInfo(int32_t userId); 117 std::pair<int32_t, CloudInfo> GetCloudInfoFromMeta(int32_t userId); 118 std::pair<int32_t, CloudInfo> GetCloudInfoFromServer(int32_t userId); 119 120 std::pair<int32_t, SchemaMeta> GetSchemaMeta(int32_t userId, const std::string &bundleName, int32_t instanceId); 121 std::pair<int32_t, SchemaMeta> GetAppSchemaFromServer(int32_t user, const std::string &bundleName); 122 123 void GetSchema(const Event &event); 124 void CloudShare(const Event &event); 125 126 Task GenTask(int32_t retry, int32_t user, Handles handles = { WORK_SUB }); 127 Task GenSubTask(Task task, int32_t user); 128 void InitSubTask(const Subscription &sub); 129 void Execute(Task task); 130 void CleanSubscription(Subscription &sub); 131 int32_t DoClean(CloudInfo &cloudInfo, const std::map<std::string, int32_t> &actions); 132 std::pair<int32_t, std::shared_ptr<DistributedData::Cursor>> PreShare(const CloudEvent::StoreInfo& storeInfo, 133 DistributedData::GenQuery& query); 134 std::vector<NativeRdb::ValuesBucket> ConvertCursor(std::shared_ptr<DistributedData::Cursor> cursor) const; 135 int32_t CheckNotifyConditions(const std::string &id, const std::string &bundleName, CloudInfo &cloudInfo); 136 std::pair<std::string, std::vector<std::string>> GetDbInfoFromExtraData( 137 const DistributedData::ExtraData &extraData, const SchemaMeta &schemaMeta); 138 std::shared_ptr<DistributedData::SharingCenter> GetSharingHandle(const HapInfo& hapInfo); 139 std::shared_ptr<ExecutorPool> executor_; 140 SyncManager syncManager_; 141 std::mutex mutex_; 142 TaskId subTask_ = ExecutorPool::INVALID_TASK_ID; 143 uint64_t expireTime_ = static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::milliseconds>( 144 std::chrono::system_clock::now().time_since_epoch()).count()); 145 146 static constexpr Handle WORK_CLOUD_INFO_UPDATE = &CloudServiceImpl::UpdateCloudInfo; 147 static constexpr Handle WORK_SCHEMA_UPDATE = &CloudServiceImpl::UpdateSchema; 148 static constexpr Handle WORK_SUB = &CloudServiceImpl::DoSubscribe; 149 static constexpr Handle WORK_RELEASE = &CloudServiceImpl::ReleaseUserInfo; 150 static constexpr Handle WORK_DO_CLOUD_SYNC = &CloudServiceImpl::DoCloudSync; 151 static constexpr Handle WORK_STOP_CLOUD_SYNC = &CloudServiceImpl::StopCloudSync; 152 }; 153 } // namespace OHOS::DistributedData 154 155 #endif // OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_CLOUD_SERVICE_IMPL_H 156