1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef UPLOAD_TASK_NAPIV9_H 17 #define UPLOAD_TASK_NAPIV9_H 18 19 #include <string> 20 #include <vector> 21 #include "upload_config.h" 22 #include "upload_task.h" 23 #include "upload_common.h" 24 #include "async_call.h" 25 #include "progress_callback.h" 26 #include "header_receive_callback.h" 27 #include "notify_callback.h" 28 #include "i_progress_callback.h" 29 #include "i_header_receive_callback.h" 30 #include "i_notify_callback.h" 31 #include "context.h" 32 #include "ability_context.h" 33 #include "data_ability_helper.h" 34 35 namespace OHOS::Request::UploadNapi { 36 using namespace OHOS::Request::Upload; 37 class UploadTaskNapiV9 { 38 public: 39 static napi_value JsUploadFile(napi_env env, napi_callback_info info); 40 static napi_value JsOn(napi_env env, napi_callback_info info); 41 static napi_value JsOff(napi_env env, napi_callback_info info); 42 static napi_value JsDelete(napi_env env, napi_callback_info info); 43 44 UploadTaskNapiV9 &operator=(std::shared_ptr<Upload::UploadTask> &&uploadTask); 45 bool operator==(const std::shared_ptr<Upload::UploadTask> &uploadTask); 46 47 private: 48 static napi_value GetCtor(napi_env env); 49 static napi_value Initialize(napi_env env, napi_callback_info info); 50 static napi_status InitParam(napi_env env, napi_callback_info info, napi_value &self, UploadTaskNapiV9 *proxy); 51 static napi_status GetContext(napi_env env, napi_value *argv, 52 std::shared_ptr<OHOS::AbilityRuntime::Context>& context); 53 static uint32_t InitFileArray(const std::shared_ptr<Upload::UploadConfig> &config, 54 std::shared_ptr<OHOS::AbilityRuntime::Context> &context, int64_t &totalSize, std::vector<FileData> &fileDatas); 55 56 std::shared_ptr<Upload::UploadTask> napiUploadTask_ = nullptr; 57 std::shared_ptr<Upload::UploadConfig> napiUploadConfig_ = nullptr; 58 59 struct RemoveContextInfo : public AsyncCall::Context { 60 UploadTaskNapiV9 *proxy = nullptr; 61 bool removeStatus = false; 62 napi_status status = napi_generic_failure; RemoveContextInfoRemoveContextInfo63 RemoveContextInfo() : Context(nullptr, nullptr) {}; RemoveContextInfoRemoveContextInfo64 RemoveContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) {}; ~RemoveContextInfoRemoveContextInfo65 virtual ~RemoveContextInfo() {}; 66 operatorRemoveContextInfo67 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 68 { 69 NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); 70 NAPI_CALL_BASE(env, napi_unwrap(env, self, reinterpret_cast<void **>(&proxy)), napi_invalid_arg); 71 NAPI_ASSERT_BASE(env, proxy != nullptr, "there is no native upload task", napi_invalid_arg); 72 return Context::operator()(env, argc, argv, self); 73 } operatorRemoveContextInfo74 napi_status operator()(napi_env env, napi_value *result) override 75 { 76 if (status != napi_ok) { 77 return status; 78 } 79 return Context::operator()(env, result); 80 } 81 }; 82 83 struct JsParam { 84 std::string type; 85 napi_value callback; 86 napi_value self; 87 }; 88 89 using Exec = std::function<napi_status(napi_env, napi_value, napi_value)>; 90 static std::map<std::string, Exec> onTypeHandlers_; 91 static std::map<std::string, Exec> offTypeHandlers_; 92 static napi_status OnProgress(napi_env env, napi_value callback, napi_value self); 93 static napi_status OnHeaderReceive(napi_env env, napi_value callback, napi_value self); 94 static napi_status OnFail(napi_env env, napi_value callback, napi_value self); 95 static napi_status OnComplete(napi_env env, napi_value callback, napi_value self); 96 static napi_status OffProgress(napi_env env, napi_value callback, napi_value self); 97 static napi_status OffHeaderReceive(napi_env env, napi_value callback, napi_value self); 98 static napi_status OffFail(napi_env env, napi_value callback, napi_value self); 99 static napi_status OffComplete(napi_env env, napi_value callback, napi_value self); 100 static napi_status ParseParam(napi_env env, napi_callback_info info, bool IsRequiredParam, JsParam &jsParam); 101 static uint32_t CheckFilePath(const std::shared_ptr<Upload::UploadConfig> &config, 102 std::shared_ptr<OHOS::AbilityRuntime::Context> &context, std::vector<Upload::TaskState> &taskStates); 103 static uint32_t CheckAbilityPath(const std::string &fileUri, 104 std::shared_ptr<OHOS::AbilityRuntime::Context> &context); 105 static uint32_t CheckInternalPath(const std::string &fileUri, 106 std::shared_ptr<OHOS::AbilityRuntime::Context> &context); 107 108 std::shared_ptr<Upload::IProgressCallback> onProgress_ = nullptr; 109 std::shared_ptr<Upload::IHeaderReceiveCallback> onHeaderReceive_ = nullptr; 110 std::shared_ptr<Upload::INotifyCallback> onFail_ = nullptr; 111 std::shared_ptr<Upload::INotifyCallback> onComplete_ = nullptr; 112 std::shared_ptr<OHOS::AbilityRuntime::Context> context_ = nullptr; 113 }; 114 } // namespace OHOS::Request::UploadNapi 115 #endif // UPLOAD_TASK_NAPIV9_H