mirror of
https://gitee.com/openharmony/filemanagement_app_file_service
synced 2024-11-23 08:00:16 +00:00
!1221 fix crash in native callback
Merge pull request !1221 from zhuangzhuang/master
This commit is contained in:
commit
c1ed62e154
@ -138,12 +138,8 @@ public:
|
||||
ErrCode OnProcess(std::function<void(ErrCode, const std::string)> callback) override;
|
||||
|
||||
public:
|
||||
explicit ExtBackupJs(AbilityRuntime::JsRuntime &jsRuntime) : jsRuntime_(jsRuntime) {}
|
||||
~ExtBackupJs()
|
||||
{
|
||||
jsRuntime_.FreeNativeReference(std::move(jsObj_));
|
||||
}
|
||||
|
||||
explicit ExtBackupJs(AbilityRuntime::JsRuntime &jsRuntime);
|
||||
~ExtBackupJs();
|
||||
private:
|
||||
int CallJsMethod(const std::string &funcName,
|
||||
AbilityRuntime::JsRuntime &jsRuntime,
|
||||
|
@ -46,6 +46,8 @@
|
||||
namespace OHOS::FileManagement::Backup {
|
||||
using namespace std;
|
||||
constexpr size_t ARGC_ONE = 1;
|
||||
static std::mutex g_extBackupValidLock;
|
||||
static bool g_isExtBackupValid = true;
|
||||
|
||||
static string GetSrcPath(const AppExecFwk::AbilityInfo &info)
|
||||
{
|
||||
@ -103,6 +105,11 @@ static napi_status DealNapiException(napi_env env, napi_value &exception, std::s
|
||||
|
||||
static napi_value PromiseCallback(napi_env env, napi_callback_info info)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_extBackupValidLock);
|
||||
if (!g_isExtBackupValid) {
|
||||
HILOGE("ExtBackup is invalid");
|
||||
return nullptr;
|
||||
}
|
||||
HILOGI("Promise callback.");
|
||||
void *data = nullptr;
|
||||
if (napi_get_cb_info(env, info, nullptr, 0, nullptr, &data) != napi_ok) {
|
||||
@ -140,6 +147,12 @@ static napi_value PromiseCatchCallback(napi_env env, napi_callback_info info)
|
||||
HILOGE("Failed to throw an exception, %{public}d", throwStatus);
|
||||
return nullptr;
|
||||
}
|
||||
std::lock_guard<std::mutex> lock(g_extBackupValidLock);
|
||||
if (!g_isExtBackupValid) {
|
||||
HILOGE("ExtBackup is invalid");
|
||||
data = nullptr;
|
||||
return nullptr;
|
||||
}
|
||||
callbackInfo->callback(BError(BError::Codes::EXT_THROW_EXCEPTION), exceptionInfo);
|
||||
data = nullptr;
|
||||
HILOGI("Promise catch callback end.");
|
||||
@ -148,6 +161,11 @@ static napi_value PromiseCatchCallback(napi_env env, napi_callback_info info)
|
||||
|
||||
static napi_value PromiseCallbackEx(napi_env env, napi_callback_info info)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_extBackupValidLock);
|
||||
if (!g_isExtBackupValid) {
|
||||
HILOGE("ExtBackup is invalid");
|
||||
return nullptr;
|
||||
}
|
||||
HILOGI("PromiseEx callback.");
|
||||
void *data = nullptr;
|
||||
std::string str;
|
||||
@ -185,6 +203,12 @@ static napi_value PromiseCatchCallbackEx(napi_env env, napi_callback_info info)
|
||||
HILOGE("Failed to throw an exception, %{public}d", throwStatus);
|
||||
return nullptr;
|
||||
}
|
||||
std::lock_guard<std::mutex> lock(g_extBackupValidLock);
|
||||
if (!g_isExtBackupValid) {
|
||||
HILOGE("ExtBackup is invalid");
|
||||
data = nullptr;
|
||||
return nullptr;
|
||||
}
|
||||
callbackInfoEx->callbackParam(BError(BError::Codes::EXT_THROW_EXCEPTION), exceptionInfo);
|
||||
data = nullptr;
|
||||
HILOGI("PromiseEx catch callback end.");
|
||||
@ -422,6 +446,20 @@ napi_value AttachBackupExtensionContext(napi_env env, void *value, void *)
|
||||
return contextObj;
|
||||
}
|
||||
|
||||
ExtBackupJs::ExtBackupJs(AbilityRuntime::JsRuntime &jsRuntime) : jsRuntime_(jsRuntime)
|
||||
{
|
||||
g_isExtBackupValid = true;
|
||||
HILOGI("ExtBackupJs::ExtBackupJs.");
|
||||
}
|
||||
|
||||
ExtBackupJs::~ExtBackupJs()
|
||||
{
|
||||
jsRuntime_.FreeNativeReference(std::move(jsObj_));
|
||||
std::lock_guard<std::mutex> lock(g_extBackupValidLock);
|
||||
g_isExtBackupValid = false;
|
||||
HILOGI("ExtBackupJs::~ExtBackupJs.");
|
||||
}
|
||||
|
||||
void ExtBackupJs::ExportJsContext(void)
|
||||
{
|
||||
auto env = jsRuntime_.GetNapiEnv();
|
||||
|
Loading…
Reference in New Issue
Block a user