diff --git a/frameworks/native/cloudsync_kit_inner/include/cloud_downloaded_callback_client.h b/frameworks/native/cloudsync_kit_inner/include/cloud_downloaded_callback_client.h new file mode 100644 index 0000000000000000000000000000000000000000..271f0f4bc1ce3ba532eda8331d08e241a18e38d8 --- /dev/null +++ b/frameworks/native/cloudsync_kit_inner/include/cloud_downloaded_callback_client.h @@ -0,0 +1,33 @@ +/* + * 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_CLOUD_DOWNLOADED_CALLBACK_CLIENT_H +#define OHOS_FILEMGMT_CLOUD_DOWNLOADED_CALLBACK_CLIENT_H + +#include "cloud_downloaded_callback_stub.h" + +namespace OHOS::FileManagement::CloudSync { +class CloudDownloadedCallbackClient final : public CloudDownloadedCallbackStub { +public: + explicit CloudDownloadedCallbackClient(const std::shared_ptr &callback) : callback_(callback) {} + + void OnDownloadedResult(int32_t result) override; + +private: + std::shared_ptr callback_; +}; +} // namespace OHOS::FileManagement::CloudSync + +#endif // OHOS_FILEMGMT_CLOUD_DOWNLOADED_CALLBACK_CLIENT_H diff --git a/frameworks/native/cloudsync_kit_inner/include/cloud_downloaded_callback_stub.h b/frameworks/native/cloudsync_kit_inner/include/cloud_downloaded_callback_stub.h new file mode 100644 index 0000000000000000000000000000000000000000..db2184833a0d27a2f3137c731572e49ffb9cc447 --- /dev/null +++ b/frameworks/native/cloudsync_kit_inner/include/cloud_downloaded_callback_stub.h @@ -0,0 +1,39 @@ +/* +* 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_CLOUD_DOWLOADED_CALLBACK_STUB_H +#define OHOS_FILEMGMT_CLOUD_DOWLOADED_CALLBACK_STUB_H + +#include + +#include "i_cloud_downloaded_callback.h" +#include "iremote_stub.h" + +namespace OHOS::FileManagement::CloudSync { +class CloudDownloadedCallbackStub : public IRemoteStub { +public: + CloudDownloadedCallbackStub(); + virtual ~CloudDownloadedCallbackStub() = default; + int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; + +private: + using ServiceInterface = int32_t (CloudDownloadedCallbackStub::*)(MessageParcel &data, MessageParcel &reply); + std::map opToInterfaceMap_; + + int32_t HandleOnDownloadedResult(MessageParcel &data, MessageParcel &reply); +}; +} // namespace OHOS::FileManagement::CloudSync + +#endif // OHOS_FILEMGMT_CLOUD_DOWNLOADED_CALLBACK_STUB_H diff --git a/frameworks/native/cloudsync_kit_inner/include/cloud_process_callback_client.h b/frameworks/native/cloudsync_kit_inner/include/cloud_process_callback_client.h new file mode 100644 index 0000000000000000000000000000000000000000..a58d7d8fbcf84b1000e9cd3742f2f0ad472fcf22 --- /dev/null +++ b/frameworks/native/cloudsync_kit_inner/include/cloud_process_callback_client.h @@ -0,0 +1,33 @@ +/* + * 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_CLOUD_PROCESS_CALLBACK_CLIENT_H +#define OHOS_FILEMGMT_CLOUD_PROCESS_CALLBACK_CLIENT_H + +#include "cloud_process_callback_stub.h" + +namespace OHOS::FileManagement::CloudSync { +class CloudProcessCallbackClient final : public CloudProcessCallbackStub { +public: + explicit CloudProcessCallbackClient(const std::shared_ptr &callback) : callback_(callback) {} + + void OnDownloadProcess(int64_t downloadedSize, int64_t totalSize) override; + +private: + std::shared_ptr callback_; +}; +} // namespace OHOS::FileManagement::CloudSync + +#endif // OHOS_FILEMGMT_CLOUD_PROCESS_CALLBACK_CLIENT_H diff --git a/frameworks/native/cloudsync_kit_inner/include/cloud_process_callback_stub.h b/frameworks/native/cloudsync_kit_inner/include/cloud_process_callback_stub.h new file mode 100644 index 0000000000000000000000000000000000000000..9ee18fdceba1ff6612dba535ad6a021bfb18a371 --- /dev/null +++ b/frameworks/native/cloudsync_kit_inner/include/cloud_process_callback_stub.h @@ -0,0 +1,39 @@ +/* +* 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_CLOUD_PROCESS_CALLBACK_STUB_H +#define OHOS_FILEMGMT_CLOUD_PROCESS_CALLBACK_STUB_H + +#include + +#include "i_cloud_process_callback.h" +#include "iremote_stub.h" + +namespace OHOS::FileManagement::CloudSync { +class CloudProcessCallbackStub : public IRemoteStub { +public: + CloudProcessCallbackStub(); + virtual ~CloudProcessCallbackStub() = default; + int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; + +private: + using ServiceInterface = int32_t (CloudProcessCallbackStub::*)(MessageParcel &data, MessageParcel &reply); + std::map opToInterfaceMap_; + + int32_t HandleOnProcess(MessageParcel &data, MessageParcel &reply); +}; +} // namespace OHOS::FileManagement::CloudSync + +#endif // OHOS_FILEMGMT_CLOUD_PROCESS_CALLBACK_STUB_H 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 index e9627947ac56f04d4040b745c83da6a9783cc460..660fedbea1b8d0136f3f2a4d849e37f1cf2236d9 100644 --- a/frameworks/native/cloudsync_kit_inner/include/cloud_sync_manager_impl.h +++ b/frameworks/native/cloudsync_kit_inner/include/cloud_sync_manager_impl.h @@ -37,6 +37,8 @@ public: int32_t NotifyDataChange(const std::string &accoutId, const std::string &bundleName) override; int32_t EnableCloud(const std::string &accoutId, const SwitchDataObj &switchData) override; int32_t DisableCloud(const std::string &accoutId) override; + int32_t DownloadFile(const std::string &url, const std::shared_ptr processCallback, + const std::shared_ptr downloadedCallback) override ; private: CloudSyncManagerImpl() = default; void SetDeathRecipient(const sptr &remoteObject); 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 index e8269e09eb29cfbd526d103cf7bbd86793085412..a55a021d8884734a4c33a5d2693989db87da8ee1 100644 --- a/frameworks/native/cloudsync_kit_inner/include/cloud_sync_service_proxy.h +++ b/frameworks/native/cloudsync_kit_inner/include/cloud_sync_service_proxy.h @@ -38,6 +38,8 @@ public: int32_t NotifyDataChange(const std::string &accoutId, const std::string &bundleName) override; int32_t EnableCloud(const std::string &accoutId, const SwitchDataObj &switchData) override; int32_t DisableCloud(const std::string &accoutId) override; + int32_t DownloadFile(const std::string &url, const sptr &processCallback, + const sptr &downloadedCallback) override; class ServiceProxyLoadCallback : public SystemAbilityLoadCallbackStub { public: diff --git a/frameworks/native/cloudsync_kit_inner/src/cloud_downloaded_callback_client.cpp b/frameworks/native/cloudsync_kit_inner/src/cloud_downloaded_callback_client.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9f7dbd43441022c89925683b56772c3f4d37fcd8 --- /dev/null +++ b/frameworks/native/cloudsync_kit_inner/src/cloud_downloaded_callback_client.cpp @@ -0,0 +1,31 @@ +/* + * 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 "cloud_downloaded_callback_client.h" +#include "utils_log.h" + +namespace OHOS::FileManagement::CloudSync { +using namespace std; + +void CloudDownloadedCallbackClient::OnDownloadedResult(int32_t result) +{ + if (!callback_) { + LOGE("callback_ is null, maybe not registered"); + return; + } + callback_->OnDownloadedResult(result); +} + +} // namespace OHOS::FileManagement::CloudSync diff --git a/frameworks/native/cloudsync_kit_inner/src/cloud_downloaded_callback_stub.cpp b/frameworks/native/cloudsync_kit_inner/src/cloud_downloaded_callback_stub.cpp new file mode 100644 index 0000000000000000000000000000000000000000..229dabc60d6a2fd3945e792aff591956318fbae6 --- /dev/null +++ b/frameworks/native/cloudsync_kit_inner/src/cloud_downloaded_callback_stub.cpp @@ -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. + */ + +#include "cloud_downloaded_callback_stub.h" +#include "dfs_error.h" +#include "utils_log.h" + +namespace OHOS::FileManagement::CloudSync { +using namespace std; + +CloudDownloadedCallbackStub::CloudDownloadedCallbackStub() +{ + opToInterfaceMap_[SERVICE_CMD_ON_DOWNLOADED] = &CloudDownloadedCallbackStub::HandleOnDownloadedResult; +} + +int32_t CloudDownloadedCallbackStub::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 CloudDownloadedCallbackStub::HandleOnDownloadedResult(MessageParcel &data, MessageParcel &reply) +{ + int32_t result = data.ReadInt32(); + OnDownloadedResult(result); + return E_OK; +} +} // namespace OHOS::FileManagement::CloudSync diff --git a/frameworks/native/cloudsync_kit_inner/src/cloud_process_callback_client.cpp b/frameworks/native/cloudsync_kit_inner/src/cloud_process_callback_client.cpp new file mode 100644 index 0000000000000000000000000000000000000000..137ad0c29b8a973000d5cadc983c2bb0272cf84a --- /dev/null +++ b/frameworks/native/cloudsync_kit_inner/src/cloud_process_callback_client.cpp @@ -0,0 +1,31 @@ +/* + * 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 "cloud_process_callback_client.h" +#include "utils_log.h" + +namespace OHOS::FileManagement::CloudSync { +using namespace std; + +void CloudProcessCallbackClient::OnDownloadProcess(int64_t downloadedSize, int64_t totalSize) +{ + if (!callback_) { + LOGE("callback_ is null, maybe not registered"); + return; + } + callback_->OnDownloadProcess(downloadedSize, totalSize); +} + +} // namespace OHOS::FileManagement::CloudSync diff --git a/frameworks/native/cloudsync_kit_inner/src/cloud_process_callback_stub.cpp b/frameworks/native/cloudsync_kit_inner/src/cloud_process_callback_stub.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3be742f2019521bddb72f2ed595514042fb0b907 --- /dev/null +++ b/frameworks/native/cloudsync_kit_inner/src/cloud_process_callback_stub.cpp @@ -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. + */ + +#include "cloud_process_callback_stub.h" +#include "dfs_error.h" +#include "utils_log.h" + +namespace OHOS::FileManagement::CloudSync { +using namespace std; + +CloudProcessCallbackStub::CloudProcessCallbackStub() +{ + opToInterfaceMap_[SERVICE_CMD_ON_PROCESS] = &CloudProcessCallbackStub::HandleOnProcess; +} + +int32_t CloudProcessCallbackStub::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 CloudProcessCallbackStub::HandleOnProcess(MessageParcel &data, MessageParcel &reply) +{ + int64_t downloadedSize = data.ReadInt64(); + int64_t totalSize = data.ReadInt64(); + OnDownloadProcess(downloadedSize, totalSize); + return E_OK; +} +} // namespace OHOS::FileManagement::CloudSync 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 index 5649281a46b0d115de0d6579ad86cdf503725a2b..60dc860e8136281d868a67913e189529b803f6a6 100644 --- a/frameworks/native/cloudsync_kit_inner/src/cloud_sync_manager_impl.cpp +++ b/frameworks/native/cloudsync_kit_inner/src/cloud_sync_manager_impl.cpp @@ -13,6 +13,8 @@ * limitations under the License. */ +#include "cloud_downloaded_callback_client.h" +#include "cloud_process_callback_client.h" #include "cloud_sync_manager_impl.h" #include "cloud_sync_callback_client.h" #include "cloud_sync_service_proxy.h" @@ -89,6 +91,21 @@ int32_t CloudSyncManagerImpl::NotifyDataChange(const std::string &accoutId, cons return ret; } +int32_t CloudSyncManagerImpl::DownloadFile(const std::string &url, const std::shared_ptr processCallback, + const std::shared_ptr downloadedCallback) +{ + LOGI("Download File start"); + auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance(); + if (!CloudSyncServiceProxy) { + LOGE("proxy is null"); + return E_SA_LOAD_FAILED; + } + int32_t ret = CloudSyncServiceProxy->DownloadFile(url, + sptr(new (std::nothrow) CloudProcessCallbackClient(processCallback)), + sptr(new (std::nothrow) CloudDownloadedCallbackClient(downloadedCallback))); + LOGI("Download file ret %{public}d", ret); + return ret; +} void CloudSyncManagerImpl::SetDeathRecipient(const sptr &remoteObject) { auto deathCallback = [this](const wptr &obj) { 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 index 783345316e7620bf3532fc3e1eff0590242e4738..761db206d8223f2675cda117dcafa6c27e76f026 100644 --- a/frameworks/native/cloudsync_kit_inner/src/cloud_sync_service_proxy.cpp +++ b/frameworks/native/cloudsync_kit_inner/src/cloud_sync_service_proxy.cpp @@ -305,6 +305,55 @@ int32_t CloudSyncServiceProxy::NotifyDataChange(const std::string &accoutId, con return reply.ReadInt32(); } +int32_t CloudSyncServiceProxy::DownloadFile(const std::string &url, const sptr &processCallback, + const sptr &downloadedCallback) +{ + LOGI("Start DownloadFile"); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!processCallback || !downloadedCallback) { + LOGI("Empty callback stub"); + return E_INVAL_ARG; + } + + if (!data.WriteInterfaceToken(GetDescriptor())) { + LOGE("Failed to write interface token"); + return E_BROKEN_IPC; + } + + if (!data.WriteString(url)) { + LOGE("Failed to send the cloud id"); + return E_INVAL_ARG; + } + + if (!data.WriteRemoteObject(processCallback)) { + LOGE("Failed to send the callback stub"); + return E_INVAL_ARG; + } + + if (!data.WriteRemoteObject(downloadedCallback)) { + LOGE("Failed to send the callback stub"); + return E_INVAL_ARG; + } + + auto remote = Remote(); + if (!remote) { + LOGE("remote is nullptr"); + return E_BROKEN_IPC; + } + int32_t ret = remote->SendRequest(ICloudSyncService::SERVICE_CMD_DOWNLOAD_FILE, 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 E_BROKEN_IPC; + } + LOGI("DownloadFile Success"); + return reply.ReadInt32(); +} + sptr CloudSyncServiceProxy::GetInstance() { LOGI("getinstance"); diff --git a/interfaces/inner_api/native/cloudsync_kit_inner/BUILD.gn b/interfaces/inner_api/native/cloudsync_kit_inner/BUILD.gn index a6473dfb331d75c8a65407bc78786c0e45d21e1f..13724cad31a14f94dc98933b6c0ef412b32b396e 100644 --- a/interfaces/inner_api/native/cloudsync_kit_inner/BUILD.gn +++ b/interfaces/inner_api/native/cloudsync_kit_inner/BUILD.gn @@ -27,6 +27,10 @@ config("private_config") { ohos_shared_library("cloudsync_kit_inner") { sources = [ + "${distributedfile_path}/frameworks/native/cloudsync_kit_inner/src/cloud_downloaded_callback_client.cpp", + "${distributedfile_path}/frameworks/native/cloudsync_kit_inner/src/cloud_downloaded_callback_stub.cpp", + "${distributedfile_path}/frameworks/native/cloudsync_kit_inner/src/cloud_process_callback_client.cpp", + "${distributedfile_path}/frameworks/native/cloudsync_kit_inner/src/cloud_process_callback_stub.cpp", "${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_common.cpp", diff --git a/interfaces/inner_api/native/cloudsync_kit_inner/cloud_downloaded_callback.h b/interfaces/inner_api/native/cloudsync_kit_inner/cloud_downloaded_callback.h new file mode 100644 index 0000000000000000000000000000000000000000..315e28cafa238c1779dbc97c65a0449b4a1647d8 --- /dev/null +++ b/interfaces/inner_api/native/cloudsync_kit_inner/cloud_downloaded_callback.h @@ -0,0 +1,29 @@ +/* + * 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_CLOUD_DOWNLOADED_CALLBACK_H +#define OHOS_FILEMGMT_CLOUD_DOWNLOADED_CALLBACK_H + +#include + +namespace OHOS::FileManagement::CloudSync { +class CloudDownloadedCallback { +public: + virtual ~CloudDownloadedCallback() = default; + virtual void OnDownloadedResult(int32_t result) = 0; +}; +} // namespace OHOS::FileManagement::CloudSync + +#endif // OHOS_FILEMGMT_CLOUD_DOWNLOADED_CALLBACK_H diff --git a/interfaces/inner_api/native/cloudsync_kit_inner/cloud_process_callback.h b/interfaces/inner_api/native/cloudsync_kit_inner/cloud_process_callback.h new file mode 100644 index 0000000000000000000000000000000000000000..0cb0272c6a6bc9e248629093da729e98733487b0 --- /dev/null +++ b/interfaces/inner_api/native/cloudsync_kit_inner/cloud_process_callback.h @@ -0,0 +1,29 @@ +/* + * 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_CLOUD_PROCESS_CALLBACK_H +#define OHOS_FILEMGMT_CLOUD_PROCESS_CALLBACK_H + +#include + +namespace OHOS::FileManagement::CloudSync { +class CloudProcessCallback { +public: + virtual ~CloudProcessCallback() = default; + virtual void OnDownloadProcess(int64_t downloadedSize, int64_t totalSize) = 0; +}; +} // namespace OHOS::FileManagement::CloudSync + +#endif // OHOS_FILEMGMT_CLOUD_PROCESS_CALLBACK_H 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 index b6d34e582df3ccd9f28b93ce19164e041f4d972e..9c432dec2e15505581e1f3d6015f888e881ac1ae 100644 --- a/interfaces/inner_api/native/cloudsync_kit_inner/cloud_sync_manager.h +++ b/interfaces/inner_api/native/cloudsync_kit_inner/cloud_sync_manager.h @@ -18,6 +18,8 @@ #include +#include "cloud_downloaded_callback.h" +#include "cloud_process_callback.h" #include "cloud_sync_callback.h" #include "cloud_sync_common.h" @@ -66,6 +68,8 @@ public: * @return int32_t 同步返回执行结果 */ virtual int32_t Clean(const std::string &accountId, const CleanOptions &cleanOptions) = 0; + virtual int32_t DownloadFile(const std::string &url, const std::shared_ptr processCallback, + const std::shared_ptr downloadedCallback)= 0; }; } // namespace OHOS::FileManagement::CloudSync diff --git a/interfaces/inner_api/native/cloudsync_kit_inner/i_cloud_downloaded_callback.h b/interfaces/inner_api/native/cloudsync_kit_inner/i_cloud_downloaded_callback.h new file mode 100644 index 0000000000000000000000000000000000000000..685b08110182db6b811d8fef5bc6823162a940ac --- /dev/null +++ b/interfaces/inner_api/native/cloudsync_kit_inner/i_cloud_downloaded_callback.h @@ -0,0 +1,33 @@ +/* + * 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_I_CLOUD_DOWNLOADED_CALLBACK_H +#define OHOS_FILEMGMT_I_CLOUD_DOWNLOADED_CALLBACK_H + +#include "cloud_downloaded_callback.h" +#include "iremote_broker.h" + +namespace OHOS::FileManagement::CloudSync { +class ICloudDownloadedCallback : public CloudDownloadedCallback, public IRemoteBroker { +public: + enum { + SERVICE_CMD_ON_DOWNLOADED = 0, + }; + + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.Filemanagement.Dfs.ICloudDownloadedCallback") +}; +} // namespace OHOS::FileManagement::CloudSync + +#endif // OHOS_FILEMGMT_I_CLOUD_DOWNLOADED_CALLBACK_H diff --git a/interfaces/inner_api/native/cloudsync_kit_inner/i_cloud_process_callback.h b/interfaces/inner_api/native/cloudsync_kit_inner/i_cloud_process_callback.h new file mode 100644 index 0000000000000000000000000000000000000000..1eb3aa8c13ac8b7635d0278fc59f630818b436a8 --- /dev/null +++ b/interfaces/inner_api/native/cloudsync_kit_inner/i_cloud_process_callback.h @@ -0,0 +1,33 @@ +/* + * 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_I_CLOUD_PROCESS_CALLBACK_H +#define OHOS_FILEMGMT_I_CLOUD_PROCESS_CALLBACK_H + +#include "cloud_process_callback.h" +#include "iremote_broker.h" + +namespace OHOS::FileManagement::CloudSync { +class ICloudProcessCallback : public CloudProcessCallback, public IRemoteBroker { +public: + enum { + SERVICE_CMD_ON_PROCESS = 0, + }; + + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.Filemanagement.Dfs.ICloudProcessCallback") +}; +} // namespace OHOS::FileManagement::CloudSync + +#endif // OHOS_FILEMGMT_I_CLOUD_PROCESS_CALLBACK_H 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 index 9b87db0ef22649993a95872a65f60ee9a77ceb60..7f483f72195ff2e962e7377ab6b71240c3090acb 100644 --- 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 @@ -34,6 +34,7 @@ public: SERVICE_CMD_ENABLE_CLOUD, SERVICE_CMD_DISABLE_CLOUD, SERVICE_CMD_CLEAN, + SERVICE_CMD_DOWNLOAD_FILE, }; DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.Filemanagement.Dfs.ICloudSyncService") @@ -46,6 +47,8 @@ public: virtual int32_t NotifyDataChange(const std::string &accoutId, const std::string &bundleName) = 0; virtual int32_t EnableCloud(const std::string &accoutId, const SwitchDataObj &switchData) = 0; virtual int32_t DisableCloud(const std::string &accoutId) = 0; + virtual int32_t DownloadFile(const std::string &url, const sptr &processCallback, + const sptr &downloadedCallback) = 0; }; } // namespace OHOS::FileManagement::CloudSync diff --git a/services/cloudsyncservice/BUILD.gn b/services/cloudsyncservice/BUILD.gn index cd83f632e7544420c64613cdbb59c2cbe5e418fd..73ec8309b2595ab1bf49359d434bf5019f093d0b 100644 --- a/services/cloudsyncservice/BUILD.gn +++ b/services/cloudsyncservice/BUILD.gn @@ -40,6 +40,8 @@ ohos_shared_library("cloudsync_sa") { ] sources = [ + "src/ipc/cloud_downloaded_callback_proxy.cpp", + "src/ipc/cloud_process_callback_proxy.cpp", "src/ipc/cloud_sync_callback_manager.cpp", "src/ipc/cloud_sync_callback_proxy.cpp", "src/ipc/cloud_sync_service.cpp", diff --git a/services/cloudsyncservice/include/data_sync/data_handler.h b/services/cloudsyncservice/include/data_sync/data_handler.h index d943e4b8e239320b06692ba94a6c3427489c6e0c..4edfa87976a86e17d248a9e08756673c8b78e492 100644 --- a/services/cloudsyncservice/include/data_sync/data_handler.h +++ b/services/cloudsyncservice/include/data_sync/data_handler.h @@ -38,6 +38,8 @@ public: const DriveKit::DKError &)>> &resultCallback) = 0; virtual int32_t GetRetryRecords(std::vector &records) = 0; + virtual int32_t GetDownloadAsset(std::string cloudId, + std::vector &outAssetsToDownload) = 0; /* upload */ virtual int32_t GetCreatedRecords(std::vector &records) = 0; virtual int32_t GetDeletedRecords(std::vector &records) = 0; diff --git a/services/cloudsyncservice/include/data_sync/data_sync_manager.h b/services/cloudsyncservice/include/data_sync/data_sync_manager.h index 11beda0c1520f1decd7e5f60c01f0c56c1a3317c..386c955d5baca113e9dcb2b0f40197981d4ea3e8 100644 --- a/services/cloudsyncservice/include/data_sync/data_sync_manager.h +++ b/services/cloudsyncservice/include/data_sync/data_sync_manager.h @@ -21,6 +21,8 @@ #include #include +#include "i_cloud_process_callback.h" +#include "i_cloud_downloaded_callback.h" #include "data_sync/data_syncer.h" namespace OHOS::FileManagement::CloudSync { @@ -39,6 +41,11 @@ public: 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; + int32_t DownloadSourceFile(const std::string bundleName, + const int32_t userId, + const std::string url, + const sptr processCallback, + const sptr downloadedCallback); private: std::vector> dataSyncers_; @@ -48,4 +55,4 @@ private: }; } // namespace OHOS::FileManagement::CloudSync -#endif // OHOS_FILEMGMT_DATA_SYNC_MANAGER_H \ No newline at end of file +#endif // OHOS_FILEMGMT_DATA_SYNC_MANAGER_H diff --git a/services/cloudsyncservice/include/data_sync/data_syncer.h b/services/cloudsyncservice/include/data_sync/data_syncer.h index cf5a5814c92d25776e51a6bd72829433f1604dfc..84b124c5873892adc4e00568748a33eae82bf986 100644 --- a/services/cloudsyncservice/include/data_sync/data_syncer.h +++ b/services/cloudsyncservice/include/data_sync/data_syncer.h @@ -25,6 +25,8 @@ #include "cloud_sync_constants.h" #include "data_handler.h" #include "data_sync/sync_state_manager.h" +#include "i_cloud_downloaded_callback.h" +#include "i_cloud_process_callback.h" #include "sdk_helper.h" #include "task.h" @@ -46,6 +48,9 @@ public: /* sync */ virtual int32_t StartSync(bool forceFlag, SyncTriggerType triggerType); virtual int32_t StopSync(SyncTriggerType triggerType); + virtual int32_t DownloadSourceFile(const std::string url, + const sptr processCallback, + const sptr downloadedCallback); /* properties */ std::string GetBundleName() const; @@ -66,6 +71,11 @@ protected: virtual void Schedule() = 0; void Abort(); + /* download source file */ + int32_t DownloadInner(std::shared_ptr handler, + const std::string url, + const sptr processCallback, + const sptr downloadedCallback); /* notify */ void CompletePull(); void CompletePush(); diff --git a/services/cloudsyncservice/include/data_sync/gallery_data_sync/file_data_convertor.h b/services/cloudsyncservice/include/data_sync/gallery_data_sync/file_data_convertor.h index 445eded7b78f374ba50ba2323744dbdcc5d4d44d..7d1d0ea6bd46467c94227f79a17b15fd8c64982d 100644 --- a/services/cloudsyncservice/include/data_sync/gallery_data_sync/file_data_convertor.h +++ b/services/cloudsyncservice/include/data_sync/gallery_data_sync/file_data_convertor.h @@ -45,6 +45,9 @@ public: int32_t Convert(DriveKit::DKRecord &record, NativeRdb::ResultSet &resultSet); std::string GetThumbPath(const std::string &path, const std::string &key); + /* path conversion */ + std::string GetLowerPath(const std::string &path); + std::string GetLowerTmpPath(const std::string &path); private: /* record id */ @@ -85,9 +88,6 @@ private: int32_t HandleThumbnail(DriveKit::DKRecordFieldList &list, std::string &path); int32_t HandleLcd(DriveKit::DKRecordFieldList &list, std::string &path); - /* path conversion */ - std::string GetLowerPath(const std::string &path); - /* identifier */ int32_t userId_; std::string bundleName_; diff --git a/services/cloudsyncservice/include/data_sync/gallery_data_sync/file_data_handler.h b/services/cloudsyncservice/include/data_sync/gallery_data_sync/file_data_handler.h index 58c702006f80b8242c1774e78cc66da3e9fac723..0047fd20c044fd9c0b47027ea935675c4fa7a1df 100644 --- a/services/cloudsyncservice/include/data_sync/gallery_data_sync/file_data_handler.h +++ b/services/cloudsyncservice/include/data_sync/gallery_data_sync/file_data_handler.h @@ -41,6 +41,8 @@ public: const DriveKit::DKError &)>> &downloadResultCallback) override; virtual int32_t GetRetryRecords(std::vector &records) override; + int32_t GetDownloadAsset(std::string cloudId, + std::vector &outAssetsToDownload) override; /* upload */ int32_t GetCreatedRecords(std::vector &records) override; int32_t GetDeletedRecords(std::vector &records) override; diff --git a/services/cloudsyncservice/include/data_sync/gallery_data_sync/gallery_data_syncer.h b/services/cloudsyncservice/include/data_sync/gallery_data_sync/gallery_data_syncer.h index d87fed75207f4733102755ea96ec75d36e712f92..7b8a62564a9781feb0cf623f5ba8ba8a3be5e9a3 100644 --- a/services/cloudsyncservice/include/data_sync/gallery_data_sync/gallery_data_syncer.h +++ b/services/cloudsyncservice/include/data_sync/gallery_data_sync/gallery_data_syncer.h @@ -30,6 +30,9 @@ public: virtual ~GalleryDataSyncer() = default; virtual void Schedule() override; + int32_t DownloadSourceFile(const std::string url, + const sptr processCallback, + const sptr downloadedCallback) override; private: enum { diff --git a/services/cloudsyncservice/include/ipc/cloud_downloaded_callback_proxy.h b/services/cloudsyncservice/include/ipc/cloud_downloaded_callback_proxy.h new file mode 100644 index 0000000000000000000000000000000000000000..a75672a9d2e380d88ee212d6c93698150f6ad5d9 --- /dev/null +++ b/services/cloudsyncservice/include/ipc/cloud_downloaded_callback_proxy.h @@ -0,0 +1,35 @@ +/* + * 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_CLOUD_DOWNLOADED_CALLBACK_PROXY_H +#define OHOS_FILEMGMT_CLOUD_DOWNLOADED_CALLBACK_PROXY_H + +#include "i_cloud_downloaded_callback.h" + +#include "iremote_proxy.h" + +namespace OHOS::FileManagement::CloudSync { +class CloudDownloadedCallbackProxy : public IRemoteProxy { +public: + explicit CloudDownloadedCallbackProxy(const sptr &impl) : IRemoteProxy(impl) {} + ~CloudDownloadedCallbackProxy() override {} + + void OnDownloadedResult(int32_t result) override; +private: + static inline BrokerDelegator delegator_; +}; +} // namespace OHOS::FileManagement::CloudSync + +#endif // OHOS_FILEMGMT_CLOUD_DOWNLOADED_CALLBACK_PROXY_H diff --git a/services/cloudsyncservice/include/ipc/cloud_process_callback_proxy.h b/services/cloudsyncservice/include/ipc/cloud_process_callback_proxy.h new file mode 100644 index 0000000000000000000000000000000000000000..9becbb56d376d73d7bd83a634cdba1c7275a9007 --- /dev/null +++ b/services/cloudsyncservice/include/ipc/cloud_process_callback_proxy.h @@ -0,0 +1,35 @@ +/* + * 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_CLOUD_PROCESS_CALLBACK_PROXY_H +#define OHOS_FILEMGMT_CLOUD_PROCESS_CALLBACK_PROXY_H + +#include "i_cloud_process_callback.h" + +#include "iremote_proxy.h" + +namespace OHOS::FileManagement::CloudSync { +class CloudProcessCallbackProxy : public IRemoteProxy { +public: + explicit CloudProcessCallbackProxy(const sptr &impl) : IRemoteProxy(impl) {} + ~CloudProcessCallbackProxy() override {} + + void OnDownloadProcess(int64_t downloadedSize, int64_t totalSize) override; +private: + static inline BrokerDelegator delegator_; +}; +} // namespace OHOS::FileManagement::CloudSync + +#endif // OHOS_FILEMGMT_CLOUD_PROCESS_CALLBACK_PROXY_H diff --git a/services/cloudsyncservice/include/ipc/cloud_sync_service.h b/services/cloudsyncservice/include/ipc/cloud_sync_service.h index 83072f43e397ca4eb738a83ba97ba90a2954badb..9d3ac52a54acc9ef2bbaa190374fa07013888b68 100644 --- a/services/cloudsyncservice/include/ipc/cloud_sync_service.h +++ b/services/cloudsyncservice/include/ipc/cloud_sync_service.h @@ -22,6 +22,8 @@ #include "system_ability.h" #include "cloud_sync_service_stub.h" +#include "i_cloud_downloaded_callback.h" +#include "i_cloud_process_callback.h" #include "i_cloud_sync_callback.h" #include "sync_rule/battery_status_listener.h" @@ -41,6 +43,8 @@ public: int32_t NotifyDataChange(const std::string &accoutId, const std::string &bundleName) override; int32_t EnableCloud(const std::string &accoutId, const SwitchDataObj &switchData) override; int32_t DisableCloud(const std::string &accoutId) override; + int32_t DownloadFile(const std::string &url, const sptr &processCallback, + const sptr &downloadedCallback) override; private: void OnStart() override; diff --git a/services/cloudsyncservice/include/ipc/cloud_sync_service_stub.h b/services/cloudsyncservice/include/ipc/cloud_sync_service_stub.h index b9c4ff2e098c19e5ef833a4012a770737ad50988..d62a418a51a1e83467ef3a4a9c97488edd39a4af 100644 --- a/services/cloudsyncservice/include/ipc/cloud_sync_service_stub.h +++ b/services/cloudsyncservice/include/ipc/cloud_sync_service_stub.h @@ -38,6 +38,7 @@ private: int32_t HandleNotifyDataChange(MessageParcel &data, MessageParcel &reply); int32_t HandleEnableCloud(MessageParcel &data, MessageParcel &reply); int32_t HandleDisableCloud(MessageParcel &data, MessageParcel &reply); + int32_t HandleDownloadFile(MessageParcel &data, MessageParcel &reply); }; } // 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 e5cf4e31715462a648f3b398d36036f57bf03106..0710aecf60e5192521e75466ccf6385384be80de 100644 --- a/services/cloudsyncservice/src/data_sync/data_sync_manager.cpp +++ b/services/cloudsyncservice/src/data_sync/data_sync_manager.cpp @@ -33,6 +33,7 @@ DataSyncManager::DataSyncManager() { bundleNameConversionMap_["com.ohos.medialibrary.medialibrarydata"] = "com.ohos.photos"; bundleNameConversionMap_["com.ohos.photos"] = "com.ohos.photos"; + bundleNameConversionMap_["hdcd"] = "com.ohos.photos"; } int32_t DataSyncManager::TriggerStartSync(const std::string bundleName, @@ -89,6 +90,29 @@ int32_t DataSyncManager::TriggerStopSync(const std::string bundleName, return E_OK; } +int32_t DataSyncManager::DownloadSourceFile(const std::string bundleName, + const int32_t userId, + const std::string url, + const sptr processCallback, + const sptr downloadedCallback) +{ + auto it = bundleNameConversionMap_.find(bundleName); + if (it == bundleNameConversionMap_.end()) { + LOGE("trigger stop sync failed, bundleName: %{private}s, useId: %{private}d", bundleName.c_str(), userId); + return E_INVAL_ARG; + } + std::string appBundleName(it->second); + auto dataSyncer = GetDataSyncer(appBundleName, userId); + if (!dataSyncer) { + LOGE("Get dataSyncer failed, bundleName: %{private}s", appBundleName.c_str()); + return E_SYNCER_NUM_OUT_OF_RANGE; + } + std::thread([dataSyncer, url, processCallback, downloadedCallback]() { + dataSyncer->DownloadSourceFile(url, processCallback, downloadedCallback); + }).detach(); + return E_OK; +} + int32_t DataSyncManager::TriggerRecoverySync(SyncTriggerType triggerType) { std::vector needSyncApps; @@ -153,4 +177,4 @@ int32_t DataSyncManager::IsSkipSync(const std::string bundleName, const int32_t } return E_OK; } -} // namespace OHOS::FileManagement::CloudSync \ No newline at end of file +} // namespace OHOS::FileManagement::CloudSync diff --git a/services/cloudsyncservice/src/data_sync/data_syncer.cpp b/services/cloudsyncservice/src/data_sync/data_syncer.cpp index 9b817a7bbb30afa1c8599762b9a75b3909959277..5966878891c13d57416ca3fb3eaa396bf8b4e4de 100644 --- a/services/cloudsyncservice/src/data_sync/data_syncer.cpp +++ b/services/cloudsyncservice/src/data_sync/data_syncer.cpp @@ -120,6 +120,13 @@ int32_t DataSyncer::StopSync(SyncTriggerType triggerType) return E_OK; } +int32_t DataSyncer::DownloadSourceFile(const std::string url, + const sptr processCallback, + const sptr downloadedCallback) +{ + return E_OK; +} + void DataSyncer::Abort() { LOGI("%{private}d %{private}s aborts", userId_, bundleName_.c_str()); @@ -322,6 +329,48 @@ void DataSyncer::OnFetchRecords(const std::shared_ptr context, std::s } } +int32_t DataSyncer::DownloadInner(std::shared_ptr handler, + const std::string url, + const sptr processCallback, + const sptr downloadedCallback) +{ + DKDownloadId id; + auto ctx = std::make_shared(handler); + std::vector assetsToDownload; + int32_t ret = handler->GetDownloadAsset(url, assetsToDownload); + if (ret != E_OK) { + LOGE("handler on fetch records err %{public}d", ret); + return ret; + } + + auto downloadResultCallback = [downloadedCallback](std::shared_ptr context, + std::shared_ptr database, + const std::map &results, + const DriveKit::DKError &err) { + LOGI("download result %{public}d", err.serverErrorCode); + if (downloadedCallback != nullptr) { + downloadedCallback->OnDownloadedResult(err.serverErrorCode); + } + }; + auto downloadResultPtr = std::make_shared, + std::shared_ptr, + const std::map &, + const DriveKit::DKError &)>>(downloadResultCallback); + auto downloadProcessCallback = [processCallback](std::shared_ptr context, + DKDownloadAsset asset, TotalSize totalSize, + DownloadSize downloadSize) { + if (processCallback != nullptr) { + processCallback->OnDownloadProcess(downloadSize, totalSize); + } + }; + auto downloadProcessPtr = std::make_shared, DKDownloadAsset, TotalSize, DownloadSize)>>(downloadProcessCallback); + + auto dctx = make_shared(handler, assetsToDownload, id, + downloadResultPtr, ctx, nullptr, downloadProcessPtr); + DownloadAssets(static_pointer_cast(dctx)); + return E_OK; +} + void DataSyncer::OnFetchDatabaseChanges(const std::shared_ptr context, std::shared_ptr database, std::shared_ptr> records, DKQueryCursor nextCursor, diff --git a/services/cloudsyncservice/src/data_sync/gallery_data_sync/file_data_convertor.cpp b/services/cloudsyncservice/src/data_sync/gallery_data_sync/file_data_convertor.cpp index fa71f957a6c602a456ccb9b3f0dc4a00bd11c5d4..0d55d76e8323b990f6070b55dd5010e5ecfd0d68 100644 --- a/services/cloudsyncservice/src/data_sync/gallery_data_sync/file_data_convertor.cpp +++ b/services/cloudsyncservice/src/data_sync/gallery_data_sync/file_data_convertor.cpp @@ -453,6 +453,16 @@ string FileDataConvertor::GetLowerPath(const std::string &path) sandboxPrefix_.size()); } +string FileDataConvertor::GetLowerTmpPath(const std::string &path) +{ + size_t pos = path.find_first_of(sandboxPrefix_); + if (pos == string::npos) { + LOGE("invalid path %{private}s", path.c_str()); + return ""; + } + return prefix_ + to_string(userId_) + suffix_ + path.substr(pos + + sandboxPrefix_.size()) + ".tmp"; +} string FileDataConvertor::GetThumbPath(const std::string &path, const std::string &key) { diff --git a/services/cloudsyncservice/src/data_sync/gallery_data_sync/file_data_handler.cpp b/services/cloudsyncservice/src/data_sync/gallery_data_sync/file_data_handler.cpp index f8d39b015d18a4d5dd40d27ee1344988834a20cd..fcc4f1ad293786c5eecca8efaf9a7919378eab70 100644 --- a/services/cloudsyncservice/src/data_sync/gallery_data_sync/file_data_handler.cpp +++ b/services/cloudsyncservice/src/data_sync/gallery_data_sync/file_data_handler.cpp @@ -548,8 +548,12 @@ void FileDataHandler::AppendToDownload(const DKRecord &record, } string path; prop[MEDIA_DATA_DB_FILE_PATH].GetString(path); - const string &suffix = fieldKey == "lcd" ? LCD_SUFFIX : THUMB_SUFFIX; - downloadAsset.downLoadPath = createConvertor_.GetThumbPath(path, suffix); + if (fieldKey != "content") { + const string &suffix = fieldKey == "lcd" ? LCD_SUFFIX : THUMB_SUFFIX; + downloadAsset.downLoadPath = createConvertor_.GetThumbPath(path, suffix); + } else { + downloadAsset.downLoadPath = createConvertor_.GetLowerTmpPath(path); + } downloadAsset.asset.assetName = GetFileName(downloadAsset.downLoadPath); downloadAsset.downLoadPath = GetParentDir(downloadAsset.downLoadPath); ForceCreateDirectory(downloadAsset.downLoadPath); @@ -650,6 +654,38 @@ int32_t FileDataHandler::GetMetaModifiedRecords(vector &records) return E_OK; } +int32_t FileDataHandler::GetDownloadAsset(std::string cloudId, vector &outAssetsToDownload) +{ + vector records; + NativeRdb::AbsRdbPredicates predicates = NativeRdb::AbsRdbPredicates(TABLE_NAME); + predicates.SetWhereClause(Media::MEDIA_DATA_DB_CLOUD_ID + " = ?"); + predicates.SetWhereArgs({cloudId}); + predicates.Limit(LIMIT_SIZE); + auto resultSet = Query(predicates, GALLERY_FILE_COLUMNS); + if (resultSet == nullptr) { + LOGE("get nullptr created result"); + return E_RDB; + } + int32_t rowCount = 0; + int32_t ret = resultSet->GetRowCount(rowCount); + if (ret != 0) { + LOGE("result set get row count err %{public}d", ret); + return E_RDB; + } + + ret = localConvertor_.ResultSetToRecords(move(resultSet), records); + if (ret != 0) { + LOGE("result set to records err %{public}d", ret); + return ret; + } + for (const auto &record : records) { + AppendToDownload(record, "content", outAssetsToDownload); + } + + return E_OK; +} + + int32_t FileDataHandler::GetFileModifiedRecords(vector &records) { /* build predicates */ diff --git a/services/cloudsyncservice/src/data_sync/gallery_data_sync/gallery_data_syncer.cpp b/services/cloudsyncservice/src/data_sync/gallery_data_sync/gallery_data_syncer.cpp index 10665df069ddd087c92ac6c2022ab6639a88893f..cbb172544ba51c9b13319b7f887a823a2f2ad3a1 100644 --- a/services/cloudsyncservice/src/data_sync/gallery_data_sync/gallery_data_syncer.cpp +++ b/services/cloudsyncservice/src/data_sync/gallery_data_sync/gallery_data_syncer.cpp @@ -55,6 +55,13 @@ GalleryDataSyncer::GalleryDataSyncer(const std::string bundleName, const int32_t fileHandler_ = make_shared(userId, bundleName, rdb_); } +int32_t GalleryDataSyncer::DownloadSourceFile(const std::string url, + const sptr processCallback, + const sptr downloadedCallback) +{ + return DownloadInner(fileHandler_, url, processCallback, downloadedCallback); +} + void GalleryDataSyncer::Schedule() { LOGI("schedule to stage %{public}d", ++stage_); diff --git a/services/cloudsyncservice/src/ipc/cloud_downloaded_callback_proxy.cpp b/services/cloudsyncservice/src/ipc/cloud_downloaded_callback_proxy.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0816fc46e85e2efc4b971f8b3b583ab45cb36030 --- /dev/null +++ b/services/cloudsyncservice/src/ipc/cloud_downloaded_callback_proxy.cpp @@ -0,0 +1,59 @@ +/* + * 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 "ipc/cloud_downloaded_callback_proxy.h" + +#include + +#include "dfs_error.h" +#include "utils_log.h" + +namespace OHOS::FileManagement::CloudSync { +using namespace std; + +void CloudDownloadedCallbackProxy::OnDownloadedResult(int32_t result) +{ + LOGI("Start"); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(GetDescriptor())) { + LOGE("Failed to write interface token"); + return; + } + + if (!data.WriteInt32(result)) { + LOGE("Failed to send the resutl"); + return; + } + + auto remote = Remote(); + if (!remote) { + LOGE("remote is nullptr"); + return; + } + int32_t ret = remote->SendRequest(ICloudDownloadedCallback::SERVICE_CMD_ON_DOWNLOADED, 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 diff --git a/services/cloudsyncservice/src/ipc/cloud_process_callback_proxy.cpp b/services/cloudsyncservice/src/ipc/cloud_process_callback_proxy.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5748b4ad369c95ba4d82da820bd0225b66fcb5c1 --- /dev/null +++ b/services/cloudsyncservice/src/ipc/cloud_process_callback_proxy.cpp @@ -0,0 +1,63 @@ +/* + * 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 "ipc/cloud_process_callback_proxy.h" + +#include + +#include "dfs_error.h" +#include "utils_log.h" + +namespace OHOS::FileManagement::CloudSync { +using namespace std; + +void CloudProcessCallbackProxy::OnDownloadProcess(int64_t downloadedSize, int64_t totalSize) +{ + LOGI("Start"); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(GetDescriptor())) { + LOGE("Failed to write interface token"); + return; + } + + if (!data.WriteInt64(downloadedSize)) { + LOGE("Failed to send the type"); + return; + } + + if (!data.WriteInt64(totalSize)) { + LOGE("Failed to send the state"); + return; + } + + auto remote = Remote(); + if (!remote) { + LOGE("remote is nullptr"); + return; + } + int32_t ret = remote->SendRequest(ICloudProcessCallback::SERVICE_CMD_ON_PROCESS, 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 diff --git a/services/cloudsyncservice/src/ipc/cloud_sync_callback_manager.cpp b/services/cloudsyncservice/src/ipc/cloud_sync_callback_manager.cpp index 8323885946190a1d7fa5cc854d49ad423a71aade..8117af2c241ccbd00c53f6ef0fb5b04643bbdc74 100644 --- a/services/cloudsyncservice/src/ipc/cloud_sync_callback_manager.cpp +++ b/services/cloudsyncservice/src/ipc/cloud_sync_callback_manager.cpp @@ -73,4 +73,4 @@ void CloudSyncCallbackManager::NotifySyncStateChanged(const SyncType type, const auto callback = bind(&CloudSyncCallbackManager::Notify, this, _1, _2, type, state); callbackListMap_.Iterate(callback); } -} // namespace OHOS::FileManagement::CloudSync \ No newline at end of file +} // namespace OHOS::FileManagement::CloudSync diff --git a/services/cloudsyncservice/src/ipc/cloud_sync_service.cpp b/services/cloudsyncservice/src/ipc/cloud_sync_service.cpp index 988346ac156f90be538db45ced089239f08b8361..518e8f0a02eaae173410a48309f245e6a47887c2 100644 --- a/services/cloudsyncservice/src/ipc/cloud_sync_service.cpp +++ b/services/cloudsyncservice/src/ipc/cloud_sync_service.cpp @@ -145,4 +145,20 @@ int32_t CloudSyncService::Clean(const std::string &accountId, const CleanOptions { return E_OK; } + +int32_t CloudSyncService::DownloadFile(const std::string &url, const sptr &processCallback, const sptr &downloadedCallback) +{ + auto callerUserId = DfsuAccessTokenHelper::GetUserId(); + auto processCb = iface_cast(processCallback); + auto downloadedCb = iface_cast(downloadedCallback); + string bundleName; + LOGI("download file start"); + if (DfsuAccessTokenHelper::GetCallerBundleName(bundleName)) { + return E_INVAL_ARG; + } + if (callerUserId == 0) { + callerUserId = 100; // for root user change id to main user for test + } + return dataSyncManager_->DownloadSourceFile(bundleName, callerUserId, url, processCb, downloadedCb); +} } // namespace OHOS::FileManagement::CloudSync diff --git a/services/cloudsyncservice/src/ipc/cloud_sync_service_stub.cpp b/services/cloudsyncservice/src/ipc/cloud_sync_service_stub.cpp index 23af67c94b233510118ef8dc62b61b8abb00cf6f..a209b5e39f08410f6a15bc56ebf0e573e0e2eaaa 100644 --- a/services/cloudsyncservice/src/ipc/cloud_sync_service_stub.cpp +++ b/services/cloudsyncservice/src/ipc/cloud_sync_service_stub.cpp @@ -30,6 +30,7 @@ CloudSyncServiceStub::CloudSyncServiceStub() opToInterfaceMap_[SERVICE_CMD_NOTIFY_DATA_CHANGE] = &CloudSyncServiceStub::HandleNotifyDataChange; opToInterfaceMap_[SERVICE_CMD_ENABLE_CLOUD] = &CloudSyncServiceStub::HandleEnableCloud; opToInterfaceMap_[SERVICE_CMD_DISABLE_CLOUD] = &CloudSyncServiceStub::HandleDisableCloud; + opToInterfaceMap_[SERVICE_CMD_DOWNLOAD_FILE] = &CloudSyncServiceStub::HandleDownloadFile; } int32_t CloudSyncServiceStub::OnRemoteRequest(uint32_t code, @@ -197,4 +198,25 @@ int32_t CloudSyncServiceStub::HandleEnableCloud(MessageParcel &data, MessageParc LOGI("End EnableCloud"); return res; } + +int32_t CloudSyncServiceStub::HandleDownloadFile(MessageParcel &data, MessageParcel &reply) +{ + LOGI("Begin DownloadFile"); + if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC_MANAGER)) { + LOGE("permission denied"); + return E_PERMISSION_DENIED; + } + if (!DfsuAccessTokenHelper::IsSystemApp()) { + LOGE("caller hap is not system hap"); + return E_PERMISSION_SYSTEM; + } + string url = data.ReadString(); + auto processCallback = data.ReadRemoteObject(); + auto downloadedCallback = data.ReadRemoteObject(); + + int32_t res = DownloadFile(url, processCallback, downloadedCallback); + reply.WriteInt32(res); + LOGI("End DownloadFile"); + return res; +} } // namespace OHOS::FileManagement::CloudSync