File: editor_panel_manager.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 (164 lines) | stat: -rw-r--r-- 6,301 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
// 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_PANEL_MANAGER_H_
#define CHROME_BROWSER_ASH_INPUT_METHOD_EDITOR_PANEL_MANAGER_H_

#include <optional>
#include <string>
#include <string_view>
#include <vector>

#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/observer_list_types.h"
#include "chrome/browser/ash/input_method/editor_consent_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/services/orca/public/mojom/orca_service.mojom.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"

namespace ash::input_method {

using GetEditorPanelContextCallback =
    base::OnceCallback<void(const chromeos::editor_menu::EditorContext&)>;

class EditorPanelManager {
 public:
  virtual ~EditorPanelManager() = default;
  // Gets the context of editor feature required for the context menu card.
  virtual void GetEditorPanelContext(
      GetEditorPanelContextCallback callback) = 0;

  // Invoked when the promo card is implicitly dismissed (e.g. the
  // user clicked out of the promo card).
  virtual void OnPromoCardDismissed() = 0;

  // Invoked when the promo card is explicitly dismissed via clicking
  // the button.
  virtual void OnPromoCardDeclined() = 0;

  // Invoked when the consent is rejected by the user.
  virtual void OnConsentRejected() = 0;

  // Starts the editing flow, showing the notice screen if needed.
  virtual void StartEditingFlow() = 0;

  // Starts the rewrite flow with a preset text query, showing the notice screen
  // if needed.
  virtual void StartEditingFlowWithPreset(const std::string& text_query_id) = 0;

  // Starts the write/rewrite flow with a freeform query, showing the notice
  // screen if needed.
  virtual void StartEditingFlowWithFreeform(const std::string& text) = 0;

  // Invoked when the editor menu is shown or hidden.
  virtual void OnEditorMenuVisibilityChanged(bool visible) = 0;

  // Reports the mode of the editor panel.
  virtual void LogEditorMode(chromeos::editor_menu::EditorMode mode) = 0;

  // Invoked when the consent is approved by the user.
  virtual void OnConsentApproved() = 0;

  // Invoked when the magic boost promo card is explicitly declined by the user.
  virtual void OnMagicBoostPromoCardDeclined() = 0;

  // Determines if editor should require an opt-in.
  virtual bool ShouldOptInEditor() = 0;
};

// Interface to handle communication between the context menu editor panel entry
// point and the backend of the editor feature. This includes providing context
// to determine what should be shown on the editor panel and handling events
// from the editor panel.
class EditorPanelManagerImpl : public EditorPanelManager {
 public:
  class Delegate {
   public:
    virtual ~Delegate() = default;

    virtual void BindEditorClient(
        mojo::PendingReceiver<orca::mojom::EditorClient> pending_receiver) = 0;

    virtual void OnPromoCardDeclined() = 0;
    virtual void ProcessConsentAction(ConsentAction consent_action) = 0;
    virtual void HandleTrigger(
        std::optional<std::string_view> preset_query_id,
        std::optional<std::string_view> freeform_text) = 0;
    virtual chromeos::editor_menu::EditorMode GetEditorMode() const = 0;
    virtual chromeos::editor_menu::EditorTextSelectionMode
    GetEditorTextSelectionMode() const = 0;
    virtual chromeos::editor_menu::EditorConsentStatus GetConsentStatus()
        const = 0;
    virtual EditorMetricsRecorder* GetMetricsRecorder() = 0;
    virtual EditorOpportunityMode GetEditorOpportunityMode() const = 0;
    virtual std::vector<EditorBlockedReason> GetBlockedReasons() const = 0;

    virtual void CacheContext() = 0;
  };

  class Observer : public base::CheckedObserver {
   public:
    virtual void OnEditorModeChanged(
        chromeos::editor_menu::EditorMode mode) = 0;
  };

  explicit EditorPanelManagerImpl(Delegate* delegate);
  EditorPanelManagerImpl(const EditorPanelManagerImpl&) = delete;
  EditorPanelManagerImpl& operator=(const EditorPanelManagerImpl&) = delete;
  ~EditorPanelManagerImpl() override;

  void GetEditorPanelContext(GetEditorPanelContextCallback callback) override;
  void OnPromoCardDismissed() override;
  void OnPromoCardDeclined() override;
  void OnConsentRejected() override;
  void StartEditingFlow() override;
  void StartEditingFlowWithPreset(const std::string& text_query_id) override;
  void StartEditingFlowWithFreeform(const std::string& text) override;
  void OnEditorMenuVisibilityChanged(bool visible) override;
  void LogEditorMode(chromeos::editor_menu::EditorMode mode) override;

  void BindEditorClient();
  bool IsEditorMenuVisible() const;
  void AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);
  void NotifyEditorModeChanged(chromeos::editor_menu::EditorMode mode);
  void RequestCacheContext();

  // Used by the Magic Boost opt-in flow. Virtual for testing.
  void OnConsentApproved() override;
  void OnMagicBoostPromoCardDeclined() override;

  bool ShouldOptInEditor() override;

  // For testing
  void SetEditorClientForTesting(
      mojo::PendingRemote<orca::mojom::EditorClient> client_for_testing);

 private:
  void OnGetPresetTextQueriesResult(
      GetEditorPanelContextCallback callback,
      chromeos::editor_menu::EditorMode panel_mode,
      std::vector<orca::mojom::PresetTextQueryPtr> queries);

  raw_ptr<Delegate> delegate_;
  mojo::Remote<orca::mojom::EditorClient> editor_client_remote_;

  bool is_editor_menu_visible_ = false;

  base::ObserverList<EditorPanelManagerImpl::Observer> observers_;

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

}  // namespace ash::input_method

#endif  // CHROME_BROWSER_ASH_INPUT_METHOD_EDITOR_PANEL_MANAGER_H_