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