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 JS_PRINT_CALLBACK_H 17 #define JS_PRINT_CALLBACK_H 18 19 #include <memory> 20 #include <mutex> 21 #include <uv.h> 22 #include <vector> 23 24 #include "napi/native_api.h" 25 26 class NativeValue; 27 class NativeEngine; 28 29 namespace OHOS { 30 namespace AbilityRuntime { 31 class JsRuntime; 32 class JsPrintCallback : public std::enable_shared_from_this<JsPrintCallback> { 33 public: 34 explicit JsPrintCallback(JsRuntime &jsRutime); 35 ~JsPrintCallback() = default; 36 NativeValue *Exec(NativeValue *jsObj, const std::string &name, NativeValue *const *argv = nullptr, size_t argc = 0, 37 bool isSync = true); 38 39 private: 40 uv_loop_s *GetJsLoop(JsRuntime &jsRuntime); 41 bool BuildJsWorker(NativeValue *jsObj, const std::string &name, 42 NativeValue *const *argv, size_t argc, bool isSync); 43 44 private: 45 struct JsWorkParam { 46 std::shared_ptr<JsPrintCallback> self; 47 NativeEngine *nativeEngine; 48 NativeValue *jsObj; 49 NativeValue *jsMethod; 50 NativeValue *const *argv; 51 size_t argc; 52 NativeValue *jsResult; 53 bool isSync; 54 bool isCompleted; JsWorkParamJsWorkParam55 JsWorkParam() 56 { 57 self = nullptr; 58 nativeEngine = nullptr; 59 jsObj = nullptr; 60 jsMethod = nullptr; 61 argv = nullptr; 62 argc = 0; 63 jsResult = nullptr; 64 isSync = false; 65 isCompleted = false; 66 } 67 }; 68 69 JsRuntime &jsRuntime_; 70 uv_work_t *jsWorker_; 71 72 JsPrintCallback::JsWorkParam jsParam_; 73 74 std::mutex conditionMutex_; 75 std::condition_variable syncCon_; 76 static constexpr int SYNC_TIME_OUT = 1000; 77 }; 78 } // namespace AbilityRuntime 79 } // namespace OHOS 80 #endif // JS_PRINT_CALLBACK_H 81