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