diff --git a/jsapi/worker/message_queue.cpp b/jsapi/worker/message_queue.cpp index 855dc65aa4f7b5b0606f4e44e4054853035b8bdb..b886f8f1b611e5fe13bc66f61e37d333e9c89132 100644 --- a/jsapi/worker/message_queue.cpp +++ b/jsapi/worker/message_queue.cpp @@ -57,4 +57,4 @@ void MessageQueue::Clear(napi_env env) } queueLock_.unlock(); } -} // namespace worker \ No newline at end of file +} // namespace OHOS::CCRuntime::Worker diff --git a/jsapi/worker/message_queue.h b/jsapi/worker/message_queue.h index eeee987d58bde850e56c38ac67e160ee35f43347..9273b5fc685e1a1dfa60ce7e1cffa96f0188eded 100644 --- a/jsapi/worker/message_queue.h +++ b/jsapi/worker/message_queue.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef FOUNDATION_CCRUNTIME_JSAPI_WORKER_MESSAGE_QUEUE_H -#define FOUNDATION_CCRUNTIME_JSAPI_WORKER_MESSAGE_QUEUE_H +#ifndef JSAPI_WORKER_MESSAGE_QUEUE_H_ +#define JSAPI_WORKER_MESSAGE_QUEUE_H_ #include #include @@ -39,4 +39,4 @@ private: std::queue queue_; }; } // namespace OHOS::CCRuntime::Worker -#endif // FOUNDATION_CCRUNTIME_JSAPI_WORKER_MESSAGE_QUEUE_H +#endif // JSAPI_WORKER_MESSAGE_QUEUE_H_ diff --git a/jsapi/worker/native_module_worker.cpp b/jsapi/worker/native_module_worker.cpp index b30437511843780172e21fa12a3d04d9e330e938..a5f2d67c55ad7b084cfb193d2472c083965ed25e 100644 --- a/jsapi/worker/native_module_worker.cpp +++ b/jsapi/worker/native_module_worker.cpp @@ -24,7 +24,7 @@ static napi_module g_workerModule = { .nm_filename = nullptr, .nm_register_func = OHOS::CCRuntime::Worker::Worker::InitWorker, .nm_modname = "worker", - .nm_priv = ((void*)0), + .nm_priv = reinterpret_cast(0), .reserved = { 0 }, }; /* diff --git a/jsapi/worker/thread.h b/jsapi/worker/thread.h index 73a83614d0dce4da8c58c1ac6a8e3b032a9d441a..3e5f1c495860fa569cf808ea0f6e2b74d1cee5cb 100644 --- a/jsapi/worker/thread.h +++ b/jsapi/worker/thread.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef FOUNDATION_CCRUNTIME_JSAPI_WORKER_THREAD_H -#define FOUNDATION_CCRUNTIME_JSAPI_WORKER_THREAD_H +#ifndef JSAPI_WORKER_THREAD_H_ +#define JSAPI_WORKER_THREAD_H_ #include @@ -36,4 +36,4 @@ private: }; } // namespace OHOS::CCRuntime::Worker -#endif // #define FOUNDATION_CCRUNTIME_JSAPI_WORKER_THREAD_H \ No newline at end of file +#endif // #define JSAPI_WORKER_THREAD_H_ diff --git a/jsapi/worker/worker.cpp b/jsapi/worker/worker.cpp index 511167ff52bb15114c9a82de60b4c8251a880f62..f7a08d24f067ca376235fd12445c7ea89d00f20e 100644 --- a/jsapi/worker/worker.cpp +++ b/jsapi/worker/worker.cpp @@ -41,7 +41,7 @@ void Worker::StartExecuteInThread(napi_env env, const char* script) script_ = std::string(script); CloseHelp::DeletePointer(script, true); - // 4. create WorkerRunner to Execute + // 3. create WorkerRunner to Execute if (!runner_) { runner_ = std::make_unique(WorkerStartCallback(ExecuteInThread, this)); } @@ -320,8 +320,8 @@ void Worker::HostOnMessageInner() // receive close signal. if (data == nullptr) { HILOG_INFO("worker:: worker received close signal"); - uv_close((uv_handle_t*)&hostOnMessageSignal_, nullptr); - uv_close((uv_handle_t*)&hostOnErrorSignal_, nullptr); + uv_close(reinterpret_cast(&hostOnMessageSignal_), nullptr); + uv_close(reinterpret_cast(&hostOnErrorSignal_), nullptr); CloseHostCallback(); return; } @@ -351,7 +351,7 @@ void Worker::HostOnMessageInner() void Worker::TerminateWorker() { // when there is no active handle, worker loop will stop automatic. - uv_close((uv_handle_t*)&workerOnMessageSignal_, nullptr); + uv_close(reinterpret_cast(&workerOnMessageSignal_), nullptr); CloseWorkerCallback(); uv_loop_t* loop = GetWorkerLoop(); if (loop != nullptr) { @@ -451,7 +451,7 @@ napi_value Worker::PostMessage(napi_env env, napi_callback_info cbinfo) napi_value thisVar = nullptr; napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, nullptr); Worker* worker = nullptr; - napi_unwrap(env, thisVar, (void**)&worker); + napi_unwrap(env, thisVar, reinterpret_cast(&worker)); if (worker == nullptr) { HILOG_ERROR("worker:: worker is nullptr when PostMessage, maybe worker is terminated"); @@ -492,7 +492,7 @@ napi_value Worker::PostMessageToHost(napi_env env, napi_callback_info cbinfo) napi_value* argv = new napi_value[argc]; [[maybe_unused]] ObjectScope scope(argv, true); Worker* worker = nullptr; - napi_get_cb_info(env, cbinfo, &argc, argv, nullptr, (void**)&worker); + napi_get_cb_info(env, cbinfo, &argc, argv, nullptr, reinterpret_cast(&worker)); if (worker == nullptr) { HILOG_ERROR("worker:: when post message to host occur worker is nullptr"); @@ -553,7 +553,7 @@ napi_value Worker::Terminate(napi_env env, napi_callback_info cbinfo) napi_value thisVar = nullptr; napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, nullptr); Worker* worker = nullptr; - napi_unwrap(env, thisVar, (void**)&worker); + napi_unwrap(env, thisVar, reinterpret_cast(&worker)); if (worker == nullptr) { HILOG_ERROR("worker:: worker is nullptr when Terminate, maybe worker is terminated"); return nullptr; @@ -591,7 +591,7 @@ napi_value Worker::CancelTask(napi_env env, napi_callback_info cbinfo) napi_value thisVar = nullptr; napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, nullptr); Worker* worker = nullptr; - napi_unwrap(env, thisVar, (void**)&worker); + napi_unwrap(env, thisVar, reinterpret_cast(&worker)); if (worker == nullptr) { HILOG_ERROR("worker:: worker is nullptr when CancelTask, maybe worker is terminated"); return nullptr; @@ -611,7 +611,7 @@ napi_value Worker::CancelTask(napi_env env, napi_callback_info cbinfo) napi_value Worker::ParentPortCancelTask(napi_env env, napi_callback_info cbinfo) { Worker* worker = nullptr; - napi_get_cb_info(env, cbinfo, nullptr, nullptr, nullptr, (void**)&worker); + napi_get_cb_info(env, cbinfo, nullptr, nullptr, nullptr, reinterpret_cast(&worker)); if (worker == nullptr) { HILOG_ERROR("worker:: worker is nullptr when CancelTask, maybe worker is terminated"); return nullptr; @@ -706,15 +706,15 @@ napi_value Worker::WorkerConstructor(napi_env env, napi_callback_info cbinfo) napi_wrap( env, thisVar, worker, [](napi_env env, void* data, void* hint) { - Worker* worker = (Worker*)data; + Worker* worker = reinterpret_cast(data); { std::lock_guard lock(worker->liveStatusLock_); if (worker->UpdateHostState(INACTIVE)) { - if (!uv_is_closing((uv_handle_t*)&worker->hostOnMessageSignal_)) { - uv_close((uv_handle_t*)&worker->hostOnMessageSignal_, nullptr); + if (!uv_is_closing(reinterpret_cast(&worker->hostOnMessageSignal_))) { + uv_close(reinterpret_cast(&worker->hostOnMessageSignal_), nullptr); } - if (!uv_is_closing((uv_handle_t*)&worker->hostOnErrorSignal_)) { - uv_close((uv_handle_t*)&worker->hostOnErrorSignal_, nullptr); + if (!uv_is_closing(reinterpret_cast(&worker->hostOnErrorSignal_))) { + uv_close(reinterpret_cast(&worker->hostOnErrorSignal_), nullptr); } worker->ReleaseHostThreadContent(); } @@ -868,7 +868,7 @@ napi_value Worker::RemoveListener(napi_env env, napi_callback_info cbinfo) } Worker* worker = nullptr; - napi_unwrap(env, thisVar, (void**)&worker); + napi_unwrap(env, thisVar, reinterpret_cast(&worker)); if (worker == nullptr) { HILOG_ERROR("worker:: worker is nullptr when RemoveListener, maybe worker is terminated"); return nullptr; @@ -925,7 +925,7 @@ napi_value Worker::DispatchEvent(napi_env env, napi_callback_info cbinfo) } Worker* worker = nullptr; - napi_unwrap(env, thisVar, (void**)&worker); + napi_unwrap(env, thisVar, reinterpret_cast(&worker)); if (worker == nullptr) { HILOG_ERROR("worker:: worker is nullptr when DispatchEvent, maybe worker is terminated"); return NapiValueHelp::GetBooleanValue(env, false); @@ -983,7 +983,7 @@ napi_value Worker::RemoveAllListener(napi_env env, napi_callback_info cbinfo) napi_value thisVar = nullptr; napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, nullptr); Worker* worker = nullptr; - napi_unwrap(env, thisVar, (void**)&worker); + napi_unwrap(env, thisVar, reinterpret_cast(&worker)); if (worker == nullptr) { HILOG_ERROR("worker:: worker is nullptr when RemoveAllListener, maybe worker is terminated"); return nullptr; @@ -1154,7 +1154,7 @@ void Worker::ReleaseHostThreadContent() napi_value thisVar = nullptr; napi_get_reference_value(hostEnv_, workerRef_, &thisVar); Worker* worker = nullptr; - napi_remove_wrap(hostEnv_, thisVar, (void**)&worker); + napi_remove_wrap(hostEnv_, thisVar, reinterpret_cast(&worker)); // 4. set workerRef_ be null napi_delete_reference(hostEnv_, workerRef_); } @@ -1173,7 +1173,7 @@ napi_value Worker::ParentPortAddEventListener(napi_env env, napi_callback_info c napi_value* args = new napi_value[argc]; [[maybe_unused]] ObjectScope scope(args, true); Worker* worker = nullptr; - napi_get_cb_info(env, cbinfo, &argc, args, nullptr, (void**)&worker); + napi_get_cb_info(env, cbinfo, &argc, args, nullptr, reinterpret_cast(&worker)); if (!NapiValueHelp::IsString(args[0])) { napi_throw_error(env, nullptr, "Worker 1st param must be string with on"); @@ -1221,7 +1221,7 @@ napi_value Worker::ParentPortAddEventListener(napi_env env, napi_callback_info c napi_value Worker::ParentPortRemoveAllListener(napi_env env, napi_callback_info cbinfo) { Worker* worker = nullptr; - napi_get_cb_info(env, cbinfo, nullptr, nullptr, nullptr, (void**)&worker); + napi_get_cb_info(env, cbinfo, nullptr, nullptr, nullptr, reinterpret_cast(&worker)); if (worker == nullptr) { HILOG_ERROR("worker:: when post message to host occur worker is nullptr"); @@ -1249,7 +1249,7 @@ napi_value Worker::ParentPortDispatchEvent(napi_env env, napi_callback_info cbin napi_value* args = new napi_value[argc]; [[maybe_unused]] ObjectScope scope(args, true); Worker* worker = nullptr; - napi_get_cb_info(env, cbinfo, &argc, args, nullptr, (void**)&worker); + napi_get_cb_info(env, cbinfo, &argc, args, nullptr, reinterpret_cast(&worker)); if (!NapiValueHelp::IsObject(args[0])) { napi_throw_error(env, nullptr, "worker DispatchEvent 1st param must be Event"); @@ -1309,7 +1309,7 @@ napi_value Worker::ParentPortRemoveEventListener(napi_env env, napi_callback_inf napi_value* args = new napi_value[argc]; [[maybe_unused]] ObjectScope scope(args, true); Worker* worker = nullptr; - napi_get_cb_info(env, cbinfo, &argc, args, nullptr, (void**)&worker); + napi_get_cb_info(env, cbinfo, &argc, args, nullptr, reinterpret_cast(&worker)); if (!NapiValueHelp::IsString(args[0])) { napi_throw_error(env, nullptr, "Worker 1st param must be string with on"); diff --git a/jsapi/worker/worker.h b/jsapi/worker/worker.h index a8ffc094136ce814759c2f07043b6eaefad9a7de..08b30f152066e2b31a361eb24b2be6093c5ab97c 100644 --- a/jsapi/worker/worker.h +++ b/jsapi/worker/worker.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef FOUNDATION_CCRUNTIME_JSAPI_WORKER_H -#define FOUNDATION_CCRUNTIME_JSAPI_WORKER_H +#ifndef JSAPI_WORKER_WORKER_H_ +#define JSAPI_WORKER_WORKER_H_ #include #include @@ -93,38 +93,213 @@ public: napi_ref ref_ {nullptr}; }; + /** + * Creates a worker instance. + * + * @param env NAPI environment parameters. + * @param thisVar URL of the script to be executed by the worker. + */ Worker(napi_env env, napi_ref thisVar); + + /** + * The destructor of the Worker. + */ ~Worker(); + /** + * The host thread receives the information. + * + * @param req The value of the object passed in by the js layer. + */ static void HostOnMessage(const uv_async_t* req); + + /** + * The host thread receives the information. + * + * @param req The value of the object passed in by the js layer. + */ static void HostOnError(const uv_async_t* req); + + /** + * The worker thread receives the information. + * + * @param req The value of the object passed in by the js layer. + */ static void WorkerOnMessage(const uv_async_t* req); + + /** + * ExecuteIn in thread. + * + * @param data The worker pointer. + */ static void ExecuteInThread(const void* data); + /** + * Post a message. + * + * @param env NAPI environment parameters. + * @param thisVar The callback information of the js layer. + */ static napi_value PostMessage(napi_env env, napi_callback_info cbinfo); + + /** + * Add event listeners to host. + * + * @param env NAPI environment parameters. + * @param cbinfo The callback information of the js layer. + */ static napi_value PostMessageToHost(napi_env env, napi_callback_info cbinfo); + + /** + * Terminates the worker thread to stop the worker from receiving messages. + * + * @param env NAPI environment parameters. + * @param cbinfo The callback information of the js layer. + */ static napi_value Terminate(napi_env env, napi_callback_info cbinfo); + + /** + * Close the worker. + * + * @param env NAPI environment parameters. + * @param cbinfo The callback information of the js layer. + */ static napi_value CloseWorker(napi_env env, napi_callback_info cbinfo); + + /** + * Adds an event listener to the worker. + * + * @param env NAPI environment parameters. + * @param cbinfo The callback information of the js layer. + */ static napi_value On(napi_env env, napi_callback_info cbinfo); + + /** + * Adds an event listener to the worker and removes the event listener automatically after it is invoked once. + * + * @param env NAPI environment parameters. + * @param cbinfo The callback information of the js layer. + */ static napi_value Once(napi_env env, napi_callback_info cbinfo); + + /** + * Removes an event listener to the worker. + * + * @param env NAPI environment parameters. + * @param cbinfo The callback information of the js layer. + */ static napi_value Off(napi_env env, napi_callback_info cbinfo); + + /** + * Add event listeners. + * + * @param env NAPI environment parameters. + * @param cbinfo The callback information of the js layer. + */ static napi_value AddEventListener(napi_env env, napi_callback_info cbinfo); + + /** + * Dispatch the event. + * + * @param env NAPI environment parameters. + * @param cbinfo The callback information of the js layer. + */ static napi_value DispatchEvent(napi_env env, napi_callback_info cbinfo); + + /** + * Remove the event listener. + * + * @param env NAPI environment parameters. + * @param cbinfo The callback information of the js layer. + */ static napi_value RemoveEventListener(napi_env env, napi_callback_info cbinfo); + + /** + * Remove all listener. + * + * @param env NAPI environment parameters. + * @param cbinfo The callback information of the js layer. + */ static napi_value RemoveAllListener(napi_env env, napi_callback_info cbinfo); + /** + * Add the listener. + * + * @param env NAPI environment parameters. + * @param cbinfo The callback information of the js layer. + */ static napi_value AddListener(napi_env env, napi_callback_info cbinfo, ListenerMode mode); + + /** + * Remove the listener. + * + * @param env NAPI environment parameters. + * @param cbinfo The callback information of the js layer. + */ static napi_value RemoveListener(napi_env env, napi_callback_info cbinfo); + /** + * The constructor of worker. + * + * @param env NAPI environment parameters. + * @param cbinfo The callback information of the js layer. + */ static napi_value WorkerConstructor(napi_env env, napi_callback_info cbinfo); + + /** + * Initialize the worker. + * + * @param env NAPI environment parameters. + * @param cbinfo The callback information of the js layer. + */ static napi_value InitWorker(napi_env env, napi_value exports); + /** + * Cancel the task. + * + * @param env NAPI environment parameters. + * @param cbinfo The callback information of the js layer. + */ static napi_value CancelTask(napi_env env, napi_callback_info cbinfo); + + /** + * The parent port cancels the task. + * + * @param env NAPI environment parameters. + * @param cbinfo The callback information of the js layer. + */ static napi_value ParentPortCancelTask(napi_env env, napi_callback_info cbinfo); + /** + * The parent port adds an event listener. + * + * @param env NAPI environment parameters. + * @param cbinfo The callback information of the js layer. + */ static napi_value ParentPortAddEventListener(napi_env env, napi_callback_info cbinfo); + + /** + * The parent port removes all event listener. + * + * @param env NAPI environment parameters. + * @param cbinfo The callback information of the js layer. + */ static napi_value ParentPortRemoveAllListener(napi_env env, napi_callback_info cbinfo); + + /** + * The parent port dispatch the event listener. + * + * @param env NAPI environment parameters. + * @param cbinfo The callback information of the js layer. + */ static napi_value ParentPortDispatchEvent(napi_env env, napi_callback_info cbinfo); + + /** + * The parent port removes the event listener. + * + * @param env NAPI environment parameters. + * @param cbinfo The callback information of the js layer. + */ static napi_value ParentPortRemoveEventListener(napi_env env, napi_callback_info cbinfo); void StartExecuteInThread(napi_env env, const char* script); @@ -278,4 +453,4 @@ private: std::recursive_mutex liveStatusLock_ {}; }; } // namespace OHOS::CCRuntime::Worker -#endif // FOUNDATION_CCRUNTIME_JSAPI_WORKER_H +#endif // JSAPI_WORKER_WORKER_H_ diff --git a/jsapi/worker/worker_helper.h b/jsapi/worker/worker_helper.h index 59d0988c344bae5164f3ccd2f61a3e28712a2546..734a5a25e983947544ab99fe06bc537681dc264d 100644 --- a/jsapi/worker/worker_helper.h +++ b/jsapi/worker/worker_helper.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef FOUNDATION_CCRUNTIME_JSAPI_WORKER_HELP_H -#define FOUNDATION_CCRUNTIME_JSAPI_WORKER_HELP_H +#ifndef JSAPI_WORKER_WORKER_HELPER_H_ +#define JSAPI_WORKER_WORKER_HELPER_H_ #include "napi/native_api.h" #include "napi/native_node_api.h" @@ -87,4 +87,4 @@ private: bool isArray_; }; } // namespace OHOS::CCRuntime::Worker -#endif // FOUNDATION_CCRUNTIME_JSAPI_NAPI_VALUE_HELP_H +#endif // JSAPI_WORKER_WORKER_HELPER_H_ diff --git a/jsapi/worker/worker_runner.h b/jsapi/worker/worker_runner.h index bf70fe7059c9bbec86975b961e994b1a338c521a..027b166129688373da892734891e225bc2249abe 100644 --- a/jsapi/worker/worker_runner.h +++ b/jsapi/worker/worker_runner.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef FOUNDATION_CCRUNTIME_JSAPI_WORK_RUNNER_H -#define FOUNDATION_CCRUNTIME_JSAPI_WORK_RUNNER_H +#ifndef JSAPI_WORKER_WORKER_RUNNER_H_ +#define JSAPI_WORKER_WORKER_RUNNER_H_ #include @@ -59,4 +59,4 @@ private: uv_thread_t selfThreadId_ {0}; }; } // namespace OHOS::CCRuntime::Worker -#endif // FOUNDATION_CCRUNTIME_JSAPI_WORK_RUNNER_H \ No newline at end of file +#endif // JSAPI_WORKER_WORKER_RUNNER_H_