#ifndef CHROME_BROWSER_ACTOR_ACTOR_KEYED_SERVICE_H_
#define CHROME_BROWSER_ACTOR_ACTOR_KEYED_SERVICE_H_
#include <memory>
#include <vector>
#include "base/callback_list.h"
#include "base/compiler_specific.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observation.h"
#include "base/types/expected.h"
#include "chrome/browser/actor/actor_task.h"
#include "chrome/browser/actor/actor_task_delegate.h"
#include "chrome/browser/actor/aggregated_journal.h"
#include "chrome/browser/page_content_annotations/multi_source_page_context_fetcher.h"
#include "chrome/browser/profiles/profile_observer.h"
#include "chrome/common/actor/action_result.h"
#include "chrome/common/actor/task_id.h"
#include "chrome/common/buildflags.h"
#include "components/download/content/public/all_download_item_notifier.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/sessions/core/session_id.h"
#include "components/tabs/public/tab_interface.h"
class Profile;
namespace content {
class BrowserContext;
}
namespace actor {
namespace ui {
class ActorUiStateManagerInterface;
}
class ActorPolicyChecker;
class ActorTaskMetadata;
class ToolRequest;
class ActorKeyedService : public KeyedService,
public ProfileObserver,
public download::AllDownloadItemNotifier::Observer {
public:
explicit ActorKeyedService(Profile* profile);
ActorKeyedService(const ActorKeyedService&) = delete;
ActorKeyedService& operator=(const ActorKeyedService&) = delete;
~ActorKeyedService() override;
void Shutdown() override;
static ActorKeyedService* Get(content::BrowserContext* context);
void SetActorUiStateManagerForTesting(
std::unique_ptr<ui::ActorUiStateManagerInterface> ausm);
TaskId AddActiveTask(std::unique_ptr<ActorTask> task);
const std::map<TaskId, const ActorTask*> GetActiveTasks() const;
std::vector<TaskId> FindTaskIdsInActive(
base::FunctionRef<bool(const ActorTask&)> predicate) const;
void ResetForTesting();
TaskId CreateTask();
TaskId CreateTaskWithOptions(webui::mojom::TaskOptionsPtr options,
base::WeakPtr<ActorTaskDelegate> delegate);
using PerformActionsCallback = base::OnceCallback<void(
mojom::ActionResultCode ,
std::optional<size_t> ,
std::vector<ActionResultWithLatencyInfo> )>;
void PerformActions(TaskId task_id,
std::vector<std::unique_ptr<ToolRequest>>&& actions,
ActorTaskMetadata task_metadata,
PerformActionsCallback callback);
void StopTask(TaskId task_id, ActorTask::StoppedReason stop_reason);
ActorTask* GetTask(TaskId task_id);
AggregatedJournal& GetJournal() LIFETIME_BOUND { return journal_; }
ui::ActorUiStateManagerInterface* GetActorUiStateManager();
ActorPolicyChecker& GetPolicyChecker();
bool IsActiveOnTab(const tabs::TabInterface& tab) const;
TaskId GetTaskFromTab(const tabs::TabInterface& tab) const;
Profile* GetProfile();
using TabObservationResult =
page_content_annotations::FetchPageContextResultCallbackArg;
void RequestTabObservation(
tabs::TabInterface& tab,
TaskId task_id,
base::OnceCallback<void(TabObservationResult)> callback);
static std::optional<std::string> ExtractErrorMessageIfFailed(
const TabObservationResult& result);
using TaskStateChangedCallback =
base::RepeatingCallback<void(TaskId, ActorTask::State)>;
base::CallbackListSubscription AddTaskStateChangedCallback(
TaskStateChangedCallback callback);
void NotifyTaskStateChanged(TaskId task_id, ActorTask::State state);
void OnActOnWebCapabilityChanged(bool can_act_on_web);
using ActOnWebCapabilityChangedCallback = base::RepeatingCallback<void(bool)>;
base::CallbackListSubscription AddActOnWebCapabilityChangedCallback(
ActOnWebCapabilityChangedCallback callback);
const ActorTask* GetActingActorTaskForWebContents(
content::WebContents* web_contents);
using CreateActorTabCallback = base::OnceCallback<void(tabs::TabInterface*)>;
void CreateActorTab(TaskId task_id,
bool open_in_background,
tabs::TabHandle initiator_tab_handle,
SessionID initiator_window_id,
CreateActorTabCallback callback);
void OnDownloadCreated(content::DownloadManager* manager,
download::DownloadItem* item) override;
void OnProfileInitializationComplete(Profile* profile) override;
base::WeakPtr<ActorKeyedService> GetWeakPtr();
private:
void OnActionsFinished(
PerformActionsCallback callback,
actor::mojom::ActionResultPtr action_result,
std::optional<size_t> index_of_failed_action,
std::vector<ActionResultWithLatencyInfo> action_results);
void StopAllTasks(ActorTask::StoppedReason stop_reason);
AggregatedJournal journal_;
std::unique_ptr<download::AllDownloadItemNotifier> download_notifier_;
base::ScopedObservation<Profile, ProfileObserver> profile_observation_{this};
std::unique_ptr<ui::ActorUiStateManagerInterface> actor_ui_state_manager_;
std::map<TaskId, std::unique_ptr<ActorTask>> active_tasks_;
TaskId::Generator next_task_id_;
std::unique_ptr<ActorPolicyChecker> policy_checker_;
base::RepeatingCallbackList<void(TaskId, ActorTask::State)>
tab_state_change_callback_list_;
base::RepeatingCallbackList<ActOnWebCapabilityChangedCallback::RunType>
act_on_web_capability_changed_callback_list_;
raw_ptr<Profile> profile_;
base::WeakPtrFactory<ActorKeyedService> weak_ptr_factory_{this};
};
}
#endif