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 OHOS_ABILITY_UNCAUGHT_EXCEPTION_CALLBACK_H 17 #define OHOS_ABILITY_UNCAUGHT_EXCEPTION_CALLBACK_H 18 19 #include <string> 20 21 #include "native_engine/impl/ark/ark_native_engine.h" 22 #include "native_engine/native_engine.h" 23 #include "source_map.h" 24 #include "source_map_operator.h" 25 26 namespace OHOS { 27 namespace JsEnv { 28 struct ErrorObject { 29 std::string name; 30 std::string message; 31 std::string stack; 32 }; 33 34 struct UncaughtExceptionInfo { 35 std::string hapPath; 36 std::function<void(std::string summary, const JsEnv::ErrorObject errorObj)> uncaughtTask; 37 }; 38 39 class NapiUncaughtExceptionCallback final { 40 public: NapiUncaughtExceptionCallback(std::function<void (const std::string summary,const JsEnv::ErrorObject errorObj)> uncaughtTask,std::shared_ptr<SourceMapOperator> sourceMapOperator,napi_env env)41 NapiUncaughtExceptionCallback( 42 std::function<void(const std::string summary, const JsEnv::ErrorObject errorObj)> uncaughtTask, 43 std::shared_ptr<SourceMapOperator> sourceMapOperator, napi_env env) 44 : uncaughtTask_(uncaughtTask), sourceMapOperator_(sourceMapOperator), env_(env) 45 {} 46 47 ~NapiUncaughtExceptionCallback() = default; 48 49 void operator()(napi_value obj); 50 51 void operator()(panda::TryCatch& trycatch); 52 53 void CallbackTask(napi_value& obj); 54 55 std::string GetNativeStrFromJsTaggedObj(napi_value obj, const char* key); 56 57 static std::string GetFuncNameAndBuildId(std::string nativeStack); 58 59 static std::string GetSubmitterStackLocal(); 60 61 private: 62 std::function<void(std::string summary, const JsEnv::ErrorObject errorObj)> uncaughtTask_; 63 std::shared_ptr<SourceMapOperator> sourceMapOperator_ = nullptr; 64 napi_env env_ = nullptr; 65 }; 66 } // namespace JsEnv 67 } // namespace OHOS 68 #endif // OHOS_ABILITY_UNCAUGHT_EXCEPTION_CALLBACK_H 69