blob: c41c8d48072e11822ff3f0efaf4a6ee44f0d1bb3 [file]
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_RENDERER_ACTOR_TOOL_EXECUTOR_H_
#define CHROME_RENDERER_ACTOR_TOOL_EXECUTOR_H_
#include "base/functional/callback_forward.h"
#include "base/memory/raw_ref.h"
#include "base/memory/weak_ptr.h"
#include "chrome/common/actor.mojom-forward.h"
#include "chrome/common/actor/task_id.h"
#include "chrome/common/chrome_render_frame.mojom.h"
#include "chrome/renderer/actor/tool_base.h"
namespace content {
class RenderFrame;
} // namespace content
namespace actor {
class Journal;
// Renderer-side tool executor.
//
// This class is responsible for receiving tool request messages and invoking
// the requested tool in the renderer.
class ToolExecutor {
public:
using ToolExecutorCallback = base::OnceCallback<void(mojom::ActionResultPtr)>;
explicit ToolExecutor(content::RenderFrame* frame, Journal& journal);
~ToolExecutor();
ToolExecutor(const ToolExecutor&) = delete;
ToolExecutor& operator=(const ToolExecutor&) = delete;
mojom::InitializeToolResultPtr InitializeTool(
mojom::ToolInvocationPtr request);
void ExecuteTool(const actor::TaskId& task_id, ToolExecutorCallback callback);
void InvokeTool(mojom::ToolInvocationPtr request,
ToolExecutorCallback callback);
void CancelTool(const actor::TaskId& task_id);
private:
mojom::InitializeToolResultPtr InitializeToolImpl(
mojom::ToolInvocationPtr request);
void ToolFinished(mojom::ActionResultPtr result);
// Tracks the execution phase of the ToolExecutor.
enum class ExecutionPhase {
kStart,
kInitialized,
kExecuting,
};
ExecutionPhase phase_ = ExecutionPhase::kStart;
bool performed_scroll_into_view_ = false;
bool is_split_execution_ = false;
// Raw ref since the executor is owned by the RenderFrameObserver which has
// the same lifetime as RenderFrame.
base::raw_ref<content::RenderFrame> frame_;
std::unique_ptr<ToolBase> tool_;
base::raw_ref<Journal> journal_;
ToolExecutorCallback completion_callback_;
std::unique_ptr<Journal::PendingAsyncEntry> invoke_journal_entry_;
std::unique_ptr<Journal::PendingAsyncEntry> execute_journal_entry_;
base::WeakPtrFactory<ToolExecutor> weak_ptr_factory_{this};
};
} // namespace actor
#endif // CHROME_RENDERER_ACTOR_TOOL_EXECUTOR_H_