File: editor_mediator.h

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 (200 lines) | stat: -rw-r--r-- 7,989 bytes parent folder | download | duplicates (3)
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
// 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.

#ifndef CHROME_BROWSER_ASH_INPUT_METHOD_EDITOR_MEDIATOR_H_
#define CHROME_BROWSER_ASH_INPUT_METHOD_EDITOR_MEDIATOR_H_

#include <optional>

#include "base/memory/raw_ptr.h"
#include "base/scoped_observation.h"
#include "chrome/browser/ash/input_method/editor_announcer.h"
#include "chrome/browser/ash/input_method/editor_client_connector.h"
#include "chrome/browser/ash/input_method/editor_consent_store.h"
#include "chrome/browser/ash/input_method/editor_event_proxy.h"
#include "chrome/browser/ash/input_method/editor_event_sink.h"
#include "chrome/browser/ash/input_method/editor_geolocation_provider.h"
#include "chrome/browser/ash/input_method/editor_metrics_recorder.h"
#include "chrome/browser/ash/input_method/editor_panel_manager.h"
#include "chrome/browser/ash/input_method/editor_query_context.h"
#include "chrome/browser/ash/input_method/editor_service_connector.h"
#include "chrome/browser/ash/input_method/editor_switch.h"
#include "chrome/browser/ash/input_method/editor_system_actuator.h"
#include "chrome/browser/ash/input_method/editor_text_query_provider.h"
#include "chrome/browser/ash/input_method/editor_transition_enums.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/webui/ash/mako/mako_bubble_coordinator.h"
#include "chromeos/ash/components/editor_menu/public/cpp/editor_consent_status.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 "components/keyed_service/core/keyed_service.h"
#include "components/prefs/pref_change_registrar.h"
#include "ui/display/display_observer.h"

namespace display {
enum class TabletState;
}  // namespace display

namespace ash {
namespace input_method {

// Acts as a central "connector" for all things related to the orca project.
// This includes all current (and future) trigger points, providing the required
// plumbing to broker mojo connections from WebUIs and other clients, and
// providing an overall unified interface for the backend of the project.
class EditorMediator : public EditorContext::Observer,
                       public EditorContext::System,
                       public EditorEventSink,
                       public EditorPanelManagerImpl::Delegate,
                       public EditorSwitch::Observer,
                       public EditorSystemActuator::System,
                       public display::DisplayObserver,
                       public KeyedService {
 public:
  EditorMediator(
      Profile* profile,
      std::unique_ptr<EditorGeolocationProvider> editor_geolocation_provider);
  ~EditorMediator() override;

  // Binds a new editor instance request from a client.
  void BindEditorClient(mojo::PendingReceiver<orca::mojom::EditorClient>
                            pending_receiver) override;

  // EditorContext::Observer
  void OnContextUpdated() override;
  void OnImeChange(std::string_view engine_id) override;

  // EditorContext::System
  std::optional<ukm::SourceId> GetUkmSourceId() override;

  // EditorEventSink overrides
  void OnFocus(int context_id) override;
  void OnBlur() override;
  void OnActivateIme(std::string_view engine_id) override;
  void OnSurroundingTextChanged(const std::u16string& text,
                                gfx::Range selection_range) override;

  // EditorPanelManager::Delegate overrides
  void OnPromoCardDeclined() override;
  // TODO(b/301869966): Consider removing default parameters once the context
  // menu Orca entry is removed.
  void HandleTrigger(
      std::optional<std::string_view> preset_query_id = std::nullopt,
      std::optional<std::string_view> freeform_text = std::nullopt) override;
  chromeos::editor_menu::EditorMode GetEditorMode() const override;
  chromeos::editor_menu::EditorTextSelectionMode GetEditorTextSelectionMode()
      const override;
  chromeos::editor_menu::EditorConsentStatus GetConsentStatus() const override;
  // This method is currently used for metric purposes to understand the ratio
  // of requests being blocked vs. the potential requests that can be
  // accommodated.
  EditorOpportunityMode GetEditorOpportunityMode() const override;
  std::vector<EditorBlockedReason> GetBlockedReasons() const override;
  void CacheContext() override;
  EditorMetricsRecorder* GetMetricsRecorder() override;

  // display::DisplayObserver overrides
  void OnDisplayTabletStateChanged(display::TabletState state) override;

  // EditorSystemActuator::System overrides
  void Announce(const std::u16string& message) override;
  // EditorSystemActuator::System / EditorPanelManagerImpl::Delegate override
  void ProcessConsentAction(ConsentAction consent_action) override;
  void ShowUI() override;
  void CloseUI() override;
  size_t GetSelectedTextLength() override;

  // EditorSwitch::Observer overrides
  void OnEditorModeChanged(chromeos::editor_menu::EditorMode mode) override;

  // KeyedService overrides
  void Shutdown() override;

  void ShowNotice(EditorNoticeTransitionAction transition_action);

  // Checks if the feature should be visible.
  bool IsAllowedForUse();

  // Checks if feedbacks can be accessed.
  bool CanAccessFeedback();

  // Checks if the review notice banner can be shown in the settings page.
  bool CanShowNoticeBanner() const;

  EditorPanelManagerImpl* panel_manager() { return &panel_manager_; }

  MakoBubbleCoordinator& mako_bubble_coordinator_for_testing() {
    return mako_bubble_coordinator_;
  }

  bool SetTextQueryProviderResponseForTesting(
      const std::vector<std::string>& mock_results);
  void FetchAndUpdateInputContextForTesting();
  void OverrideEditorModeForTesting(
      chromeos::editor_menu::EditorMode editor_mode);

 private:
  struct SurroundingText {
    std::u16string text;
    gfx::Range selection_range;
  };

  class ServiceConnection {
   public:
    ServiceConnection(Profile* profile,
                      EditorMediator* mediator,
                      EditorMetricsRecorder* metrics_recorder,
                      EditorServiceConnector* service_connector);
    ~ServiceConnection();

    EditorEventProxy* editor_event_proxy();
    EditorClientConnector* editor_client_connector();
    EditorTextQueryProvider* text_query_provider();
    EditorSystemActuator* system_actuator();

   private:
    std::unique_ptr<EditorEventProxy> editor_event_proxy_;
    std::unique_ptr<EditorClientConnector> editor_client_connector_;
    std::unique_ptr<EditorTextQueryProvider> text_query_provider_;
    std::unique_ptr<EditorSystemActuator> system_actuator_;
  };

  void OnTextFieldContextualInfoChanged(const TextFieldContextualInfo& info);
  void OnEditorServiceConnected(bool is_connection_bound);
  bool IsServiceConnected();
  void ResetEditorConnections();

  bool GetUserPref();
  void SetUserPref(bool value);

  // Not owned by this class
  raw_ptr<Profile> profile_;

  EditorPanelManagerImpl panel_manager_;
  std::unique_ptr<EditorGeolocationProvider> editor_geolocation_provider_;
  MakoBubbleCoordinator mako_bubble_coordinator_;
  EditorContext editor_context_;

  std::unique_ptr<EditorSwitch> editor_switch_;
  std::unique_ptr<EditorMetricsRecorder> metrics_recorder_;
  std::unique_ptr<EditorConsentStore> consent_store_;
  std::unique_ptr<EditorServiceConnector> editor_service_connector_;
  std::unique_ptr<ServiceConnection> service_connection_;
  EditorLiveRegionAnnouncer announcer_;
  SurroundingText surrounding_text_;

  std::optional<chromeos::editor_menu::EditorMode>
      editor_mode_override_for_testing_;

  std::optional<EditorQueryContext> query_context_;

  display::ScopedDisplayObserver display_observer_{this};

  base::WeakPtrFactory<EditorMediator> weak_ptr_factory_{this};
};

}  // namespace input_method
}  // namespace ash

#endif  // CHROME_BROWSER_ASH_INPUT_METHOD_EDITOR_MEDIATOR_H_