1 /* 2 * Copyright (c) 2023 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 LEGACY_DOWNLOAD_MANAGER_H 17 #define LEGACY_DOWNLOAD_MANAGER_H 18 19 #include <atomic> 20 #include <functional> 21 #include <map> 22 #include <mutex> 23 24 #include "js_common.h" 25 #include "legacy/download_task.h" 26 #include "napi/native_api.h" 27 #include "napi/native_common.h" 28 #include "visibility.h" 29 30 namespace OHOS::Request::Legacy { 31 class RequestManager { 32 public: 33 static bool IsLegacy(napi_env env, napi_callback_info info); 34 35 static napi_value Download(napi_env env, napi_callback_info info); 36 37 REQUEST_API static napi_value OnDownloadComplete(napi_env env, napi_callback_info info); 38 39 static void OnTaskDone(const std::string &token, bool successful, const std::string &errMsg); 40 41 private: 42 using ArgsGenerator = std::function<void(napi_env env, napi_ref *recv, int &argc, napi_value *argv)>; 43 44 struct DownloadDescriptor { 45 DownloadTask *task_{}; 46 std::string filename_; 47 napi_env env_{}; 48 napi_ref this_{}; 49 napi_ref successCb_{}; 50 napi_ref failCb_{}; 51 }; 52 53 struct CallFunctionData { 54 napi_env env_{}; 55 napi_ref func_{}; 56 ArgsGenerator generator_; 57 }; 58 59 static std::string GetTaskToken(); 60 61 static std::string GetCacheDir(napi_env env); 62 63 static std::string GetFilenameFromUrl(std::string &url); 64 65 static bool IsPathValid(const std::string &dir, const std::string &filename); 66 67 static bool HasSameFilename(const std::string &filename); 68 69 static std::vector<std::string> ParseHeader(napi_env env, napi_value option); 70 71 static DownloadTask::DownloadOption ParseOption(napi_env env, napi_value option); 72 73 static void CallFailCallback(napi_env env, napi_value object, const std::string &msg); 74 75 static void CallSuccessCallback(napi_env env, napi_value object, const std::string &token); 76 77 static void CallFunctionAsync(napi_env env, napi_ref func, const ArgsGenerator &generator); 78 79 static std::atomic<uint32_t> taskId_; 80 static std::mutex lock_; 81 static std::map<std::string, RequestManager::DownloadDescriptor> downloadDescriptors_; 82 83 static constexpr int DOWNLOAD_ARGC = 1; 84 static constexpr int SUCCESS_CB_ARGC = 1; 85 static constexpr int FAIL_CB_ARGC = 2; 86 static constexpr int FAIL_CB_DOWNLOAD_ERROR = 400; 87 static constexpr int FAIL_CB_TASK_NOT_EXIST = 401; 88 static constexpr int MAX_CB_ARGS = 2; 89 90 static inline const std::string URI_PREFIX = "internal://cache/"; 91 }; 92 } // namespace OHOS::Request::Legacy 93 #endif // LEGACY_DOWNLOAD_MANAGER_H