#include "chrome/browser/compose/compose_session.h"
#include <cmath>
#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/metrics/histogram_macros.h"
#include "base/metrics/user_metrics.h"
#include "base/strings/string_tokenizer.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/thread_pool.h"
#include "base/time/time.h"
#include "base/timer/elapsed_timer.h"
#include "base/timer/timer.h"
#include "base/values.h"
#include "chrome/browser/compose/compose_ax_serialization_utils.h"
#include "chrome/browser/content_extraction/inner_text.h"
#include "chrome/browser/feedback/show_feedback_page.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_dialogs.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/common/compose/type_conversions.h"
#include "chrome/common/webui_url_constants.h"
#include "components/autofill/core/common/form_field_data.h"
#include "components/compose/core/browser/compose_features.h"
#include "components/compose/core/browser/compose_manager_impl.h"
#include "components/compose/core/browser/compose_metrics.h"
#include "components/compose/core/browser/compose_utils.h"
#include "components/compose/core/browser/config.h"
#include "components/optimization_guide/core/model_execution/feature_keys.h"
#include "components/optimization_guide/core/model_execution/optimization_guide_model_execution_error.h"
#include "components/optimization_guide/core/model_quality/feature_type_map.h"
#include "components/optimization_guide/core/optimization_guide_features.h"
#include "components/optimization_guide/core/optimization_guide_model_executor.h"
#include "components/optimization_guide/core/optimization_guide_util.h"
#include "components/optimization_guide/proto/features/compose.pb.h"
#include "components/optimization_guide/proto/model_quality_service.pb.h"
#include "components/strings/grit/components_strings.h"
#include "content/public/browser/network_service_instance.h"
#include "content/public/browser/page_navigator.h"
#include "content/public/browser/web_contents_user_data.h"
#include "content/public/common/referrer.h"
#include "mojo/public/cpp/bindings/callback_helpers.h"
#include "ui/accessibility/ax_mode.h"
#include "ui/accessibility/ax_tree_update.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/page_transition_types.h"
#include "ui/base/window_open_disposition.h"
#include "ui/gfx/geometry/rect_f.h"
namespace {
bool IsValidComposePrompt(const std::string& prompt) { … }
const char kComposeBugReportURL[] = …;
const char kOnDeviceComposeBugReportURL[] = …;
const char kComposeLearnMorePageURL[] = …;
const char kComposeFeedbackSurveyURL[] = …;
const char kSignInPageURL[] = …;
const char kOnDeviceComposeFeedbackSurveyURL[] = …;
compose::EvalLocation GetEvalLocation(
const optimization_guide::OptimizationGuideModelStreamingExecutionResult&
result) { … }
}
class ComposeState { … };
ComposeSession::ComposeSession(
content::WebContents* web_contents,
optimization_guide::OptimizationGuideModelExecutor* executor,
base::Token session_id,
InnerTextProvider* inner_text,
autofill::FieldGlobalId node_id,
bool is_page_language_supported,
Observer* observer,
ComposeCallback callback)
: … { … }
base::optional_ref<ComposeState> ComposeSession::LastResponseState() { … }
base::optional_ref<ComposeState> ComposeSession::CurrentState(int offset) { … }
ComposeSession::~ComposeSession() { … }
void ComposeSession::Bind(
mojo::PendingReceiver<compose::mojom::ComposeSessionUntrustedPageHandler>
handler,
mojo::PendingRemote<compose::mojom::ComposeUntrustedDialog> dialog) { … }
void ComposeSession::LogCancelEdit() { … }
void ComposeSession::Compose(const std::string& input, bool is_input_edited) { … }
void ComposeSession::Rewrite(compose::mojom::StyleModifier style) { … }
void ComposeSession::LogEditInput() { … }
void ComposeSession::MakeRequest(
optimization_guide::proto::ComposeRequest request,
compose::ComposeRequestReason request_reason,
bool is_input_edited) { … }
bool ComposeSession::HasNecessaryPageContext() const { … }
void ComposeSession::RequestWithSession(
const optimization_guide::proto::ComposeRequest& request,
compose::ComposeRequestReason request_reason,
bool is_input_edited) { … }
void ComposeSession::ComposeRequestTimeout(int id) { … }
void ComposeSession::ModelExecutionCallback(
const base::ElapsedTimer& request_timer,
int request_id,
compose::ComposeRequestReason request_reason,
bool was_input_edited,
optimization_guide::OptimizationGuideModelStreamingExecutionResult result) { … }
void ComposeSession::ModelExecutionProgress(
optimization_guide::StreamingResponse result) { … }
void ComposeSession::ModelExecutionComplete(
base::TimeDelta request_delta,
compose::ComposeRequestReason request_reason,
bool was_input_edited,
optimization_guide::OptimizationGuideModelStreamingExecutionResult result) { … }
void ComposeSession::AddNewResponseToHistory(
std::unique_ptr<ComposeState> new_state) { … }
void ComposeSession::EraseForwardStatesInHistory() { … }
void ComposeSession::ProcessError(
compose::EvalLocation eval_location,
compose::mojom::ComposeStatus error,
compose::ComposeRequestReason request_reason) { … }
void ComposeSession::RequestInitialState(RequestInitialStateCallback callback) { … }
void ComposeSession::SaveWebUIState(const std::string& webui_state) { … }
void ComposeSession::AcceptComposeResult(
AcceptComposeResultCallback success_callback) { … }
void ComposeSession::RecoverFromErrorState(
RecoverFromErrorStateCallback callback) { … }
void ComposeSession::Undo(UndoCallback callback) { … }
void ComposeSession::Redo(RedoCallback callback) { … }
void ComposeSession::OpenBugReportingLink() { … }
void ComposeSession::OpenComposeLearnMorePage() { … }
void ComposeSession::OpenFeedbackSurveyLink() { … }
void ComposeSession::OpenSignInPage() { … }
bool ComposeSession::CanShowFeedbackPage() { … }
void ComposeSession::OpenFeedbackPage(std::string feedback_id) { … }
void ComposeSession::SetUserFeedback(compose::mojom::UserFeedback feedback) { … }
void ComposeSession::EditResult(const std::string& new_result,
EditResultCallback callback) { … }
void ComposeSession::InitializeWithText(std::string_view selected_text) { … }
void ComposeSession::MaybeRefreshPageContext(bool has_selection) { … }
void ComposeSession::UpdateInnerTextAndContinueComposeIfNecessary(
int request_id,
std::unique_ptr<content_extraction::InnerTextResult> result) { … }
void ComposeSession::UpdateAXSnapshotAndContinueComposeIfNecessary(
int request_id,
ui::AXTreeUpdate& update) { … }
void ComposeSession::TryContinueComposeWithContext() { … }
void ComposeSession::RefreshInnerText() { … }
void ComposeSession::RefreshAXSnapshot() { … }
void ComposeSession::SetFirstRunCloseReason(
compose::ComposeFreOrMsbbSessionCloseReason close_reason) { … }
void ComposeSession::SetFirstRunCompleted() { … }
void ComposeSession::SetMSBBCloseReason(
compose::ComposeFreOrMsbbSessionCloseReason close_reason) { … }
void ComposeSession::SetCloseReason(
compose::ComposeSessionCloseReason close_reason) { … }
bool ComposeSession::HasExpired() { … }
void ComposeSession::SetQualityLogEntryUponError(
std::unique_ptr<optimization_guide::ModelQualityLogEntry> log_entry,
base::TimeDelta request_time,
bool was_input_edited) { … }
void ComposeSession::set_current_msbb_state(bool msbb_enabled) { … }
void ComposeSession::SetSkipFeedbackUiForTesting(bool allowed) { … }