File: editor_panel_manager.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (220 lines) | stat: -rw-r--r-- 8,306 bytes parent folder | download | duplicates (6)
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// 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_panel_manager.h"

#include <string_view>
#include <utility>

#include "ash/constants/ash_features.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/notreached.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ash/input_method/editor_consent_enums.h"
#include "chrome/browser/ash/input_method/editor_metrics_enums.h"
#include "chrome/browser/ash/input_method/editor_metrics_recorder.h"
#include "chromeos/ash/components/editor_menu/public/cpp/editor_consent_status.h"
#include "chromeos/ash/components/editor_menu/public/cpp/editor_context.h"
#include "chromeos/ash/components/editor_menu/public/cpp/editor_mode.h"
#include "chromeos/ash/components/editor_menu/public/cpp/editor_text_selection_mode.h"
#include "chromeos/ash/components/editor_menu/public/cpp/preset_text_query.h"
#include "chromeos/ash/services/orca/public/mojom/orca_service.mojom.h"

namespace ash::input_method {

chromeos::editor_menu::PresetQueryCategory ToPresetQueryCategory(
    const orca::mojom::PresetTextQueryType query_type) {
  switch (query_type) {
    case orca::mojom::PresetTextQueryType::kUnknown:
      return chromeos::editor_menu::PresetQueryCategory::kUnknown;
    case orca::mojom::PresetTextQueryType::kShorten:
      return chromeos::editor_menu::PresetQueryCategory::kShorten;
    case orca::mojom::PresetTextQueryType::kElaborate:
      return chromeos::editor_menu::PresetQueryCategory::kElaborate;
    case orca::mojom::PresetTextQueryType::kRephrase:
      return chromeos::editor_menu::PresetQueryCategory::kRephrase;
    case orca::mojom::PresetTextQueryType::kFormalize:
      return chromeos::editor_menu::PresetQueryCategory::kFormalize;
    case orca::mojom::PresetTextQueryType::kEmojify:
      return chromeos::editor_menu::PresetQueryCategory::kEmojify;
    case orca::mojom::PresetTextQueryType::kProofread:
      return chromeos::editor_menu::PresetQueryCategory::kProofread;
  }
}

EditorPanelManagerImpl::EditorPanelManagerImpl(Delegate* delegate)
    : delegate_(delegate) {}

EditorPanelManagerImpl::~EditorPanelManagerImpl() = default;

void EditorPanelManagerImpl::BindEditorClient() {
  if (editor_client_remote_.is_bound() &&
      !base::FeatureList::IsEnabled(ash::features::kOrcaServiceConnection)) {
    return;
  }

  editor_client_remote_.reset();
  delegate_->BindEditorClient(
      editor_client_remote_.BindNewPipeAndPassReceiver());
  editor_client_remote_.reset_on_disconnect();
}

void EditorPanelManagerImpl::GetEditorPanelContext(
    GetEditorPanelContextCallback callback) {
  chromeos::editor_menu::EditorMode editor_panel_mode =
      delegate_->GetEditorMode();

  if (editor_panel_mode != chromeos::editor_menu::EditorMode::kSoftBlocked &&
      editor_panel_mode != chromeos::editor_menu::EditorMode::kHardBlocked &&
      editor_client_remote_.is_bound()) {
    editor_client_remote_->GetPresetTextQueries(
        base::BindOnce(&EditorPanelManagerImpl::OnGetPresetTextQueriesResult,
                       weak_ptr_factory_.GetWeakPtr(), std::move(callback),
                       editor_panel_mode));
    return;
  }

  std::move(callback).Run(chromeos::editor_menu::EditorContext(
      /*mode=*/editor_panel_mode,
      /*text_selection_mode=*/delegate_->GetEditorTextSelectionMode(),
      /*consent_status_settled=*/delegate_->GetConsentStatus() !=
          chromeos::editor_menu::EditorConsentStatus::kUnset,
      chromeos::editor_menu::PresetTextQueries()));
}

void EditorPanelManagerImpl::OnPromoCardDismissed() {}

void EditorPanelManagerImpl::OnPromoCardDeclined() {
  delegate_->OnPromoCardDeclined();
  delegate_->GetMetricsRecorder()->LogEditorState(
      EditorStates::kPromoCardExplicitDismissal);
}

void EditorPanelManagerImpl::OnConsentRejected() {
  delegate_->ProcessConsentAction(ConsentAction::kDecline);
}

void EditorPanelManagerImpl::StartEditingFlow() {
  delegate_->HandleTrigger(/*preset_query_id=*/std::nullopt,
                           /*freeform_text=*/std::nullopt);
}

void EditorPanelManagerImpl::StartEditingFlowWithPreset(
    const std::string& text_query_id) {
  delegate_->HandleTrigger(/*preset_query_id=*/text_query_id,
                           /*freeform_text=*/std::nullopt);
}

void EditorPanelManagerImpl::StartEditingFlowWithFreeform(
    const std::string& text) {
  delegate_->HandleTrigger(/*preset_query_id=*/std::nullopt,
                           /*freeform_text=*/text);
}

void EditorPanelManagerImpl::OnGetPresetTextQueriesResult(
    GetEditorPanelContextCallback callback,
    chromeos::editor_menu::EditorMode mode,
    std::vector<orca::mojom::PresetTextQueryPtr> queries) {
  chromeos::editor_menu::PresetTextQueries text_queries;

  for (const auto& query : queries) {
    text_queries.push_back(chromeos::editor_menu::PresetTextQuery(
        query->id, base::UTF8ToUTF16(query->label),
        ToPresetQueryCategory(query->type)));
  }

  std::move(callback).Run(chromeos::editor_menu::EditorContext(
      mode, /*text_selection_mode=*/delegate_->GetEditorTextSelectionMode(),
      /*consent_status_settled=*/delegate_->GetConsentStatus() !=
          chromeos::editor_menu::EditorConsentStatus::kUnset,

      text_queries));
}

void EditorPanelManagerImpl::OnEditorMenuVisibilityChanged(bool visible) {
  is_editor_menu_visible_ = visible;
}

bool EditorPanelManagerImpl::IsEditorMenuVisible() const {
  return is_editor_menu_visible_;
}

void EditorPanelManagerImpl::LogEditorMode(
    chromeos::editor_menu::EditorMode mode) {
  EditorOpportunityMode opportunity_mode =
      delegate_->GetEditorOpportunityMode();
  EditorMetricsRecorder* logger = delegate_->GetMetricsRecorder();
  logger->SetMode(opportunity_mode);
  logger->SetTone(EditorTone::kUnset);
  if (opportunity_mode == EditorOpportunityMode::kRewrite ||
      opportunity_mode == EditorOpportunityMode::kWrite) {
    logger->LogEditorState(EditorStates::kNativeUIShowOpportunity);
  }

  if (mode == chromeos::editor_menu::EditorMode::kRewrite ||
      mode == chromeos::editor_menu::EditorMode::kWrite) {
    logger->LogEditorState(EditorStates::kNativeUIShown);
  }

  if (mode == chromeos::editor_menu::EditorMode::kHardBlocked ||
      mode == chromeos::editor_menu::EditorMode::kSoftBlocked) {
    logger->LogEditorState(EditorStates::kBlocked);
    for (EditorBlockedReason blocked_reason : delegate_->GetBlockedReasons()) {
      logger->LogEditorState(ToEditorStatesMetric(blocked_reason));
    }
  }

  if (mode == chromeos::editor_menu::EditorMode::kConsentNeeded) {
    logger->LogEditorState(EditorStates::kPromoCardImpression);
  }
}

void EditorPanelManagerImpl::AddObserver(
    EditorPanelManagerImpl::Observer* observer) {
  observers_.AddObserver(observer);
}

void EditorPanelManagerImpl::RemoveObserver(
    EditorPanelManagerImpl::Observer* observer) {
  observers_.RemoveObserver(observer);
}

void EditorPanelManagerImpl::NotifyEditorModeChanged(chromeos::editor_menu::EditorMode mode) {
  for (EditorPanelManagerImpl::Observer& obs : observers_) {
    obs.OnEditorModeChanged(mode);
  }
}

void EditorPanelManagerImpl::RequestCacheContext() {
  delegate_->CacheContext();
}

void EditorPanelManagerImpl::OnConsentApproved() {
  delegate_->ProcessConsentAction(ConsentAction::kApprove);
}

void EditorPanelManagerImpl::OnMagicBoostPromoCardDeclined() {
  // Reject consent and follow the normal workflow similar to when Orca's promo
  // card is declined.
  OnConsentRejected();
  OnPromoCardDeclined();
}

bool EditorPanelManagerImpl::ShouldOptInEditor() {
  chromeos::editor_menu::EditorMode editor_panel_mode =
      delegate_->GetEditorMode();
  chromeos::editor_menu::EditorConsentStatus consent_status =
      delegate_->GetConsentStatus();

  return editor_panel_mode != chromeos::editor_menu::EditorMode::kHardBlocked &&
         consent_status == chromeos::editor_menu::EditorConsentStatus::kUnset;
}

void EditorPanelManagerImpl::SetEditorClientForTesting(
    mojo::PendingRemote<orca::mojom::EditorClient> client_for_testing) {
  editor_client_remote_.Bind(std::move(client_for_testing));
}

}  // namespace ash::input_method