From 96f6786ab83202553e0d39c7f93cf50d070911c2 Mon Sep 17 00:00:00 2001 From: anyueling Date: Wed, 24 Aug 2022 20:35:34 +0800 Subject: [PATCH 1/9] =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E8=A1=A5=E5=85=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: anyueling --- upload/frameworks/libs/include/upload_task.h | 1 - upload/frameworks/libs/src/upload_task.cpp | 9 ---- .../kits/napi/src/upload_task_napi.cpp | 45 ++++++++++++++++--- 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/upload/frameworks/libs/include/upload_task.h b/upload/frameworks/libs/include/upload_task.h index 48cf1c72..c9be2ca0 100644 --- a/upload/frameworks/libs/include/upload_task.h +++ b/upload/frameworks/libs/include/upload_task.h @@ -52,7 +52,6 @@ public: UPLOAD_API virtual bool Remove(); UPLOAD_API virtual void On(Type type, void *callback); UPLOAD_API virtual void Off(Type type, void *callback); - UPLOAD_API virtual void Off(Type type); UPLOAD_API void ExecuteTask(); static void Run(void *arg); virtual void OnRun(); diff --git a/upload/frameworks/libs/src/upload_task.cpp b/upload/frameworks/libs/src/upload_task.cpp index 49aa64fa..e52d8fdb 100644 --- a/upload/frameworks/libs/src/upload_task.cpp +++ b/upload/frameworks/libs/src/upload_task.cpp @@ -64,13 +64,6 @@ void UploadTask::On(Type type, void *callback) SetCallback(type, callback); } -void UploadTask::Off(Type type) -{ - UPLOAD_HILOGD(UPLOAD_MODULE_FRAMEWORK, "Off. In."); - std::lock_guard guard(mutex_); - SetCallback(type, nullptr); -} - void UploadTask::Off(Type type, void *callback) { UPLOAD_HILOGD(UPLOAD_MODULE_FRAMEWORK, "Off. In."); @@ -82,8 +75,6 @@ void UploadTask::Off(Type type, void *callback) if (type == TYPE_PROGRESS_CALLBACK && progressCallback_ != nullptr) { ((IProgressCallback*)callback)->Progress(uploadedSize_, totalSize_); - } else { - UPLOAD_HILOGD(UPLOAD_MODULE_FRAMEWORK, "Off. type[%{public}d] not match.", type); } SetCallback(type, nullptr); } diff --git a/upload/interfaces/kits/napi/src/upload_task_napi.cpp b/upload/interfaces/kits/napi/src/upload_task_napi.cpp index e5a1c491..e1a5d8b3 100644 --- a/upload/interfaces/kits/napi/src/upload_task_napi.cpp +++ b/upload/interfaces/kits/napi/src/upload_task_napi.cpp @@ -298,27 +298,60 @@ napi_status UploadTaskNapi::OffHeaderReceive(napi_env env, napi_status UploadTaskNapi::OffFail(napi_env env, size_t argc, napi_value *argv, napi_value self, napi_value *result) { UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "Enter OffFail."); - NAPI_ASSERT_BASE(env, argc == 0, "argc should be 0", napi_invalid_arg); + NAPI_ASSERT_BASE(env, argc == 0 || argc == 1, "argc should be 0 or 1", napi_invalid_arg); NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); + std::shared_ptr callback = nullptr; + + if (argc == 1) { + UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "OffFail. argc == 1."); + napi_valuetype valueType = napi_undefined; + napi_typeof(env, argv[0], &valueType); + NAPI_ASSERT_BASE(env, valueType == napi_function, "callback is not a function", napi_invalid_arg); + } + UploadTaskNapi *proxy = nullptr; NAPI_CALL_BASE(env, napi_unwrap(env, self, reinterpret_cast(&proxy)), napi_invalid_arg); NAPI_ASSERT_BASE(env, proxy != nullptr, "there is no native upload task", napi_invalid_arg); - proxy->napiUploadTask_->Off(TYPE_FAIL_CALLBACK); + + if (proxy->onFail_ == nullptr) { + UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "Fail. proxy->onFail_ == nullptr."); + return napi_generic_failure; + } else { + callback = std::make_shared(proxy, env, argv[0]); + proxy->napiUploadTask_->Off(TYPE_FAIL_CALLBACK, (void *)(callback.get())); + proxy->onFail_ = nullptr; + } return napi_ok; } -napi_status UploadTaskNapi::OffComplete(napi_env env, size_t argc, napi_value *argv, - napi_value self, napi_value *result) +napi_status UploadTaskNapi::OffComplete(napi_env env, size_t argc, napi_value *argv, napi_value self, napi_value *result) { UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "Enter OffComplete."); - NAPI_ASSERT_BASE(env, argc == 0, "argc should be 0", napi_invalid_arg); + NAPI_ASSERT_BASE(env, argc == 0 || argc == 1, "argc should be 0 or 1", napi_invalid_arg); NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); + std::shared_ptr callback = nullptr; + + if (argc == 1) { + UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "OffComplete. argc == 1."); + napi_valuetype valueType = napi_undefined; + napi_typeof(env, argv[0], &valueType); + NAPI_ASSERT_BASE(env, valueType == napi_function, "callback is not a function", napi_invalid_arg); + } + UploadTaskNapi *proxy = nullptr; NAPI_CALL_BASE(env, napi_unwrap(env, self, reinterpret_cast(&proxy)), napi_invalid_arg); NAPI_ASSERT_BASE(env, proxy != nullptr, "there is no native upload task", napi_invalid_arg); - proxy->napiUploadTask_->Off(TYPE_COMPLETE_CALLBACK); + + if (proxy->onComplete_ == nullptr) { + UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "CompleteCallback. proxy->onFail_ == nullptr."); + return napi_generic_failure; + } else { + callback = std::make_shared(proxy, env, argv[0]); + proxy->napiUploadTask_->Off(TYPE_FAIL_CALLBACK, (void *)(callback.get())); + proxy->onComplete_ = nullptr; + } return napi_ok; } From fb42396199011841f30c2a2f716cf178f741fbba Mon Sep 17 00:00:00 2001 From: anyueling Date: Thu, 25 Aug 2022 10:04:00 +0800 Subject: [PATCH 2/9] =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E8=A1=A5=E5=85=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: anyueling --- upload/interfaces/kits/napi/src/upload_task_napi.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/upload/interfaces/kits/napi/src/upload_task_napi.cpp b/upload/interfaces/kits/napi/src/upload_task_napi.cpp index e1a5d8b3..a4afaa9e 100644 --- a/upload/interfaces/kits/napi/src/upload_task_napi.cpp +++ b/upload/interfaces/kits/napi/src/upload_task_napi.cpp @@ -295,7 +295,8 @@ napi_status UploadTaskNapi::OffHeaderReceive(napi_env env, return napi_ok; } -napi_status UploadTaskNapi::OffFail(napi_env env, size_t argc, napi_value *argv, napi_value self, napi_value *result) +napi_status UploadTaskNapi::OffFail(napi_env env, + size_t argc, napi_value *argv, napi_value self, napi_value *result) { UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "Enter OffFail."); NAPI_ASSERT_BASE(env, argc == 0 || argc == 1, "argc should be 0 or 1", napi_invalid_arg); From a951d34d7612e96564c84ac46b1bc420b5868d2c Mon Sep 17 00:00:00 2001 From: anyueling Date: Thu, 25 Aug 2022 10:56:33 +0800 Subject: [PATCH 3/9] =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E8=A1=A5=E5=85=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: anyueling --- upload/interfaces/kits/napi/src/upload_task_napi.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/upload/interfaces/kits/napi/src/upload_task_napi.cpp b/upload/interfaces/kits/napi/src/upload_task_napi.cpp index a4afaa9e..d5eeec81 100644 --- a/upload/interfaces/kits/napi/src/upload_task_napi.cpp +++ b/upload/interfaces/kits/napi/src/upload_task_napi.cpp @@ -326,7 +326,8 @@ napi_status UploadTaskNapi::OffFail(napi_env env, return napi_ok; } -napi_status UploadTaskNapi::OffComplete(napi_env env, size_t argc, napi_value *argv, napi_value self, napi_value *result) +napi_status UploadTaskNapi::OffComplete(napi_env env, + size_t argc, napi_value *argv, napi_value self, napi_value *result) { UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "Enter OffComplete."); NAPI_ASSERT_BASE(env, argc == 0 || argc == 1, "argc should be 0 or 1", napi_invalid_arg); From 6bcb9a611d4fa517a927e96082dfd2208c236d7d Mon Sep 17 00:00:00 2001 From: anyueling Date: Thu, 25 Aug 2022 14:47:00 +0800 Subject: [PATCH 4/9] =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E8=A1=A5=E5=85=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: anyueling --- upload/interfaces/kits/js/@ohos.request.d.ts | 1711 ++++++++--------- .../kits/napi/include/upload_task_napi.h | 1 + .../kits/napi/src/upload_task_napi.cpp | 36 +- 3 files changed, 875 insertions(+), 873 deletions(-) diff --git a/upload/interfaces/kits/js/@ohos.request.d.ts b/upload/interfaces/kits/js/@ohos.request.d.ts index c29275d0..71240158 100644 --- a/upload/interfaces/kits/js/@ohos.request.d.ts +++ b/upload/interfaces/kits/js/@ohos.request.d.ts @@ -1,856 +1,855 @@ -/* - * 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 { AsyncCallback } from './basic'; -import { Callback } from './basic'; -import BaseContext from './application/BaseContext'; - -/** - * upload and download - * - * @import request from '@ohos.request'; - * @permission ohos.permission.INTERNET - */ -declare namespace request { - - /** - * Bit flag indicating download is allowed when using the cellular network. - * @syscap SystemCapability.MiscServices.Download - * @since 6 - * @permission ohos.permission.INTERNET - */ - const NETWORK_MOBILE: number; - - /** - * Bit flag indicating download is allowed when using the WLAN. - * @syscap SystemCapability.MiscServices.Download - * @since 6 - * @permission ohos.permission.INTERNET - */ - const NETWORK_WIFI: number; - - /** - * Indicates that the download cannot be resumed for some temporary errors. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - */ - const ERROR_CANNOT_RESUME: number; - - /** - * Indicates that no storage device, such as an SD card, is found. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - */ - const ERROR_DEVICE_NOT_FOUND: number; - - /** - * Indicates that files to be downloaded already exist, and that the download session cannot overwrite the existing files. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - */ - const ERROR_FILE_ALREADY_EXISTS: number; - - /** - * Indicates that a file operation fails. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - */ - const ERROR_FILE_ERROR: number; - - /** - * Indicates that the HTTP transmission fails. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - */ - const ERROR_HTTP_DATA_ERROR: number; - - /** - * Indicates insufficient storage space. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - */ - const ERROR_INSUFFICIENT_SPACE: number; - - /** - * Indicates an error caused by too many network redirections. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - */ - const ERROR_TOO_MANY_REDIRECTS: number; - - /** - * Indicates an HTTP code that cannot be identified. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - */ - const ERROR_UNHANDLED_HTTP_CODE: number; - - /** - * Indicates an undefined error. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - */ - const ERROR_UNKNOWN: number; - - /** - * Indicates that the download is paused and waiting for a WLAN connection, because the file size exceeds the maximum allowed for a session using the cellular network. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - */ - const PAUSED_QUEUED_FOR_WIFI: number; - - /** - * Indicates that the download is paused for some reasons. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - */ - const PAUSED_UNKNOWN: number; - - /** - * Indicates that the download is paused due to a network problem, for example, network disconnection. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - */ - const PAUSED_WAITING_FOR_NETWORK: number; - - /** - * Indicates that a network error occurs, and the download session will be retried. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - */ - const PAUSED_WAITING_TO_RETRY: number; - - /** - * Indicates that the download session has failed and will not be retried. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - */ - const SESSION_FAILED: number; - - /** - * Indicates that the download session has been paused. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - */ - const SESSION_PAUSED: number; - - /** - * Indicates that the download session is being scheduled. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - */ - const SESSION_PENDING: number; - - /** - * Indicates that the download session is in progress. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - */ - const SESSION_RUNNING: number; - - /** - * Indicates that the download session is completed. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - */ - const SESSION_SUCCESSFUL: number; - - /** - * Starts a download session. - * @syscap SystemCapability.MiscServices.Download - * @since 6 - * @param config download config - * @param callback Indicate the callback function to receive DownloadTask. - * @permission ohos.permission.INTERNET - * @return - - * @FAModelOnly - */ - function download(config: DownloadConfig, callback: AsyncCallback): void; - - /** - * Starts a download session. - * @syscap SystemCapability.MiscServices.Download - * @since 9 - * @param BaseContext Indicates the application BaseContext. - * @param config download config - * @param callback Indicate the callback function to receive DownloadTask. - * @permission ohos.permission.INTERNET - * @return - - */ - function download(context: BaseContext, config: DownloadConfig, callback: AsyncCallback): void; - - /** - * Starts a download session. - * @syscap SystemCapability.MiscServices.Download - * @since 6 - * @param config download config - * @permission ohos.permission.INTERNET - * @return - - * @FAModelOnly - */ - function download(config: DownloadConfig): Promise; - - /** - * Starts a download session. - * @syscap SystemCapability.MiscServices.Download - * @since 9 - * @param BaseContext Indicates the application BaseContext. - * @param config download config - * @permission ohos.permission.INTERNET - * @return - - */ - function download(context: BaseContext, config: DownloadConfig): Promise; - - /** - * Starts a upload session. - * @syscap SystemCapability.MiscServices.Upload - * @since 6 - * @param config upload config - * @param callback Indicate the callback function to receive UploadTask. - * @permission ohos.permission.INTERNET - * @return - - * @FAModelOnly - */ - function upload(config: UploadConfig, callback: AsyncCallback): void; - - /** - * Starts a upload session. - * @syscap SystemCapability.MiscServices.Upload - * @since 9 - * @param BaseContext Indicates the application BaseContext. - * @param config upload config - * @param callback Indicate the callback function to receive UploadTask. - * @permission ohos.permission.INTERNET - * @return - - */ - function upload(context: BaseContext, config: UploadConfig, callback: AsyncCallback): void; - - /** - * Starts a upload session. - * @syscap SystemCapability.MiscServices.Upload - * @since 6 - * @param config upload config - * @permission ohos.permission.INTERNET - * @return - - * @FAModelOnly - */ - function upload(config: UploadConfig): Promise; - - /** - * Starts a upload session. - * @syscap SystemCapability.MiscServices.Upload - * @since 9 - * @param BaseContext Indicates the application BaseContext. - * @param config upload config - * @permission ohos.permission.INTERNET - * @return - - */ - function upload(context: BaseContext, config: UploadConfig): Promise; - - /** - * DownloadConfig data Structure - * - * @name DownloadConfig - * @since 6 - * @syscap SystemCapability.MiscServices.Download - * @permission ohos.permission.INTERNET - */ - interface DownloadConfig { - /** - * Resource address. - * - * @since 6 - * @permission ohos.permission.INTERNET - */ - url: string; - /** - * Adds an HTTP or HTTPS header to be included with the download request. - * - * @since 6 - * @permission ohos.permission.INTERNET - */ - header?: Object; - /** - * Allows download under a metered connection. - * - * @since 6 - * @permission ohos.permission.INTERNET - */ - enableMetered?: boolean; - /** - * Allows download in a roaming network. - * - * @since 6 - * @permission ohos.permission.INTERNET - */ - enableRoaming?: boolean; - /** - * Sets the description of a download session. - * - * @since 6 - * @permission ohos.permission.INTERNET - */ - description?: string; - /** - * Sets the network type allowed for download. - * - * @since 6 - * @permission ohos.permission.INTERNET - */ - networkType?: number; - /** - * Sets the path for downloads. - * - * @since 7 - * @permission ohos.permission.INTERNET - */ - filePath?: string; - /** - * Sets a download session title. - * - * @since 6 - * @permission ohos.permission.INTERNET - */ - title?: string; - /** - * Whether to display the background. - * - * @since 9 - * @permission N/A - */ - background?: boolean; - } - - /** - * DownloadInfo data Structure - * - * @name DownloadInfo - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - */ - interface DownloadInfo { - /** - * the description of a file to be downloaded. - * - * @since 7 - * @permission ohos.permission.INTERNET - */ - description: string; - /** - * the real-time downloads size (in bytes). - * - * @since 7 - * @permission ohos.permission.INTERNET - */ - downloadedBytes: number; - /** - * the ID of a file to be downloaded. - * - * @since 7 - * @permission ohos.permission.INTERNET - */ - downloadId: number; - /** - * a download failure cause, which can be any DownloadSession.ERROR_* constant. - * - * @since 7 - * @permission ohos.permission.INTERNET - */ - failedReason: number; - /** - * the name of a file to be downloaded. - * - * @since 7 - * @permission ohos.permission.INTERNET - */ - fileName: string; - /** - * the URI of a stored file. - * - * @since 7 - * @permission ohos.permission.INTERNET - */ - filePath: string; - /** - * the reason why a session is paused, which can be any DownloadSession.PAUSED_* constant. - * - * @since 7 - * @permission ohos.permission.INTERNET - */ - pausedReason: number; - /** - * the download status code, which can be any DownloadSession.SESSION_* constant. - * - * @since 7 - * @permission ohos.permission.INTERNET - */ - status: number; - /** - * the URI of files to be downloaded. - * - * @since 7 - * @permission ohos.permission.INTERNET - */ - targetURI: string; - /** - * the title of a file to be downloaded. - * - * @since 7 - * @permission ohos.permission.INTERNET - */ - downloadTitle: string; - /** - * the total size of files to be downloaded (in bytes). - * - * @since 7 - * @permission ohos.permission.INTERNET - */ - downloadTotalBytes: number; - } - - interface DownloadTask { - /** - * Called when the current download session is in process. - * @syscap SystemCapability.MiscServices.Download - * @since 6 - * @param type progress Indicates the download task progress. - * @param callback The callback function for the download progress change event - * receivedSize the length of downloaded data, in bytes - * totalSize he length of data expected to be downloaded, in bytes. - * @permission ohos.permission.INTERNET - * @return - - */ - on(type: 'progress', callback: (receivedSize: number, totalSize: number) => void): void; - - /** - * Called when the current download session is in process. - * @syscap SystemCapability.MiscServices.Download - * @since 6 - * @param type progress Indicates the download task progress. - * @param callback The callback function for the download progress change event - * receivedSize the length of downloaded data, in bytes - * totalSize he length of data expected to be downloaded, in bytes. - * @permission ohos.permission.INTERNET - * @return - - */ - off(type: 'progress', callback?: (receivedSize: number, totalSize: number) => void): void; - - /** - * Called when the current download session complete、pause or remove. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @param type Indicates the download session event type - * complete: download task completed, - * pause: download task stopped, - * remove: download task deleted. - * @param callback The callback function for the download complete、pause or remove change event. - * @permission ohos.permission.INTERNET - * @return - - */ - on(type: 'complete' | 'pause' | 'remove', callback: () => void): void; - - /** - * Called when the current download session complete、pause or remove. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @param type Indicates the download session event type - * complete: download task completed, - * pause: download task stopped, - * remove: download task deleted. - * @param callback The callback function for the download complete、pause or remove change event. - * @permission ohos.permission.INTERNET - * @return - - */ - off(type: 'complete' | 'pause' | 'remove', callback?: () => void): void; - - /** - * Called when the current download session fails. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @param type Indicates the download session type, fail: download task has failed. - * @param callback The callback function for the download fail change event - * err The error code for download task. - * @permission ohos.permission.INTERNET - * @return - - */ - on(type: 'fail', callback: (err: number) => void): void; - - /** - * Called when the current download session fails. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @param type Indicates the download session type, fail: download task has failed. - * @param callback Indicate the callback function to receive err. - * err The error code for download task. - * @permission ohos.permission.INTERNET - * @return - - */ - off(type: 'fail', callback?: (err: number) => void): void; - - /** - * Deletes a download session and the downloaded files. - * @syscap SystemCapability.MiscServices.Download - * @since 6 - * @param callback Indicates asynchronous invoking Result. - * @permission ohos.permission.INTERNET - * @return - - */ - remove(callback: AsyncCallback): void; - - /** - * Deletes a download session and the downloaded files. - * @syscap SystemCapability.MiscServices.Download - * @since 6 - * @permission ohos.permission.INTERNET - * @return - - */ - remove(): Promise; - - /** - * Pause a download session. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @param callback Indicates asynchronous invoking Result. - * @permission ohos.permission.INTERNET - * @return - - */ - pause(callback: AsyncCallback): void; - - /** - * Pause a download session. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - * @return - - */ - pause(): Promise; - - /** - * Resume a paused download session. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @param callback Indicates asynchronous invoking Result. - * @permission ohos.permission.INTERNET - * @return - - */ - resume(callback: AsyncCallback): void; - - /** - * Resume a paused download session. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - * @return - - */ - resume(): Promise; - - /** - * Queries download information of a session, which is defined in DownloadSession.DownloadInfo. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @param callback Indicate the callback function to receive download info. - * @permission ohos.permission.INTERNET - * @return - - */ - query(callback: AsyncCallback): void; - - /** - * Queries download information of a session, which is defined in DownloadSession.DownloadInfo. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - * @return - - */ - query(): Promise; - - /** - * Queries the MIME type of the download file. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @param callback Indicate the callback function to receive download file MIME type. - * @permission ohos.permission.INTERNET - * @return - - */ - queryMimeType(callback: AsyncCallback): void; - - /** - * Queries the MIME type of the download file. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - * @return - - */ - queryMimeType(): Promise; - } - - /** - * File data Structure - * - * @name File - * @since 6 - * @syscap SystemCapability.MiscServices.Download - * @permission ohos.permission.INTERNET - */ - interface File { - /** - * When multipart is submitted, the file name in the request header. - * - * @since 6 - * @permission ohos.permission.INTERNET - */ - filename: string; - /** - * When multipart is submitted, the name of the form item. The default is file. - * - * @since 6 - * @permission ohos.permission.INTERNET - */ - name: string; - /** - * The local storage path of the file (please refer to the storage directory definition for path usage). - * - * @since 6 - * @permission ohos.permission.INTERNET - */ - uri: string; - /** - * The content type of the file is obtained by default according to the suffix of the file name or path. - * - * @since 6 - * @permission ohos.permission.INTERNET - */ - type: string; - } - - /** - * RequestData data Structure - * - * @name RequestData - * @since 6 - * @syscap SystemCapability.MiscServices.Download - * @permission ohos.permission.INTERNET - */ - interface RequestData { - /** - * Represents the name of the form element. - * - * @since 6 - * @permission ohos.permission.INTERNET - */ - name: string; - /** - * Represents the value of the form element. - * - * @since 6 - * @permission ohos.permission.INTERNET - */ - value: string; - } - - /** - * UploadConfig data Structure - * - * @name UploadConfig - * @since 6 - * @syscap SystemCapability.MiscServices.Upload - * @permission ohos.permission.INTERNET - */ - interface UploadConfig { - /** - * Resource address. - * - * @since 6 - * @permission ohos.permission.INTERNET - */ - url: string; - /** - * Adds an HTTP or HTTPS header to be included with the upload request. - * - * @since 6 - * @permission ohos.permission.INTERNET - */ - header: Object; - /** - * Request method: POST, PUT. The default POST. - * - * @since 6 - * @permission ohos.permission.INTERNET - */ - method: string; - /** - * A list of files to be uploaded. Please use multipart/form-data to submit. - * - * @since 6 - * @permission ohos.permission.INTERNET - */ - files: Array; - /** - * The requested form data. - * - * @since 6 - * @permission ohos.permission.INTERNET - */ - data: Array; - } - - /** - * TaskState data Structure - * - * @name TaskState - * @since 9 - * @syscap SystemCapability.MiscServices.Upload - * @permission ohos.permission.INTERNET - */ - interface TaskState { - /** - * Upload file path. - * - * @since 9 - * @permission ohos.permission.INTERNET - */ - path: string; - /** - * Upload task return value. - * - * @since 9 - * @permission ohos.permission.INTERNET - */ - responseCode: number; - /** - * Upload task information. - * - * @since 9 - * @permission ohos.permission.INTERNET - */ - message: string; - } - - interface UploadTask { - /** - * Called when the current upload session is in process. - * @syscap SystemCapability.MiscServices.Upload - * @since 6 - * @param type progress Indicates the upload task progress. - * @param callback The callback function for the upload progress change event - * uploadedSize The length of uploaded data, in bytes - * totalSize The length of data expected to be uploaded, in bytes. - * @permission ohos.permission.INTERNET - * @return - - */ - on(type: 'progress', callback: (uploadedSize: number, totalSize: number) => void): void; - - /** - * Called when the current upload session is in process. - * @syscap SystemCapability.MiscServices.Upload - * @since 6 - * @param type progress Indicates the upload task progress. - * @param callback The callback function for the upload progress change event - * uploadedSize The length of uploaded data, in bytes - * totalSize The length of data expected to be uploaded, in bytes. - * @permission ohos.permission.INTERNET - * @return - - */ - off(type: 'progress', callback?: (uploadedSize: number, totalSize: number) => void): void; - - /** - * Called when the header of the current upload session has been received. - * @syscap SystemCapability.MiscServices.Upload - * @since 7 - * @param type headerReceive Indicates the upload task headed receive. - * @param callback The callback function for the HTTP Response Header event - * header HTTP Response Header returned by the developer server. - * @permission ohos.permission.INTERNET - * @return - - */ - on(type: 'headerReceive', callback: (header: object) => void): void; - - /** - * Called when the header of the current upload session has been received. - * @syscap SystemCapability.MiscServices.Upload - * @since 7 - * @param type headerReceive Indicates the upload task headed receive. - * @param callback The callback function for the HTTP Response Header event - * header HTTP Response Header returned by the developer server. - * @permission ohos.permission.INTERNET - * @return - - */ - off(type: 'headerReceive', callback?: (header: object) => void): void; - - /** - * Called when the current upload session complete or fail. - * @syscap SystemCapability.MiscServices.Upload - * @since 9 - * @param type Indicates the upload session event type - * complete: upload task completed - * fail: upload task failed - * @param callback The callback function for the upload complete or fail change event. - * @permission ohos.permission.INTERNET - * @return - - */ - on(type:'complete' | 'fail', callback: Callback>): void; - - /** - * Called when the current upload session complete or fail. - * @syscap SystemCapability.MiscServices.Upload - * @since 9 - * @param type Indicates the upload session event type - * complete: upload task completed - * fail: upload task failed - * @permission ohos.permission.INTERNET - * @return - - */ - off(type:'complete' | 'fail'): void; - - /** - * Deletes a upload session. - * @syscap SystemCapability.MiscServices.Upload - * @since 6 - * @param callback Indicates asynchronous invoking Result. - * @permission ohos.permission.INTERNET - * @return - - */ - remove(callback: AsyncCallback): void; - - /** - * Deletes a upload session. - * @syscap SystemCapability.MiscServices.Upload - * @since 6 - * @permission ohos.permission.INTERNET - * @return - - */ - remove(): Promise; - } -} - -export default request; - +/* + * 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 { AsyncCallback } from './basic'; +import { Callback } from './basic'; +import BaseContext from './application/BaseContext'; + +/** + * upload and download + * + * @import request from '@ohos.request'; + * @permission ohos.permission.INTERNET + */ +declare namespace request { + + /** + * Bit flag indicating download is allowed when using the cellular network. + * @syscap SystemCapability.MiscServices.Download + * @since 6 + * @permission ohos.permission.INTERNET + */ + const NETWORK_MOBILE: number; + + /** + * Bit flag indicating download is allowed when using the WLAN. + * @syscap SystemCapability.MiscServices.Download + * @since 6 + * @permission ohos.permission.INTERNET + */ + const NETWORK_WIFI: number; + + /** + * Indicates that the download cannot be resumed for some temporary errors. + * @syscap SystemCapability.MiscServices.Download + * @since 7 + * @permission ohos.permission.INTERNET + */ + const ERROR_CANNOT_RESUME: number; + + /** + * Indicates that no storage device, such as an SD card, is found. + * @syscap SystemCapability.MiscServices.Download + * @since 7 + * @permission ohos.permission.INTERNET + */ + const ERROR_DEVICE_NOT_FOUND: number; + + /** + * Indicates that files to be downloaded already exist, and that the download session cannot overwrite the existing files. + * @syscap SystemCapability.MiscServices.Download + * @since 7 + * @permission ohos.permission.INTERNET + */ + const ERROR_FILE_ALREADY_EXISTS: number; + + /** + * Indicates that a file operation fails. + * @syscap SystemCapability.MiscServices.Download + * @since 7 + * @permission ohos.permission.INTERNET + */ + const ERROR_FILE_ERROR: number; + + /** + * Indicates that the HTTP transmission fails. + * @syscap SystemCapability.MiscServices.Download + * @since 7 + * @permission ohos.permission.INTERNET + */ + const ERROR_HTTP_DATA_ERROR: number; + + /** + * Indicates insufficient storage space. + * @syscap SystemCapability.MiscServices.Download + * @since 7 + * @permission ohos.permission.INTERNET + */ + const ERROR_INSUFFICIENT_SPACE: number; + + /** + * Indicates an error caused by too many network redirections. + * @syscap SystemCapability.MiscServices.Download + * @since 7 + * @permission ohos.permission.INTERNET + */ + const ERROR_TOO_MANY_REDIRECTS: number; + + /** + * Indicates an HTTP code that cannot be identified. + * @syscap SystemCapability.MiscServices.Download + * @since 7 + * @permission ohos.permission.INTERNET + */ + const ERROR_UNHANDLED_HTTP_CODE: number; + + /** + * Indicates an undefined error. + * @syscap SystemCapability.MiscServices.Download + * @since 7 + * @permission ohos.permission.INTERNET + */ + const ERROR_UNKNOWN: number; + + /** + * Indicates that the download is paused and waiting for a WLAN connection, because the file size exceeds the maximum allowed for a session using the cellular network. + * @syscap SystemCapability.MiscServices.Download + * @since 7 + * @permission ohos.permission.INTERNET + */ + const PAUSED_QUEUED_FOR_WIFI: number; + + /** + * Indicates that the download is paused for some reasons. + * @syscap SystemCapability.MiscServices.Download + * @since 7 + * @permission ohos.permission.INTERNET + */ + const PAUSED_UNKNOWN: number; + + /** + * Indicates that the download is paused due to a network problem, for example, network disconnection. + * @syscap SystemCapability.MiscServices.Download + * @since 7 + * @permission ohos.permission.INTERNET + */ + const PAUSED_WAITING_FOR_NETWORK: number; + + /** + * Indicates that a network error occurs, and the download session will be retried. + * @syscap SystemCapability.MiscServices.Download + * @since 7 + * @permission ohos.permission.INTERNET + */ + const PAUSED_WAITING_TO_RETRY: number; + + /** + * Indicates that the download session has failed and will not be retried. + * @syscap SystemCapability.MiscServices.Download + * @since 7 + * @permission ohos.permission.INTERNET + */ + const SESSION_FAILED: number; + + /** + * Indicates that the download session has been paused. + * @syscap SystemCapability.MiscServices.Download + * @since 7 + * @permission ohos.permission.INTERNET + */ + const SESSION_PAUSED: number; + + /** + * Indicates that the download session is being scheduled. + * @syscap SystemCapability.MiscServices.Download + * @since 7 + * @permission ohos.permission.INTERNET + */ + const SESSION_PENDING: number; + + /** + * Indicates that the download session is in progress. + * @syscap SystemCapability.MiscServices.Download + * @since 7 + * @permission ohos.permission.INTERNET + */ + const SESSION_RUNNING: number; + + /** + * Indicates that the download session is completed. + * @syscap SystemCapability.MiscServices.Download + * @since 7 + * @permission ohos.permission.INTERNET + */ + const SESSION_SUCCESSFUL: number; + + /** + * Starts a download session. + * @syscap SystemCapability.MiscServices.Download + * @since 6 + * @param config download config + * @param callback Indicate the callback function to receive DownloadTask. + * @permission ohos.permission.INTERNET + * @return - + * @FAModelOnly + */ + function download(config: DownloadConfig, callback: AsyncCallback): void; + + /** + * Starts a download session. + * @syscap SystemCapability.MiscServices.Download + * @since 9 + * @param BaseContext Indicates the application BaseContext. + * @param config download config + * @param callback Indicate the callback function to receive DownloadTask. + * @permission ohos.permission.INTERNET + * @return - + */ + function download(context: BaseContext, config: DownloadConfig, callback: AsyncCallback): void; + + /** + * Starts a download session. + * @syscap SystemCapability.MiscServices.Download + * @since 6 + * @param config download config + * @permission ohos.permission.INTERNET + * @return - + * @FAModelOnly + */ + function download(config: DownloadConfig): Promise; + + /** + * Starts a download session. + * @syscap SystemCapability.MiscServices.Download + * @since 9 + * @param BaseContext Indicates the application BaseContext. + * @param config download config + * @permission ohos.permission.INTERNET + * @return - + */ + function download(context: BaseContext, config: DownloadConfig): Promise; + + /** + * Starts a upload session. + * @syscap SystemCapability.MiscServices.Upload + * @since 6 + * @param config upload config + * @param callback Indicate the callback function to receive UploadTask. + * @permission ohos.permission.INTERNET + * @return - + * @FAModelOnly + */ + function upload(config: UploadConfig, callback: AsyncCallback): void; + + /** + * Starts a upload session. + * @syscap SystemCapability.MiscServices.Upload + * @since 9 + * @param BaseContext Indicates the application BaseContext. + * @param config upload config + * @param callback Indicate the callback function to receive UploadTask. + * @permission ohos.permission.INTERNET + * @return - + */ + function upload(context: BaseContext, config: UploadConfig, callback: AsyncCallback): void; + + /** + * Starts a upload session. + * @syscap SystemCapability.MiscServices.Upload + * @since 6 + * @param config upload config + * @permission ohos.permission.INTERNET + * @return - + * @FAModelOnly + */ + function upload(config: UploadConfig): Promise; + + /** + * Starts a upload session. + * @syscap SystemCapability.MiscServices.Upload + * @since 9 + * @param BaseContext Indicates the application BaseContext. + * @param config upload config + * @permission ohos.permission.INTERNET + * @return - + */ + function upload(context: BaseContext, config: UploadConfig): Promise; + + /** + * DownloadConfig data Structure + * + * @name DownloadConfig + * @since 6 + * @syscap SystemCapability.MiscServices.Download + * @permission ohos.permission.INTERNET + */ + interface DownloadConfig { + /** + * Resource address. + * + * @since 6 + * @permission ohos.permission.INTERNET + */ + url: string; + /** + * Adds an HTTP or HTTPS header to be included with the download request. + * + * @since 6 + * @permission ohos.permission.INTERNET + */ + header?: Object; + /** + * Allows download under a metered connection. + * + * @since 6 + * @permission ohos.permission.INTERNET + */ + enableMetered?: boolean; + /** + * Allows download in a roaming network. + * + * @since 6 + * @permission ohos.permission.INTERNET + */ + enableRoaming?: boolean; + /** + * Sets the description of a download session. + * + * @since 6 + * @permission ohos.permission.INTERNET + */ + description?: string; + /** + * Sets the network type allowed for download. + * + * @since 6 + * @permission ohos.permission.INTERNET + */ + networkType?: number; + /** + * Sets the path for downloads. + * + * @since 7 + * @permission ohos.permission.INTERNET + */ + filePath?: string; + /** + * Sets a download session title. + * + * @since 6 + * @permission ohos.permission.INTERNET + */ + title?: string; + /** + * Allow download background task notifications. + * + * @since 9 + */ + background?: boolean; + } + + /** + * DownloadInfo data Structure + * + * @name DownloadInfo + * @syscap SystemCapability.MiscServices.Download + * @since 7 + * @permission ohos.permission.INTERNET + */ + interface DownloadInfo { + /** + * the description of a file to be downloaded. + * + * @since 7 + * @permission ohos.permission.INTERNET + */ + description: string; + /** + * the real-time downloads size (in bytes). + * + * @since 7 + * @permission ohos.permission.INTERNET + */ + downloadedBytes: number; + /** + * the ID of a file to be downloaded. + * + * @since 7 + * @permission ohos.permission.INTERNET + */ + downloadId: number; + /** + * a download failure cause, which can be any DownloadSession.ERROR_* constant. + * + * @since 7 + * @permission ohos.permission.INTERNET + */ + failedReason: number; + /** + * the name of a file to be downloaded. + * + * @since 7 + * @permission ohos.permission.INTERNET + */ + fileName: string; + /** + * the URI of a stored file. + * + * @since 7 + * @permission ohos.permission.INTERNET + */ + filePath: string; + /** + * the reason why a session is paused, which can be any DownloadSession.PAUSED_* constant. + * + * @since 7 + * @permission ohos.permission.INTERNET + */ + pausedReason: number; + /** + * the download status code, which can be any DownloadSession.SESSION_* constant. + * + * @since 7 + * @permission ohos.permission.INTERNET + */ + status: number; + /** + * the URI of files to be downloaded. + * + * @since 7 + * @permission ohos.permission.INTERNET + */ + targetURI: string; + /** + * the title of a file to be downloaded. + * + * @since 7 + * @permission ohos.permission.INTERNET + */ + downloadTitle: string; + /** + * the total size of files to be downloaded (in bytes). + * + * @since 7 + * @permission ohos.permission.INTERNET + */ + downloadTotalBytes: number; + } + + interface DownloadTask { + /** + * Called when the current download session is in process. + * @syscap SystemCapability.MiscServices.Download + * @since 6 + * @param type progress Indicates the download task progress. + * @param callback The callback function for the download progress change event + * receivedSize the length of downloaded data, in bytes + * totalSize he length of data expected to be downloaded, in bytes. + * @permission ohos.permission.INTERNET + * @return - + */ + on(type: 'progress', callback: (receivedSize: number, totalSize: number) => void): void; + + /** + * Called when the current download session is in process. + * @syscap SystemCapability.MiscServices.Download + * @since 6 + * @param type progress Indicates the download task progress. + * @param callback The callback function for the download progress change event + * receivedSize the length of downloaded data, in bytes + * totalSize he length of data expected to be downloaded, in bytes. + * @permission ohos.permission.INTERNET + * @return - + */ + off(type: 'progress', callback?: (receivedSize: number, totalSize: number) => void): void; + + /** + * Called when the current download session complete、pause or remove. + * @syscap SystemCapability.MiscServices.Download + * @since 7 + * @param type Indicates the download session event type + * complete: download task completed, + * pause: download task stopped, + * remove: download task deleted. + * @param callback The callback function for the download complete、pause or remove change event. + * @permission ohos.permission.INTERNET + * @return - + */ + on(type: 'complete' | 'pause' | 'remove', callback: () => void): void; + + /** + * Called when the current download session complete、pause or remove. + * @syscap SystemCapability.MiscServices.Download + * @since 7 + * @param type Indicates the download session event type + * complete: download task completed, + * pause: download task stopped, + * remove: download task deleted. + * @param callback The callback function for the download complete、pause or remove change event. + * @permission ohos.permission.INTERNET + * @return - + */ + off(type: 'complete' | 'pause' | 'remove', callback?: () => void): void; + + /** + * Called when the current download session fails. + * @syscap SystemCapability.MiscServices.Download + * @since 7 + * @param type Indicates the download session type, fail: download task has failed. + * @param callback The callback function for the download fail change event + * err The error code for download task. + * @permission ohos.permission.INTERNET + * @return - + */ + on(type: 'fail', callback: (err: number) => void): void; + + /** + * Called when the current download session fails. + * @syscap SystemCapability.MiscServices.Download + * @since 7 + * @param type Indicates the download session type, fail: download task has failed. + * @param callback Indicate the callback function to receive err. + * err The error code for download task. + * @permission ohos.permission.INTERNET + * @return - + */ + off(type: 'fail', callback?: (err: number) => void): void; + + /** + * Deletes a download session and the downloaded files. + * @syscap SystemCapability.MiscServices.Download + * @since 6 + * @param callback Indicates asynchronous invoking Result. + * @permission ohos.permission.INTERNET + * @return - + */ + remove(callback: AsyncCallback): void; + + /** + * Deletes a download session and the downloaded files. + * @syscap SystemCapability.MiscServices.Download + * @since 6 + * @permission ohos.permission.INTERNET + * @return - + */ + remove(): Promise; + + /** + * Pause a download session. + * @syscap SystemCapability.MiscServices.Download + * @since 7 + * @param callback Indicates asynchronous invoking Result. + * @permission ohos.permission.INTERNET + * @return - + */ + pause(callback: AsyncCallback): void; + + /** + * Pause a download session. + * @syscap SystemCapability.MiscServices.Download + * @since 7 + * @permission ohos.permission.INTERNET + * @return - + */ + pause(): Promise; + + /** + * Resume a paused download session. + * @syscap SystemCapability.MiscServices.Download + * @since 7 + * @param callback Indicates asynchronous invoking Result. + * @permission ohos.permission.INTERNET + * @return - + */ + resume(callback: AsyncCallback): void; + + /** + * Resume a paused download session. + * @syscap SystemCapability.MiscServices.Download + * @since 7 + * @permission ohos.permission.INTERNET + * @return - + */ + resume(): Promise; + + /** + * Queries download information of a session, which is defined in DownloadSession.DownloadInfo. + * @syscap SystemCapability.MiscServices.Download + * @since 7 + * @param callback Indicate the callback function to receive download info. + * @permission ohos.permission.INTERNET + * @return - + */ + query(callback: AsyncCallback): void; + + /** + * Queries download information of a session, which is defined in DownloadSession.DownloadInfo. + * @syscap SystemCapability.MiscServices.Download + * @since 7 + * @permission ohos.permission.INTERNET + * @return - + */ + query(): Promise; + + /** + * Queries the MIME type of the download file. + * @syscap SystemCapability.MiscServices.Download + * @since 7 + * @param callback Indicate the callback function to receive download file MIME type. + * @permission ohos.permission.INTERNET + * @return - + */ + queryMimeType(callback: AsyncCallback): void; + + /** + * Queries the MIME type of the download file. + * @syscap SystemCapability.MiscServices.Download + * @since 7 + * @permission ohos.permission.INTERNET + * @return - + */ + queryMimeType(): Promise; + } + + /** + * File data Structure + * + * @name File + * @since 6 + * @syscap SystemCapability.MiscServices.Download + * @permission ohos.permission.INTERNET + */ + interface File { + /** + * When multipart is submitted, the file name in the request header. + * + * @since 6 + * @permission ohos.permission.INTERNET + */ + filename: string; + /** + * When multipart is submitted, the name of the form item. The default is file. + * + * @since 6 + * @permission ohos.permission.INTERNET + */ + name: string; + /** + * The local storage path of the file (please refer to the storage directory definition for path usage). + * + * @since 6 + * @permission ohos.permission.INTERNET + */ + uri: string; + /** + * The content type of the file is obtained by default according to the suffix of the file name or path. + * + * @since 6 + * @permission ohos.permission.INTERNET + */ + type: string; + } + + /** + * RequestData data Structure + * + * @name RequestData + * @since 6 + * @syscap SystemCapability.MiscServices.Download + * @permission ohos.permission.INTERNET + */ + interface RequestData { + /** + * Represents the name of the form element. + * + * @since 6 + * @permission ohos.permission.INTERNET + */ + name: string; + /** + * Represents the value of the form element. + * + * @since 6 + * @permission ohos.permission.INTERNET + */ + value: string; + } + + /** + * UploadConfig data Structure + * + * @name UploadConfig + * @since 6 + * @syscap SystemCapability.MiscServices.Upload + * @permission ohos.permission.INTERNET + */ + interface UploadConfig { + /** + * Resource address. + * + * @since 6 + * @permission ohos.permission.INTERNET + */ + url: string; + /** + * Adds an HTTP or HTTPS header to be included with the upload request. + * + * @since 6 + * @permission ohos.permission.INTERNET + */ + header: Object; + /** + * Request method: POST, PUT. The default POST. + * + * @since 6 + * @permission ohos.permission.INTERNET + */ + method: string; + /** + * A list of files to be uploaded. Please use multipart/form-data to submit. + * + * @since 6 + * @permission ohos.permission.INTERNET + */ + files: Array; + /** + * The requested form data. + * + * @since 6 + * @permission ohos.permission.INTERNET + */ + data: Array; + } + + /** + * TaskState data Structure + * + * @name TaskState + * @since 9 + * @syscap SystemCapability.MiscServices.Upload + * @permission ohos.permission.INTERNET + */ + interface TaskState { + /** + * Upload file path. + * + * @since 9 + * @permission ohos.permission.INTERNET + */ + path: string; + /** + * Upload task return value. + * + * @since 9 + * @permission ohos.permission.INTERNET + */ + responseCode: number; + /** + * Upload task information. + * + * @since 9 + * @permission ohos.permission.INTERNET + */ + message: string; + } + + interface UploadTask { + /** + * Called when the current upload session is in process. + * @syscap SystemCapability.MiscServices.Upload + * @since 6 + * @param type progress Indicates the upload task progress. + * @param callback The callback function for the upload progress change event + * uploadedSize The length of uploaded data, in bytes + * totalSize The length of data expected to be uploaded, in bytes. + * @permission ohos.permission.INTERNET + * @return - + */ + on(type: 'progress', callback: (uploadedSize: number, totalSize: number) => void): void; + + /** + * Called when the current upload session is in process. + * @syscap SystemCapability.MiscServices.Upload + * @since 6 + * @param type progress Indicates the upload task progress. + * @param callback The callback function for the upload progress change event + * uploadedSize The length of uploaded data, in bytes + * totalSize The length of data expected to be uploaded, in bytes. + * @permission ohos.permission.INTERNET + * @return - + */ + off(type: 'progress', callback?: (uploadedSize: number, totalSize: number) => void): void; + + /** + * Called when the header of the current upload session has been received. + * @syscap SystemCapability.MiscServices.Upload + * @since 7 + * @param type headerReceive Indicates the upload task headed receive. + * @param callback The callback function for the HTTP Response Header event + * header HTTP Response Header returned by the developer server. + * @permission ohos.permission.INTERNET + * @return - + */ + on(type: 'headerReceive', callback: (header: object) => void): void; + + /** + * Called when the header of the current upload session has been received. + * @syscap SystemCapability.MiscServices.Upload + * @since 7 + * @param type headerReceive Indicates the upload task headed receive. + * @param callback The callback function for the HTTP Response Header event + * header HTTP Response Header returned by the developer server. + * @permission ohos.permission.INTERNET + * @return - + */ + off(type: 'headerReceive', callback?: (header: object) => void): void; + + /** + * Called when the current upload session complete or fail. + * @syscap SystemCapability.MiscServices.Upload + * @since 9 + * @param type Indicates the upload session event type + * complete: upload task completed + * fail: upload task failed + * @param callback The callback function for the upload complete or fail change event. + * @permission ohos.permission.INTERNET + * @return - + */ + on(type:'complete' | 'fail', callback: Callback>): void; + + /** + * Called when the current upload session complete or fail. + * @syscap SystemCapability.MiscServices.Upload + * @since 9 + * @param type Indicates the upload session event type + * complete: upload task completed + * fail: upload task failed + * @permission ohos.permission.INTERNET + * @return - + */ + off(type:'complete' | 'fail', callback?: Callback>): void; + + /** + * Deletes a upload session. + * @syscap SystemCapability.MiscServices.Upload + * @since 6 + * @param callback Indicates asynchronous invoking Result. + * @permission ohos.permission.INTERNET + * @return - + */ + remove(callback: AsyncCallback): void; + + /** + * Deletes a upload session. + * @syscap SystemCapability.MiscServices.Upload + * @since 6 + * @permission ohos.permission.INTERNET + * @return - + */ + remove(): Promise; + } +} + +export default request; + diff --git a/upload/interfaces/kits/napi/include/upload_task_napi.h b/upload/interfaces/kits/napi/include/upload_task_napi.h index 6b5f8c22..52a98e9e 100644 --- a/upload/interfaces/kits/napi/include/upload_task_napi.h +++ b/upload/interfaces/kits/napi/include/upload_task_napi.h @@ -117,6 +117,7 @@ private: size_t argc, napi_value *argv, napi_value self, napi_value *result); static napi_status OffFail(napi_env env, size_t argc, napi_value *argv, napi_value self, napi_value *result); static napi_status OffComplete(napi_env env, size_t argc, napi_value *argv, napi_value self, napi_value *result); + static napi_status CheckInput(napi_env env, size_t argc, napi_value *argv, napi_value self); std::shared_ptr onProgress_ = nullptr; std::shared_ptr onHeaderReceive_ = nullptr; diff --git a/upload/interfaces/kits/napi/src/upload_task_napi.cpp b/upload/interfaces/kits/napi/src/upload_task_napi.cpp index d5eeec81..c1f9f68e 100644 --- a/upload/interfaces/kits/napi/src/upload_task_napi.cpp +++ b/upload/interfaces/kits/napi/src/upload_task_napi.cpp @@ -295,22 +295,30 @@ napi_status UploadTaskNapi::OffHeaderReceive(napi_env env, return napi_ok; } -napi_status UploadTaskNapi::OffFail(napi_env env, - size_t argc, napi_value *argv, napi_value self, napi_value *result) +napi_status UploadTaskNapi::CheckInput(napi_env env, size_t argc, napi_value *argv, napi_value self) { - UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "Enter OffFail."); NAPI_ASSERT_BASE(env, argc == 0 || argc == 1, "argc should be 0 or 1", napi_invalid_arg); NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); - std::shared_ptr callback = nullptr; - if (argc == 1) { - UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "OffFail. argc == 1."); + UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "Input. argc == 1."); napi_valuetype valueType = napi_undefined; napi_typeof(env, argv[0], &valueType); NAPI_ASSERT_BASE(env, valueType == napi_function, "callback is not a function", napi_invalid_arg); } + return napi_ok; +} +napi_status UploadTaskNapi::OffFail(napi_env env, + size_t argc, napi_value *argv, napi_value self, napi_value *result) +{ + UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "Enter OffFail."); + napi_status status = CheckInput(env, argc, argv, self); + if (status != napi_ok) { + return status; + } + + std::shared_ptr callback = nullptr; UploadTaskNapi *proxy = nullptr; NAPI_CALL_BASE(env, napi_unwrap(env, self, reinterpret_cast(&proxy)), napi_invalid_arg); NAPI_ASSERT_BASE(env, proxy != nullptr, "there is no native upload task", napi_invalid_arg); @@ -326,26 +334,20 @@ napi_status UploadTaskNapi::OffFail(napi_env env, return napi_ok; } + napi_status UploadTaskNapi::OffComplete(napi_env env, size_t argc, napi_value *argv, napi_value self, napi_value *result) { UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "Enter OffComplete."); - NAPI_ASSERT_BASE(env, argc == 0 || argc == 1, "argc should be 0 or 1", napi_invalid_arg); - NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); - - std::shared_ptr callback = nullptr; - - if (argc == 1) { - UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "OffComplete. argc == 1."); - napi_valuetype valueType = napi_undefined; - napi_typeof(env, argv[0], &valueType); - NAPI_ASSERT_BASE(env, valueType == napi_function, "callback is not a function", napi_invalid_arg); + napi_status status = CheckInput(env, argc, argv, self); + if (status != napi_ok) { + return status; } + std::shared_ptr callback = nullptr; UploadTaskNapi *proxy = nullptr; NAPI_CALL_BASE(env, napi_unwrap(env, self, reinterpret_cast(&proxy)), napi_invalid_arg); NAPI_ASSERT_BASE(env, proxy != nullptr, "there is no native upload task", napi_invalid_arg); - if (proxy->onComplete_ == nullptr) { UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "CompleteCallback. proxy->onFail_ == nullptr."); return napi_generic_failure; From 54d5e27165b9bf920be52444a24a07d537f515bf Mon Sep 17 00:00:00 2001 From: anyueling Date: Thu, 25 Aug 2022 15:48:35 +0800 Subject: [PATCH 5/9] =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E8=A1=A5=E5=85=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: anyueling --- upload/interfaces/kits/js/@ohos.request.d.ts | 5 +-- .../kits/napi/include/upload_task_napi.h | 1 + .../kits/napi/src/upload_task_napi.cpp | 44 ++++++++++--------- 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/upload/interfaces/kits/js/@ohos.request.d.ts b/upload/interfaces/kits/js/@ohos.request.d.ts index c29275d0..5d0d0919 100644 --- a/upload/interfaces/kits/js/@ohos.request.d.ts +++ b/upload/interfaces/kits/js/@ohos.request.d.ts @@ -342,10 +342,9 @@ declare namespace request { */ title?: string; /** - * Whether to display the background. + * Allow download background task notifications. * * @since 9 - * @permission N/A */ background?: boolean; } @@ -829,7 +828,7 @@ declare namespace request { * @permission ohos.permission.INTERNET * @return - */ - off(type:'complete' | 'fail'): void; + off(type:'complete' | 'fail', callback?: Callback>): void; /** * Deletes a upload session. diff --git a/upload/interfaces/kits/napi/include/upload_task_napi.h b/upload/interfaces/kits/napi/include/upload_task_napi.h index 6b5f8c22..52a98e9e 100644 --- a/upload/interfaces/kits/napi/include/upload_task_napi.h +++ b/upload/interfaces/kits/napi/include/upload_task_napi.h @@ -117,6 +117,7 @@ private: size_t argc, napi_value *argv, napi_value self, napi_value *result); static napi_status OffFail(napi_env env, size_t argc, napi_value *argv, napi_value self, napi_value *result); static napi_status OffComplete(napi_env env, size_t argc, napi_value *argv, napi_value self, napi_value *result); + static napi_status CheckInput(napi_env env, size_t argc, napi_value *argv, napi_value self); std::shared_ptr onProgress_ = nullptr; std::shared_ptr onHeaderReceive_ = nullptr; diff --git a/upload/interfaces/kits/napi/src/upload_task_napi.cpp b/upload/interfaces/kits/napi/src/upload_task_napi.cpp index d5eeec81..bb3e3044 100644 --- a/upload/interfaces/kits/napi/src/upload_task_napi.cpp +++ b/upload/interfaces/kits/napi/src/upload_task_napi.cpp @@ -295,20 +295,27 @@ napi_status UploadTaskNapi::OffHeaderReceive(napi_env env, return napi_ok; } +napi_status UploadTaskNapi::CheckInput(napi_env env, size_t argc, napi_value *argv, napi_value self) +{ + NAPI_ASSERT_BASE(env, argc == 0 || argc == 1, "argc should be 0 or 1", napi_invalid_arg); + NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); + + if (argc == 1) { + UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "Input. argc == 1."); + napi_valuetype valueType = napi_undefined; + napi_typeof(env, argv[0], &valueType); + NAPI_ASSERT_BASE(env, valueType == napi_function, "callback is not a function", napi_invalid_arg); + } + return napi_ok; +} + napi_status UploadTaskNapi::OffFail(napi_env env, size_t argc, napi_value *argv, napi_value self, napi_value *result) { UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "Enter OffFail."); - NAPI_ASSERT_BASE(env, argc == 0 || argc == 1, "argc should be 0 or 1", napi_invalid_arg); - NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); - - std::shared_ptr callback = nullptr; - - if (argc == 1) { - UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "OffFail. argc == 1."); - napi_valuetype valueType = napi_undefined; - napi_typeof(env, argv[0], &valueType); - NAPI_ASSERT_BASE(env, valueType == napi_function, "callback is not a function", napi_invalid_arg); + napi_status status = CheckInput(env, argc, argv, self); + if (status != napi_ok) { + return status; } UploadTaskNapi *proxy = nullptr; @@ -319,6 +326,7 @@ napi_status UploadTaskNapi::OffFail(napi_env env, UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "Fail. proxy->onFail_ == nullptr."); return napi_generic_failure; } else { + std::shared_ptr callback = nullptr; callback = std::make_shared(proxy, env, argv[0]); proxy->napiUploadTask_->Off(TYPE_FAIL_CALLBACK, (void *)(callback.get())); proxy->onFail_ = nullptr; @@ -326,30 +334,24 @@ napi_status UploadTaskNapi::OffFail(napi_env env, return napi_ok; } + napi_status UploadTaskNapi::OffComplete(napi_env env, size_t argc, napi_value *argv, napi_value self, napi_value *result) { UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "Enter OffComplete."); - NAPI_ASSERT_BASE(env, argc == 0 || argc == 1, "argc should be 0 or 1", napi_invalid_arg); - NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); - - std::shared_ptr callback = nullptr; - - if (argc == 1) { - UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "OffComplete. argc == 1."); - napi_valuetype valueType = napi_undefined; - napi_typeof(env, argv[0], &valueType); - NAPI_ASSERT_BASE(env, valueType == napi_function, "callback is not a function", napi_invalid_arg); + napi_status status = CheckInput(env, argc, argv, self); + if (status != napi_ok) { + return status; } UploadTaskNapi *proxy = nullptr; NAPI_CALL_BASE(env, napi_unwrap(env, self, reinterpret_cast(&proxy)), napi_invalid_arg); NAPI_ASSERT_BASE(env, proxy != nullptr, "there is no native upload task", napi_invalid_arg); - if (proxy->onComplete_ == nullptr) { UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "CompleteCallback. proxy->onFail_ == nullptr."); return napi_generic_failure; } else { + std::shared_ptr callback = nullptr; callback = std::make_shared(proxy, env, argv[0]); proxy->napiUploadTask_->Off(TYPE_FAIL_CALLBACK, (void *)(callback.get())); proxy->onComplete_ = nullptr; From 399b2fe63dfea0ff22740602f98501edc267de71 Mon Sep 17 00:00:00 2001 From: anyueling Date: Thu, 25 Aug 2022 15:57:01 +0800 Subject: [PATCH 6/9] =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E8=A1=A5=E5=85=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: anyueling --- upload/interfaces/kits/napi/src/upload_task_napi.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/upload/interfaces/kits/napi/src/upload_task_napi.cpp b/upload/interfaces/kits/napi/src/upload_task_napi.cpp index 81464544..b35440ca 100644 --- a/upload/interfaces/kits/napi/src/upload_task_napi.cpp +++ b/upload/interfaces/kits/napi/src/upload_task_napi.cpp @@ -313,7 +313,7 @@ napi_status UploadTaskNapi::OffFail(napi_env env, size_t argc, napi_value *argv, napi_value self, napi_value *result) { UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "Enter OffFail."); - napi_status status = CheckInput(env, argc, argv, self); + napi_status status = CheckOffCompleteParam(env, argc, argv, self); if (status != napi_ok) { return status; } @@ -339,7 +339,7 @@ napi_status UploadTaskNapi::OffComplete(napi_env env, size_t argc, napi_value *argv, napi_value self, napi_value *result) { UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "Enter OffComplete."); - napi_status status = CheckInput(env, argc, argv, self); + napi_status status = CheckOffCompleteParam(env, argc, argv, self); if (status != napi_ok) { return status; } From d6895d6534a6541f3336caa2cb71589e90087037 Mon Sep 17 00:00:00 2001 From: anyueling Date: Thu, 25 Aug 2022 15:58:19 +0800 Subject: [PATCH 7/9] =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E8=A1=A5=E5=85=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: anyueling --- upload/interfaces/kits/js/@ohos.request.d.ts | 858 ------------------- 1 file changed, 858 deletions(-) diff --git a/upload/interfaces/kits/js/@ohos.request.d.ts b/upload/interfaces/kits/js/@ohos.request.d.ts index a473fce5..5d0d0919 100644 --- a/upload/interfaces/kits/js/@ohos.request.d.ts +++ b/upload/interfaces/kits/js/@ohos.request.d.ts @@ -1,4 +1,3 @@ -<<<<<<< HEAD /* * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); @@ -854,860 +853,3 @@ declare namespace request { export default request; -======= -/* - * 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 { AsyncCallback } from './basic'; -import { Callback } from './basic'; -import BaseContext from './application/BaseContext'; - -/** - * upload and download - * - * @import request from '@ohos.request'; - * @permission ohos.permission.INTERNET - */ -declare namespace request { - - /** - * Bit flag indicating download is allowed when using the cellular network. - * @syscap SystemCapability.MiscServices.Download - * @since 6 - * @permission ohos.permission.INTERNET - */ - const NETWORK_MOBILE: number; - - /** - * Bit flag indicating download is allowed when using the WLAN. - * @syscap SystemCapability.MiscServices.Download - * @since 6 - * @permission ohos.permission.INTERNET - */ - const NETWORK_WIFI: number; - - /** - * Indicates that the download cannot be resumed for some temporary errors. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - */ - const ERROR_CANNOT_RESUME: number; - - /** - * Indicates that no storage device, such as an SD card, is found. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - */ - const ERROR_DEVICE_NOT_FOUND: number; - - /** - * Indicates that files to be downloaded already exist, and that the download session cannot overwrite the existing files. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - */ - const ERROR_FILE_ALREADY_EXISTS: number; - - /** - * Indicates that a file operation fails. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - */ - const ERROR_FILE_ERROR: number; - - /** - * Indicates that the HTTP transmission fails. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - */ - const ERROR_HTTP_DATA_ERROR: number; - - /** - * Indicates insufficient storage space. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - */ - const ERROR_INSUFFICIENT_SPACE: number; - - /** - * Indicates an error caused by too many network redirections. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - */ - const ERROR_TOO_MANY_REDIRECTS: number; - - /** - * Indicates an HTTP code that cannot be identified. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - */ - const ERROR_UNHANDLED_HTTP_CODE: number; - - /** - * Indicates an undefined error. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - */ - const ERROR_UNKNOWN: number; - - /** - * Indicates that the download is paused and waiting for a WLAN connection, because the file size exceeds the maximum allowed for a session using the cellular network. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - */ - const PAUSED_QUEUED_FOR_WIFI: number; - - /** - * Indicates that the download is paused for some reasons. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - */ - const PAUSED_UNKNOWN: number; - - /** - * Indicates that the download is paused due to a network problem, for example, network disconnection. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - */ - const PAUSED_WAITING_FOR_NETWORK: number; - - /** - * Indicates that a network error occurs, and the download session will be retried. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - */ - const PAUSED_WAITING_TO_RETRY: number; - - /** - * Indicates that the download session has failed and will not be retried. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - */ - const SESSION_FAILED: number; - - /** - * Indicates that the download session has been paused. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - */ - const SESSION_PAUSED: number; - - /** - * Indicates that the download session is being scheduled. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - */ - const SESSION_PENDING: number; - - /** - * Indicates that the download session is in progress. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - */ - const SESSION_RUNNING: number; - - /** - * Indicates that the download session is completed. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - */ - const SESSION_SUCCESSFUL: number; - - /** - * Starts a download session. - * @syscap SystemCapability.MiscServices.Download - * @since 6 - * @param config download config - * @param callback Indicate the callback function to receive DownloadTask. - * @permission ohos.permission.INTERNET - * @return - - * @FAModelOnly - */ - function download(config: DownloadConfig, callback: AsyncCallback): void; - - /** - * Starts a download session. - * @syscap SystemCapability.MiscServices.Download - * @since 9 - * @param BaseContext Indicates the application BaseContext. - * @param config download config - * @param callback Indicate the callback function to receive DownloadTask. - * @permission ohos.permission.INTERNET - * @return - - */ - function download(context: BaseContext, config: DownloadConfig, callback: AsyncCallback): void; - - /** - * Starts a download session. - * @syscap SystemCapability.MiscServices.Download - * @since 6 - * @param config download config - * @permission ohos.permission.INTERNET - * @return - - * @FAModelOnly - */ - function download(config: DownloadConfig): Promise; - - /** - * Starts a download session. - * @syscap SystemCapability.MiscServices.Download - * @since 9 - * @param BaseContext Indicates the application BaseContext. - * @param config download config - * @permission ohos.permission.INTERNET - * @return - - */ - function download(context: BaseContext, config: DownloadConfig): Promise; - - /** - * Starts a upload session. - * @syscap SystemCapability.MiscServices.Upload - * @since 6 - * @param config upload config - * @param callback Indicate the callback function to receive UploadTask. - * @permission ohos.permission.INTERNET - * @return - - * @FAModelOnly - */ - function upload(config: UploadConfig, callback: AsyncCallback): void; - - /** - * Starts a upload session. - * @syscap SystemCapability.MiscServices.Upload - * @since 9 - * @param BaseContext Indicates the application BaseContext. - * @param config upload config - * @param callback Indicate the callback function to receive UploadTask. - * @permission ohos.permission.INTERNET - * @return - - */ - function upload(context: BaseContext, config: UploadConfig, callback: AsyncCallback): void; - - /** - * Starts a upload session. - * @syscap SystemCapability.MiscServices.Upload - * @since 6 - * @param config upload config - * @permission ohos.permission.INTERNET - * @return - - * @FAModelOnly - */ - function upload(config: UploadConfig): Promise; - - /** - * Starts a upload session. - * @syscap SystemCapability.MiscServices.Upload - * @since 9 - * @param BaseContext Indicates the application BaseContext. - * @param config upload config - * @permission ohos.permission.INTERNET - * @return - - */ - function upload(context: BaseContext, config: UploadConfig): Promise; - - /** - * DownloadConfig data Structure - * - * @name DownloadConfig - * @since 6 - * @syscap SystemCapability.MiscServices.Download - * @permission ohos.permission.INTERNET - */ - interface DownloadConfig { - /** - * Resource address. - * - * @since 6 - * @permission ohos.permission.INTERNET - */ - url: string; - /** - * Adds an HTTP or HTTPS header to be included with the download request. - * - * @since 6 - * @permission ohos.permission.INTERNET - */ - header?: Object; - /** - * Allows download under a metered connection. - * - * @since 6 - * @permission ohos.permission.INTERNET - */ - enableMetered?: boolean; - /** - * Allows download in a roaming network. - * - * @since 6 - * @permission ohos.permission.INTERNET - */ - enableRoaming?: boolean; - /** - * Sets the description of a download session. - * - * @since 6 - * @permission ohos.permission.INTERNET - */ - description?: string; - /** - * Sets the network type allowed for download. - * - * @since 6 - * @permission ohos.permission.INTERNET - */ - networkType?: number; - /** - * Sets the path for downloads. - * - * @since 7 - * @permission ohos.permission.INTERNET - */ - filePath?: string; - /** - * Sets a download session title. - * - * @since 6 - * @permission ohos.permission.INTERNET - */ - title?: string; - /** - * Allow download background task notifications. - * - * @since 9 - */ - background?: boolean; - } - - /** - * DownloadInfo data Structure - * - * @name DownloadInfo - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - */ - interface DownloadInfo { - /** - * the description of a file to be downloaded. - * - * @since 7 - * @permission ohos.permission.INTERNET - */ - description: string; - /** - * the real-time downloads size (in bytes). - * - * @since 7 - * @permission ohos.permission.INTERNET - */ - downloadedBytes: number; - /** - * the ID of a file to be downloaded. - * - * @since 7 - * @permission ohos.permission.INTERNET - */ - downloadId: number; - /** - * a download failure cause, which can be any DownloadSession.ERROR_* constant. - * - * @since 7 - * @permission ohos.permission.INTERNET - */ - failedReason: number; - /** - * the name of a file to be downloaded. - * - * @since 7 - * @permission ohos.permission.INTERNET - */ - fileName: string; - /** - * the URI of a stored file. - * - * @since 7 - * @permission ohos.permission.INTERNET - */ - filePath: string; - /** - * the reason why a session is paused, which can be any DownloadSession.PAUSED_* constant. - * - * @since 7 - * @permission ohos.permission.INTERNET - */ - pausedReason: number; - /** - * the download status code, which can be any DownloadSession.SESSION_* constant. - * - * @since 7 - * @permission ohos.permission.INTERNET - */ - status: number; - /** - * the URI of files to be downloaded. - * - * @since 7 - * @permission ohos.permission.INTERNET - */ - targetURI: string; - /** - * the title of a file to be downloaded. - * - * @since 7 - * @permission ohos.permission.INTERNET - */ - downloadTitle: string; - /** - * the total size of files to be downloaded (in bytes). - * - * @since 7 - * @permission ohos.permission.INTERNET - */ - downloadTotalBytes: number; - } - - interface DownloadTask { - /** - * Called when the current download session is in process. - * @syscap SystemCapability.MiscServices.Download - * @since 6 - * @param type progress Indicates the download task progress. - * @param callback The callback function for the download progress change event - * receivedSize the length of downloaded data, in bytes - * totalSize he length of data expected to be downloaded, in bytes. - * @permission ohos.permission.INTERNET - * @return - - */ - on(type: 'progress', callback: (receivedSize: number, totalSize: number) => void): void; - - /** - * Called when the current download session is in process. - * @syscap SystemCapability.MiscServices.Download - * @since 6 - * @param type progress Indicates the download task progress. - * @param callback The callback function for the download progress change event - * receivedSize the length of downloaded data, in bytes - * totalSize he length of data expected to be downloaded, in bytes. - * @permission ohos.permission.INTERNET - * @return - - */ - off(type: 'progress', callback?: (receivedSize: number, totalSize: number) => void): void; - - /** - * Called when the current download session complete、pause or remove. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @param type Indicates the download session event type - * complete: download task completed, - * pause: download task stopped, - * remove: download task deleted. - * @param callback The callback function for the download complete、pause or remove change event. - * @permission ohos.permission.INTERNET - * @return - - */ - on(type: 'complete' | 'pause' | 'remove', callback: () => void): void; - - /** - * Called when the current download session complete、pause or remove. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @param type Indicates the download session event type - * complete: download task completed, - * pause: download task stopped, - * remove: download task deleted. - * @param callback The callback function for the download complete、pause or remove change event. - * @permission ohos.permission.INTERNET - * @return - - */ - off(type: 'complete' | 'pause' | 'remove', callback?: () => void): void; - - /** - * Called when the current download session fails. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @param type Indicates the download session type, fail: download task has failed. - * @param callback The callback function for the download fail change event - * err The error code for download task. - * @permission ohos.permission.INTERNET - * @return - - */ - on(type: 'fail', callback: (err: number) => void): void; - - /** - * Called when the current download session fails. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @param type Indicates the download session type, fail: download task has failed. - * @param callback Indicate the callback function to receive err. - * err The error code for download task. - * @permission ohos.permission.INTERNET - * @return - - */ - off(type: 'fail', callback?: (err: number) => void): void; - - /** - * Deletes a download session and the downloaded files. - * @syscap SystemCapability.MiscServices.Download - * @since 6 - * @param callback Indicates asynchronous invoking Result. - * @permission ohos.permission.INTERNET - * @return - - */ - remove(callback: AsyncCallback): void; - - /** - * Deletes a download session and the downloaded files. - * @syscap SystemCapability.MiscServices.Download - * @since 6 - * @permission ohos.permission.INTERNET - * @return - - */ - remove(): Promise; - - /** - * Pause a download session. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @param callback Indicates asynchronous invoking Result. - * @permission ohos.permission.INTERNET - * @return - - */ - pause(callback: AsyncCallback): void; - - /** - * Pause a download session. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - * @return - - */ - pause(): Promise; - - /** - * Resume a paused download session. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @param callback Indicates asynchronous invoking Result. - * @permission ohos.permission.INTERNET - * @return - - */ - resume(callback: AsyncCallback): void; - - /** - * Resume a paused download session. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - * @return - - */ - resume(): Promise; - - /** - * Queries download information of a session, which is defined in DownloadSession.DownloadInfo. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @param callback Indicate the callback function to receive download info. - * @permission ohos.permission.INTERNET - * @return - - */ - query(callback: AsyncCallback): void; - - /** - * Queries download information of a session, which is defined in DownloadSession.DownloadInfo. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - * @return - - */ - query(): Promise; - - /** - * Queries the MIME type of the download file. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @param callback Indicate the callback function to receive download file MIME type. - * @permission ohos.permission.INTERNET - * @return - - */ - queryMimeType(callback: AsyncCallback): void; - - /** - * Queries the MIME type of the download file. - * @syscap SystemCapability.MiscServices.Download - * @since 7 - * @permission ohos.permission.INTERNET - * @return - - */ - queryMimeType(): Promise; - } - - /** - * File data Structure - * - * @name File - * @since 6 - * @syscap SystemCapability.MiscServices.Download - * @permission ohos.permission.INTERNET - */ - interface File { - /** - * When multipart is submitted, the file name in the request header. - * - * @since 6 - * @permission ohos.permission.INTERNET - */ - filename: string; - /** - * When multipart is submitted, the name of the form item. The default is file. - * - * @since 6 - * @permission ohos.permission.INTERNET - */ - name: string; - /** - * The local storage path of the file (please refer to the storage directory definition for path usage). - * - * @since 6 - * @permission ohos.permission.INTERNET - */ - uri: string; - /** - * The content type of the file is obtained by default according to the suffix of the file name or path. - * - * @since 6 - * @permission ohos.permission.INTERNET - */ - type: string; - } - - /** - * RequestData data Structure - * - * @name RequestData - * @since 6 - * @syscap SystemCapability.MiscServices.Download - * @permission ohos.permission.INTERNET - */ - interface RequestData { - /** - * Represents the name of the form element. - * - * @since 6 - * @permission ohos.permission.INTERNET - */ - name: string; - /** - * Represents the value of the form element. - * - * @since 6 - * @permission ohos.permission.INTERNET - */ - value: string; - } - - /** - * UploadConfig data Structure - * - * @name UploadConfig - * @since 6 - * @syscap SystemCapability.MiscServices.Upload - * @permission ohos.permission.INTERNET - */ - interface UploadConfig { - /** - * Resource address. - * - * @since 6 - * @permission ohos.permission.INTERNET - */ - url: string; - /** - * Adds an HTTP or HTTPS header to be included with the upload request. - * - * @since 6 - * @permission ohos.permission.INTERNET - */ - header: Object; - /** - * Request method: POST, PUT. The default POST. - * - * @since 6 - * @permission ohos.permission.INTERNET - */ - method: string; - /** - * A list of files to be uploaded. Please use multipart/form-data to submit. - * - * @since 6 - * @permission ohos.permission.INTERNET - */ - files: Array; - /** - * The requested form data. - * - * @since 6 - * @permission ohos.permission.INTERNET - */ - data: Array; - } - - /** - * TaskState data Structure - * - * @name TaskState - * @since 9 - * @syscap SystemCapability.MiscServices.Upload - * @permission ohos.permission.INTERNET - */ - interface TaskState { - /** - * Upload file path. - * - * @since 9 - * @permission ohos.permission.INTERNET - */ - path: string; - /** - * Upload task return value. - * - * @since 9 - * @permission ohos.permission.INTERNET - */ - responseCode: number; - /** - * Upload task information. - * - * @since 9 - * @permission ohos.permission.INTERNET - */ - message: string; - } - - interface UploadTask { - /** - * Called when the current upload session is in process. - * @syscap SystemCapability.MiscServices.Upload - * @since 6 - * @param type progress Indicates the upload task progress. - * @param callback The callback function for the upload progress change event - * uploadedSize The length of uploaded data, in bytes - * totalSize The length of data expected to be uploaded, in bytes. - * @permission ohos.permission.INTERNET - * @return - - */ - on(type: 'progress', callback: (uploadedSize: number, totalSize: number) => void): void; - - /** - * Called when the current upload session is in process. - * @syscap SystemCapability.MiscServices.Upload - * @since 6 - * @param type progress Indicates the upload task progress. - * @param callback The callback function for the upload progress change event - * uploadedSize The length of uploaded data, in bytes - * totalSize The length of data expected to be uploaded, in bytes. - * @permission ohos.permission.INTERNET - * @return - - */ - off(type: 'progress', callback?: (uploadedSize: number, totalSize: number) => void): void; - - /** - * Called when the header of the current upload session has been received. - * @syscap SystemCapability.MiscServices.Upload - * @since 7 - * @param type headerReceive Indicates the upload task headed receive. - * @param callback The callback function for the HTTP Response Header event - * header HTTP Response Header returned by the developer server. - * @permission ohos.permission.INTERNET - * @return - - */ - on(type: 'headerReceive', callback: (header: object) => void): void; - - /** - * Called when the header of the current upload session has been received. - * @syscap SystemCapability.MiscServices.Upload - * @since 7 - * @param type headerReceive Indicates the upload task headed receive. - * @param callback The callback function for the HTTP Response Header event - * header HTTP Response Header returned by the developer server. - * @permission ohos.permission.INTERNET - * @return - - */ - off(type: 'headerReceive', callback?: (header: object) => void): void; - - /** - * Called when the current upload session complete or fail. - * @syscap SystemCapability.MiscServices.Upload - * @since 9 - * @param type Indicates the upload session event type - * complete: upload task completed - * fail: upload task failed - * @param callback The callback function for the upload complete or fail change event. - * @permission ohos.permission.INTERNET - * @return - - */ - on(type:'complete' | 'fail', callback: Callback>): void; - - /** - * Called when the current upload session complete or fail. - * @syscap SystemCapability.MiscServices.Upload - * @since 9 - * @param type Indicates the upload session event type - * complete: upload task completed - * fail: upload task failed - * @permission ohos.permission.INTERNET - * @return - - */ - off(type:'complete' | 'fail', callback?: Callback>): void; - - /** - * Deletes a upload session. - * @syscap SystemCapability.MiscServices.Upload - * @since 6 - * @param callback Indicates asynchronous invoking Result. - * @permission ohos.permission.INTERNET - * @return - - */ - remove(callback: AsyncCallback): void; - - /** - * Deletes a upload session. - * @syscap SystemCapability.MiscServices.Upload - * @since 6 - * @permission ohos.permission.INTERNET - * @return - - */ - remove(): Promise; - } -} - -export default request; - ->>>>>>> 6bcb9a611d4fa517a927e96082dfd2208c236d7d From b97d3e8c6361aa43950e59f970dfcf000bda2a8e Mon Sep 17 00:00:00 2001 From: anyueling Date: Thu, 25 Aug 2022 16:57:10 +0800 Subject: [PATCH 8/9] =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E8=A1=A5=E5=85=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: anyueling --- upload/interfaces/kits/napi/src/upload_task_napi.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/upload/interfaces/kits/napi/src/upload_task_napi.cpp b/upload/interfaces/kits/napi/src/upload_task_napi.cpp index b35440ca..f3ed6b67 100644 --- a/upload/interfaces/kits/napi/src/upload_task_napi.cpp +++ b/upload/interfaces/kits/napi/src/upload_task_napi.cpp @@ -326,9 +326,8 @@ napi_status UploadTaskNapi::OffFail(napi_env env, UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "Fail. proxy->onFail_ == nullptr."); return napi_generic_failure; } else { - std::shared_ptr callback = nullptr; - callback = std::make_shared(proxy, env, argv[0]); - proxy->napiUploadTask_->Off(TYPE_FAIL_CALLBACK, (void *)(callback.get())); + std::shared_ptr callback = std::make_shared(proxy, env, argv[0]); + proxy->napiUploadTask_->Off(TYPE_FAIL_CALLBACK, callback.get()); proxy->onFail_ = nullptr; } return napi_ok; @@ -351,9 +350,8 @@ napi_status UploadTaskNapi::OffComplete(napi_env env, UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "CompleteCallback. proxy->onFail_ == nullptr."); return napi_generic_failure; } else { - std::shared_ptr callback = nullptr; - callback = std::make_shared(proxy, env, argv[0]); - proxy->napiUploadTask_->Off(TYPE_FAIL_CALLBACK, (void *)(callback.get())); + std::shared_ptr callback = std::make_shared(proxy, env, argv[0]); + proxy->napiUploadTask_->Off(TYPE_COMPLETE_CALLBACK, callback.get()); proxy->onComplete_ = nullptr; } return napi_ok; From 1110163e33b9c0e46444e17fd5ac0e7728a3277b Mon Sep 17 00:00:00 2001 From: anyueling Date: Thu, 25 Aug 2022 18:47:45 +0800 Subject: [PATCH 9/9] =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E8=A1=A5=E5=85=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: anyueling --- upload/interfaces/kits/napi/src/upload_task_napi.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/upload/interfaces/kits/napi/src/upload_task_napi.cpp b/upload/interfaces/kits/napi/src/upload_task_napi.cpp index f3ed6b67..1ab64e98 100644 --- a/upload/interfaces/kits/napi/src/upload_task_napi.cpp +++ b/upload/interfaces/kits/napi/src/upload_task_napi.cpp @@ -347,7 +347,7 @@ napi_status UploadTaskNapi::OffComplete(napi_env env, NAPI_CALL_BASE(env, napi_unwrap(env, self, reinterpret_cast(&proxy)), napi_invalid_arg); NAPI_ASSERT_BASE(env, proxy != nullptr, "there is no native upload task", napi_invalid_arg); if (proxy->onComplete_ == nullptr) { - UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "CompleteCallback. proxy->onFail_ == nullptr."); + UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "CompleteCallback. proxy->OffComplete_ == nullptr."); return napi_generic_failure; } else { std::shared_ptr callback = std::make_shared(proxy, env, argv[0]);