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_FILEMGMT_CLOUD_SYNC_SERVICE_H 17 #define OHOS_FILEMGMT_CLOUD_SYNC_SERVICE_H 18 19 #include <map> 20 21 #include "iservice_registry.h" 22 #include "nocopyable.h" 23 #include "system_ability.h" 24 #include "system_ability_load_callback_stub.h" 25 26 #include "cloud_sync_service_stub.h" 27 #include "file_transfer_manager.h" 28 #include "i_cloud_download_callback.h" 29 #include "i_cloud_sync_callback.h" 30 #include "sync_rule/battery_status_listener.h" 31 #include "sync_rule/screen_status_listener.h" 32 #include "svc_death_recipient.h" 33 34 namespace OHOS::FileManagement::CloudSync { 35 class CloudSyncService final : public SystemAbility, public CloudSyncServiceStub, protected NoCopyable { 36 DECLARE_SYSTEM_ABILITY(CloudSyncService); 37 38 public: 39 explicit CloudSyncService(int32_t saID, bool runOnCreate = true); 40 virtual ~CloudSyncService() = default; 41 int32_t UnRegisterCallbackInner() override; 42 int32_t RegisterCallbackInner(const sptr<IRemoteObject> &remoteObject) override; 43 int32_t StartSyncInner(bool forceFlag) override; 44 int32_t TriggerSyncInner(const std::string &bundleName, const int32_t &userId) override; 45 int32_t StopSyncInner() override; 46 int32_t ChangeAppSwitch(const std::string &accoutId, const std::string &bundleName, bool status) override; 47 int32_t Clean(const std::string &accountId, const CleanOptions &cleanOptions) override; 48 int32_t NotifyDataChange(const std::string &accoutId, const std::string &bundleName) override; 49 int32_t NotifyEventChange(int32_t userId, const std::string &eventId, const std::string &extraData) override; 50 int32_t EnableCloud(const std::string &accoutId, const SwitchDataObj &switchData) override; 51 int32_t DisableCloud(const std::string &accoutId) override; 52 int32_t StartDownloadFile(const std::string &path) override; 53 int32_t StartFileCache(const std::string &uri) override; 54 int32_t StopDownloadFile(const std::string &path) override; 55 int32_t RegisterDownloadFileCallback(const sptr<IRemoteObject> &downloadCallback) override; 56 int32_t UnregisterDownloadFileCallback() override; 57 int32_t UploadAsset(const int32_t userId, const std::string &request, std::string &result) override; 58 int32_t DownloadFile(const int32_t userId, const std::string &bundleName, AssetInfoObj &assetInfoObj) override; 59 int32_t DownloadAsset(const uint64_t taskId, 60 const int32_t userId, 61 const std::string &bundleName, 62 const std::string &networkId, 63 AssetInfoObj &assetInfoObj) override; 64 int32_t RegisterDownloadAssetCallback(const sptr<IRemoteObject> &remoteObject) override; 65 int32_t DeleteAsset(const int32_t userId, const std::string &uri) override; 66 int32_t GetSyncTimeInner(int64_t &syncTime) override; 67 int32_t CleanCacheInner(const std::string &uri) override; 68 void SetDeathRecipient(const sptr<IRemoteObject> &remoteObject); 69 70 private: 71 std::string GetHmdfsPath(const std::string &uri, int32_t userId); 72 void OnStart(const SystemAbilityOnDemandReason& startReason) override; 73 void OnStop() override; 74 void PublishSA(); 75 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 76 void Init(); 77 void HandleStartReason(const SystemAbilityOnDemandReason &startReason); 78 79 class LoadRemoteSACallback : public SystemAbilityLoadCallbackStub { 80 public: 81 void OnLoadSACompleteForRemote(const std::string &deviceId, 82 int32_t systemAbilityId, 83 const sptr<IRemoteObject> &remoteObject); 84 std::condition_variable proxyConVar_; 85 std::atomic<bool> isLoadSuccess_{false}; 86 }; 87 88 int32_t LoadRemoteSA(const std::string &deviceId); 89 90 static inline std::mutex loadRemoteSAMutex_; 91 92 std::shared_ptr<DataSyncManager> dataSyncManager_; 93 std::shared_ptr<BatteryStatusListener> batteryStatusListener_; 94 std::shared_ptr<ScreenStatusListener> screenStatusListener_; 95 std::shared_ptr<FileTransferManager> fileTransferManager_; 96 sptr<SvcDeathRecipient> deathRecipient_; 97 static inline std::map<std::string, sptr<OHOS::IRemoteObject>> remoteObjectMap_; 98 }; 99 } // namespace OHOS::FileManagement::CloudSync 100 101 #endif // OHOS_FILEMGMT_CLOUD_SYNC_SERVICE_H 102