File: mahi_manager.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (197 lines) | stat: -rw-r--r-- 7,238 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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROMEOS_COMPONENTS_MAHI_PUBLIC_CPP_MAHI_MANAGER_H_
#define CHROMEOS_COMPONENTS_MAHI_PUBLIC_CPP_MAHI_MANAGER_H_

#include <stdint.h>

#include <string>
#include <vector>

#include "base/component_export.h"
#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/unguessable_token.h"
#include "chromeos/crosapi/mojom/mahi.mojom.h"
#include "ui/gfx/image/image_skia.h"

class GURL;

namespace gfx {
class Rect;
}  // namespace gfx

namespace chromeos {

struct COMPONENT_EXPORT(MAHI_PUBLIC_CPP) MahiOutline {
  int id;
  std::u16string outline_content;

  bool operator==(const MahiOutline&) const;
};

enum class COMPONENT_EXPORT(MAHI_PUBLIC_CPP) MahiGetContentResponseStatus {
  // Error types can be fleshed out during implementation.
  kSuccess = 0,
  kContentExtractionError = 1,
  kUnknownError = 2,
};

// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
// List os possible response statuses for a Mahi request.
enum class COMPONENT_EXPORT(MAHI_PUBLIC_CPP) MahiResponseStatus {
  kSuccess = 0,
  kUnknownError = 1,
  kInappropriate = 2,
  kLowQuota = 3,
  kQuotaLimitHit = 4,
  kResourceExhausted = 5,
  kContentExtractionError = 6,
  kCantFindOutputData = 7,
  kRestrictedCountry = 8,
  kUnsupportedLanguage = 9,
  kMaxValue = kUnsupportedLanguage,
};

// An interface serves as the connection between mahi system and the UI.
class COMPONENT_EXPORT(MAHI_PUBLIC_CPP) MahiManager {
 public:
  MahiManager(const MahiManager&) = delete;
  MahiManager& operator=(const MahiManager&) = delete;

  virtual ~MahiManager();

  static MahiManager* Get();

  // Gets information about the content on the corresponding surface.
  virtual std::u16string GetContentTitle() = 0;
  virtual gfx::ImageSkia GetContentIcon() = 0;
  virtual GURL GetContentUrl() = 0;

  // Gets the source selected text. Should only be called when the existing
  // panel is an elucidation result.
  virtual std::u16string GetSelectedText() = 0;

  // Returns the text content on the corresponding surface.
  using MahiContentCallback =
      base::OnceCallback<void(std::u16string, MahiGetContentResponseStatus)>;
  virtual void GetContent(MahiContentCallback callback) = 0;

  // Returns the quick summary of the current active content on the
  // corresponding surface.
  using MahiSummaryCallback =
      base::OnceCallback<void(std::u16string, MahiResponseStatus)>;
  virtual void GetSummary(MahiSummaryCallback callback) = 0;

  // Returns the elucidated / simplified text of the current selected text on
  // the corresponding surface.
  using MahiElucidationCallback =
      base::OnceCallback<void(std::u16string, MahiResponseStatus)>;
  virtual void GetElucidation(MahiElucidationCallback callback) = 0;

  // Returns the outlines of the current active content on the corresponding
  // surface.
  using MahiOutlinesCallback =
      base::OnceCallback<void(std::vector<MahiOutline>, MahiResponseStatus)>;
  virtual void GetOutlines(MahiOutlinesCallback callback) = 0;

  // Goes to the content that is associated with `outline_id`.
  virtual void GoToOutlineContent(int outline_id) = 0;

  // Answers the provided `question` with a once callback.
  // `current_panel_content` is a boolean to determine if the question is
  // regarding the current content displayed on the panel.
  using MahiAnswerQuestionCallback =
      base::OnceCallback<void(std::optional<std::u16string>,
                              MahiResponseStatus)>;
  virtual void AnswerQuestion(const std::u16string& question,
                              bool current_panel_content,
                              MahiAnswerQuestionCallback callback) = 0;

  // Answers the provided `question` with a repeating callback.
  // `current_panel_content` is a boolean to determine if the question is
  // regarding the current content displayed on the panel.
  using MahiAnswerQuestionCallbackRepeating =
      base::RepeatingCallback<void(std::optional<std::u16string>,
                                   MahiResponseStatus)>;
  virtual void AnswerQuestionRepeating(
      const std::u16string& question,
      bool current_panel_content,
      MahiAnswerQuestionCallbackRepeating callback) = 0;

  // If this function is set to true, then multiple answers can be returned
  // consecutively from the server after one question is asked. If this is set
  // to false, then only one answer back from the server is allowed. If this
  // value is set to true then the MahiAnswerQuestionCallbackRepeating callback
  // function will be used within the AnswerQuestionRepeating function. Else,
  // the MahiAnswerQuestionCallback callback will be used within the
  // AnswerQuestion function.
  virtual bool AllowRepeatingAnswers() = 0;

  // Gets suggested question for the content currently displayed in the panel.
  using MahiGetSuggestedQuestionCallback =
      base::OnceCallback<void(std::u16string, MahiResponseStatus)>;
  virtual void GetSuggestedQuestion(
      MahiGetSuggestedQuestionCallback callback) = 0;

  // Set page info of current focused page
  virtual void SetCurrentFocusedPageInfo(
      crosapi::mojom::MahiPageInfoPtr info) = 0;

  virtual void OnContextMenuClicked(
      crosapi::mojom::MahiContextMenuRequestPtr context_menu_request) = 0;

  // Opens the feedback dialog.
  virtual void OpenFeedbackDialog() = 0;

  // Opens the Mahi panel on the display specified by `display_id`. The panel
  // is positied on top of the provided `mahi_menu_bounds`.
  virtual void OpenMahiPanel(int64_t display_id,
                             const gfx::Rect& mahi_menu_bounds) = 0;

  // Check if the feature is enabled.
  virtual bool IsEnabled() = 0;

  // Called when a Media app PDF window is focused, to notify Mahi about the
  // refresh avaiablity.
  virtual void SetMediaAppPDFFocused() = 0;

  // Called when a Media app PDF window is closed. This allows Mahi to hide the
  // refresh banner targeted to it.
  virtual void MediaAppPDFClosed(
      const base::UnguessableToken media_app_client_id) {}

  // If current page info is associated to a Media app PDF window, returns its
  // client id.
  virtual std::optional<base::UnguessableToken> GetMediaAppPDFClientId() const;

  // Clear the cache.
  virtual void ClearCache() {}

 protected:
  MahiManager();
};

// A scoped object that set the global instance of
// `chromeos::MahiManager::Get()` to the provided object pointer. The real
// instance will be restored when this scoped object is destructed. This class
// is used in testing and mocking.
class COMPONENT_EXPORT(MAHI_PUBLIC_CPP) ScopedMahiManagerSetter {
 public:
  explicit ScopedMahiManagerSetter(MahiManager* manager);
  ScopedMahiManagerSetter(const ScopedMahiManagerSetter&) = delete;
  ScopedMahiManagerSetter& operator=(const ScopedMahiManagerSetter&) = delete;
  ~ScopedMahiManagerSetter();

 private:
  static ScopedMahiManagerSetter* instance_;

  raw_ptr<MahiManager> real_manager_instance_ = nullptr;
};

}  // namespace chromeos

#endif  // CHROMEOS_COMPONENTS_MAHI_PUBLIC_CPP_MAHI_MANAGER_H_