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 22 #include "cloud/cloud_event.h" 23 #include "cloud/cloud_extra_data.h" 24 #include "cloud/cloud_info.h" 25 #include "cloud_notifier_proxy.h" 26 #include "cloud/schema_meta.h" 27 #include "cloud/sharing_center.h" 28 #include "cloud/subscription.h" 29 #include "cloud_service_stub.h" 30 #include "dfx/dfx_types.h" 31 #include "feature/static_acts.h" 32 #include "store/general_store.h" 33 #include "sync_manager.h" 34 #include "values_bucket.h" 35 36 namespace OHOS::CloudData { 37 using namespace DistributedRdb; 38 class CloudServiceImpl : public CloudServiceStub { 39 public: 40 using CloudLastSyncInfo = DistributedData::CloudLastSyncInfo; 41 using StoreMetaData = DistributedData::StoreMetaData; 42 using StoreInfo = DistributedData::StoreInfo; 43 CloudServiceImpl(); 44 ~CloudServiceImpl() = default; 45 int32_t EnableCloud(const std::string &id, const std::map<std::string, int32_t> &switches) override; 46 int32_t DisableCloud(const std::string &id) override; 47 int32_t ChangeAppSwitch(const std::string &id, const std::string &bundleName, int32_t appSwitch) override; 48 int32_t Clean(const std::string &id, const std::map<std::string, int32_t> &actions) override; 49 int32_t NotifyDataChange(const std::string &id, const std::string &bundleName) override; 50 int32_t NotifyDataChange(const std::string &eventId, const std::string &extraData, int32_t userId) override; 51 std::pair<int32_t, std::map<std::string, StatisticInfos>> QueryStatistics( 52 const std::string &id, const std::string &bundleName, const std::string &storeId) override; 53 std::pair<int32_t, QueryLastResults> QueryLastSyncInfo( 54 const std::string &id, const std::string &bundleName, const std::string &storeId) override; 55 int32_t SetGlobalCloudStrategy(Strategy strategy, const std::vector<CommonType::Value> &values) override; 56 int32_t CloudSync(const std::string &bundleName, const std::string &storeId, const Option &option, 57 const AsyncDetail &async) override; 58 int32_t InitNotifier(sptr<IRemoteObject> notifier) override; 59 60 std::pair<int32_t, std::vector<NativeRdb::ValuesBucket>> AllocResourceAndShare(const std::string &storeId, 61 const DistributedRdb::PredicatesMemo &predicates, const std::vector<std::string> &columns, 62 const Participants &participants) override; 63 int32_t Share(const std::string &sharingRes, const Participants &participants, Results &results) override; 64 int32_t Unshare(const std::string &sharingRes, const Participants &participants, Results &results) override; 65 int32_t Exit(const std::string &sharingRes, std::pair<int32_t, std::string> &result) override; 66 int32_t ChangePrivilege(const std::string &sharingRes, const Participants &participants, Results &results) override; 67 int32_t Query(const std::string &sharingRes, QueryResults &results) override; 68 int32_t QueryByInvitation(const std::string &invitation, QueryResults &results) override; 69 int32_t ConfirmInvitation(const std::string &invitation, int32_t confirmation, 70 std::tuple<int32_t, std::string, std::string> &result) override; 71 int32_t ChangeConfirmation( 72 const std::string &sharingRes, int32_t confirmation, std::pair<int32_t, std::string> &result) override; 73 74 int32_t SetCloudStrategy(Strategy strategy, const std::vector<CommonType::Value> &values) override; 75 76 int32_t OnInitialize() override; 77 int32_t OnBind(const BindInfo &info) override; 78 int32_t OnUserChange(uint32_t code, const std::string &user, const std::string &account) override; 79 int32_t OnReady(const std::string &device) override; 80 int32_t Offline(const std::string &device) override; 81 int32_t OnScreenUnlocked(int32_t user) override; 82 83 private: 84 using StaticActs = DistributedData::StaticActs; 85 class CloudStatic : public StaticActs { 86 public: ~CloudStatic()87 ~CloudStatic() override{}; 88 int32_t OnAppUninstall(const std::string &bundleName, int32_t user, int32_t index) override; 89 int32_t OnAppInstall(const std::string &bundleName, int32_t user, int32_t index) override; 90 int32_t OnAppUpdate(const std::string &bundleName, int32_t user, int32_t index) override; 91 private: 92 bool CloudDriverCheck(const std::string &bundleName, int32_t user); 93 }; 94 class Factory { 95 public: 96 Factory() noexcept; 97 ~Factory(); 98 99 private: 100 std::shared_ptr<CloudServiceImpl> product_; 101 std::shared_ptr<CloudStatic> staticActs_; 102 }; 103 static Factory factory_; 104 enum class CloudSyncScene { 105 ENABLE_CLOUD = 0, 106 DISABLE_CLOUD = 1, 107 SWITCH_ON = 2, 108 SWITCH_OFF = 3, 109 QUERY_SYNC_INFO = 4, 110 USER_CHANGE = 5, 111 USER_UNLOCK = 6, 112 NETWORK_RECOVERY = 7, 113 SERVICE_INIT = 8, 114 ACCOUNT_STOP = 9, 115 }; 116 117 using Database = DistributedData::Database; 118 using CloudInfo = DistributedData::CloudInfo; 119 using SchemaMeta = DistributedData::SchemaMeta; 120 using Event = DistributedData::Event; 121 using CloudEvent = DistributedData::CloudEvent; 122 using Subscription = DistributedData::Subscription; 123 using Handle = bool (CloudServiceImpl::*)(int32_t, CloudSyncScene); 124 using Handles = std::deque<Handle>; 125 using Task = ExecutorPool::Task; 126 using TaskId = ExecutorPool::TaskId; 127 using Duration = ExecutorPool::Duration; 128 using AutoCache = DistributedData::AutoCache; 129 130 struct HapInfo { 131 int32_t user; 132 int32_t instIndex; 133 std::string bundleName; 134 }; 135 136 struct SyncAgent { 137 SyncAgent() = default; 138 sptr<CloudNotifierProxy> notifier_; 139 }; 140 141 static std::map<std::string, int32_t> ConvertAction(const std::map<std::string, int32_t> &actions); 142 static HapInfo GetHapInfo(uint32_t tokenId); 143 static std::string GetDfxFaultType(CloudSyncScene scene); 144 145 static constexpr uint64_t INVALID_SUB_TIME = 0; 146 static constexpr int32_t RETRY_TIMES = 3; 147 static constexpr int32_t RETRY_INTERVAL = 60; 148 static constexpr int32_t EXPIRE_INTERVAL = 2 * 24; // 2 day 149 static constexpr int32_t WAIT_TIME = 30; // 30 seconds 150 static constexpr int32_t DEFAULT_USER = 0; 151 static constexpr int32_t TIME_BEFORE_SUB = 12 * 60 * 60 * 1000; // 12hours, ms 152 static constexpr int32_t SUBSCRIPTION_INTERVAL = 60 * 60 * 1000; // 1hours 153 154 bool UpdateCloudInfo(int32_t user, CloudSyncScene scene); 155 bool UpdateSchema(int32_t user, CloudSyncScene scene); 156 bool DoSubscribe(int32_t user, CloudSyncScene scene); 157 bool ReleaseUserInfo(int32_t user, CloudSyncScene scene); 158 bool DoCloudSync(int32_t user, CloudSyncScene scene); 159 bool StopCloudSync(int32_t user, CloudSyncScene scene); 160 bool CleanWaterVersion(int32_t user); 161 162 static std::pair<int32_t, CloudInfo> GetCloudInfo(int32_t userId); 163 static std::pair<int32_t, CloudInfo> GetCloudInfoFromMeta(int32_t userId); 164 static std::pair<int32_t, CloudInfo> GetCloudInfoFromServer(int32_t userId); 165 static int32_t UpdateCloudInfoFromServer(int32_t user); 166 static std::pair<int32_t, SchemaMeta> GetAppSchemaFromServer(int32_t user, const std::string &bundleName); 167 static Details HandleGenDetails(const DistributedData::GenDetails &details); 168 169 void OnAsyncComplete(uint32_t tokenId, uint32_t seqNum, Details &&result); 170 std::pair<int32_t, SchemaMeta> GetSchemaMeta(int32_t userId, const std::string &bundleName, int32_t instanceId); 171 void UpgradeSchemaMeta(int32_t user, const SchemaMeta &schemaMeta); 172 std::map<std::string, StatisticInfos> ExecuteStatistics( 173 const std::string &storeId, const CloudInfo &cloudInfo, const SchemaMeta &schemaMeta); 174 StatisticInfos QueryStatistics(const StoreMetaData &storeMetaData, const DistributedData::Database &database); 175 std::pair<bool, StatisticInfo> QueryTableStatistic(const std::string &tableName, AutoCache::Store store); 176 std::string BuildStatisticSql(const std::string &tableName); 177 178 void GetSchema(const Event &event); 179 void CloudShare(const Event &event); 180 void DoSync(const Event &event); 181 182 Task GenTask(int32_t retry, int32_t user, CloudSyncScene scene, Handles handles = { WORK_SUB }); 183 Task GenSubTask(Task task, int32_t user); 184 void InitSubTask(const Subscription &sub, uint64_t minInterval = 0); 185 void Execute(Task task); 186 void CleanSubscription(Subscription &sub); 187 int32_t DoClean(const CloudInfo &cloudInfo, const std::map<std::string, int32_t> &actions); 188 void DoClean(int32_t user, const SchemaMeta &schemaMeta, int32_t action); 189 std::pair<int32_t, std::shared_ptr<DistributedData::Cursor>> PreShare( 190 const StoreInfo &storeInfo, DistributedData::GenQuery &query); 191 std::vector<NativeRdb::ValuesBucket> ConvertCursor(std::shared_ptr<DistributedData::Cursor> cursor) const; 192 int32_t CheckNotifyConditions(const std::string &id, const std::string &bundleName, CloudInfo &cloudInfo); 193 std::map<std::string, std::vector<std::string>> GetDbInfoFromExtraData( 194 const DistributedData::ExtraData &extraData, const SchemaMeta &schemaMeta); 195 std::shared_ptr<DistributedData::SharingCenter> GetSharingHandle(const HapInfo &hapInfo); 196 bool GetStoreMetaData(StoreMetaData &meta); 197 bool DoKvCloudSync(int32_t userId, const std::string &bundleName = "", int32_t triggerMode = 0); 198 199 using SaveStrategy = int32_t (*)(const std::vector<CommonType::Value> &values, const HapInfo &hapInfo); 200 static const SaveStrategy STRATEGY_SAVERS[Strategy::STRATEGY_BUTT]; 201 static int32_t SaveNetworkStrategy(const std::vector<CommonType::Value> &values, const HapInfo &hapInfo); 202 static void Report(const std::string &faultType, DistributedDataDfx::Fault errCode, const std::string &bundleName, 203 const std::string &appendix); 204 QueryLastResults AssembleLastResults(const std::vector<Database> &databases, 205 const std::map<std::string, CloudLastSyncInfo> &lastSyncInfos); 206 207 static std::pair<int32_t, SchemaMeta> GetSchemaFromHap(const HapInfo &hapInfo); 208 static int32_t UpdateSchemaFromHap(const HapInfo &hapInfo); 209 static int32_t UpdateSchemaFromServer(int32_t user); 210 static int32_t UpdateSchemaFromServer(const CloudInfo &cloudInfo, int32_t user); 211 static void UpdateE2eeEnable(const std::string &schemaKey, bool newE2eeEnable, const std::string &bundleName); 212 static void UpdateClearWaterMark( 213 const HapInfo &hapInfo, const SchemaMeta &newSchemaMeta, const SchemaMeta &schemaMeta); 214 215 std::shared_ptr<ExecutorPool> executor_; 216 SyncManager syncManager_; 217 std::mutex mutex_; 218 std::mutex rwMetaMutex_; 219 TaskId subTask_ = ExecutorPool::INVALID_TASK_ID; 220 uint64_t expireTime_ = static_cast<uint64_t>( 221 std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()) 222 .count()); 223 ConcurrentMap<uint32_t, SyncAgent> syncAgents_; 224 225 static constexpr Handle WORK_CLOUD_INFO_UPDATE = &CloudServiceImpl::UpdateCloudInfo; 226 static constexpr Handle WORK_SCHEMA_UPDATE = &CloudServiceImpl::UpdateSchema; 227 static constexpr Handle WORK_SUB = &CloudServiceImpl::DoSubscribe; 228 static constexpr Handle WORK_RELEASE = &CloudServiceImpl::ReleaseUserInfo; 229 static constexpr Handle WORK_DO_CLOUD_SYNC = &CloudServiceImpl::DoCloudSync; 230 static constexpr Handle WORK_STOP_CLOUD_SYNC = &CloudServiceImpl::StopCloudSync; 231 }; 232 } // namespace OHOS::CloudData 233 234 #endif // OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_CLOUD_SERVICE_IMPL_H