diff --git a/BUILD.gn b/BUILD.gn index 656f569b8fd3a64f1b9dd371cac876e9b5150806..d87bad7a673d8a06300020c0c00c297cd32a39c6 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -20,6 +20,13 @@ group("services_target") { "${services_path}:distributedfile_etc", "${services_path}:distributedfile_sa_profile", "${services_path}/distributedfiledaemon:libdistributedfiledaemon", + "${services_path}/cloudsyncservice:cloudsync_sa", + ] +} + +group("cloudsync_kit_inner_target") { + deps = [ + "interfaces/inner_api/native/cloudsync_kit_inner:cloudsync_kit_inner", ] } diff --git a/bundle.json b/bundle.json index c76b1bcda63a6ba021694ed06ea59e4d19d550bf..52dcd2251ea2d336eedcebc4ee3f02e7073ab117 100644 --- a/bundle.json +++ b/bundle.json @@ -31,7 +31,7 @@ "samgr", "utils" ], - "third_party": [ + "third_party": [ "json_modern_c++" ] }, @@ -40,10 +40,27 @@ "base_group": [], "fwk_group": [], "service_group": [ - "//foundation/filemanagement/dfs_service:services_target" + "//foundation/filemanagement/dfs_service:services_target", + "//foundation/filemanagement/dfs_service:cloudsync_kit_inner_target" ] }, - "test": [ + "inner_kits": [ + { + "name": "//foundation/filemanagement/dfs_service/interfaces/inner_api/native/cloudsync_kit_inner:cloudsync_kit_inner", + "header": { + "header_files": [ + "cloud_sync_callback.h", + "cloud_sync_manager.h", + "i_cloud_sync_callback.h", + "i_cloud_sync_service.h", + "svc_death_recipient.h", + "cloud_sync_constants.h" + ], + "header_base": "//foundation/filemanagement/dfs_service/interfaces/inner_api/native/cloudsync_kit_inner" + } + } + ], + "test": [ "//foundation/filemanagement/dfs_service:dfs_test_moudule" ] } diff --git a/distributedfile.gni b/distributedfile.gni index ae0317878735b0f6c7962af1a73f10e8f4ac7137..d3bac39ad3072ca3849c22f2f2388c15f50f0eca 100755 --- a/distributedfile.gni +++ b/distributedfile.gni @@ -19,3 +19,6 @@ distributedfile_path = "//foundation/filemanagement/dfs_service" utils_path = "${distributedfile_path}/utils" services_path = "${distributedfile_path}/services" innerkits_native_path = "${distributedfile_path}/interfaces/innerkits/native" +path_samgr = "//foundation/systemabilitymgr/samgr" +path_base = "//commonlibrary/c_utils/base" +path_ipc = "//foundation/communication/ipc" diff --git a/frameworks/native/cloudsync_kit_inner/include/cloud_sync_callback_client.h b/frameworks/native/cloudsync_kit_inner/include/cloud_sync_callback_client.h new file mode 100644 index 0000000000000000000000000000000000000000..d847e44f608d28e58356fc699b674333c4fbae04 --- /dev/null +++ b/frameworks/native/cloudsync_kit_inner/include/cloud_sync_callback_client.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2022 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 "cloud_sync_callback_stub.h" + +#ifndef OHOS_FILEMGMT_CLOUD_SYNC_CALLBACK_CLIENT_H +#define OHOS_FILEMGMT_CLOUD_SYNC_CALLBACK_CLIENT_H + +namespace OHOS::FileManagement::CloudSync { +class CloudSyncCallbackClient final : public CloudSyncCallbackStub { +public: + explicit CloudSyncCallbackClient(const std::shared_ptr &callback) : callback_(callback) {} + + void OnSyncStateChanged(SyncType type, SyncPromptState state) override; + +private: + std::shared_ptr callback_; +}; +} // namespace OHOS::FileManagement::CloudSync + +#endif // OHOS_FILEMGMT_CLOUD_SYNC_CALLBACK_CLIENT_H \ No newline at end of file diff --git a/frameworks/native/cloudsync_kit_inner/include/cloud_sync_callback_stub.h b/frameworks/native/cloudsync_kit_inner/include/cloud_sync_callback_stub.h new file mode 100644 index 0000000000000000000000000000000000000000..c518cb35b841c45edcf8304321dbb89ccd6ae6c7 --- /dev/null +++ b/frameworks/native/cloudsync_kit_inner/include/cloud_sync_callback_stub.h @@ -0,0 +1,39 @@ +/* +* Copyright (c) 2022 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_CLOUD_SYNC_CALLBACK_STUB_H +#define OHOS_FILEMGMT_CLOUD_SYNC_CALLBACK_STUB_H + +#include + +#include "i_cloud_sync_callback.h" +#include "iremote_stub.h" + +namespace OHOS::FileManagement::CloudSync { +class CloudSyncCallbackStub : public IRemoteStub { +public: + CloudSyncCallbackStub(); + virtual ~CloudSyncCallbackStub() = default; + int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; + +private: + using ServiceInterface = int32_t (CloudSyncCallbackStub::*)(MessageParcel &data, MessageParcel &reply); + std::map opToInterfaceMap_; + + int32_t HandleOnSyncStateChanged(MessageParcel &data, MessageParcel &reply); +}; +} // namespace OHOS::FileManagement::CloudSync + +#endif // OHOS_FILEMGMT_CLOUD_SYNC_CALLBACK_STUB_H \ No newline at end of file diff --git a/frameworks/native/cloudsync_kit_inner/include/cloud_sync_manager_impl.h b/frameworks/native/cloudsync_kit_inner/include/cloud_sync_manager_impl.h new file mode 100644 index 0000000000000000000000000000000000000000..1df4a2bb16affb9403ee4b52f5879d9f6415fd82 --- /dev/null +++ b/frameworks/native/cloudsync_kit_inner/include/cloud_sync_manager_impl.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2022 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_CLOUD_SYNC_MANAGER_IMPL_H +#define OHOS_FILEMGMT_CLOUD_SYNC_MANAGER_IMPL_H + +#include + +#include "cloud_sync_manager.h" +#include "svc_death_recipient.h" + +namespace OHOS::FileManagement::CloudSync { +class CloudSyncManagerImpl final : public CloudSyncManager { +public: + static CloudSyncManagerImpl &GetInstance(); + + int32_t StartSync(SyncType type, bool forceFlag, const std::shared_ptr callback) override; + int32_t StopSync() override; + +private: + void SetDeathRecipient(const sptr &remoteObject); + + std::atomic isFirstCall{false}; + sptr deathRecipient_; +}; +} // namespace OHOS::FileManagement::CloudSync + +#endif // OHOS_FILEMGMT_CLOUD_SYNC_MANAGER_IMPL_H \ No newline at end of file diff --git a/frameworks/native/cloudsync_kit_inner/include/cloud_sync_service_proxy.h b/frameworks/native/cloudsync_kit_inner/include/cloud_sync_service_proxy.h new file mode 100644 index 0000000000000000000000000000000000000000..b0fcc869770b040a6c2e04da54cd50de22c98f15 --- /dev/null +++ b/frameworks/native/cloudsync_kit_inner/include/cloud_sync_service_proxy.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2022 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_CLOUD_SYNC_SERVICE_PROXY_H +#define OHOS_FILEMGMT_CLOUD_SYNC_SERVICE_PROXY_H + +#include "i_cloud_sync_service.h" +#include "iremote_proxy.h" +#include "system_ability_load_callback_stub.h" + +namespace OHOS::FileManagement::CloudSync { +class CloudSyncServiceProxy : public IRemoteProxy { +public: + explicit CloudSyncServiceProxy(const sptr &impl) : IRemoteProxy(impl) {} + ~CloudSyncServiceProxy() override {} + + static sptr GetInstance(); + + int32_t RegisterCallbackInner(const std::string &appPackageName, const sptr &remoteObject) override; + int32_t StartSyncInner(const std::string &appPackageName, SyncType type, bool forceFlag) override; + int32_t StopSyncInner(const std::string &appPackageName) override; + + class ServiceProxyLoadCallback : public SystemAbilityLoadCallbackStub { + public: + void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr &remoteObject) override; + void OnLoadSystemAbilityFail(int32_t systemAbilityId) override; + + std::condition_variable proxyConVar_; + std::atomic isLoadSuccess_{false}; + }; + +private: + static inline std::mutex proxyMutex_; + static inline sptr serviceProxy_; + static inline BrokerDelegator delegator_; +}; +} // namespace OHOS::FileManagement::CloudSync + +#endif // OHOS_FILEMGMT_CLOUD_SYNC_SERVICE_PROXY_H \ No newline at end of file diff --git a/frameworks/native/cloudsync_kit_inner/src/cloud_sync_callback_client.cpp b/frameworks/native/cloudsync_kit_inner/src/cloud_sync_callback_client.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3563ba94ac9390963f5368b0856d5c7d01fcf502 --- /dev/null +++ b/frameworks/native/cloudsync_kit_inner/src/cloud_sync_callback_client.cpp @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2022 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 "cloud_sync_callback_client.h" +#include "utils_log.h" + +namespace OHOS::FileManagement::CloudSync { +using namespace std; + +void CloudSyncCallbackClient::OnSyncStateChanged(SyncType type, SyncPromptState state) +{ + if (!callback_) { + LOGE("callback_ is null, maybe not registered"); + return; + } + callback_->OnSyncStateChanged(type, state); +} + +} // namespace OHOS::FileManagement::CloudSync \ No newline at end of file diff --git a/frameworks/native/cloudsync_kit_inner/src/cloud_sync_callback_stub.cpp b/frameworks/native/cloudsync_kit_inner/src/cloud_sync_callback_stub.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2301aca731588aa3fc84e51ff2e0fa6b30fbba10 --- /dev/null +++ b/frameworks/native/cloudsync_kit_inner/src/cloud_sync_callback_stub.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2022 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 "cloud_sync_callback_stub.h" +#include "dfs_error.h" +#include "utils_log.h" + +namespace OHOS::FileManagement::CloudSync { +using namespace std; + +CloudSyncCallbackStub::CloudSyncCallbackStub() +{ + opToInterfaceMap_[SERVICE_CMD_ON_SYNC_STATE_CHANGED] = &CloudSyncCallbackStub::HandleOnSyncStateChanged; +} + +int32_t CloudSyncCallbackStub::OnRemoteRequest(uint32_t code, + MessageParcel &data, + MessageParcel &reply, + MessageOption &option) +{ + if (data.ReadInterfaceToken() != GetDescriptor()) { + return E_SERVICE_DESCRIPTOR_IS_EMPTY; + } + auto interfaceIndex = opToInterfaceMap_.find(code); + if (interfaceIndex == opToInterfaceMap_.end() || !interfaceIndex->second) { + LOGE("Cannot response request %d: unknown tranction", code); + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } + return (this->*(interfaceIndex->second))(data, reply); +} + +int32_t CloudSyncCallbackStub::HandleOnSyncStateChanged(MessageParcel &data, MessageParcel &reply) +{ + LOGI("start"); + SyncType type = SyncType(data.ReadInt32()); + SyncPromptState state = SyncPromptState(data.ReadInt32()); + OnSyncStateChanged(type, state); + LOGI("end"); + return E_OK; +} +} // namespace OHOS::FileManagement::CloudSync \ No newline at end of file diff --git a/frameworks/native/cloudsync_kit_inner/src/cloud_sync_manager.cpp b/frameworks/native/cloudsync_kit_inner/src/cloud_sync_manager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8c29106f49edbf29f8cc457a2017db356aedd5bc --- /dev/null +++ b/frameworks/native/cloudsync_kit_inner/src/cloud_sync_manager.cpp @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2022 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 "cloud_sync_manager_impl.h" + +namespace OHOS::FileManagement::CloudSync { +using namespace std; +CloudSyncManager &CloudSyncManager::GetInstance() +{ + return CloudSyncManagerImpl::GetInstance(); +} +} // namespace OHOS::FileManagement::CloudSync \ No newline at end of file diff --git a/frameworks/native/cloudsync_kit_inner/src/cloud_sync_manager_impl.cpp b/frameworks/native/cloudsync_kit_inner/src/cloud_sync_manager_impl.cpp new file mode 100644 index 0000000000000000000000000000000000000000..679ba03afdaeb2a954641f3f51538dd570f94c64 --- /dev/null +++ b/frameworks/native/cloudsync_kit_inner/src/cloud_sync_manager_impl.cpp @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2022 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 "cloud_sync_manager_impl.h" +#include "cloud_sync_callback_client.h" +#include "cloud_sync_service_proxy.h" +#include "dfs_error.h" +#include "utils_log.h" + +namespace OHOS::FileManagement::CloudSync { +using namespace std; +CloudSyncManagerImpl &CloudSyncManagerImpl::GetInstance() +{ + static CloudSyncManagerImpl instance; + return instance; +} + +int32_t CloudSyncManagerImpl::StartSync(SyncType type, bool forceFlag, const std::shared_ptr callback) +{ + if (!callback) { + LOGE("callback is null"); + return E_INVAL_ARG; + } + auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance(); + if (!CloudSyncServiceProxy) { + LOGE("proxy is null"); + return E_SA_LOAD_FAILED; + } + std::string appPackageName = "com.ohos.photos"; + if (!isFirstCall) { + LOGI("Register callback"); + auto ret = + CloudSyncServiceProxy->RegisterCallbackInner(appPackageName, sptr(new (std::nothrow)CloudSyncCallbackClient(callback))); + if (ret) { + LOGE("Register callback failed"); + return ret; + } + SetDeathRecipient(CloudSyncServiceProxy->AsObject()); + isFirstCall = true; + } + + return CloudSyncServiceProxy->StartSyncInner(appPackageName, type, forceFlag); +} + +int32_t CloudSyncManagerImpl::StopSync() +{ + auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance(); + if (!CloudSyncServiceProxy) { + LOGE("proxy is null"); + return E_SA_LOAD_FAILED; + } + std::string appPackageName = "com.ohos.photos"; + return CloudSyncServiceProxy->StopSyncInner(appPackageName); +} + +void CloudSyncManagerImpl::SetDeathRecipient(const sptr &remoteObject) +{ + auto deathCallback = [&](const wptr &obj) { + LOGI("service died. Died remote obj = %{private}p", obj.GetRefPtr()); + isFirstCall = false; + }; + deathRecipient_ = sptr(new SvcDeathRecipient(deathCallback)); + remoteObject->AddDeathRecipient(deathRecipient_); +} +} // namespace OHOS::FileManagement::CloudSync \ No newline at end of file diff --git a/frameworks/native/cloudsync_kit_inner/src/cloud_sync_service_proxy.cpp b/frameworks/native/cloudsync_kit_inner/src/cloud_sync_service_proxy.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f8d65f23eb0cc35d9b336e459fcfaa521f9b8661 --- /dev/null +++ b/frameworks/native/cloudsync_kit_inner/src/cloud_sync_service_proxy.cpp @@ -0,0 +1,181 @@ +/* + * Copyright (c) 2022 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 "cloud_sync_service_proxy.h" + +#include + +#include "dfs_error.h" +#include "iservice_registry.h" +#include "system_ability_definition.h" +#include "utils_log.h" + +namespace OHOS::FileManagement::CloudSync { +using namespace std; + +constexpr int LOADSA_TIMEOUT_MS = 4000; + +int32_t CloudSyncServiceProxy::RegisterCallbackInner(const string &appPackageName, + const sptr &remoteObject) +{ + LOGI("Start RegisterCallbackInner"); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!remoteObject) { + LOGI("Empty callback stub"); + return E_INVAL_ARG; + } + data.WriteInterfaceToken(GetDescriptor()); + + if (!data.WriteRemoteObject(remoteObject)) { + LOGE("Failed to send the callback stub"); + return E_INVAL_ARG; + } + + if (!data.WriteString(appPackageName)) { + LOGE("Failed to send the appPackageName"); + return E_INVAL_ARG; + } + + int32_t ret = Remote()->SendRequest(ICloudSyncService::SERVICE_CMD_REGISTER_CALLBACK, data, reply, option); + if (ret != E_OK) { + stringstream ss; + ss << "Failed to send out the requeset, errno:" << ret; + LOGE("%{public}s, appPackageName:%{private}s", ss.str().c_str(), appPackageName.c_str()); + return E_BROKEN_IPC; + } + LOGI("RegisterCallbackInner Success"); + return reply.ReadInt32(); +} + +int32_t CloudSyncServiceProxy::StartSyncInner(const std::string &appPackageName, SyncType type, bool forceFlag) +{ + LOGI("Start Sync"); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + data.WriteInterfaceToken(GetDescriptor()); + + if (!data.WriteString(appPackageName)) { + LOGE("Failed to send the appPackageName"); + return E_INVAL_ARG; + } + + if (!data.WriteInt32(static_cast(type))) { + LOGE("Failed to send the sync type"); + return E_INVAL_ARG; + } + + if (!data.WriteBool(forceFlag)) { + LOGE("Failed to send the force flag"); + return E_INVAL_ARG; + } + + int32_t ret = Remote()->SendRequest(ICloudSyncService::SERVICE_CMD_START_SYNC, data, reply, option); + if (ret != E_OK) { + stringstream ss; + ss << "Failed to send out the requeset, errno:" << ret; + LOGE("appPackageName:%{private}s", appPackageName.c_str()); + return E_BROKEN_IPC; + } + LOGI("StartSyncInner Success"); + return reply.ReadInt32(); +} + +int32_t CloudSyncServiceProxy::StopSyncInner(const std::string &appPackageName) +{ + LOGI("StopSync"); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + data.WriteInterfaceToken(GetDescriptor()); + + if (!data.WriteString(appPackageName)) { + LOGE("Failed to send the appPackageName"); + return E_INVAL_ARG; + } + + int32_t ret = Remote()->SendRequest(ICloudSyncService::SERVICE_CMD_STOP_SYNC, data, reply, option); + if (ret != E_OK) { + stringstream ss; + ss << "Failed to send out the requeset, errno:" << ret; + LOGE("appPackageName:%{private}s", appPackageName.c_str()); + return E_BROKEN_IPC; + } + LOGI("StopSyncInner Success"); + return reply.ReadInt32(); +} + +sptr CloudSyncServiceProxy::GetInstance() +{ + LOGE("getinstance"); + unique_lock lock(proxyMutex_); + if (serviceProxy_ != nullptr) { + return serviceProxy_; + } + + auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgr == nullptr) { + LOGE("Samgr is nullptr"); + return nullptr; + } + sptr cloudSyncLoadCallback = new ServiceProxyLoadCallback(); + if (cloudSyncLoadCallback == nullptr) { + LOGE("cloudSyncLoadCallback is nullptr"); + return nullptr; + } + int32_t ret = samgr->LoadSystemAbility(FILEMANAGEMENT_CLOUD_SYNC_SERVICE_SA_ID, cloudSyncLoadCallback); + if (ret != E_OK) { + LOGE("Failed to Load systemAbility, systemAbilityId:%{pulbic}d, ret code:%{pulbic}d", + FILEMANAGEMENT_CLOUD_SYNC_SERVICE_SA_ID, ret); + return nullptr; + } + + auto waitStatus = cloudSyncLoadCallback->proxyConVar_.wait_for( + lock, std::chrono::milliseconds(LOADSA_TIMEOUT_MS), + [cloudSyncLoadCallback]() { return cloudSyncLoadCallback->isLoadSuccess_.load(); }); + + if (!waitStatus) { + LOGE("Load CloudSynd SA timeout"); + return nullptr; + } + return serviceProxy_; +} + +void CloudSyncServiceProxy::ServiceProxyLoadCallback::OnLoadSystemAbilitySuccess( + int32_t systemAbilityId, + const sptr &remoteObject) +{ + LOGI("Load CloudSync SA success,systemAbilityId:%{public}d, remoteObj result:%{private}s", systemAbilityId, + (remoteObject == nullptr ? "false" : "true")); + unique_lock lock(proxyMutex_); + serviceProxy_ = iface_cast(remoteObject); + isLoadSuccess_.store(true); + proxyConVar_.notify_one(); +} + +void CloudSyncServiceProxy::ServiceProxyLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId) +{ + LOGI("Load CloudSync SA failed,systemAbilityId:%{public}d", systemAbilityId); + unique_lock lock(proxyMutex_); + serviceProxy_ = nullptr; + isLoadSuccess_.store(false); + proxyConVar_.notify_one(); +} + +} // namespace OHOS::FileManagement::CloudSync \ No newline at end of file diff --git a/interfaces/inner_api/native/cloudsync_kit_inner/BUILD.gn b/interfaces/inner_api/native/cloudsync_kit_inner/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..994cd116293161b9acfd7eadbf2c044128c81089 --- /dev/null +++ b/interfaces/inner_api/native/cloudsync_kit_inner/BUILD.gn @@ -0,0 +1,60 @@ +# Copyright (C) 2022 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. + +import("//build/ohos.gni") +import("//foundation/filemanagement/dfs_service/distributedfile.gni") + +config("public_config") { + include_dirs = [ + ".", + ] +} + +config("private_config") { + include_dirs = [ + "${distributedfile_path}/frameworks/native/cloudsync_kit_inner/include", + "${distributedfile_path}/interfaces/inner_api/native/cloudsync_kit_inner", + ] +} + +ohos_shared_library("cloudsync_kit_inner") { + sources = [ + "${distributedfile_path}/frameworks/native/cloudsync_kit_inner/src/cloud_sync_callback_client.cpp", + "${distributedfile_path}/frameworks/native/cloudsync_kit_inner/src/cloud_sync_callback_stub.cpp", + "${distributedfile_path}/frameworks/native/cloudsync_kit_inner/src/cloud_sync_service_proxy.cpp", + "${distributedfile_path}/frameworks/native/cloudsync_kit_inner/src/cloud_sync_manager_impl.cpp", + "${distributedfile_path}/frameworks/native/cloudsync_kit_inner/src/cloud_sync_manager.cpp", + ] + + defines = [ + "LOG_DOMAIN=0xD004306", + "LOG_TAG=\"CLOUDSYNC_API\"", + ] + + configs = [ ":private_config" ] + public_configs = [ ":public_config" ] + + external_deps = [ + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] + + public_deps = [ + "${utils_path}:libdistributedfileutils", + ] + + use_exceptions = true + part_name = "dfs_service" + subsystem_name = "filemanagement" +} \ No newline at end of file diff --git a/interfaces/inner_api/native/cloudsync_kit_inner/cloud_sync_callback.h b/interfaces/inner_api/native/cloudsync_kit_inner/cloud_sync_callback.h new file mode 100644 index 0000000000000000000000000000000000000000..fb0132356e34cfd23c435ede0d8b0b288ae8a162 --- /dev/null +++ b/interfaces/inner_api/native/cloudsync_kit_inner/cloud_sync_callback.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2022 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_CLOUD_SYNC_CALLBACK_H +#define OHOS_FILEMGMT_CLOUD_SYNC_CALLBACK_H + +#include + +#include "cloud_sync_constants.h" + +namespace OHOS::FileManagement::CloudSync { +class CloudSyncCallback { +public: + virtual ~CloudSyncCallback() = default; + /** + * @brief 同步状态变更通知回调 + * + * @param type 0:upload 1:download 2:all + * @param state 同步状态 + * SYNC_STATE_DEFAULT 同步完成 + * SYNC_STATE_SYNCING 同步中 + * SYNC_STATE_FAILED 同步失败 + * SYNC_STATE_PAUSED_NO_NETWORK 未连接网络 + * SYNC_STATE_PAUSED_FOR_WIFI 未连接wifi + * SYNC_STATE_PAUSED_FOR_BATTERY 低电量 + * SYNC_STATE_BATTERY_TOO_LOW 电量过低 + * SYNC_STATE_PAUSED_FOR_SPACE_TOO_LOW 云空间满 + */ + virtual void OnSyncStateChanged(SyncType type, SyncPromptState state) = 0; +}; +} // namespace OHOS::FileManagement::CloudSync + +#endif // OHOS_FILEMGMT_CLOUD_SYNC_CALLBACK_H \ No newline at end of file diff --git a/interfaces/inner_api/native/cloudsync_kit_inner/cloud_sync_constants.h b/interfaces/inner_api/native/cloudsync_kit_inner/cloud_sync_constants.h new file mode 100644 index 0000000000000000000000000000000000000000..000d674516db6b9a8dd8275c60f098b2af1b2f8b --- /dev/null +++ b/interfaces/inner_api/native/cloudsync_kit_inner/cloud_sync_constants.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2022 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_CLOUD_SYNC_CONSTANTS_H +#define OHOS_FILEMGMT_CLOUD_SYNC_CONSTANTS_H + +namespace OHOS::FileManagement::CloudSync { +enum class SyncType : int32_t { + UPLOAD = 0, + DOWNLOAD, + ALL, +}; + +enum class SyncPromptState : int32_t { + SYNC_STATE_DEFAULT = 0, + SYNC_STATE_SYNCING, + SYNC_STATE_FAILED, + SYNC_STATE_PAUSED_NO_NETWORK, + SYNC_STATE_PAUSED_FOR_WIFI, + SYNC_STATE_PAUSED_FOR_BATTERY, + SYNC_STATE_BATTERY_TOO_LOW, + SYNC_STATE_PAUSED_FOR_SPACE_TOO_LOW, +}; + +} // namespace OHOS::FileManagement::CloudSync + +#endif // OHOS_FILEMGMT_CLOUD_SYNC_CONSTANTS_H \ No newline at end of file diff --git a/interfaces/inner_api/native/cloudsync_kit_inner/cloud_sync_manager.h b/interfaces/inner_api/native/cloudsync_kit_inner/cloud_sync_manager.h new file mode 100644 index 0000000000000000000000000000000000000000..2cd9bc8302d45d00a272394af82de851dc3baf4a --- /dev/null +++ b/interfaces/inner_api/native/cloudsync_kit_inner/cloud_sync_manager.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2022 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_CLOUD_SYNC_MANAGER_H +#define OHOS_FILEMGMT_CLOUD_SYNC_MANAGER_H + +#include + +#include "cloud_sync_callback.h" + +namespace OHOS::FileManagement::CloudSync { +class CloudSyncManager { +public: + static CloudSyncManager &GetInstance(); + /** + * @brief 启动同步 + * + * @param type upload:仅上行 download:仅下行 all:先下行,再上行 + * @param forceFlag 是否强制继续同步,当前仅支持低电量下暂停后继续上传 + * @param callback 注册同步回调 + * @return int32_t 同步返回执行结果 + */ + virtual int32_t StartSync(SyncType type, bool forceFlag, const std::shared_ptr callback) = 0; + /** + * @brief 停止同步 + * + * @return int32_t 同步返回执行结果 + */ + virtual int32_t StopSync() = 0; +}; +} // namespace OHOS::FileManagement::CloudSync + +#endif // OHOS_FILEMGMT_CLOUD_SYNC_MANAGER_H \ No newline at end of file diff --git a/interfaces/inner_api/native/cloudsync_kit_inner/i_cloud_sync_callback.h b/interfaces/inner_api/native/cloudsync_kit_inner/i_cloud_sync_callback.h new file mode 100644 index 0000000000000000000000000000000000000000..b6d987366f99f55428035b5b9f921cf945068926 --- /dev/null +++ b/interfaces/inner_api/native/cloudsync_kit_inner/i_cloud_sync_callback.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2022 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_I_CLOUD_SYNC_CALLBACK_H +#define OHOS_FILEMGMT_I_CLOUD_SYNC_CALLBACK_H + +#include "cloud_sync_callback.h" +#include "iremote_broker.h" + +namespace OHOS::FileManagement::CloudSync { +class ICloudSyncCallback : public CloudSyncCallback, public IRemoteBroker { +public: + enum { + SERVICE_CMD_ON_SYNC_STATE_CHANGED = 0, + }; + + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.Filemanagement.Dfs.ICloudSyncCallback") +}; +} // namespace OHOS::FileManagement::CloudSync + +#endif // OHOS_FILEMGMT_I_CLOUD_SYNC_CALLBACK_H \ No newline at end of file diff --git a/interfaces/inner_api/native/cloudsync_kit_inner/i_cloud_sync_service.h b/interfaces/inner_api/native/cloudsync_kit_inner/i_cloud_sync_service.h new file mode 100644 index 0000000000000000000000000000000000000000..1855a95e05d2c2d4d15b32bd9cd8f92b0bc5c085 --- /dev/null +++ b/interfaces/inner_api/native/cloudsync_kit_inner/i_cloud_sync_service.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2022 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_I_CLOUD_SYNC_SERVICE_H +#define OHOS_FILEMGMT_I_CLOUD_SYNC_SERVICE_H + +#include "iremote_broker.h" +#include "cloud_sync_constants.h" + +namespace OHOS::FileManagement::CloudSync { +class ICloudSyncService : public IRemoteBroker { +public: + enum { + SERVICE_CMD_REGISTER_CALLBACK = 0, + SERVICE_CMD_START_SYNC, + SERVICE_CMD_STOP_SYNC, + }; + + enum { + CLOUD_SYNC_SERVICE_SUCCESS = 0, + CLOUD_SYNC_SERVICE_DESCRIPTOR_IS_EMPTY, + }; + + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.Filemanagement.Dfs.ICloudSyncService") + + virtual int32_t RegisterCallbackInner(const std::string &appPackageName, + const sptr &remoteObject) = 0; + virtual int32_t StartSyncInner(const std::string &appPackageName, SyncType type, bool forceFlag) = 0; + virtual int32_t StopSyncInner(const std::string &appPackageName) = 0; +}; +} // namespace OHOS::FileManagement::CloudSync + +#endif // OHOS_FILEMGMT_I_CLOUD_SYNC_SERVICE_H \ No newline at end of file diff --git a/interfaces/inner_api/native/cloudsync_kit_inner/svc_death_recipient.h b/interfaces/inner_api/native/cloudsync_kit_inner/svc_death_recipient.h new file mode 100644 index 0000000000000000000000000000000000000000..8578d023e78c2452459e4d2e9e5dbb7115d16670 --- /dev/null +++ b/interfaces/inner_api/native/cloudsync_kit_inner/svc_death_recipient.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2022 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_SVC_DEATH_RECIPIENT_H +#define OHOS_FILEMGMT_SVC_DEATH_RECIPIENT_H + +#include + +#include "iremote_object.h" + +namespace OHOS::FileManagement::CloudSync { +class SvcDeathRecipient : public IRemoteObject::DeathRecipient { +public: + explicit SvcDeathRecipient(std::function &)> functor) : functor_(functor){}; + void OnRemoteDied(const wptr &object) override + { + object->RemoveDeathRecipient(this); + functor_(object); + } + +private: + std::function &)> functor_; +}; +} // namespace OHOS::FileManagement::CloudSync + +#endif // OHOS_FILEMGMT_SVC_DEATH_RECIPIENT_H \ No newline at end of file diff --git a/services/5204.xml b/services/5204.xml new file mode 100644 index 0000000000000000000000000000000000000000..212cf74d32708e457955a443146394ca30c5809e --- /dev/null +++ b/services/5204.xml @@ -0,0 +1,30 @@ + + + + distributedfiledaemon + + libcloudsync_sa.z.so + + + 5204 + libcloudsync_sa.z.so + + + false + false + 1 + + diff --git a/services/BUILD.gn b/services/BUILD.gn index f601645d632b38519de7160659af7adbb62fe4e0..6889159f1c0bd36194a7c9d456729ce45c6fa690 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -15,7 +15,10 @@ import("//build/ohos/sa_profile/sa_profile.gni") import("//foundation/filemanagement/dfs_service/distributedfile.gni") ohos_sa_profile("distributedfile_sa_profile") { - sources = [ "5201.xml" ] + sources = [ + "5201.xml", + "5204.xml" + ] part_name = "dfs_service" } diff --git a/services/cloudsyncservice/BUILD.gn b/services/cloudsyncservice/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..3bb1204cb8ea980e1293865b1842b8ca59b1153f --- /dev/null +++ b/services/cloudsyncservice/BUILD.gn @@ -0,0 +1,51 @@ +# Copyright (C) 2022 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. + +import("//build/ohos.gni") +import("//foundation/filemanagement/dfs_service/distributedfile.gni") + +ohos_shared_library("cloudsync_sa") { + sources = [ + "src/ipc/cloud_sync_service_stub.cpp", + "src/ipc/cloud_sync_service.cpp", + "src/ipc/cloud_sync_callback_proxy.cpp", + "src/ipc/cloud_sync_callback_manager.cpp", + #"src/datasync/gallery/gallery_data_syncer.cpp", + #"src/datasync/data_sync_manager.cpp", + #"src/datasync/data_syncer.cpp" + ] + + defines = [ + "LOG_DOMAIN=0xD004307", + "LOG_TAG=\"CLOUDSYNC_SA\"", + ] + + include_dirs = [ + "include", + ] + + deps = [ + "${utils_path}:libdistributedfileutils", + ] + + external_deps = [ + "ipc:ipc_core", + "safwk:system_ability_fwk", + "dfs_service:cloudsync_kit_inner", + "samgr:samgr_proxy", + ] + + use_exceptions = true + part_name = "dfs_service" + subsystem_name = "filemanagement" +} \ No newline at end of file diff --git a/services/cloudsyncservice/include/ipc/cloud_sync_callback_manager.h b/services/cloudsyncservice/include/ipc/cloud_sync_callback_manager.h new file mode 100644 index 0000000000000000000000000000000000000000..ca0831a0053db2f3a6904c0a0b6bc2618c65d0d3 --- /dev/null +++ b/services/cloudsyncservice/include/ipc/cloud_sync_callback_manager.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2022 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_CLOUD_SYNC_CALLBACK_MANAGER_H +#define OHOS_FILEMGMT_CLOUD_SYNC_CALLBACK_MANAGER_H + +#include "i_cloud_sync_callback.h" +#include "safe_map.h" +#include "svc_death_recipient.h" + +namespace OHOS::FileManagement::CloudSync { +class CloudSyncCallbackManager : public RefBase { +public: + struct CallbackInfo { + sptr callbackProxy_; + sptr deathRecipient_; + uint32_t token_; + }; + + void AddCallback(const std::string &appPackageName, const sptr &callback); + sptr GetCallbackProxy(const std::string &appPackageName); + +private: + void SetDeathRecipient(const std::string &appPackageName, CallbackInfo &cbInfo); + + SafeMap callbackListMap_; +}; +} // namespace OHOS::FileManagement::CloudSync + +#endif // OHOS_FILEMGMT_CLOUD_SYNC_CALLBACK_MANAGER_H \ No newline at end of file diff --git a/services/cloudsyncservice/include/ipc/cloud_sync_callback_proxy.h b/services/cloudsyncservice/include/ipc/cloud_sync_callback_proxy.h new file mode 100644 index 0000000000000000000000000000000000000000..8623056724f15794e6e64cf5b680406795a01f4d --- /dev/null +++ b/services/cloudsyncservice/include/ipc/cloud_sync_callback_proxy.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2022 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_CLOUD_SYNC_CALLBACK_PROXY_H +#define OHOS_FILEMGMT_CLOUD_SYNC_CALLBACK_PROXY_H + +#include "i_cloud_sync_callback.h" + +#include "iremote_proxy.h" + +namespace OHOS::FileManagement::CloudSync { +class CloudSyncCallbackProxy : public IRemoteProxy { +public: + explicit CloudSyncCallbackProxy(const sptr &impl) : IRemoteProxy(impl) {} + ~CloudSyncCallbackProxy() override {} + + void OnSyncStateChanged(SyncType type, SyncPromptState state) override; +private: + static inline BrokerDelegator delegator_; +}; +} // namespace OHOS::FileManagement::CloudSync + +#endif // OHOS_FILEMGMT_CLOUD_SYNC_CALLBACK_PROXY_H \ No newline at end of file diff --git a/services/cloudsyncservice/include/ipc/cloud_sync_service.h b/services/cloudsyncservice/include/ipc/cloud_sync_service.h new file mode 100644 index 0000000000000000000000000000000000000000..5bb4ec0344d458ef608dc88875ba59d6c3c86ae8 --- /dev/null +++ b/services/cloudsyncservice/include/ipc/cloud_sync_service.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2022 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_CLOUD_SYNC_SERVICE_H +#define OHOS_FILEMGMT_CLOUD_SYNC_SERVICE_H + +#include + +#include "cloud_sync_callback_manager.h" +#include "cloud_sync_service_stub.h" +#include "i_cloud_sync_callback.h" +#include "nocopyable.h" +#include "system_ability.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) {} + virtual ~CloudSyncService() = default; + + int32_t RegisterCallbackInner(const std::string &appPackageName, const sptr &remoteObject) override; + int32_t StartSyncInner(const std::string &appPackageName, SyncType type, bool forceFlag) override; + int32_t StopSyncInner(const std::string &appPackageName) override; + +private: + void OnStart() override; + void OnStop() override; + void PublishSA(); + + CloudSyncCallbackManager callbackManager_; +}; +} // namespace OHOS::FileManagement::CloudSync + +#endif // OHOS_FILEMGMT_CLOUD_SYNC_SERVICE_H \ No newline at end of file diff --git a/services/cloudsyncservice/include/ipc/cloud_sync_service_stub.h b/services/cloudsyncservice/include/ipc/cloud_sync_service_stub.h new file mode 100644 index 0000000000000000000000000000000000000000..dfbf2726842fef694bc2410f8eaaeba981e2df0c --- /dev/null +++ b/services/cloudsyncservice/include/ipc/cloud_sync_service_stub.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2022 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_CLOUD_SYNC_SERVICE_STUB_H +#define OHOS_FILEMGMT_CLOUD_SYNC_SERVICE_STUB_H + +#include + +#include "i_cloud_sync_service.h" +#include "iremote_stub.h" + +namespace OHOS::FileManagement::CloudSync { +class CloudSyncServiceStub : public IRemoteStub { +public: + CloudSyncServiceStub(); + virtual ~CloudSyncServiceStub() = default; + int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; + +private: + using ServiceInterface = int32_t (CloudSyncServiceStub::*)(MessageParcel &data, MessageParcel &reply); + std::map opToInterfaceMap_; + + int32_t HandleRegisterCallbackInner(MessageParcel &data, MessageParcel &reply); + int32_t HandleStartSyncInner(MessageParcel &data, MessageParcel &reply); + int32_t HandleStopSyncInner(MessageParcel &data, MessageParcel &reply); +}; +} // namespace OHOS::FileManagement::CloudSync + +#endif // OHOS_FILEMGMT_CLOUD_SYNC_SERVICE_STUB_H \ 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 new file mode 100644 index 0000000000000000000000000000000000000000..b67500206a9fecddf216688c7ea3b6192a6ca914 --- /dev/null +++ b/services/cloudsyncservice/src/ipc/cloud_sync_callback_manager.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2022 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 "ipc/cloud_sync_callback_manager.h" +#include "utils_log.h" + +namespace OHOS::FileManagement::CloudSync { +using namespace std; + +void CloudSyncCallbackManager::AddCallback(const std::string &appPackageName, const sptr &callback) +{ + CallbackInfo callbackInfo; + callbackInfo.callbackProxy_ = callback; + SetDeathRecipient(appPackageName, callbackInfo); + /*Delete and then insert when the key exists, ensuring that the final value is inserted.*/ + callbackListMap_.EnsureInsert(appPackageName, callbackInfo); +} + +void CloudSyncCallbackManager::SetDeathRecipient(const std::string &appPackageName, CallbackInfo &cbInfo) +{ + auto remoteObject = cbInfo.callbackProxy_->AsObject(); + if (!remoteObject) { + LOGE("Remote object can't be nullptr"); + return; // throw + } + + auto deathCb = [this, packageName{appPackageName}](const wptr &obj) { + callbackListMap_.Erase(packageName); + LOGI("client died, Died remote obj:%{private}p, map size:%{public}d, packageName:%{private}s", obj.GetRefPtr(), + callbackListMap_.Size(), packageName.c_str()); + }; + cbInfo.deathRecipient_ = sptr(new SvcDeathRecipient(deathCb)); + remoteObject->AddDeathRecipient(cbInfo.deathRecipient_); +} + +sptr CloudSyncCallbackManager::GetCallbackProxy(const std::string &appPackageName) +{ + CallbackInfo cbInfo; + if (callbackListMap_.Find(appPackageName, cbInfo)) { + return cbInfo.callbackProxy_; + } + + LOGE("not found callback, appPackageName: %{public}s", appPackageName.c_str()); + return nullptr; +} + +} // namespace OHOS::FileManagement::CloudSync \ No newline at end of file diff --git a/services/cloudsyncservice/src/ipc/cloud_sync_callback_proxy.cpp b/services/cloudsyncservice/src/ipc/cloud_sync_callback_proxy.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6cd0d094d6aa85a8d0b992069a4395990c9e5de1 --- /dev/null +++ b/services/cloudsyncservice/src/ipc/cloud_sync_callback_proxy.cpp @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2022 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 "ipc/cloud_sync_callback_proxy.h" + +#include + +#include "dfs_error.h" +#include "utils_log.h" + +namespace OHOS::FileManagement::CloudSync { +using namespace std; + +void CloudSyncCallbackProxy::OnSyncStateChanged(SyncType type, SyncPromptState state) +{ + LOGI("Start"); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + data.WriteInterfaceToken(GetDescriptor()); + + if (!data.WriteInt32(static_cast(type))) { + LOGE("Failed to send the type"); + return; + } + + if (!data.WriteInt32(static_cast(state))) { + LOGE("Failed to send the state"); + return; + } + + int32_t ret = Remote()->SendRequest(ICloudSyncCallback::SERVICE_CMD_ON_SYNC_STATE_CHANGED, data, reply, option); + if (ret != E_OK) { + stringstream ss; + ss << "Failed to send out the requeset, errno:" << ret; + LOGE("%{public}s", ss.str().c_str()); + return; + } + LOGI("End"); + return; +} + +} // namespace OHOS::FileManagement::CloudSync \ No newline at end of file diff --git a/services/cloudsyncservice/src/ipc/cloud_sync_service.cpp b/services/cloudsyncservice/src/ipc/cloud_sync_service.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e6fbdbff959efba2872ff8028dada0acd5947020 --- /dev/null +++ b/services/cloudsyncservice/src/ipc/cloud_sync_service.cpp @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2022 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 "ipc/cloud_sync_service.h" + +// #include "iremote_object.h" +#include "dfs_error.h" +#include "system_ability_definition.h" +#include "utils_log.h" + +namespace OHOS::FileManagement::CloudSync { +using namespace std; +using namespace OHOS; + +REGISTER_SYSTEM_ABILITY_BY_ID(CloudSyncService, FILEMANAGEMENT_CLOUD_SYNC_SERVICE_SA_ID, false); + +void CloudSyncService::PublishSA() +{ + LOGI("Begin to init"); + bool ret = SystemAbility::Publish(this); + if (!ret) { + throw runtime_error(" Failed to publish the daemon"); + } + LOGI("Init finished successfully"); +} + +void CloudSyncService::OnStart() +{ + LOGI("Begin to start service"); + try { + PublishSA(); + } catch (const exception &e) { + LOGE("%{public}s", e.what()); + } + LOGI("Start service successfully"); +} + +void CloudSyncService::OnStop() +{ + LOGI("Stop finished successfully"); +} + +int32_t CloudSyncService::RegisterCallbackInner(const string &appPackageName, const sptr &remoteObject) +{ + if (remoteObject == nullptr) { + LOGE("callback is nullptr"); + return E_INVAL_ARG; + } + + if (appPackageName.empty()) { + LOGE("appPackageName is invalid"); + return E_INVAL_ARG; + } + + auto callback = iface_cast(remoteObject); + if (!remoteObject) { + LOGE("remoteObject is nullptr"); + return E_INVAL_ARG; + } + + callbackManager_.AddCallback(appPackageName, callback); + return E_OK; +} + +int32_t CloudSyncService::StartSyncInner(const std::string &appPackageName, SyncType type, bool forceFlag) +{ + auto callbackProxy_ = callbackManager_.GetCallbackProxy(appPackageName); + if (!callbackProxy_) { + LOGE("not found object, appPackageName = %{private}s", appPackageName.c_str()); + return E_INVAL_ARG; + } + SyncPromptState state = SyncPromptState::SYNC_STATE_DEFAULT; + callbackProxy_->OnSyncStateChanged(SyncType::ALL, state); + return E_OK; +} + +int32_t CloudSyncService::StopSyncInner(const std::string &appPackageName) +{ + auto callbackProxy_ = callbackManager_.GetCallbackProxy(appPackageName); + if (!callbackProxy_) { + LOGE("not found object, appPackageName = %{private}s", appPackageName.c_str()); + return E_INVAL_ARG; + } + SyncPromptState state = SyncPromptState::SYNC_STATE_DEFAULT; + callbackProxy_->OnSyncStateChanged(SyncType::ALL, state); + return E_OK; +} +} // namespace OHOS::FileManagement::CloudSync \ No newline at end of file diff --git a/services/cloudsyncservice/src/ipc/cloud_sync_service_stub.cpp b/services/cloudsyncservice/src/ipc/cloud_sync_service_stub.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a4b55bfbcbb8fa6431cc8ee3934688080cebd1e0 --- /dev/null +++ b/services/cloudsyncservice/src/ipc/cloud_sync_service_stub.cpp @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2022 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 "ipc/cloud_sync_service_stub.h" +#include "dfs_error.h" +#include "dfsu_permission_checker.h" +#include "utils_log.h" + +namespace OHOS::FileManagement::CloudSync { +using namespace std; + +CloudSyncServiceStub::CloudSyncServiceStub() +{ + opToInterfaceMap_[SERVICE_CMD_REGISTER_CALLBACK] = &CloudSyncServiceStub::HandleRegisterCallbackInner; + opToInterfaceMap_[SERVICE_CMD_START_SYNC] = &CloudSyncServiceStub::HandleStartSyncInner; + opToInterfaceMap_[SERVICE_CMD_STOP_SYNC] = &CloudSyncServiceStub::HandleStopSyncInner; +} + +int32_t CloudSyncServiceStub::OnRemoteRequest(uint32_t code, + MessageParcel &data, + MessageParcel &reply, + MessageOption &option) +{ + if (data.ReadInterfaceToken() != GetDescriptor()) { + return E_SERVICE_DESCRIPTOR_IS_EMPTY; + } + auto interfaceIndex = opToInterfaceMap_.find(code); + if (interfaceIndex == opToInterfaceMap_.end() || !interfaceIndex->second) { + LOGE("Cannot response request %d: unknown tranction", code); + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } + if (!DfsuPermissionChecker::CheckCallerPermission(PERM_CLOUD_SYNC)) { + LOGE("permission denied"); + return E_PERMISSION_DENIED; + } + return (this->*(interfaceIndex->second))(data, reply); +} + +int32_t CloudSyncServiceStub::HandleRegisterCallbackInner(MessageParcel &data, MessageParcel &reply) +{ + LOGD("Begin RegisterCallbackInner"); + auto remoteObj = data.ReadRemoteObject(); + std::string appPackageName = data.ReadString(); + int32_t res = RegisterCallbackInner(appPackageName, remoteObj); + reply.WriteInt32(res); + LOGD("End RegisterCallbackInner"); + return res; +} + +int32_t CloudSyncServiceStub::HandleStartSyncInner(MessageParcel &data, MessageParcel &reply) +{ + LOGD("Begin StartSyncInner"); + auto appPackageName = data.ReadString(); + SyncType type = SyncType(data.ReadInt32()); + auto forceFlag = data.ReadBool(); + int32_t res = StartSyncInner(appPackageName, type, forceFlag); + reply.WriteInt32(res); + LOGD("End StartSyncInner"); + return res; +} + +int32_t CloudSyncServiceStub::HandleStopSyncInner(MessageParcel &data, MessageParcel &reply) +{ + LOGD("Begin StopSyncInner"); + auto appPackageName = data.ReadString(); + int32_t res = StopSyncInner(appPackageName); + reply.WriteInt32(res); + LOGD("End StopSyncInner"); + return res; +} +} // namespace OHOS::FileManagement::CloudSync \ No newline at end of file diff --git a/test/moduletest/BUILD.gn b/test/moduletest/BUILD.gn index e28eaf78662742d4064a21883738729608334d04..d5b1a6b66c01aea09703ede2f623b98f95f6464b 100644 --- a/test/moduletest/BUILD.gn +++ b/test/moduletest/BUILD.gn @@ -13,7 +13,7 @@ import("//build/test.gni") import("//foundation/filemanagement/dfs_service/distributedfile.gni") -module_output_path = "storage_distributed_file_manager/distributedfiledaemon" +module_output_path = "filemanagement/dfs_service/" config("module_private_config") { visibility = [ ":*" ] @@ -56,7 +56,10 @@ ohos_moduletest("DistributedFileDaemonServiceTest") { "${utils_path}:compiler_configs", ] - defines = [ "private=public" ] + defines = [ + "private=public", + "LOG_TAG=\"distributedfileTest\"", + ] deps = [ "${utils_path}:libdistributedfileutils", @@ -78,3 +81,42 @@ 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", + "//base/security/device_auth/services:deviceauth_sdk", + "//foundation/distributedhardware/device_manager/interfaces/inner_kits/native_cpp:devicemanagersdk", + "//third_party/googletest:gmock", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "ability_base:want", + "common_event_service:cesfwk_innerkits", + "dataclassification:data_transit_mgr", + "dsoftbus:softbus_client", + "init:libbegetutil", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + "dfs_service:cloudsync_kit_inner", + ] +} \ No newline at end of file diff --git a/test/moduletest/src/cloudsync_service_test.cpp b/test/moduletest/src/cloudsync_service_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8802189484fd5a0d0c8819ba8515529077ed2b8e --- /dev/null +++ b/test/moduletest/src/cloudsync_service_test.cpp @@ -0,0 +1,120 @@ +/* + * 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" + +namespace OHOS { +namespace FileManagement::CloudSync { +namespace Test { +using namespace testing::ext; +using namespace std; + +class CloudSyncDerived : public CloudSyncCallback { +public: + void OnSyncStateChanged(SyncType type, SyncPromptState state) + { + LOGI("type: %{public}d state: %{public}d", static_cast(type), static_cast(state)); + } +}; + +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: Issue Number + */ +/* + E_OK = ERR_OK, + E_SEVICE_DIED = CSS_ERR_OFFSET, + E_INVAL_ARG, + E_BROKEN_IPC, + E_SA_LOAD_FAILED, +*/ +HWTEST_F(CloudSyncServiceTest, StartSync_001, TestSize.Level1) +{ + LOGE("testcase run OK"); + shared_ptr callback = make_shared(); + SyncType type = SyncType(0); + bool forceFlag = true; + int ret = CloudSyncManager::GetInstance().StartSync(type, forceFlag, callback); + EXPECT_EQ(ret, E_OK); +} + +/** + * @tc.name: StopSync_001 + * @tc.desc: function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +/* + E_OK = ERR_OK, + E_SEVICE_DIED = CSS_ERR_OFFSET, + E_INVAL_ARG, + E_BROKEN_IPC, + E_SA_LOAD_FAILED, +*/ +HWTEST_F(CloudSyncServiceTest, StopSync_001, TestSize.Level1) +{ + LOGE("testcase run OK"); + shared_ptr callback = make_shared(); + SyncType type = SyncType(0); + bool forceFlag = true; + int ret = CloudSyncManager::GetInstance().StartSync(type, 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/utils/BUILD.gn b/utils/BUILD.gn index 87f35b1c89da680438e0bc92ec0a76d7e901a958..dad5526217c70ceca5e37e9a07e61e19ce960b32 100644 --- a/utils/BUILD.gn +++ b/utils/BUILD.gn @@ -55,16 +55,22 @@ ohos_shared_library("libdistributedfileutils") { "system/src/dfsu_fd_guard.cpp", "system/src/dfsu_mount_argument_descriptors.cpp", "system/src/utils_directory.cpp", + "system/src/dfsu_permission_checker.cpp", ] configs = [ "//build/config/compiler:exceptions" ] public_configs = [ ":utils_public_config" ] - defines = [ "LOG_TAG=\"distributedfile_utils\"" ] + defines = [ + "LOG_DOMAIN=0xD004308", + "LOG_TAG=\"distributedfile_utils\"", + ] public_deps = [ "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog", "//commonlibrary/c_utils/base:utils", + "//base/security/access_token/interfaces/innerkits/accesstoken:libaccesstoken_sdk", + "${path_ipc}/interfaces/innerkits/ipc_core:ipc_core", ] part_name = "dfs_service" diff --git a/utils/log/include/dfs_error.h b/utils/log/include/dfs_error.h new file mode 100644 index 0000000000000000000000000000000000000000..21a88a1530b261667cbb05aef5cdf08a56098a4f --- /dev/null +++ b/utils/log/include/dfs_error.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2022 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_DFS_ERROR_H +#define OHOS_FILEMGMT_DFS_ERROR_H + +#include "errors.h" + +namespace OHOS::FileManagement { +enum { + /** + * Module type : Cloud sync service + */ + CLOUD_SYNC_SERVICE_MODULE = 0x100 +}; + +constexpr ErrCode CSS_ERR_OFFSET = ErrCodeOffset(SUBSYS_FILEMANAGEMENT, CLOUD_SYNC_SERVICE_MODULE); + +enum CloudSyncServiceErrCode : ErrCode { + E_OK = ERR_OK, + E_SEVICE_DIED = CSS_ERR_OFFSET, + E_INVAL_ARG, + E_BROKEN_IPC, + E_SA_LOAD_FAILED, + E_SERVICE_DESCRIPTOR_IS_EMPTY, + E_PERMISSION_DENIED, +}; +} // namespace OHOS::FileManagement + +#endif // OHOS_FILEMGMT_DFS_ERROR_H \ No newline at end of file diff --git a/utils/log/include/utils_log.h b/utils/log/include/utils_log.h index 8b7bf699d952ff50054598a73af9de630f0b98e7..efb68036b405293d5ddbd943070e2b4fecec7fa6 100644 --- a/utils/log/include/utils_log.h +++ b/utils/log/include/utils_log.h @@ -22,8 +22,6 @@ #include "hilog/log_cpp.h" namespace OHOS { -namespace Storage { -namespace DistributedFile { #ifndef LOG_DOMAIN #define LOG_DOMAIN 0xD001600 #endif @@ -44,7 +42,5 @@ std::string GetFileNameFromFullPath(const char *str); #define LOGW(fmt, ...) PRINT_LOG(Warn, fmt, ##__VA_ARGS__) #define LOGE(fmt, ...) PRINT_LOG(Error, fmt, ##__VA_ARGS__) #define LOGF(fmt, ...) PRINT_LOG(Fatal, fmt, ##__VA_ARGS__) -} // namespace DistributedFile -} // namespace Storage } // namespace OHOS #endif // UTILS_LOG_H \ No newline at end of file diff --git a/utils/log/src/utils_log.cpp b/utils/log/src/utils_log.cpp index 9526bf9a8242d36efc6d91dbaaff143d7d93c7fb..948b545ae14c765aaefe35784a9b10506bf46535 100644 --- a/utils/log/src/utils_log.cpp +++ b/utils/log/src/utils_log.cpp @@ -16,14 +16,10 @@ #include "utils_log.h" namespace OHOS { -namespace Storage { -namespace DistributedFile { std::string GetFileNameFromFullPath(const char *str) { std::string fullPath(str); size_t pos = fullPath.find_last_of("/"); return (pos == std::string::npos) ? std::string() : fullPath.substr(pos + 1); } -} // namespace DistributedFile -} // namespace Storage } // namespace OHOS diff --git a/utils/system/include/dfsu_permission_checker.h b/utils/system/include/dfsu_permission_checker.h new file mode 100644 index 0000000000000000000000000000000000000000..5d7306c7dc02fb9908a202ca9a584ba0c83a8fa0 --- /dev/null +++ b/utils/system/include/dfsu_permission_checker.h @@ -0,0 +1,30 @@ +/* + * 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_DFSU_PERMISSION_CHECKER_H +#define OHOS_FILEMGMT_DFSU_PERMISSION_CHECKER_H + +#include + +namespace OHOS::FileManagement { +inline const std::string PERM_CLOUD_SYNC = "ohos.permission.cloud_sync"; +class DfsuPermissionChecker final { +public: + static bool CheckCallerPermission(const std::string &permissionName); + static bool CheckPermission(const std::string &permissionName); +}; +} // namespace OHOS::FileManagement + +#endif // OHOS_FILEMGMT_DFSU_PERMISSION_CHECKER_H \ No newline at end of file diff --git a/utils/system/src/dfsu_permission_checker.cpp b/utils/system/src/dfsu_permission_checker.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7a09cf168a4b1f20816cecd66dd437a849d9fea0 --- /dev/null +++ b/utils/system/src/dfsu_permission_checker.cpp @@ -0,0 +1,51 @@ +/* + * 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 "dfsu_permission_checker.h" +#include "accesstoken_kit.h" +#include "ipc_skeleton.h" +#include "utils_log.h" + +namespace OHOS::FileManagement { +using namespace std; +bool DfsuPermissionChecker::CheckCallerPermission(const std::string &permissionName) +{ + auto tokenId = IPCSkeleton::GetCallingTokenID(); + auto tokenType = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId); + if ((tokenType == Security::AccessToken::TOKEN_HAP) || (tokenType == Security::AccessToken::TOKEN_NATIVE)) { + LOGI("Token type is %{public}d", tokenType); + return CheckPermission(permissionName); + } else if (tokenType == Security::AccessToken::TOKEN_SHELL) { + LOGI("Token type is shell"); + return true; + } else { + LOGE("Unsupported token type:%{public}d", tokenType); + return false; + } +} + +bool DfsuPermissionChecker::CheckPermission(const std::string &permissionName) +{ + LOGD("VerifyCallingPermission permission %{private}s", permissionName.c_str()); + auto callerToken = IPCSkeleton::GetCallingTokenID(); + int32_t ret = Security::AccessToken::AccessTokenKit::VerifyAccessToken(callerToken, permissionName); + if (ret == Security::AccessToken::PermissionState::PERMISSION_DENIED) { + LOGE("permission %{private}s: PERMISSION_DENIED", permissionName.c_str()); + return false; + } + LOGI("verify AccessToken success"); + return true; +} +} // namespace OHOS::FileManagement \ No newline at end of file