Files
ability_ability_runtime/frameworks/native/runtime/js_worker.cpp
T
shiyu_huang b0633ac6c9 issue:#I5GZDI
Description:Worker support debug mode
Sig:SIG_ApplicationFramework
Feature or Bugfix:Feature
Binary Source:No

Signed-off-by: shiyu_huang <huangshiyu1@huawei.com>
Change-Id: I7b81d8aa32ac3ebcbfbf08cc42b86318d78c4c86
2022-07-14 16:37:52 +08:00

162 lines
4.9 KiB
C++

/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "js_worker.h"
#include <cerrno>
#include <climits>
#include <cstdlib>
#include <fstream>
#include <vector>
#include <unistd.h>
#include "connect_server_manager.h"
#include "hilog_wrapper.h"
#include "js_console_log.h"
#include "js_runtime_utils.h"
#include "native_engine/impl/ark/ark_native_engine.h"
namespace OHOS {
namespace AbilityRuntime {
namespace {
constexpr int64_t ASSET_FILE_MAX_SIZE = 20 * 1024 * 1024;
#if defined(_ARM64_)
constexpr char ARK_DEBUGGER_LIB_PATH[] = "/system/lib64/libark_debugger.z.so";
#else
constexpr char ARK_DEBUGGER_LIB_PATH[] = "/system/lib/libark_debugger.z.so";
#endif
bool g_debugMode = false;
void InitWorkerFunc(NativeEngine* nativeEngine)
{
HILOG_INFO("InitWorkerFunc called");
if (nativeEngine == nullptr) {
HILOG_ERROR("Input nativeEngine is nullptr");
return;
}
NativeObject* globalObj = ConvertNativeValueTo<NativeObject>(nativeEngine->GetGlobal());
if (globalObj == nullptr) {
HILOG_ERROR("Failed to get global object");
return;
}
InitConsoleLogModule(*nativeEngine, *globalObj);
if (g_debugMode) {
auto instanceId = gettid();
std::string instanceName = "workerThread_" + std::to_string(instanceId);
bool needBreakPoint = ConnectServerManager::Get().AddInstance(instanceId, instanceName);
auto arkNativeEngine = static_cast<ArkNativeEngine*>(nativeEngine);
auto vm = const_cast<EcmaVM*>(arkNativeEngine->GetEcmaVm());
auto workerPostTask = [nativeEngine](std::function<void()>&& callback) {
nativeEngine->CallDebuggerPostTaskFunc(std::move(callback));
};
panda::JSNApi::StartDebugger(ARK_DEBUGGER_LIB_PATH, vm, needBreakPoint, instanceId, workerPostTask);
}
}
void OffWorkerFunc(NativeEngine* nativeEngine)
{
HILOG_INFO("OffWorkerFunc called");
if (nativeEngine == nullptr) {
HILOG_ERROR("Input nativeEngine is nullptr");
return;
}
if (g_debugMode) {
auto instanceId = gettid();
ConnectServerManager::Get().RemoveInstance(instanceId);
auto arkNativeEngine = static_cast<ArkNativeEngine*>(nativeEngine);
auto vm = const_cast<EcmaVM*>(arkNativeEngine->GetEcmaVm());
panda::JSNApi::StopDebugger(vm);
}
}
bool ReadAssetData(const std::string& filePath, std::vector<uint8_t>& content)
{
char path[PATH_MAX];
if (realpath(filePath.c_str(), path) == nullptr) {
HILOG_ERROR("ReadAssetData realpath(%{private}s) failed, errno = %{public}d", filePath.c_str(), errno);
return false;
}
std::ifstream stream(path, std::ios::binary | std::ios::ate);
if (!stream.is_open()) {
HILOG_ERROR("ReadAssetData failed to open file %{private}s", filePath.c_str());
return false;
}
auto fileLen = stream.tellg();
if (fileLen > ASSET_FILE_MAX_SIZE) {
HILOG_ERROR("ReadAssetData failed, file is too large");
return false;
}
content.resize(fileLen);
stream.seekg(0);
stream.read(reinterpret_cast<char*>(content.data()), content.size());
return true;
}
struct AssetHelper final {
explicit AssetHelper(const std::string& codePath) : codePath_(codePath)
{
if (!codePath_.empty() && codePath.back() != '/') {
codePath_.append("/");
}
}
void operator()(const std::string& uri, std::vector<uint8_t>& content, std::string &ami) const
{
if (uri.empty()) {
HILOG_ERROR("Uri is empty.");
return;
}
HILOG_INFO("RegisterAssetFunc called, uri: %{private}s", uri.c_str());
size_t index = uri.find_last_of(".");
if (index == std::string::npos) {
HILOG_ERROR("Invalid uri");
return;
}
ami = codePath_ + uri.substr(0, index) + ".abc";
HILOG_INFO("Get asset, ami: %{private}s", ami.c_str());
if (!ReadAssetData(ami, content)) {
HILOG_ERROR("Get asset content failed.");
return;
}
}
std::string codePath_;
};
}
void InitWorkerModule(NativeEngine& engine, const std::string& codePath)
{
engine.SetInitWorkerFunc(InitWorkerFunc);
engine.SetOffWorkerFunc(OffWorkerFunc);
engine.SetGetAssetFunc(AssetHelper(codePath));
}
void StartDebuggerInWorkerModule()
{
g_debugMode = true;
}
} // namespace AbilityRuntime
} // namespace OHOS