1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/input_method/editor_system_actuator.h"
#include <memory>
#include "ash/public/cpp/new_window_delegate.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "chrome/browser/ash/input_method/editor_feedback.h"
#include "chrome/browser/ash/input_method/editor_metrics_enums.h"
#include "chrome/browser/ash/input_method/editor_metrics_recorder.h"
#include "chrome/browser/ash/input_method/editor_text_insertion.h"
#include "chrome/browser/ash/input_method/url_utils.h"
#include "chromeos/strings/grit/chromeos_strings.h"
#include "ui/base/l10n/l10n_util.h"
#include "url/url_constants.h"
namespace ash::input_method {
namespace {
constexpr base::TimeDelta kAnnouncementDelay = base::Milliseconds(200);
constexpr std::string_view
kDomainsRequiringParagraphConcatenationWhenInsertingText[] = {
"notion",
"medium",
"onedrive.live",
};
bool IsUrlAllowed(const GURL& url) {
return url.SchemeIs(url::kHttpsScheme) ||
url.spec().starts_with("chrome://os-settings/osLanguages/input") ||
url.spec().starts_with("chrome://os-settings/systemPreferences");
}
EditorTextInsertion::InsertionStrategy GetInsertionStrategy(const GURL& url) {
for (std::string_view domain :
kDomainsRequiringParagraphConcatenationWhenInsertingText) {
if (IsSubDomain(url, domain)) {
return EditorTextInsertion::InsertionStrategy::kInsertAsASingleParagraph;
}
}
return EditorTextInsertion::InsertionStrategy::kInsertAsMultipleParagraphs;
}
} // namespace
EditorSystemActuator::EditorSystemActuator(
Profile* profile,
mojo::PendingAssociatedReceiver<orca::mojom::SystemActuator> receiver,
System* system)
: profile_(profile),
system_actuator_receiver_(this, std::move(receiver)),
system_(system) {}
EditorSystemActuator::~EditorSystemActuator() = default;
void EditorSystemActuator::InsertText(const std::string& text) {
EditorMetricsRecorder* logger = system_->GetMetricsRecorder();
logger->LogEditorState(EditorStates::kTextInsertionRequested);
// After making an announcement there needs to be a small delay to ensure any
// other announcements triggered from a text insertion do not collide with the
// original announcement.
system_->Announce(
l10n_util::GetStringUTF16(IDS_EDITOR_ANNOUNCEMENT_TEXT_FOR_INSERTION));
announcement_delay_.Start(
FROM_HERE, kAnnouncementDelay,
base::BindOnce(&EditorSystemActuator::QueueTextInsertion,
weak_ptr_factory_.GetWeakPtr(), text));
}
void EditorSystemActuator::ApproveConsent() {
system_->ProcessConsentAction(ConsentAction::kApprove);
switch (notice_transition_action_) {
case EditorNoticeTransitionAction::kShowEditorPanel:
system_->HandleTrigger(/*preset_query_id=*/std::nullopt,
/*freeform_text=*/std::nullopt);
return;
case EditorNoticeTransitionAction::kDoNothing:
system_->CloseUI();
return;
}
}
void EditorSystemActuator::DeclineConsent() {
system_->ProcessConsentAction(ConsentAction::kDecline);
system_->CloseUI();
}
void EditorSystemActuator::OpenUrlInNewWindow(const GURL& url) {
if (!IsUrlAllowed(url)) {
mojo::ReportBadMessage("Invalid URL scheme. Only HTTPS is allowed.");
return;
}
ash::NewWindowDelegate::GetPrimary()->OpenUrl(
url, ash::NewWindowDelegate::OpenUrlFrom::kUnspecified,
ash::NewWindowDelegate::Disposition::kNewForegroundTab);
}
void EditorSystemActuator::ShowUI() {
system_->ShowUI();
}
void EditorSystemActuator::CloseUI() {
system_->GetMetricsRecorder()->LogEditorState(
EditorStates::kClickCloseButton);
system_->CloseUI();
}
void EditorSystemActuator::SubmitFeedback(const std::string& description) {
// TODO: b/384383652 - Use `ShellDelegate::SendSpecializedFeatureFeedback`
// after this is moved out of //chrome.
SendEditorFeedback(profile_, description);
system_->Announce(
l10n_util::GetStringUTF16(IDS_EDITOR_ANNOUNCEMENT_TEXT_FOR_FEEDBACK));
}
void EditorSystemActuator::OnTrigger(
orca::mojom::TriggerContextPtr trigger_context) {
EditorMetricsRecorder* logger = system_->GetMetricsRecorder();
logger->SetTone(ToEditorMetricTone(std::move(trigger_context)));
}
void EditorSystemActuator::EmitMetricEvent(
orca::mojom::MetricEvent metric_event) {
EditorMetricsRecorder* logger = system_->GetMetricsRecorder();
std::optional<EditorStates> editor_state_metric =
ToEditorStatesMetric(metric_event);
if (editor_state_metric.has_value()) {
logger->LogEditorState(editor_state_metric.value());
}
}
void EditorSystemActuator::OnFocus(int context_id) {
if (queued_text_insertion_ == nullptr) {
return;
}
if (queued_text_insertion_->HasTimedOut()) {
queued_text_insertion_ = nullptr;
return;
}
if (queued_text_insertion_->Commit()) {
EditorMetricsRecorder* logger = system_->GetMetricsRecorder();
logger->LogEditorState(EditorStates::kInsert);
logger->LogNumberOfCharactersInserted(
queued_text_insertion_->GetTextLength());
logger->LogNumberOfCharactersSelectedForInsert(
system_->GetSelectedTextLength());
queued_text_insertion_ = nullptr;
return;
}
}
void EditorSystemActuator::QueueTextInsertion(std::string pending_text) {
// The text cannot be immediately inserted as the target input is not focused
// at this point, the WebUI is focused. After closing the WebUI focus will
// return to the original text input.
queued_text_insertion_ = std::make_unique<EditorTextInsertion>(
std::move(pending_text), GetInsertionStrategy(current_url_));
EditorMetricsRecorder* logger = system_->GetMetricsRecorder();
logger->LogEditorState(EditorStates::kTextQueuedForInsertion);
system_->CloseUI();
}
void EditorSystemActuator::OnInputContextUpdated(const GURL& url) {
current_url_ = url;
}
void EditorSystemActuator::SetNoticeTransitionAction(
EditorNoticeTransitionAction transition_action) {
notice_transition_action_ = transition_action;
}
} // namespace ash::input_method
|