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 REQUEST_TASK_NAPI 17 #define REQUEST_TASK_NAPI 18 19 #include "async_call.h" 20 #include "js_common.h" 21 #include "request_notify.h" 22 23 namespace OHOS::Request { 24 class JsTask { 25 public: 26 ~JsTask(); 27 static napi_value JsCreate(napi_env env, napi_callback_info info); 28 static napi_value JsUpload(napi_env env, napi_callback_info info); 29 static napi_value JsDownload(napi_env env, napi_callback_info info); 30 static napi_value JsRequest(napi_env env, napi_callback_info info); 31 static napi_value JsRequestFile(napi_env env, napi_callback_info info); 32 33 static napi_value GetTask(napi_env env, napi_callback_info info); 34 static napi_value Remove(napi_env env, napi_callback_info info); 35 static napi_value Show(napi_env env, napi_callback_info info); 36 static napi_value Touch(napi_env env, napi_callback_info info); 37 static napi_value Search(napi_env env, napi_callback_info info); 38 static napi_value Query(napi_env env, napi_callback_info info); 39 40 std::string GetTid(); 41 void SetTid(int32_t tid); 42 void AddListener(const std::string &key, const sptr<RequestNotify> &listener); 43 void RemoveListener(const std::string &type, const std::string &tid, napi_value callback, Version version); 44 void RemoveListener(const std::string &type, const std::string &tid, Version version); 45 size_t GetListenerSize(const std::string &key); 46 void ClearListener(); 47 48 static void ReloadListener(); 49 static void ClearTaskMap(const std::string &key); 50 static void AddTaskMap(const std::string &key, JsTask *task); 51 static bool SetDirsPermission(std::vector<std::string> &dirs); 52 static bool SetPathPermission(const std::string &filepath); 53 static void ClearTaskContext(const std::string &key); 54 static void RemoveDirsPermission(const std::vector<std::string> &dirs); 55 56 Config config_; 57 static bool register_; 58 static std::mutex taskMutex_; 59 static std::map<std::string, JsTask *> taskMap_; 60 static std::mutex pathMutex_; 61 static std::map<std::string, int32_t> pathMap_; 62 std::mutex listenerMutex_; 63 std::map<std::string, std::vector<sptr<RequestNotify>>> listenerMap_; 64 65 private: 66 struct ContextInfo : public AsyncCall::Context { 67 JsTask *task = nullptr; 68 napi_ref taskRef = nullptr; 69 napi_ref jsConfig = nullptr; 70 napi_ref baseContext = nullptr; 71 Config config{}; 72 int32_t tid{}; 73 std::string token = "null"; 74 }; 75 76 struct ContextCallbackData { 77 std::shared_ptr<ContextInfo> context = nullptr; 78 }; 79 80 struct TouchContext : public AsyncCall::Context { 81 std::string tid; 82 std::string token = "null"; 83 TaskInfo taskInfo; 84 }; 85 86 static napi_value DefineClass( 87 napi_env env, const napi_property_descriptor *desc, size_t count, napi_callback cb, napi_ref *ctor); 88 static napi_value JsMain(napi_env env, napi_callback_info info, Version version); 89 static napi_value Create(napi_env env, napi_callback_info info); 90 static napi_value GetCtor(napi_env env, Version version); 91 static napi_value GetCtorV8(napi_env env); 92 static napi_value GetCtorV9(napi_env env); 93 static napi_value GetCtorV10(napi_env env); 94 static napi_value RequestFile(napi_env env, napi_callback_info info); 95 static napi_value RequestFileV8(napi_env env, napi_callback_info info); 96 static int32_t CreateExec(const std::shared_ptr<ContextInfo> &context); 97 static napi_value GetTaskCtor(napi_env env); 98 static napi_value GetTaskCreate(napi_env env, napi_callback_info info); 99 static void GetTaskExecution(std::shared_ptr<ContextInfo> context); 100 static bool GetTaskOutput(std::shared_ptr<ContextInfo> context); 101 static bool ParseGetTask(napi_env env, size_t argc, napi_value *argv, std::shared_ptr<ContextInfo> context); 102 static std::string ParseTid(napi_env env, size_t argc, napi_value *argv); 103 static napi_value TouchInner(napi_env env, napi_callback_info info, AsyncCall::Context::InputAction action, 104 std::shared_ptr<TouchContext> context); 105 static bool ParseSearch(napi_env env, size_t argc, napi_value *argv, Filter &filter); 106 static std::string ParseBundle(napi_env env, napi_value value); 107 static State ParseState(napi_env env, napi_value value); 108 static Action ParseAction(napi_env env, napi_value value); 109 static Mode ParseMode(napi_env env, napi_value value); 110 static bool ParseTouch(napi_env env, size_t argc, napi_value *argv, std::shared_ptr<TouchContext> context); 111 static int64_t ParseBefore(napi_env env, napi_value value); 112 static int64_t ParseAfter(napi_env env, napi_value value, int64_t before); 113 static bool CheckPathBaseDir(const std::string &filepath, std::string &baseDir); 114 static void AddPathMap(const std::string &filepath, const std::string &baseDir); 115 static void ResetDirAccess(const std::string &filepath); 116 static void RemovePathMap(const std::string &filepath); 117 static void AddTaskContextMap(const std::string &key, std::shared_ptr<ContextInfo> context); 118 static void UnrefTaskContextMap(std::shared_ptr<ContextInfo> context); 119 static void UvUnrefTaskContext(uv_work_t *work, int status); 120 static void RegisterForegroundResume(); 121 bool Equals(napi_env env, napi_value value, napi_ref copy); 122 123 static std::mutex createMutex_; 124 static thread_local napi_ref requestCtor; 125 static std::mutex requestMutex_; 126 static thread_local napi_ref requestFileCtor; 127 static std::mutex requestFileMutex_; 128 static thread_local napi_ref createCtor; 129 static std::mutex getTaskCreateMutex_; 130 static thread_local napi_ref getTaskCreateCtor; 131 static std::mutex taskContextMutex_; 132 static std::map<std::string, std::shared_ptr<ContextInfo>> taskContextMap_; 133 std::string tid_; 134 }; 135 } // namespace OHOS::Request 136 137 #endif // REQUEST_TASK_NAPI 138