diff --git a/cli_tool_framework/frameworks/js/napi/cli_tool_manager/include/js_cli_manager_utils.h b/cli_tool_framework/frameworks/js/napi/cli_tool_manager/include/js_cli_manager_utils.h index 975b8b94a8..11d36b21d0 100644 --- a/cli_tool_framework/frameworks/js/napi/cli_tool_manager/include/js_cli_manager_utils.h +++ b/cli_tool_framework/frameworks/js/napi/cli_tool_manager/include/js_cli_manager_utils.h @@ -25,9 +25,9 @@ namespace OHOS { namespace CliTool { +class CliSessionInfo; class CliToolEvent; class ExecOptions; -struct CliSessionInfo; /** * @brief Unwrap a string map from JavaScript object. diff --git a/cli_tool_framework/frameworks/js/napi/cli_tool_manager/src/js_cli_manager.cpp b/cli_tool_framework/frameworks/js/napi/cli_tool_manager/src/js_cli_manager.cpp index 3eac1fb171..826465a241 100644 --- a/cli_tool_framework/frameworks/js/napi/cli_tool_manager/src/js_cli_manager.cpp +++ b/cli_tool_framework/frameworks/js/napi/cli_tool_manager/src/js_cli_manager.cpp @@ -19,9 +19,9 @@ #include "cli_error_code.h" #include "cli_manager_error_utils.h" +#include "cli_session_info.h" #include "cli_tool_mgr_client.h" #include "hilog_tag_wrapper.h" -#include "icli_tool_data.h" #include "js_cli_manager_utils.h" #include "js_error_utils.h" #include "napi_common_util.h" diff --git a/cli_tool_framework/frameworks/js/napi/cli_tool_manager/src/js_cli_manager_utils.cpp b/cli_tool_framework/frameworks/js/napi/cli_tool_manager/src/js_cli_manager_utils.cpp index 0c6c3174c0..087e6f579f 100644 --- a/cli_tool_framework/frameworks/js/napi/cli_tool_manager/src/js_cli_manager_utils.cpp +++ b/cli_tool_framework/frameworks/js/napi/cli_tool_manager/src/js_cli_manager_utils.cpp @@ -17,6 +17,7 @@ #include +#include "cli_session_info.h" #include "cli_tool_event.h" #include "exec_options.h" #include "hilog_tag_wrapper.h" @@ -223,33 +224,33 @@ napi_value CreateJsCliSessionInfo(napi_env env, const CliSessionInfo &session) napi_set_named_property(env, jsObj, "status", AppExecFwk::WrapStringToJS(env, session.status)); // Set result if present - if (session.status != "running") { + if (session.status != "running" && session.result != nullptr) { napi_value jsResult = nullptr; status = napi_create_object(env, &jsResult); if (status != napi_ok) { TAG_LOGE(AAFwkTag::CLI_TOOL, "Failed to create JS ExecResult"); return nullptr; } - if (!session.result.timedOut) { - napi_value jsExitCode = AppExecFwk::WrapInt32ToJS(env, session.result.exitCode); + if (!session.result->timedOut) { + napi_value jsExitCode = AppExecFwk::WrapInt32ToJS(env, session.result->exitCode); napi_set_named_property(env, jsResult, "exitCode", jsExitCode); } - if (!session.result.outputText.empty()) { - napi_value jsOutputText = AppExecFwk::WrapStringToJS(env, session.result.outputText); + if (!session.result->outputText.empty()) { + napi_value jsOutputText = AppExecFwk::WrapStringToJS(env, session.result->outputText); napi_set_named_property(env, jsResult, "outputText", jsOutputText); } - if (!session.result.errorText.empty()) { - napi_value jsErrorText = AppExecFwk::WrapStringToJS(env, session.result.errorText); + if (!session.result->errorText.empty()) { + napi_value jsErrorText = AppExecFwk::WrapStringToJS(env, session.result->errorText); napi_set_named_property(env, jsResult, "errorText", jsErrorText); } - if (session.result.signalNumber != 0) { - napi_value jsSignalNumber = AppExecFwk::WrapInt32ToJS(env, session.result.signalNumber); + if (session.result->signalNumber != 0) { + napi_value jsSignalNumber = AppExecFwk::WrapInt32ToJS(env, session.result->signalNumber); napi_set_named_property(env, jsResult, "signalNumber", jsSignalNumber); } // Set timedOut - napi_set_named_property(env, jsResult, "timedOut", AppExecFwk::WrapBoolToJS(env, session.result.timedOut)); + napi_set_named_property(env, jsResult, "timedOut", AppExecFwk::WrapBoolToJS(env, session.result->timedOut)); // Set executionTime - napi_value jsExecutionTime = AppExecFwk::WrapInt64ToJS(env, session.result.executionTime); + napi_value jsExecutionTime = AppExecFwk::WrapInt64ToJS(env, session.result->executionTime); napi_set_named_property(env, jsResult, "executionTime", jsExecutionTime); napi_set_named_property(env, jsObj, "result", jsResult); } diff --git a/cli_tool_framework/interfaces/cli_tool/BUILD.gn b/cli_tool_framework/interfaces/cli_tool/BUILD.gn index f3b0e35a14..a24a1b9d74 100644 --- a/cli_tool_framework/interfaces/cli_tool/BUILD.gn +++ b/cli_tool_framework/interfaces/cli_tool/BUILD.gn @@ -52,11 +52,13 @@ ohos_shared_library("cli_tool_client") { sources = [ "src/cli_mgr_load_callback.cpp", "src/cli_event_reply_manager.cpp", + "src/cli_session_info.cpp", "src/cli_session_subscription_manager.cpp", "src/cli_tool_event.cpp", "src/cli_tool_mgr_client.cpp", "src/cli_tool_mgr_scheduler_recipient.cpp", "src/exec_options.cpp", + "src/exec_result.cpp", "src/exec_tool_param.cpp", "src/sub_command_info.cpp", "src/tool_info.cpp", diff --git a/cli_tool_framework/interfaces/cli_tool/ICliToolData.idl b/cli_tool_framework/interfaces/cli_tool/ICliToolData.idl index 69f7fcb3b0..c90feea27f 100644 --- a/cli_tool_framework/interfaces/cli_tool/ICliToolData.idl +++ b/cli_tool_framework/interfaces/cli_tool/ICliToolData.idl @@ -27,21 +27,3 @@ struct CommandPermission List permissions; int queryRet; }; - -struct ExecResult -{ - int exitCode; - String outputText; - String errorText; - int signalNumber; - boolean timedOut; - long executionTime; -}; - -struct CliSessionInfo -{ - String sessionId; - String toolName; - String status; - ExecResult result; -}; diff --git a/cli_tool_framework/interfaces/cli_tool/ICliToolManager.idl b/cli_tool_framework/interfaces/cli_tool/ICliToolManager.idl index 7c15bab302..3096557d38 100644 --- a/cli_tool_framework/interfaces/cli_tool/ICliToolManager.idl +++ b/cli_tool_framework/interfaces/cli_tool/ICliToolManager.idl @@ -17,6 +17,7 @@ package OHOS.CliTool; import ICliToolData; import ICliToolManagerScheduler; +sequenceable CliSessionInfo..OHOS.CliTool.CliSessionInfo; sequenceable ExecToolParam..OHOS.CliTool.ExecToolParam; sequenceable OHOS.CliTool.ToolSummary; sequenceable ToolInfo..OHOS.CliTool.ToolInfo; diff --git a/cli_tool_framework/interfaces/cli_tool/ICliToolManagerScheduler.idl b/cli_tool_framework/interfaces/cli_tool/ICliToolManagerScheduler.idl index 1b26580468..cbb3ffe3ee 100644 --- a/cli_tool_framework/interfaces/cli_tool/ICliToolManagerScheduler.idl +++ b/cli_tool_framework/interfaces/cli_tool/ICliToolManagerScheduler.idl @@ -15,7 +15,7 @@ package OHOS.CliTool; -import ICliToolData; +sequenceable CliSessionInfo..OHOS.CliTool.CliSessionInfo; sequenceable CliToolEvent..OHOS.CliTool.CliToolEvent; interface ICliToolManagerScheduler { diff --git a/cli_tool_framework/interfaces/cli_tool/include/cli_event_reply_manager.h b/cli_tool_framework/interfaces/cli_tool/include/cli_event_reply_manager.h index 4bbc795003..99c60dcca6 100644 --- a/cli_tool_framework/interfaces/cli_tool/include/cli_event_reply_manager.h +++ b/cli_tool_framework/interfaces/cli_tool/include/cli_event_reply_manager.h @@ -24,7 +24,7 @@ #include #include -#include "icli_tool_data.h" +#include "cli_session_info.h" namespace OHOS { namespace CliTool { diff --git a/cli_tool_framework/interfaces/cli_tool/include/cli_session_info.h b/cli_tool_framework/interfaces/cli_tool/include/cli_session_info.h new file mode 100644 index 0000000000..cec242bc5d --- /dev/null +++ b/cli_tool_framework/interfaces/cli_tool/include/cli_session_info.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2026 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. + */ + +#ifndef OHOS_ABILITY_RUNTIME_CLI_SESSION_INFO_H +#define OHOS_ABILITY_RUNTIME_CLI_SESSION_INFO_H + +#include + +#include "exec_result.h" +#include "parcel.h" + +namespace OHOS { +namespace CliTool { +/** + * @struct CliSessionInfo + * @brief Information about a CLI tool execution session. + */ +class CliSessionInfo : public Parcelable { +public: + std::string sessionId; + std::string toolName; + std::string status; // "running", "completed", "failed" + std::shared_ptr result = nullptr; // optional, only when status="completed" and status="failed" + + CliSessionInfo() = default; + + bool Marshalling(Parcel &parcel) const; + static CliSessionInfo *Unmarshalling(Parcel &parcel); +}; +} // namespace CliTool +} // namespace OHOS +#endif // OHOS_ABILITY_RUNTIME_CLI_SESSION_INFO_H \ No newline at end of file diff --git a/cli_tool_framework/interfaces/cli_tool/include/cli_tool_mgr_client.h b/cli_tool_framework/interfaces/cli_tool/include/cli_tool_mgr_client.h index ef1677dfc8..27537974a5 100644 --- a/cli_tool_framework/interfaces/cli_tool/include/cli_tool_mgr_client.h +++ b/cli_tool_framework/interfaces/cli_tool/include/cli_tool_mgr_client.h @@ -20,9 +20,9 @@ #include #include +#include "cli_session_info.h" #include "cli_tool_event.h" #include "exec_options.h" -#include "icli_tool_data.h" #include "icli_tool_manager.h" #include "iremote_object.h" diff --git a/cli_tool_framework/interfaces/cli_tool/include/exec_result.h b/cli_tool_framework/interfaces/cli_tool/include/exec_result.h new file mode 100644 index 0000000000..7368a8e398 --- /dev/null +++ b/cli_tool_framework/interfaces/cli_tool/include/exec_result.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2026 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. + */ + +#ifndef OHOS_ABILITY_RUNTIME_EXEC_RESULT_H +#define OHOS_ABILITY_RUNTIME_EXEC_RESULT_H + +#include + +#include "parcel.h" + +namespace OHOS { +namespace CliTool { +/** + * @brief Tool execution result + */ +class ExecResult : public Parcelable { +public: + int32_t exitCode = 1; + std::string outputText = ""; + std::string errorText = ""; + int32_t signalNumber = 0; + bool timedOut = false; + int64_t executionTime = 0; + + bool Marshalling(Parcel &parcel) const; + static ExecResult *Unmarshalling(Parcel &parcel); +}; +} // namespace CliTool +} // namespace OHOS +#endif // OHOS_ABILITY_RUNTIME_EXEC_RESULT_H \ No newline at end of file diff --git a/cli_tool_framework/interfaces/cli_tool/src/cli_session_info.cpp b/cli_tool_framework/interfaces/cli_tool/src/cli_session_info.cpp new file mode 100644 index 0000000000..16348a433b --- /dev/null +++ b/cli_tool_framework/interfaces/cli_tool/src/cli_session_info.cpp @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2026 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 "cli_session_info.h" + +#include "hilog_tag_wrapper.h" + +namespace OHOS { +namespace CliTool { +bool CliSessionInfo::Marshalling(Parcel &parcel) const +{ + if (!parcel.WriteString(sessionId)) { + return false; + } + if (!parcel.WriteString(toolName)) { + return false; + } + if (!parcel.WriteString(status)) { + return false; + } + + bool hasResult = (result != nullptr); + if (!parcel.WriteBool(hasResult)) { + return false; + } + if (hasResult && !parcel.WriteParcelable(result.get())) { + TAG_LOGE(AAFwkTag::CLI_TOOL, "Write result failed."); + return false; + } + return true; +} + +CliSessionInfo *CliSessionInfo::Unmarshalling(Parcel &parcel) +{ + auto *info = new (std::nothrow) CliSessionInfo(); + if (info && !parcel.ReadString(info->sessionId)) { + delete info; + return nullptr; + } + if (!parcel.ReadString(info->toolName)) { + delete info; + return nullptr; + } + if (!parcel.ReadString(info->status)) { + delete info; + return nullptr; + } + + bool hasResult = false; + if (!parcel.ReadBool(hasResult)) { + delete info; + return nullptr; + } + if (hasResult) { + std::shared_ptr execResult(parcel.ReadParcelable()); + if (execResult == nullptr) { + delete info; + return nullptr; + } + info->result = execResult; + } + return info; +} +} // namespace CliTool +} // namespace OHOS \ No newline at end of file diff --git a/cli_tool_framework/interfaces/cli_tool/src/exec_result.cpp b/cli_tool_framework/interfaces/cli_tool/src/exec_result.cpp new file mode 100644 index 0000000000..e0035b7fa5 --- /dev/null +++ b/cli_tool_framework/interfaces/cli_tool/src/exec_result.cpp @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2026 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 "exec_result.h" + +namespace OHOS { +namespace CliTool { +bool ExecResult::Marshalling(Parcel &parcel) const +{ + if (!parcel.WriteInt32(exitCode)) { + return false; + } + if (!parcel.WriteString(outputText)) { + return false; + } + if (!parcel.WriteString(errorText)) { + return false; + } + if (!parcel.WriteInt32(signalNumber)) { + return false; + } + if (!parcel.WriteBool(timedOut)) { + return false; + } + if (!parcel.WriteInt64(executionTime)) { + return false; + } + return true; +} + +ExecResult *ExecResult::Unmarshalling(Parcel &parcel) +{ + auto *result = new (std::nothrow) ExecResult(); + if (result && !parcel.ReadInt32(result->exitCode)) { + delete result; + return nullptr; + } + if (!parcel.ReadString(result->outputText)) { + delete result; + return nullptr; + } + if (!parcel.ReadString(result->errorText)) { + delete result; + return nullptr; + } + if (!parcel.ReadInt32(result->signalNumber)) { + delete result; + return nullptr; + } + if (!parcel.ReadBool(result->timedOut)) { + delete result; + return nullptr; + } + if (!parcel.ReadInt64(result->executionTime)) { + delete result; + return nullptr; + } + return result; +} +} // namespace CliTool +} // namespace OHOS \ No newline at end of file diff --git a/cli_tool_framework/services/climgr/include/session_record.h b/cli_tool_framework/services/climgr/include/session_record.h index 6087340535..fab86e4de3 100644 --- a/cli_tool_framework/services/climgr/include/session_record.h +++ b/cli_tool_framework/services/climgr/include/session_record.h @@ -25,7 +25,7 @@ #include #include -#include "icli_tool_data.h" +#include "cli_session_info.h" namespace OHOS { namespace CliTool { @@ -84,7 +84,7 @@ public: private: void TrimBufferedOutput(std::string &buffer); - ExecResult &BuildExecResult() const; + std::shared_ptr BuildExecResult() const; private: std::atomic state_ {SessionState::SPAWNING}; diff --git a/cli_tool_framework/services/climgr/src/session_record.cpp b/cli_tool_framework/services/climgr/src/session_record.cpp index dcaf244943..4010c68798 100644 --- a/cli_tool_framework/services/climgr/src/session_record.cpp +++ b/cli_tool_framework/services/climgr/src/session_record.cpp @@ -124,7 +124,8 @@ void SessionRecord::BuildSessionInfo(CliSessionInfo &session) const session.status = "running"; } else { session.result = BuildExecResult(); - session.status = (session.result.timedOut || session.result.exitCode != 0) ? "failed" : "completed"; + session.status = + (!session.result || session.result->timedOut || session.result->exitCode != 0) ? "failed" : "completed"; } } @@ -136,20 +137,20 @@ void SessionRecord::TrimBufferedOutput(std::string &buffer) buffer.erase(0, buffer.size() - MAX_BUFFERED_OUTPUT_BYTES); } -ExecResult &SessionRecord::BuildExecResult() const +std::shared_ptr SessionRecord::BuildExecResult() const { - ExecResult result; + auto result = std::make_shared(); std::lock_guard lock(resultMutex_); if (timedOut_) { - result.executionTime = timeoutMs; + result->executionTime = timeoutMs; } else { - result.exitCode = terminalStatus_; - result.executionTime = (endTimeMs_ > startTime) ? (endTimeMs_ - startTime) : 0; + result->exitCode = terminalStatus_; + result->executionTime = (endTimeMs_ > startTime) ? (endTimeMs_ - startTime) : 0; } - result.outputText = stdoutText_; - result.errorText = stderrText_; - result.signalNumber = signalNumber_; - result.timedOut = timedOut_; + result->outputText = stdoutText_; + result->errorText = stderrText_; + result->signalNumber = signalNumber_; + result->timedOut = timedOut_; return result; }