diff --git a/services/cloudsyncservice/BUILD.gn b/services/cloudsyncservice/BUILD.gn index be1963d67c4a5a055a21454973f8e320a279748c..f6657dfd8d2c1942cd8feb677e6c6bf258501e75 100644 --- a/services/cloudsyncservice/BUILD.gn +++ b/services/cloudsyncservice/BUILD.gn @@ -25,6 +25,7 @@ ohos_shared_library("cloudsync_sa") { "src/ipc/cloud_sync_service.cpp", "src/ipc/cloud_sync_service_stub.cpp", "src/sync_rule/battery_status.cpp", + "src/sync_rule/battery_status_listener.cpp", "src/sync_rule/cloud_status.cpp", "src/sync_rule/net_conn_callback_observer.cpp", "src/sync_rule/network_status.cpp", @@ -40,6 +41,8 @@ ohos_shared_library("cloudsync_sa") { deps = [ "${utils_path}:libdistributedfileutils" ] external_deps = [ + "ability_base:want", + "common_event_service:cesfwk_innerkits", "dfs_service:cloudsync_kit_inner", "init:libbegetutil", "ipc:ipc_core", diff --git a/services/cloudsyncservice/include/data_sync/data_sync_manager.h b/services/cloudsyncservice/include/data_sync/data_sync_manager.h index 026f22ccdfd5b0a3f15b472bc140a0e2f648e252..5663d452a1f4a4533b4ddaea0f2d1e9fc055a880 100644 --- a/services/cloudsyncservice/include/data_sync/data_sync_manager.h +++ b/services/cloudsyncservice/include/data_sync/data_sync_manager.h @@ -23,18 +23,26 @@ #include "data_sync/data_syncer.h" namespace OHOS::FileManagement::CloudSync { +constexpr int32_t INVALID_USER_ID = -1; class DataSyncManager { public: DataSyncManager() = default; ~DataSyncManager() = default; - int32_t Init(const int32_t userId); - std::shared_ptr GetDataSyncer(const std::string appPackageName, const int32_t userId); - int32_t IsSkipSync(const std::string appPackageName, const int32_t userId) const; + int32_t TriggerStartSync(const std::string bundleName, + const int32_t userId, + bool forceFlag, + SyncTriggerType triggerType); + int32_t TriggerStopSync(const std::string bundleName, const int32_t userId, SyncTriggerType triggerType); + + int32_t TriggerRecoverySync(SyncTriggerType triggerType); + std::shared_ptr GetDataSyncer(const std::string bundleName, const int32_t userId); + int32_t IsSkipSync(const std::string bundleName, const int32_t userId) const; private: std::vector> dataSyncers_; std::mutex dataSyncMutex_; + int32_t currentUserId_{INVALID_USER_ID}; }; } // namespace OHOS::FileManagement::CloudSync diff --git a/services/cloudsyncservice/include/data_sync/data_syncer.h b/services/cloudsyncservice/include/data_sync/data_syncer.h index 2e5ca6a329b62c57d38a173b9f1ce91487d23983..271f953850b4429cacb0c8e030131054553b0929 100644 --- a/services/cloudsyncservice/include/data_sync/data_syncer.h +++ b/services/cloudsyncservice/include/data_sync/data_syncer.h @@ -26,26 +26,27 @@ enum class SyncTriggerType : int32_t { APP_TRIGGER, CLOUD_TRIGGER, PENDING_TRIGGER, + BATTERY_OK_TRIGGER, }; class DataSyncer { public: - DataSyncer(const std::string appPackageName, const int32_t userId); + DataSyncer(const std::string bundleName, const int32_t userId); virtual ~DataSyncer(){}; virtual void StartSync(bool forceFlag, SyncTriggerType triggerType) = 0; virtual void StopSync(SyncTriggerType triggerType) = 0; - std::string GetAppPackageName() const; + std::string GetBundleName() const; int32_t GetUserId() const; + std::shared_ptr GetSyncStateManager(); protected: void OnSyncComplete(const int32_t code, const SyncType type); - std::shared_ptr GetSyncStateManager(); void SyncStateChangedNotify(const SyncType type, const SyncPromptState state); private: SyncPromptState GetSyncPromptState(const int32_t code); - const std::string appPackageName_; + const std::string bundleName_; const int32_t userId_; std::shared_ptr syncStateManager_; }; diff --git a/services/cloudsyncservice/include/data_sync/gallery/gallery_data_syncer.h b/services/cloudsyncservice/include/data_sync/gallery/gallery_data_syncer.h index 6283ea63a5470ca5253185e96e019725bf79dfe3..731746d9d98477363de8b71286f3ecf53b5917ba 100644 --- a/services/cloudsyncservice/include/data_sync/gallery/gallery_data_syncer.h +++ b/services/cloudsyncservice/include/data_sync/gallery/gallery_data_syncer.h @@ -21,7 +21,7 @@ namespace OHOS::FileManagement::CloudSync { class GalleryDataSyncer final : public DataSyncer { public: - GalleryDataSyncer(const std::string appPackageName, const int32_t userId) : DataSyncer(appPackageName, userId) {} + GalleryDataSyncer(const std::string bundleName, const int32_t userId) : DataSyncer(bundleName, userId) {} ~GalleryDataSyncer() = default; void StartSync(bool forceFlag, SyncTriggerType triggerType) override; diff --git a/services/cloudsyncservice/include/data_sync/sync_state_manager.h b/services/cloudsyncservice/include/data_sync/sync_state_manager.h index caeb515ea5ec4759b8358b3259db94e7431d4beb..37788763fdae5cfc09637c69ba631871979ca635 100644 --- a/services/cloudsyncservice/include/data_sync/sync_state_manager.h +++ b/services/cloudsyncservice/include/data_sync/sync_state_manager.h @@ -39,6 +39,7 @@ public: bool IsPendingSync(bool forceFlag); bool GetStopSyncFlag(); void SetStopSyncFlag(); + SyncState GetSyncState() const; private: mutable std::shared_mutex syncMutex_; diff --git a/services/cloudsyncservice/include/ipc/cloud_sync_callback_manager.h b/services/cloudsyncservice/include/ipc/cloud_sync_callback_manager.h index 7b7302189128e2fcad049253c472e3779b70064d..83db9955921c7979c69f8fdc8c596b1c50ede984 100644 --- a/services/cloudsyncservice/include/ipc/cloud_sync_callback_manager.h +++ b/services/cloudsyncservice/include/ipc/cloud_sync_callback_manager.h @@ -30,15 +30,15 @@ public: int32_t callerUserId_; }; - void AddCallback(const std::string &appPackageName, const int32_t userId, const sptr &callback); - void NotifySyncStateChanged(const std::string &appPackageName, + void AddCallback(const std::string &bundleName, const int32_t userId, const sptr &callback); + void NotifySyncStateChanged(const std::string &bundleName, const int32_t userId, const SyncType type, const SyncPromptState state); private: - void SetDeathRecipient(const std::string &appPackageName, CallbackInfo &cbInfo); - sptr GetCallbackProxy(const std::string &appPackageName, const int32_t userId); + void SetDeathRecipient(const std::string &bundleName, CallbackInfo &cbInfo); + sptr GetCallbackProxy(const std::string &bundleName, const int32_t userId); SafeMap callbackListMap_; }; diff --git a/services/cloudsyncservice/include/ipc/cloud_sync_service.h b/services/cloudsyncservice/include/ipc/cloud_sync_service.h index e6bdcabeed0ae19fc7fcb52e05d45891fe1b9fe7..483a7bafdbbeeab57632c2ac44b9397a5d8f176d 100644 --- a/services/cloudsyncservice/include/ipc/cloud_sync_service.h +++ b/services/cloudsyncservice/include/ipc/cloud_sync_service.h @@ -21,14 +21,14 @@ #include "i_cloud_sync_callback.h" #include "nocopyable.h" #include "system_ability.h" -#include "data_sync/data_sync_manager.h" +#include "sync_rule/battery_status_listener.h" namespace OHOS::FileManagement::CloudSync { class CloudSyncService final : public SystemAbility, public CloudSyncServiceStub, protected NoCopyable { DECLARE_SYSTEM_ABILITY(CloudSyncService); public: - explicit CloudSyncService(int32_t saID, bool runOnCreate = true) : SystemAbility(saID, runOnCreate) {} + explicit CloudSyncService(int32_t saID, bool runOnCreate = true); virtual ~CloudSyncService() = default; int32_t RegisterCallbackInner(const sptr &remoteObject) override; @@ -40,11 +40,11 @@ private: void OnStart() override; void OnStop() override; void PublishSA(); + void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; void Init(); - int32_t StartSync(const std::string &bundleName, bool forceFlag, SyncTriggerType triggerType); - int32_t StopSync(const std::string &bundleName, SyncTriggerType triggerType); - DataSyncManager dataSyncManager_; + std::shared_ptr dataSyncManager_; + std::shared_ptr batteryStatusListener_; }; } // namespace OHOS::FileManagement::CloudSync diff --git a/services/cloudsyncservice/include/sync_rule/battery_status.h b/services/cloudsyncservice/include/sync_rule/battery_status.h index 47a11bf532c35b869fd8b6265562693b0fdba1db..62f4f02a7c2ab90fd95ee99400ade622cc841c12 100644 --- a/services/cloudsyncservice/include/sync_rule/battery_status.h +++ b/services/cloudsyncservice/include/sync_rule/battery_status.h @@ -17,6 +17,7 @@ #define OHOS_FILEMGMT_BATTERY_STATUS_H #include +#include #ifdef SUPPORT_POWER #include "battery_info.h" @@ -34,11 +35,13 @@ public: static bool IsAllowUpload(bool forceFlag); static bool IsBatteryCapcityOkay(); static CapacityLevel GetCapacityLevel(); + static void GetInitChargingStatus(); + static void SetChargingStatus(bool status); private: - static bool IsCharingStatus(); static int32_t GetCapacity(); static inline CapacityLevel level_{LEVEL_NORMAL}; + static inline std::atomic_bool isCharging_{false}; }; } // namespace OHOS::FileManagement::CloudSync diff --git a/services/cloudsyncservice/include/sync_rule/battery_status_listener.h b/services/cloudsyncservice/include/sync_rule/battery_status_listener.h new file mode 100644 index 0000000000000000000000000000000000000000..2a29a2051a702f38af436a02a138a1e9dc26d14c --- /dev/null +++ b/services/cloudsyncservice/include/sync_rule/battery_status_listener.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_FILEMGMT_BATTERY_STATUS_LISTENER_H +#define OHOS_FILEMGMT_BATTERY_STATUS_LISTENER_H + +#include "common_event_subscriber.h" +#include "data_sync/data_sync_manager.h" + +namespace OHOS::FileManagement::CloudSync { +class BatteryStatusListener : public std::enable_shared_from_this { +public: + explicit BatteryStatusListener(std::shared_ptr dataSyncManager); + ~BatteryStatusListener(); + void Start(); + void Stop(); + void OnStatusNormal(); + void OnStatusAbnormal(); + +private: + std::shared_ptr dataSyncManager_; + std::shared_ptr commonEventSubscriber_ = nullptr; + SyncTriggerType triggerType_{SyncTriggerType::BATTERY_OK_TRIGGER}; +}; + +class BatteryStatusSubscriber : public EventFwk::CommonEventSubscriber { +public: + BatteryStatusSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo, + std::shared_ptr listener); + ~BatteryStatusSubscriber() override {} + void OnReceiveEvent(const EventFwk::CommonEventData &eventData) override; + +private: + std::shared_ptr listener_; +}; +} // namespace OHOS::FileManagement::CloudSync + +#endif // OHOS_FILEMGMT_BATTERY_STATUS_LISTENER_H \ No newline at end of file diff --git a/services/cloudsyncservice/include/sync_rule/cloud_status.h b/services/cloudsyncservice/include/sync_rule/cloud_status.h index 30fb81ac51887caace9930f33216e3daba7e71bd..1c6c89b6302b63512dd62d6565590999deabf33e 100644 --- a/services/cloudsyncservice/include/sync_rule/cloud_status.h +++ b/services/cloudsyncservice/include/sync_rule/cloud_status.h @@ -21,7 +21,7 @@ namespace OHOS::FileManagement::CloudSync { class CloudStatus { public: - static bool IsCloudStatusOkay(const std::string appPackageName); + static bool IsCloudStatusOkay(const std::string bundleName); private: }; } // namespace OHOS::FileManagement::CloudSync diff --git a/services/cloudsyncservice/src/data_sync/data_sync_manager.cpp b/services/cloudsyncservice/src/data_sync/data_sync_manager.cpp index 1e58722281e79fe31b2c140b12b7c7622a94d0b0..4720f612148e2df1cfd0743d6d8f7c79ebcfcb5e 100644 --- a/services/cloudsyncservice/src/data_sync/data_sync_manager.cpp +++ b/services/cloudsyncservice/src/data_sync/data_sync_manager.cpp @@ -15,6 +15,7 @@ #include "data_sync/data_sync_manager.h" +#include #include #include "data_sync/gallery/gallery_data_syncer.h" @@ -27,28 +28,96 @@ namespace OHOS::FileManagement::CloudSync { -int32_t DataSyncManager::Init(const int32_t userId) +int32_t DataSyncManager::TriggerStartSync(const std::string bundleName, + const int32_t userId, + bool forceFlag, + SyncTriggerType triggerType) { + LOGI("trigger sync, bundleName: %{private}s, useId: %{private}d", bundleName.c_str(), userId); + auto ret = IsSkipSync(bundleName, userId); + if (ret != E_OK) { + return ret; + } + auto dataSyncer = GetDataSyncer(bundleName, userId); + if (!dataSyncer) { + LOGE("Get dataSyncer failed, bundleName: %{private}s", bundleName.c_str()); + return E_SYNCER_NUM_OUT_OF_RANGE; + } + std::thread([dataSyncerSptr{dataSyncer}, forceFlag, triggerType]() { + dataSyncerSptr->StartSync(forceFlag, triggerType); + }).detach(); + return E_OK; +} + +int32_t DataSyncManager::TriggerStopSync(const std::string bundleName, + const int32_t userId, + SyncTriggerType triggerType) +{ + auto dataSyncer = GetDataSyncer(bundleName, userId); + if (!dataSyncer) { + LOGE("Get dataSyncer failed, bundleName: %{private}s", bundleName.c_str()); + return E_SYNCER_NUM_OUT_OF_RANGE; + } + std::thread([dataSyncerSptr{dataSyncer}, triggerType]() { + dataSyncerSptr->StopSync(triggerType); + }).detach(); + return E_OK; +} + +int32_t DataSyncManager::TriggerRecoverySync(SyncTriggerType triggerType) +{ + std::vector needSyncApps; + { + std::lock_guard lck(dataSyncMutex_); + if (currentUserId_ == INVALID_USER_ID) { + LOGE("useId is invalid"); + return E_INVAL_ARG; + } + + for (auto dataSyncer : dataSyncers_) { + if (dataSyncer->GetUserId() == currentUserId_) { + auto syncState = dataSyncer->GetSyncStateManager()->GetSyncState(); + if (syncState == SyncState::SYNC_FAILED) { + auto bundleName = dataSyncer->GetBundleName(); + needSyncApps.push_back(bundleName); + } + } + } + } + + if (needSyncApps.size() == 0) { + LOGI("not need to trigger sync"); + return E_OK; + } + + int32_t ret = E_OK; + for (auto app : needSyncApps) { + ret = TriggerStartSync(app, currentUserId_, false, triggerType); + if (ret) { + LOGE("trigger sync failed, ret = %{public}d, bundleName = %{public}s", ret, app.c_str()); + } + } return E_OK; } -std::shared_ptr DataSyncManager::GetDataSyncer(const std::string appPackageName, const int32_t userId) +std::shared_ptr DataSyncManager::GetDataSyncer(const std::string bundleName, const int32_t userId) { std::lock_guard lck(dataSyncMutex_); + currentUserId_ = userId; for (auto dataSyncer : dataSyncers_) { - if ((dataSyncer->GetAppPackageName() == appPackageName) && (dataSyncer->GetUserId() == userId)) { + if ((dataSyncer->GetBundleName() == bundleName) && (dataSyncer->GetUserId() == userId)) { return dataSyncer; } } - std::shared_ptr dataSyncer_ = std::make_shared(appPackageName, userId); + std::shared_ptr dataSyncer_ = std::make_shared(bundleName, userId); dataSyncers_.push_back(dataSyncer_); return dataSyncer_; } -int32_t DataSyncManager::IsSkipSync(const std::string appPackageName, const int32_t userId) const +int32_t DataSyncManager::IsSkipSync(const std::string bundleName, const int32_t userId) const { - if (!CloudStatus::IsCloudStatusOkay(appPackageName)) { + if (!CloudStatus::IsCloudStatusOkay(bundleName)) { LOGE("cloud status is not OK"); return E_SYNC_FAILED_CLOUD_NOT_READY; } diff --git a/services/cloudsyncservice/src/data_sync/data_syncer.cpp b/services/cloudsyncservice/src/data_sync/data_syncer.cpp index 3a93e1a8213cd98e4fbf163df07f71ad7e731651..22624bd551345badd348f7bf43bcd6ed0706ae7e 100644 --- a/services/cloudsyncservice/src/data_sync/data_syncer.cpp +++ b/services/cloudsyncservice/src/data_sync/data_syncer.cpp @@ -23,15 +23,15 @@ namespace OHOS::FileManagement::CloudSync { using namespace std; -DataSyncer::DataSyncer(const std::string appPackageName, const int32_t userId) - : appPackageName_(appPackageName), userId_(userId) +DataSyncer::DataSyncer(const std::string bundleName, const int32_t userId) + : bundleName_(bundleName), userId_(userId) { syncStateManager_ = std::make_shared(); } -std::string DataSyncer::GetAppPackageName() const +std::string DataSyncer::GetBundleName() const { - return appPackageName_; + return bundleName_; } int32_t DataSyncer::GetUserId() const @@ -60,9 +60,9 @@ void DataSyncer::OnSyncComplete(const int32_t code, SyncType type) auto state = GetSyncPromptState(code); if (code == E_OK) { - CloudSyncCallbackManager::GetInstance().NotifySyncStateChanged(appPackageName_, userId_, SyncType::ALL, state); + CloudSyncCallbackManager::GetInstance().NotifySyncStateChanged(bundleName_, userId_, SyncType::ALL, state); } else { - CloudSyncCallbackManager::GetInstance().NotifySyncStateChanged(appPackageName_, userId_, type, state); + CloudSyncCallbackManager::GetInstance().NotifySyncStateChanged(bundleName_, userId_, type, state); } } @@ -73,7 +73,7 @@ std::shared_ptr DataSyncer::GetSyncStateManager() void DataSyncer::SyncStateChangedNotify(const SyncType type, const SyncPromptState state) { - CloudSyncCallbackManager::GetInstance().NotifySyncStateChanged(appPackageName_, userId_, SyncType::ALL, state); + CloudSyncCallbackManager::GetInstance().NotifySyncStateChanged(bundleName_, userId_, SyncType::ALL, state); } SyncPromptState DataSyncer::GetSyncPromptState(const int32_t code) diff --git a/services/cloudsyncservice/src/data_sync/sync_state_manager.cpp b/services/cloudsyncservice/src/data_sync/sync_state_manager.cpp index 6665237ef9f5e7594f83275702cd27e90c006b0f..fd13e1f33228b13c9836f498e23aed3552fdd3d0 100644 --- a/services/cloudsyncservice/src/data_sync/sync_state_manager.cpp +++ b/services/cloudsyncservice/src/data_sync/sync_state_manager.cpp @@ -56,4 +56,10 @@ void SyncStateManager::SetStopSyncFlag() nextAction_ = Action::STOP; stopSyncFlag_ = true; } + +SyncState SyncStateManager::GetSyncState() const +{ + std::unique_lock lck(syncMutex_); + return state_; +} } // namespace OHOS::FileManagement::CloudSync \ No newline at end of file diff --git a/services/cloudsyncservice/src/ipc/cloud_sync_callback_manager.cpp b/services/cloudsyncservice/src/ipc/cloud_sync_callback_manager.cpp index d5b92e912c9d1f288b26d1454cd0302b4ae46798..a78cb88961861d102f689953d1c12bd283e54d5d 100644 --- a/services/cloudsyncservice/src/ipc/cloud_sync_callback_manager.cpp +++ b/services/cloudsyncservice/src/ipc/cloud_sync_callback_manager.cpp @@ -26,19 +26,19 @@ CloudSyncCallbackManager &CloudSyncCallbackManager::GetInstance() return instance; } -void CloudSyncCallbackManager::AddCallback(const std::string &appPackageName, +void CloudSyncCallbackManager::AddCallback(const std::string &bundleName, const int32_t userId, const sptr &callback) { CallbackInfo callbackInfo; callbackInfo.callbackProxy_ = callback; callbackInfo.callerUserId_ = userId; - SetDeathRecipient(appPackageName, callbackInfo); + SetDeathRecipient(bundleName, callbackInfo); /*Delete and then insert when the key exists, ensuring that the final value is inserted.*/ - callbackListMap_.EnsureInsert(appPackageName, callbackInfo); + callbackListMap_.EnsureInsert(bundleName, callbackInfo); } -void CloudSyncCallbackManager::SetDeathRecipient(const std::string &appPackageName, CallbackInfo &cbInfo) +void CloudSyncCallbackManager::SetDeathRecipient(const std::string &bundleName, CallbackInfo &cbInfo) { auto remoteObject = cbInfo.callbackProxy_->AsObject(); if (!remoteObject) { @@ -46,20 +46,20 @@ void CloudSyncCallbackManager::SetDeathRecipient(const std::string &appPackageNa return; } - auto deathCb = [this, packageName{appPackageName}](const wptr &obj) { - callbackListMap_.Erase(packageName); - LOGE("client died, map size:%{public}d, packageName:%{private}s", callbackListMap_.Size(), packageName.c_str()); + auto deathCb = [this, bundleName{bundleName}](const wptr &obj) { + callbackListMap_.Erase(bundleName); + LOGE("client died, map size:%{public}d, bundleName:%{private}s", callbackListMap_.Size(), bundleName.c_str()); }; cbInfo.deathRecipient_ = sptr(new SvcDeathRecipient(deathCb)); remoteObject->AddDeathRecipient(cbInfo.deathRecipient_); } -sptr CloudSyncCallbackManager::GetCallbackProxy(const std::string &appPackageName, +sptr CloudSyncCallbackManager::GetCallbackProxy(const std::string &bundleName, const int32_t userId) { CallbackInfo cbInfo; - if (!callbackListMap_.Find(appPackageName, cbInfo)) { - LOGE("not found callback, appPackageName: %{private}s", appPackageName.c_str()); + if (!callbackListMap_.Find(bundleName, cbInfo)) { + LOGE("not found callback, bundleName: %{private}s", bundleName.c_str()); return nullptr; } @@ -70,14 +70,14 @@ sptr CloudSyncCallbackManager::GetCallbackProxy(const std::s return cbInfo.callbackProxy_; } -void CloudSyncCallbackManager::NotifySyncStateChanged(const std::string &appPackageName, +void CloudSyncCallbackManager::NotifySyncStateChanged(const std::string &bundleName, const int32_t userId, const SyncType type, const SyncPromptState state) { - auto callbackProxy_ = GetCallbackProxy(appPackageName, userId); + auto callbackProxy_ = GetCallbackProxy(bundleName, userId); if (!callbackProxy_) { - LOGE("not found object, appPackageName = %{private}s", appPackageName.c_str()); + LOGE("not found object, bundleName = %{private}s", bundleName.c_str()); return; } callbackProxy_->OnSyncStateChanged(type, state); diff --git a/services/cloudsyncservice/src/ipc/cloud_sync_service.cpp b/services/cloudsyncservice/src/ipc/cloud_sync_service.cpp index f81fca9440a9c62b4fc0c626a39fdfacd3414451..4e7e3b43c956509ca16083ca7db1d7d5819de955 100644 --- a/services/cloudsyncservice/src/ipc/cloud_sync_service.cpp +++ b/services/cloudsyncservice/src/ipc/cloud_sync_service.cpp @@ -14,15 +14,16 @@ */ #include "ipc/cloud_sync_service.h" -#include +#include -#include "ipc/cloud_sync_callback_manager.h" #include "dfs_error.h" #include "dfsu_access_token_helper.h" -#include "system_ability_definition.h" -#include "utils_log.h" +#include "ipc/cloud_sync_callback_manager.h" +#include "sync_rule/battery_status.h" #include "sync_rule/net_conn_callback_observer.h" #include "sync_rule/network_status.h" +#include "system_ability_definition.h" +#include "utils_log.h" namespace OHOS::FileManagement::CloudSync { using namespace std; @@ -30,6 +31,12 @@ using namespace OHOS; REGISTER_SYSTEM_ABILITY_BY_ID(CloudSyncService, FILEMANAGEMENT_CLOUD_SYNC_SERVICE_SA_ID, false); +CloudSyncService::CloudSyncService(int32_t saID, bool runOnCreate) : SystemAbility(saID, runOnCreate) +{ + dataSyncManager_ = make_shared(); + batteryStatusListener_ = make_shared(dataSyncManager_); +} + void CloudSyncService::PublishSA() { LOGI("Begin to init"); @@ -42,6 +49,8 @@ void CloudSyncService::PublishSA() void CloudSyncService::Init() { NetworkStatus::InitNetwork(); + /* Get Init Charging status */ + BatteryStatus::GetInitChargingStatus(); } void CloudSyncService::OnStart() @@ -50,6 +59,7 @@ void CloudSyncService::OnStart() LOGI("Begin to start service"); try { PublishSA(); + AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID); } catch (const exception &e) { LOGE("%{public}s", e.what()); } @@ -61,6 +71,12 @@ void CloudSyncService::OnStop() LOGI("Stop finished successfully"); } +void CloudSyncService::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) +{ + LOGI("OnAddSystemAbility systemAbilityId:%{public}d added!", systemAbilityId); + batteryStatusListener_->Start(); +} + int32_t CloudSyncService::RegisterCallbackInner(const sptr &remoteObject) { if (remoteObject == nullptr) { @@ -68,76 +84,43 @@ int32_t CloudSyncService::RegisterCallbackInner(const sptr &remot return E_INVAL_ARG; } - string appPackageName; - if (DfsuAccessTokenHelper::GetCallerPackageName(appPackageName)) { + string bundleName; + if (DfsuAccessTokenHelper::GetCallerbundleName(bundleName)) { return E_INVAL_ARG; } auto callback = iface_cast(remoteObject); auto callerUserId = DfsuAccessTokenHelper::GetUserId(); - CloudSyncCallbackManager::GetInstance().AddCallback(appPackageName, callerUserId, callback); + CloudSyncCallbackManager::GetInstance().AddCallback(bundleName, callerUserId, callback); return E_OK; } int32_t CloudSyncService::StartSyncInner(bool forceFlag) { - string appPackageName; - if (DfsuAccessTokenHelper::GetCallerPackageName(appPackageName)) { + string bundleName; + if (DfsuAccessTokenHelper::GetCallerbundleName(bundleName)) { return E_INVAL_ARG; } - return StartSync(appPackageName, forceFlag, SyncTriggerType::APP_TRIGGER); + auto callerUserId = DfsuAccessTokenHelper::GetUserId(); + return dataSyncManager_->TriggerStartSync(bundleName, callerUserId, forceFlag, SyncTriggerType::APP_TRIGGER); } int32_t CloudSyncService::StopSyncInner() { - string appPackageName; - if (DfsuAccessTokenHelper::GetCallerPackageName(appPackageName)) { + string bundleName; + if (DfsuAccessTokenHelper::GetCallerbundleName(bundleName)) { return E_INVAL_ARG; } - return StopSync(appPackageName, SyncTriggerType::APP_TRIGGER); -} - -int32_t CloudSyncService::ChangeAppSwitch(const std::string &accoutId, const std::string &bundleName, bool status) -{ - if (status) { - return StartSync(bundleName, false, SyncTriggerType::CLOUD_TRIGGER); - } - return StopSync(bundleName, SyncTriggerType::CLOUD_TRIGGER); -} - -int32_t CloudSyncService::StartSync(const std::string &bundleName, bool forceFlag, SyncTriggerType triggerType) -{ auto callerUserId = DfsuAccessTokenHelper::GetUserId(); - auto ret = dataSyncManager_.Init(callerUserId); - if (ret != E_OK) { - return ret; - } - ret = dataSyncManager_.IsSkipSync(bundleName, callerUserId); - if (ret != E_OK) { - return ret; - } - auto dataSyncer = dataSyncManager_.GetDataSyncer(bundleName, callerUserId); - if (!dataSyncer) { - LOGE("Get dataSyncer failed, appPackageName: %{private}s", bundleName.c_str()); - return E_SYNCER_NUM_OUT_OF_RANGE; - } - std::thread([dataSyncerSptr{dataSyncer}, forceFlag, triggerType]() { - dataSyncerSptr->StartSync(forceFlag, triggerType); - }).detach(); - return E_OK; + return dataSyncManager_->TriggerStopSync(bundleName, callerUserId, SyncTriggerType::APP_TRIGGER); } -int32_t CloudSyncService::StopSync(const std::string &bundleName, SyncTriggerType triggerType) +int32_t CloudSyncService::ChangeAppSwitch(const std::string &accoutId, const std::string &bundleName, bool status) { auto callerUserId = DfsuAccessTokenHelper::GetUserId(); - auto dataSyncer = dataSyncManager_.GetDataSyncer(bundleName, callerUserId); - if (!dataSyncer) { - LOGE("Get dataSyncer failed, appPackageName: %{private}s", bundleName.c_str()); - return E_SYNCER_NUM_OUT_OF_RANGE; + if (status) { + return dataSyncManager_->TriggerStartSync(bundleName, callerUserId, false, SyncTriggerType::CLOUD_TRIGGER); } - std::thread([dataSyncerSptr{dataSyncer}, triggerType]() { - dataSyncerSptr->StopSync(triggerType); - }).detach(); - return E_OK; + return dataSyncManager_->TriggerStopSync(bundleName, callerUserId, SyncTriggerType::CLOUD_TRIGGER); } } // namespace OHOS::FileManagement::CloudSync \ No newline at end of file diff --git a/services/cloudsyncservice/src/sync_rule/battery_status.cpp b/services/cloudsyncservice/src/sync_rule/battery_status.cpp index e9d862f06dc4fca178153ce2939562dba62a9878..667f3275d5399e4e3aca9cada732e732c360b432 100644 --- a/services/cloudsyncservice/src/sync_rule/battery_status.cpp +++ b/services/cloudsyncservice/src/sync_rule/battery_status.cpp @@ -20,15 +20,19 @@ constexpr int32_t PAUSE_CAPACITY_LIMIT = 15; constexpr int32_t STOP_CAPACITY_LIMIT = 10; constexpr int32_t DEFAULT_BATTERY_CAPCITY = 100; -bool BatteryStatus::IsCharingStatus() + +void BatteryStatus::SetChargingStatus(bool status) +{ + isCharging_ = status; +} + +void BatteryStatus::GetInitChargingStatus() { - bool ret = true; #ifdef SUPPORT_POWER auto &batterySrvClient = PowerMgr::BatterySrvClient::GetInstance(); auto chargingStatus = batterySrvClient.GetChargingStatus(); - ret = (chargingStatus == PowerMgr::BatteryChargeState::CHARGE_STATE_ENABLE); + isCharging_ = (chargingStatus == PowerMgr::BatteryChargeState::CHARGE_STATE_ENABLE); #endif - return ret; } int32_t BatteryStatus::GetCapacity() @@ -43,7 +47,7 @@ int32_t BatteryStatus::GetCapacity() bool BatteryStatus::IsAllowUpload(bool forceFlag) { - if (IsCharingStatus()) { + if (isCharging_) { return true; } @@ -68,7 +72,7 @@ bool BatteryStatus::IsAllowUpload(bool forceFlag) bool BatteryStatus::IsBatteryCapcityOkay() { - if (IsCharingStatus()) { + if (isCharging_) { return true; } diff --git a/services/cloudsyncservice/src/sync_rule/battery_status_listener.cpp b/services/cloudsyncservice/src/sync_rule/battery_status_listener.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2075664674da598b2194b270b9d7a8dc14f962cb --- /dev/null +++ b/services/cloudsyncservice/src/sync_rule/battery_status_listener.cpp @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "sync_rule/battery_status_listener.h" + +#include "common_event_manager.h" +#include "common_event_support.h" +#include "sync_rule/battery_status.h" +#include "utils_log.h" + +namespace OHOS::FileManagement::CloudSync { + +BatteryStatusSubscriber::BatteryStatusSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo, + std::shared_ptr listener) + : EventFwk::CommonEventSubscriber(subscribeInfo), listener_(listener) +{ +} + +void BatteryStatusSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &eventData) +{ + auto action = eventData.GetWant().GetAction(); + if (action == EventFwk::CommonEventSupport::COMMON_EVENT_BATTERY_OKAY) { + LOGI("Battery status changed: BATTERY_STATUS_OKAY"); + listener_->OnStatusNormal(); + return; + } + if (action == EventFwk::CommonEventSupport::COMMON_EVENT_CHARGING) { + LOGI("Charging status changed: charging"); + BatteryStatus::SetChargingStatus(true); + } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_DISCHARGING) { + BatteryStatus::SetChargingStatus(false); + LOGI("Charging status changed: discharging"); + } else { + LOGI("OnReceiveEvent action is invalid"); + } +} + +BatteryStatusListener::BatteryStatusListener(std::shared_ptr dataSyncManager) +{ + dataSyncManager_ = dataSyncManager; +} + +BatteryStatusListener::~BatteryStatusListener() +{ + Stop(); +} + +void BatteryStatusListener::Start() +{ + /* subscribe Battery Okay Status and Charging status */ + EventFwk::MatchingSkills matchingSkills; + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_BATTERY_OKAY); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_CHARGING); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_DISCHARGING); + EventFwk::CommonEventSubscribeInfo info(matchingSkills); + commonEventSubscriber_ = std::make_shared(info, shared_from_this()); + auto subRet = EventFwk::CommonEventManager::SubscribeCommonEvent(commonEventSubscriber_); + LOGI("Subscriber end, SubscribeResult = %{public}d", subRet); +} + +void BatteryStatusListener::Stop() +{ + if (commonEventSubscriber_ != nullptr) { + bool result = EventFwk::CommonEventManager::UnSubscribeCommonEvent(commonEventSubscriber_); + LOGI("UnSubscriber end, UnSubscribeResult = %{public}d", result); + commonEventSubscriber_ = nullptr; + } +} + +void BatteryStatusListener::OnStatusNormal() +{ + if (!dataSyncManager_) { + LOGE("dataSyncManager is nullptr"); + return; + } + dataSyncManager_->TriggerRecoverySync(triggerType_); + LOGI("battery status OK"); +} + +void BatteryStatusListener::OnStatusAbnormal() {} +} // namespace OHOS::FileManagement::CloudSync \ No newline at end of file diff --git a/services/cloudsyncservice/src/sync_rule/cloud_status.cpp b/services/cloudsyncservice/src/sync_rule/cloud_status.cpp index 4b12ca50865fdbd3867367774b0df77eeda6bdcb..042fa40caf68ff4f7d63b11665949f12fffa61b8 100644 --- a/services/cloudsyncservice/src/sync_rule/cloud_status.cpp +++ b/services/cloudsyncservice/src/sync_rule/cloud_status.cpp @@ -17,7 +17,7 @@ namespace OHOS::FileManagement::CloudSync { -bool CloudStatus::IsCloudStatusOkay(const std::string appPackageName) +bool CloudStatus::IsCloudStatusOkay(const std::string bundleName) { return true; } diff --git a/test/moduletest/BUILD.gn b/test/moduletest/BUILD.gn index 312a5333f879ef8ddeb6f2ea697a3cea1dd5feb4..a887a3c34398c637f4ecf7bbc62e0f6e64713209 100644 --- a/test/moduletest/BUILD.gn +++ b/test/moduletest/BUILD.gn @@ -80,42 +80,3 @@ ohos_moduletest("DistributedFileDaemonServiceTest") { "samgr:samgr_proxy", ] } - -ohos_moduletest("CloudSyncServiceTest") { - module_out_path = module_output_path - - include_dirs = [] - sources = [] - - sources += [ "src/cloudsync_service_test.cpp" ] - - configs = [ - ":module_private_config", - "${utils_path}:compiler_configs", - ] - - defines = [ - "private=public", - "LOG_TAG=\"CLOUDSYNC_TEST\"", - ] - - deps = [ - "${utils_path}:libdistributedfileutils", - "//third_party/googletest:gmock", - "//third_party/googletest:gtest_main", - ] - - external_deps = [ - "ability_base:want", - "access_token:libaccesstoken_sdk", - "access_token:libtoken_setproc", - "common_event_service:cesfwk_innerkits", - "dataclassification:data_transit_mgr", - "dfs_service:cloudsync_kit_inner", - "dsoftbus:softbus_client", - "init:libbegetutil", - "ipc:ipc_core", - "safwk:system_ability_fwk", - "samgr:samgr_proxy", - ] -} diff --git a/test/moduletest/src/cloudsync_service_test.cpp b/test/moduletest/src/cloudsync_service_test.cpp deleted file mode 100644 index 1369623204188401bdf9996cbc11e1d476e8e42f..0000000000000000000000000000000000000000 --- a/test/moduletest/src/cloudsync_service_test.cpp +++ /dev/null @@ -1,155 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include - -#include "cloud_sync_manager.h" -#include "dfs_error.h" -#include "utils_log.h" - -#include "accesstoken_kit.h" -#include "token_setproc.h" - -namespace OHOS { -namespace FileManagement::CloudSync { -namespace Test { -using namespace testing::ext; -using namespace std; -using namespace OHOS::Security::AccessToken; - -class CloudSyncDerived : public CloudSyncCallback { -public: - void OnSyncStateChanged(SyncType type, SyncPromptState state) - { - LOGI("type: %{public}d state: %{public}d", static_cast(type), static_cast(state)); - } -}; - -static HapInfoParams g_info = {.userID = 100, - .bundleName = "hdcd", // shell process name, avoid register callback more times - .instIndex = 0, - .appIDDesc = "ohos.permission_test.demo", - .isSystemApp = true}; - -static HapPolicyParams g_policy = {.apl = APL_NORMAL, - .domain = "test.domain", - .permList = {{.permissionName = "ohos.permission.cloud_sync", - .bundleName = "hdcd", - .grantMode = 1, - .availableLevel = APL_NORMAL, - .label = "label", - .labelId = 1, - .description = "test", - .descriptionId = 1}}, - .permStateList = {{.permissionName = "ohos.permission.cloud_sync", - .isGeneral = true, - .resDeviceID = {"local"}, - .grantStatus = {PermissionState::PERMISSION_GRANTED}, - .grantFlags = {1}}}}; - -class CloudSyncServiceTest : public testing::Test { -public: - static void SetUpTestCase(void); - static void TearDownTestCase(void); - void SetUp(); - void TearDown(); -}; - -void CloudSyncServiceTest::SetUpTestCase(void) -{ - // input testsuit setup step,setup invoked before all testcases - std::cout << "SetUpTestCase" << std::endl; -} - -void CloudSyncServiceTest::TearDownTestCase(void) -{ - // input testsuit teardown step,teardown invoked after all testcases - std::cout << "TearDownTestCase" << std::endl; -} - -void CloudSyncServiceTest::SetUp(void) -{ - // input testcase setup step,setup invoked before each testcases - std::cout << "SetUp" << std::endl; -} - -void CloudSyncServiceTest::TearDown(void) -{ - // input testcase teardown step,teardown invoked after each testcases - std::cout << "TearDown" << std::endl; -} - -/** - * @tc.name: StartSync_001 - * @tc.desc: function. - * @tc.type: FUNC - * @tc.require: I6H5MH - */ -HWTEST_F(CloudSyncServiceTest, StartSync_001, TestSize.Level1) -{ - LOGE("testcase run OK"); - shared_ptr callback = make_shared(); - bool forceFlag = true; - int ret = CloudSyncManager::GetInstance().StartSync(forceFlag, callback); - EXPECT_EQ(ret, E_OK); -} - -/** - * @tc.name: StartSync_002 - * @tc.desc: function. - * @tc.type: FUNC - * @tc.require: I6H5MH - */ -HWTEST_F(CloudSyncServiceTest, StartSync_002, TestSize.Level1) -{ - LOGE("testcase run OK"); - - uint64_t selfTokenId = GetSelfTokenID(); - AccessTokenKit::AllocHapToken(g_info, g_policy); - AccessTokenIDEx tokenID = AccessTokenKit::GetHapTokenIDEx(g_info.userID, g_info.bundleName, g_info.instIndex); - SetSelfTokenID(tokenID.tokenIDEx); - - shared_ptr callback = make_shared(); - bool forceFlag = true; - int ret = CloudSyncManager::GetInstance().StartSync(forceFlag, callback); - EXPECT_EQ(ret, E_OK); - - SetSelfTokenID(selfTokenId); - auto tokenId = AccessTokenKit::GetHapTokenID(g_info.userID, g_info.bundleName, g_info.instIndex); - AccessTokenKit::DeleteToken(tokenId); -} - -/** - * @tc.name: StopSync_001 - * @tc.desc: function. - * @tc.type: FUNC - * @tc.require: I6H5MH - */ -HWTEST_F(CloudSyncServiceTest, StopSync_001, TestSize.Level1) -{ - LOGE("testcase run OK"); - shared_ptr callback = make_shared(); - bool forceFlag = true; - int ret = CloudSyncManager::GetInstance().StartSync(forceFlag, callback); - EXPECT_EQ(ret, E_OK); - - ret = CloudSyncManager::GetInstance().StopSync(); - EXPECT_EQ(ret, E_OK); -} -} // namespace Test -} // namespace FileManagement::CloudSync -} // namespace OHOS diff --git a/test/unittests/cloudsync_sa/data_sync/BUILD.gn b/test/unittests/cloudsync_sa/data_sync/BUILD.gn index 43b28b0a8df5d3d24b0d35590ec366e7313c1824..8e75d76634de837d4ca16318c9a0e90ebf3bff50 100644 --- a/test/unittests/cloudsync_sa/data_sync/BUILD.gn +++ b/test/unittests/cloudsync_sa/data_sync/BUILD.gn @@ -20,12 +20,12 @@ ohos_unittest("data_sync_manager_test") { sources = [ "${distributedfile_path}/test/unittests/cloudsync_sa/mock/battery_status_mock.cpp", "${distributedfile_path}/test/unittests/cloudsync_sa/mock/cloud_status_mock.cpp", + "${distributedfile_path}/test/unittests/cloudsync_sa/mock/network_status_mock.cpp", "${distributedfile_path}/test/unittests/cloudsync_sa/mock/cloud_sync_callback_manager_mock.cpp", "${services_path}/cloudsyncservice/src/data_sync/data_sync_manager.cpp", "${services_path}/cloudsyncservice/src/data_sync/data_syncer.cpp", "${services_path}/cloudsyncservice/src/data_sync/gallery/gallery_data_syncer.cpp", "${services_path}/cloudsyncservice/src/data_sync/sync_state_manager.cpp", - "${services_path}/cloudsyncservice/src/sync_rule/network_status.cpp", "data_sync_manager_test.cpp", ] diff --git a/test/unittests/cloudsync_sa/data_sync/data_sync_manager_test.cpp b/test/unittests/cloudsync_sa/data_sync/data_sync_manager_test.cpp index bbf5c38318f4590856f034637fc0a0b61e1b68cf..2a2f3a4445498a695b01bb77ef36a1595b19b81d 100644 --- a/test/unittests/cloudsync_sa/data_sync/data_sync_manager_test.cpp +++ b/test/unittests/cloudsync_sa/data_sync/data_sync_manager_test.cpp @@ -55,18 +55,6 @@ void DataSyncManagerTest::TearDown(void) GTEST_LOG_(INFO) << "TearDown"; } -/** - * @tc.name: InitTest - * @tc.desc: Verify the Init function - * @tc.type: FUNC - * @tc.require: I6JPKG - */ -HWTEST_F(DataSyncManagerTest, InitTest, TestSize.Level1) -{ - int32_t userId = 100; - EXPECT_EQ(E_OK, dataSyncManager_->Init(userId)); -} - /** * @tc.name: GetDataSyncerTest * @tc.desc: Verify the GetDataSyncer function @@ -79,7 +67,7 @@ HWTEST_F(DataSyncManagerTest, GetDataSyncerTest, TestSize.Level1) string bundleName = "com.ohos.test"; auto dataSyncer = dataSyncManager_->GetDataSyncer(bundleName, userId); EXPECT_EQ(userId, dataSyncer->GetUserId()); - EXPECT_EQ(bundleName, dataSyncer->GetAppPackageName()); + EXPECT_EQ(bundleName, dataSyncer->GetBundleName()); } /** @@ -95,11 +83,11 @@ HWTEST_F(DataSyncManagerTest, GetExistDataSyncerTest, TestSize.Level1) auto dataSyncer = dataSyncManager_->GetDataSyncer(bundleName, userId); EXPECT_EQ(userId, dataSyncer->GetUserId()); - EXPECT_EQ(bundleName, dataSyncer->GetAppPackageName()); + EXPECT_EQ(bundleName, dataSyncer->GetBundleName()); dataSyncer = dataSyncManager_->GetDataSyncer(bundleName, userId); EXPECT_EQ(userId, dataSyncer->GetUserId()); - EXPECT_EQ(bundleName, dataSyncer->GetAppPackageName()); + EXPECT_EQ(bundleName, dataSyncer->GetBundleName()); EXPECT_EQ(dataSyncManager_->dataSyncers_.size(), 1); } @@ -116,13 +104,13 @@ HWTEST_F(DataSyncManagerTest, GetDataSyncerdifferentUserIdTest, TestSize.Level1) auto dataSyncer = dataSyncManager_->GetDataSyncer(bundleName, userId); EXPECT_EQ(userId, dataSyncer->GetUserId()); - EXPECT_EQ(bundleName, dataSyncer->GetAppPackageName()); + EXPECT_EQ(bundleName, dataSyncer->GetBundleName()); int32_t userId2 = 101; dataSyncer = dataSyncManager_->GetDataSyncer(bundleName, userId2); EXPECT_EQ(userId2, dataSyncer->GetUserId()); - EXPECT_EQ(bundleName, dataSyncer->GetAppPackageName()); + EXPECT_EQ(bundleName, dataSyncer->GetBundleName()); EXPECT_EQ(dataSyncManager_->dataSyncers_.size(), 2); } @@ -139,13 +127,13 @@ HWTEST_F(DataSyncManagerTest, GetDataSyncerdifferentBundleNameTest, TestSize.Lev auto dataSyncer = dataSyncManager_->GetDataSyncer(bundleName, userId); EXPECT_EQ(userId, dataSyncer->GetUserId()); - EXPECT_EQ(bundleName, dataSyncer->GetAppPackageName()); + EXPECT_EQ(bundleName, dataSyncer->GetBundleName()); string bundleName2 = "com.ohos.test2"; dataSyncer = dataSyncManager_->GetDataSyncer(bundleName2, userId); EXPECT_EQ(userId, dataSyncer->GetUserId()); - EXPECT_EQ(bundleName2, dataSyncer->GetAppPackageName()); + EXPECT_EQ(bundleName2, dataSyncer->GetBundleName()); EXPECT_EQ(dataSyncManager_->dataSyncers_.size(), 2); } diff --git a/test/unittests/cloudsync_sa/ipc/BUILD.gn b/test/unittests/cloudsync_sa/ipc/BUILD.gn index 54b0c003f321dbff43bd8364cbe6672c64dda39c..c62917428d9746455d3c6bf84b6155bcaf6c75ba 100644 --- a/test/unittests/cloudsync_sa/ipc/BUILD.gn +++ b/test/unittests/cloudsync_sa/ipc/BUILD.gn @@ -47,7 +47,22 @@ ohos_unittest("cloud_sync_service_stub_test") { ohos_unittest("cloud_sync_service_test") { module_out_path = "filemanagement/dfs_service" - sources = [ "cloud_sync_service_test.cpp" ] + sources = [ + "cloud_sync_service_test.cpp", + "${distributedfile_path}/test/unittests/cloudsync_sa/mock/battery_status_mock.cpp", + "${distributedfile_path}/test/unittests/cloudsync_sa/mock/cloud_status_mock.cpp", + "${distributedfile_path}/test/unittests/cloudsync_sa/mock/network_status_mock.cpp", + "${services_path}/cloudsyncservice/src/data_sync/data_sync_manager.cpp", + "${services_path}/cloudsyncservice/src/data_sync/data_syncer.cpp", + "${services_path}/cloudsyncservice/src/data_sync/gallery/gallery_data_syncer.cpp", + "${services_path}/cloudsyncservice/src/data_sync/sync_state_manager.cpp", + "${services_path}/cloudsyncservice/src/ipc/cloud_sync_callback_manager.cpp", + "${services_path}/cloudsyncservice/src/ipc/cloud_sync_callback_proxy.cpp", + "${services_path}/cloudsyncservice/src/ipc/cloud_sync_service.cpp", + "${services_path}/cloudsyncservice/src/ipc/cloud_sync_service_stub.cpp", + "${services_path}/cloudsyncservice/src/sync_rule/battery_status_listener.cpp", + "${services_path}/cloudsyncservice/src/sync_rule/net_conn_callback_observer.cpp", + ] include_dirs = [ "${services_path}/cloudsyncservice/include/ipc", @@ -56,7 +71,6 @@ ohos_unittest("cloud_sync_service_test") { ] deps = [ - "${services_path}/cloudsyncservice:cloudsync_sa", "${utils_path}:libdistributedfileutils", "//third_party/googletest:gmock_main", "//third_party/googletest:gtest_main", @@ -64,7 +78,10 @@ ohos_unittest("cloud_sync_service_test") { external_deps = [ "ability_base:want", + "common_event_service:cesfwk_innerkits", "dfs_service:cloudsync_kit_inner", + "netmanager_base:net_conn_manager_if", + "init:libbegetutil", "ipc:ipc_core", "safwk:system_ability_fwk", "samgr:samgr_proxy", diff --git a/test/unittests/cloudsync_sa/ipc/cloud_sync_service_stub_test.cpp b/test/unittests/cloudsync_sa/ipc/cloud_sync_service_stub_test.cpp index 324bde2780ad2afc8b989305ad4b5e7a4fc1a9cc..1f8a87293ad6029d14139ca817f7fb5ca05a06ad 100644 --- a/test/unittests/cloudsync_sa/ipc/cloud_sync_service_stub_test.cpp +++ b/test/unittests/cloudsync_sa/ipc/cloud_sync_service_stub_test.cpp @@ -13,12 +13,12 @@ * limitations under the License. */ -#include #include +#include #include "cloud_sync_service_stub.h" -#include "service_callback_mock.h" #include "i_cloud_sync_service.h" +#include "service_callback_mock.h" namespace OHOS { namespace FileManagement::CloudSync { @@ -79,13 +79,13 @@ HWTEST_F(CloudSyncServiceStubTest, HandleRegisterCallbackInnerTest, TestSize.Lev MessageParcel reply; MessageOption option; EXPECT_TRUE(data.WriteInterfaceToken(ICloudSyncService::GetDescriptor())); - + sptr remote = sptr(new CloudSyncCallbackMock()); EXPECT_TRUE(data.WriteRemoteObject(remote->AsObject().GetRefPtr())); EXPECT_EQ(E_OK, service.OnRemoteRequest(ICloudSyncService::SERVICE_CMD_REGISTER_CALLBACK, data, reply, option)); remote = nullptr; - } catch(...) { + } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << " HandleRegisterCallbackInner ERROR"; } @@ -108,12 +108,12 @@ HWTEST_F(CloudSyncServiceStubTest, HandleStartSyncInnerTest, TestSize.Level1) MessageParcel reply; MessageOption option; EXPECT_TRUE(data.WriteInterfaceToken(ICloudSyncService::GetDescriptor())); - + bool forceFlag = true; EXPECT_TRUE(data.WriteBool(forceFlag)); - + EXPECT_EQ(E_OK, service.OnRemoteRequest(ICloudSyncService::SERVICE_CMD_START_SYNC, data, reply, option)); - } catch(...) { + } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << " HandleStartSyncInner ERROR"; } @@ -138,7 +138,7 @@ HWTEST_F(CloudSyncServiceStubTest, HandleStopSyncInnerTest, TestSize.Level1) EXPECT_TRUE(data.WriteInterfaceToken(ICloudSyncService::GetDescriptor())); EXPECT_EQ(E_OK, service.OnRemoteRequest(ICloudSyncService::SERVICE_CMD_STOP_SYNC, data, reply, option)); - } catch(...) { + } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << " HandleStopSyncInner ERROR"; } @@ -146,5 +146,5 @@ HWTEST_F(CloudSyncServiceStubTest, HandleStopSyncInnerTest, TestSize.Level1) } } // namespace Test -} // namespace FileManagement::CloudSync { +} // namespace FileManagement::CloudSync } // namespace OHOS diff --git a/test/unittests/cloudsync_sa/ipc/cloud_sync_service_test.cpp b/test/unittests/cloudsync_sa/ipc/cloud_sync_service_test.cpp index 6b816976f63992424382d87dac855b633bedbfb8..e4a0e19614c86d6764f1de3e5be3afc35b1de591 100644 --- a/test/unittests/cloudsync_sa/ipc/cloud_sync_service_test.cpp +++ b/test/unittests/cloudsync_sa/ipc/cloud_sync_service_test.cpp @@ -16,9 +16,9 @@ #include #include "cloud_sync_service.h" +#include "dfs_error.h" #include "service_callback_mock.h" #include "utils_log.h" -#include "dfs_error.h" namespace OHOS { namespace FileManagement::CloudSync { @@ -72,7 +72,7 @@ HWTEST_F(CloudSyncServiceTest, OnStartTest, TestSize.Level1) GTEST_LOG_(INFO) << "OnStart start"; try { g_servicePtr_->OnStart(); - } catch(...) { + } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "OnStart FAILED"; } @@ -90,7 +90,7 @@ HWTEST_F(CloudSyncServiceTest, OnStopTest, TestSize.Level1) GTEST_LOG_(INFO) << "OnStop start"; try { g_servicePtr_->OnStop(); - } catch(...) { + } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "OnStop FAILED"; } @@ -110,7 +110,7 @@ HWTEST_F(CloudSyncServiceTest, RegisterCallbackInnerTest, TestSize.Level1) sptr callback = sptr(new CloudSyncCallbackMock()); int ret = g_servicePtr_->RegisterCallbackInner(callback); EXPECT_EQ(ret, E_OK); - } catch(...) { + } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "RegisterCallbackInner FAILED"; } @@ -130,11 +130,11 @@ HWTEST_F(CloudSyncServiceTest, StartSyncInnerTest, TestSize.Level1) bool forceFlag = false; int ret = g_servicePtr_->StartSyncInner(forceFlag); EXPECT_EQ(ret, E_OK); - + forceFlag = true; ret = g_servicePtr_->StartSyncInner(forceFlag); EXPECT_EQ(ret, E_OK); - } catch(...) { + } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "StartSyncInner FAILED"; } @@ -153,7 +153,7 @@ HWTEST_F(CloudSyncServiceTest, StopSyncInnerTest, TestSize.Level1) try { int ret = g_servicePtr_->StopSyncInner(); EXPECT_EQ(ret, E_OK); - } catch(...) { + } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "StopSyncInner FAILED"; } @@ -161,5 +161,5 @@ HWTEST_F(CloudSyncServiceTest, StopSyncInnerTest, TestSize.Level1) } } // namespace Test -} // namespace FileManagement::CloudSync { +} // namespace FileManagement::CloudSync } // namespace OHOS diff --git a/test/unittests/cloudsync_sa/ipc/cloudsync_callback_manager_test.cpp b/test/unittests/cloudsync_sa/ipc/cloudsync_callback_manager_test.cpp index c55ffe1e53f2f2b515065a2a58577eb920a9de03..8d1606b2531dd5202bad24d73561d311a9c7b56c 100644 --- a/test/unittests/cloudsync_sa/ipc/cloudsync_callback_manager_test.cpp +++ b/test/unittests/cloudsync_sa/ipc/cloudsync_callback_manager_test.cpp @@ -19,8 +19,8 @@ #include "cloud_sync_callback_manager.h" #include "cloud_sync_callback_proxy.h" -#include "iservice_registry.h" #include "dfs_error.h" +#include "iservice_registry.h" #include "service_callback_mock.h" namespace OHOS { @@ -75,42 +75,42 @@ HWTEST_F(CloudSyncCallbackManagerTest, AddCallbackTest, TestSize.Level1) { GTEST_LOG_(INFO) << "AddCallbackTest Start"; try { - const string appPackageName = "com.ohos.photos"; + const string bundleName = "com.ohos.photos"; sptr callback = sptr(new CloudSyncCallbackMock()); const int userId = 0; - g_managePtr_->AddCallback(appPackageName, userId, callback); + g_managePtr_->AddCallback(bundleName, userId, callback); CloudSyncCallbackManager::CallbackInfo cbInfo; - int res = g_managePtr_->callbackListMap_.Find(appPackageName, cbInfo); + int res = g_managePtr_->callbackListMap_.Find(bundleName, cbInfo); EXPECT_EQ(res, true); EXPECT_NE(cbInfo.callbackProxy_, nullptr); - } catch(...) { + } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << " AddCallbackTest FAILED"; } GTEST_LOG_(INFO) << "AddCallbackTest End"; } - - /* - * @tc.name: GetCallbackProxyTest - * @tc.desc: Verify the GetCallbackProxy function. - * @tc.type: FUNC - * @tc.require: I6H5MH - */ + +/* + * @tc.name: GetCallbackProxyTest + * @tc.desc: Verify the GetCallbackProxy function. + * @tc.type: FUNC + * @tc.require: I6H5MH + */ HWTEST_F(CloudSyncCallbackManagerTest, GetCallbackProxyTest, TestSize.Level1) { GTEST_LOG_(INFO) << "GetCallbackProxy Start"; try { - const string appPackageName = "com.ohos.photos"; + const string bundleName = "com.ohos.photos"; sptr callback = sptr(new CloudSyncCallbackMock()); const int userId = 0; - auto callbackProxy_ = g_managePtr_->GetCallbackProxy(appPackageName, userId); + auto callbackProxy_ = g_managePtr_->GetCallbackProxy(bundleName, userId); EXPECT_EQ(callbackProxy_, nullptr); - g_managePtr_->AddCallback(appPackageName, userId, callback); + g_managePtr_->AddCallback(bundleName, userId, callback); CloudSyncCallbackManager::CallbackInfo cbInfo; - int res = g_managePtr_->callbackListMap_.Find(appPackageName, cbInfo); + int res = g_managePtr_->callbackListMap_.Find(bundleName, cbInfo); EXPECT_EQ(res, true); EXPECT_NE(cbInfo.callbackProxy_, nullptr); - } catch(...) { + } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << " GetCallbackProxy ERROR"; } @@ -127,16 +127,16 @@ HWTEST_F(CloudSyncCallbackManagerTest, SetDeathRecipientTest, TestSize.Level1) { GTEST_LOG_(INFO) << "SetDeathRecipient Start"; try { - const string appPackageName = "com.ohos.photos"; + const string bundleName = "com.ohos.photos"; sptr callback = sptr(new CloudSyncCallbackMock()); const int userId = 0; - g_managePtr_->AddCallback(appPackageName, userId, callback); + g_managePtr_->AddCallback(bundleName, userId, callback); CloudSyncCallbackManager::CallbackInfo cbInfo; - int res = g_managePtr_->callbackListMap_.Find(appPackageName, cbInfo); + int res = g_managePtr_->callbackListMap_.Find(bundleName, cbInfo); EXPECT_TRUE(res); - g_managePtr_->SetDeathRecipient(appPackageName, cbInfo); + g_managePtr_->SetDeathRecipient(bundleName, cbInfo); cbInfo.callbackProxy_ = nullptr; - } catch(...) { + } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << " SetDeathRecipient ERROR"; } @@ -144,5 +144,5 @@ HWTEST_F(CloudSyncCallbackManagerTest, SetDeathRecipientTest, TestSize.Level1) } } // namespace Test -} // namespace FileManagement::CloudSync { +} // namespace FileManagement::CloudSync } // namespace OHOS diff --git a/test/unittests/cloudsync_sa/ipc/cloudsync_callback_proxy_test.cpp b/test/unittests/cloudsync_sa/ipc/cloudsync_callback_proxy_test.cpp index fc655754716396b36d9311cc01e03fc092bc088d..2f2ecad87de7bc92ab3131718462e41dc23b41f5 100644 --- a/test/unittests/cloudsync_sa/ipc/cloudsync_callback_proxy_test.cpp +++ b/test/unittests/cloudsync_sa/ipc/cloudsync_callback_proxy_test.cpp @@ -17,8 +17,8 @@ #include #include "cloud_sync_callback_proxy.h" -#include "service_callback_mock.h" #include "dfs_error.h" +#include "service_callback_mock.h" namespace OHOS { namespace FileManagement::CloudSync { @@ -69,13 +69,11 @@ HWTEST_F(CloudSyncCallbackProxyTest, OnSyncStateChangedTest, TestSize.Level1) { GTEST_LOG_(INFO) << "OnSyncStateChanged Start"; try { - EXPECT_CALL(*mock_, SendRequest(_, _, _, _)) - .Times(1) - .WillOnce(Return(E_OK)); + EXPECT_CALL(*mock_, SendRequest(_, _, _, _)).Times(1).WillOnce(Return(E_OK)); SyncType syncType = SyncType::UPLOAD; SyncPromptState syncPromptState = SyncPromptState::SYNC_STATE_SYNCING; proxy_->OnSyncStateChanged(syncType, syncPromptState); - } catch(...) { + } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << " OnSyncStateChanged ERROR"; } @@ -83,5 +81,5 @@ HWTEST_F(CloudSyncCallbackProxyTest, OnSyncStateChangedTest, TestSize.Level1) } } // namespace Test -} // namespace FileManagement::CloudSync { +} // namespace FileManagement::CloudSync } // namespace OHOS diff --git a/test/unittests/cloudsync_sa/mock/battery_status_mock.cpp b/test/unittests/cloudsync_sa/mock/battery_status_mock.cpp index 33745b9573d450bfe32faa8612782a5484ce35c0..72466955385bc9dc1a77c112e137099b80ba38e7 100644 --- a/test/unittests/cloudsync_sa/mock/battery_status_mock.cpp +++ b/test/unittests/cloudsync_sa/mock/battery_status_mock.cpp @@ -17,9 +17,12 @@ namespace OHOS::FileManagement::CloudSync { constexpr int32_t DEFAULT_BATTERY_CAPCITY = 100; -bool BatteryStatus::IsCharingStatus() +void BatteryStatus::SetChargingStatus(bool status) +{ +} + +void BatteryStatus::GetInitChargingStatus() { - return true; } int32_t BatteryStatus::GetCapacity() diff --git a/test/unittests/cloudsync_sa/mock/cloud_status_mock.cpp b/test/unittests/cloudsync_sa/mock/cloud_status_mock.cpp index 4b12ca50865fdbd3867367774b0df77eeda6bdcb..042fa40caf68ff4f7d63b11665949f12fffa61b8 100644 --- a/test/unittests/cloudsync_sa/mock/cloud_status_mock.cpp +++ b/test/unittests/cloudsync_sa/mock/cloud_status_mock.cpp @@ -17,7 +17,7 @@ namespace OHOS::FileManagement::CloudSync { -bool CloudStatus::IsCloudStatusOkay(const std::string appPackageName) +bool CloudStatus::IsCloudStatusOkay(const std::string bundleName) { return true; } diff --git a/test/unittests/cloudsync_sa/mock/cloud_sync_callback_manager_mock.cpp b/test/unittests/cloudsync_sa/mock/cloud_sync_callback_manager_mock.cpp index 7f783cf2b8acd4631bbc3bf23ef2270b04fd15e2..327376a54e74bdf57326a808d1c397398ed7f9bf 100644 --- a/test/unittests/cloudsync_sa/mock/cloud_sync_callback_manager_mock.cpp +++ b/test/unittests/cloudsync_sa/mock/cloud_sync_callback_manager_mock.cpp @@ -26,21 +26,21 @@ CloudSyncCallbackManager &CloudSyncCallbackManager::GetInstance() return instance; } -void CloudSyncCallbackManager::AddCallback(const std::string &appPackageName, +void CloudSyncCallbackManager::AddCallback(const std::string &bundleName, const int32_t userId, const sptr &callback) { } -void CloudSyncCallbackManager::SetDeathRecipient(const std::string &appPackageName, CallbackInfo &cbInfo) {} +void CloudSyncCallbackManager::SetDeathRecipient(const std::string &bundleName, CallbackInfo &cbInfo) {} -sptr CloudSyncCallbackManager::GetCallbackProxy(const std::string &appPackageName, +sptr CloudSyncCallbackManager::GetCallbackProxy(const std::string &bundleName, const int32_t userId) { return nullptr; } -void CloudSyncCallbackManager::NotifySyncStateChanged(const std::string &appPackageName, +void CloudSyncCallbackManager::NotifySyncStateChanged(const std::string &bundleName, const int32_t userId, const SyncType type, const SyncPromptState state) diff --git a/test/unittests/cloudsync_sa/mock/network_status_mock.cpp b/test/unittests/cloudsync_sa/mock/network_status_mock.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1f5a654c1b24f1dec04c73bfba64187f1fb8e7d6 --- /dev/null +++ b/test/unittests/cloudsync_sa/mock/network_status_mock.cpp @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "sync_rule/network_status.h" +#include "dfs_error.h" + +namespace OHOS::FileManagement::CloudSync { + +int32_t NetworkStatus::RegisterNetConnCallback() +{ + return E_OK; +} + +int32_t NetworkStatus::GetDefaultNet() +{ + return E_OK; +} + +void NetworkStatus::SetNetConnStatus(NetManagerStandard::NetAllCapabilities &netAllCap) +{ +} + +int32_t NetworkStatus::GetAndRegisterNetwork() +{ + int32_t res = GetDefaultNet(); + if (res != E_OK) { + return res; + } + + return RegisterNetConnCallback(); +} + +void NetworkStatus::InitNetwork() +{ +} + +void NetworkStatus::SetNetConnStatus(NetworkStatus::NetConnStatus netStatus) +{ + return; +} + +NetworkStatus::NetConnStatus NetworkStatus::GetNetConnStatus() +{ + return NetworkStatus::NetConnStatus::WIFI_CONNECT; +} +} // namespace OHOS::FileManagement::CloudSync \ No newline at end of file diff --git a/utils/system/include/dfsu_access_token_helper.h b/utils/system/include/dfsu_access_token_helper.h index 4ecaab91a2e179961546e7f1558ffe50e685ce42..d2c8cbb0dc58de6d242db3970b91cf4b904e3246 100644 --- a/utils/system/include/dfsu_access_token_helper.h +++ b/utils/system/include/dfsu_access_token_helper.h @@ -24,9 +24,9 @@ inline const std::string PERM_CLOUD_SYNC = "ohos.permission.CLOUDFILE_SYNC"; class DfsuAccessTokenHelper final { public: static bool CheckCallerPermission(const std::string &permissionName); - static int32_t GetCallerPackageName(std::string &packageName); + static int32_t GetCallerbundleName(std::string &bundleName); static bool CheckPermission(uint32_t tokenId, const std::string &permissionName); - static int32_t GetPackageNameByToken(uint32_t tokenId, std::string &packageName); + static int32_t GetBundleNameByToken(uint32_t tokenId, std::string &bundleName); static bool IsSystemApp(); static int32_t GetUserId(); }; diff --git a/utils/system/src/dfsu_access_token_helper.cpp b/utils/system/src/dfsu_access_token_helper.cpp index 110c215491f792bcc6629d219f321bedb7044d91..8215767593f446a87c87a93f854b50c059529359 100644 --- a/utils/system/src/dfsu_access_token_helper.cpp +++ b/utils/system/src/dfsu_access_token_helper.cpp @@ -55,13 +55,13 @@ bool DfsuAccessTokenHelper::CheckPermission(uint32_t tokenId, const std::string return true; } -int32_t DfsuAccessTokenHelper::GetCallerPackageName(std::string &packageName) +int32_t DfsuAccessTokenHelper::GetCallerbundleName(std::string &bundleName) { auto tokenId = IPCSkeleton::GetCallingTokenID(); - return GetPackageNameByToken(tokenId, packageName); + return GetBundleNameByToken(tokenId, bundleName); } -int32_t DfsuAccessTokenHelper::GetPackageNameByToken(uint32_t tokenId, std::string &packageName) +int32_t DfsuAccessTokenHelper::GetBundleNameByToken(uint32_t tokenId, std::string &bundleName) { int32_t tokenType = AccessTokenKit::GetTokenTypeFlag(tokenId); switch (tokenType) { @@ -71,7 +71,7 @@ int32_t DfsuAccessTokenHelper::GetPackageNameByToken(uint32_t tokenId, std::stri LOGE("get hap token info fail"); return E_GET_TOKEN_INFO_ERROR; } - packageName = hapInfo.bundleName; + bundleName = hapInfo.bundleName; break; } case TOKEN_NATIVE: @@ -82,7 +82,7 @@ int32_t DfsuAccessTokenHelper::GetPackageNameByToken(uint32_t tokenId, std::stri LOGE("get native token info fail"); return E_GET_TOKEN_INFO_ERROR; } - packageName = tokenInfo.processName; + bundleName = tokenInfo.processName; break; } default: { @@ -90,7 +90,7 @@ int32_t DfsuAccessTokenHelper::GetPackageNameByToken(uint32_t tokenId, std::stri return E_GET_TOKEN_INFO_ERROR; } } - if (packageName.empty()) { + if (bundleName.empty()) { LOGE("package name is empty"); return E_INVAL_ARG; }