File: translate_ui_delegate.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 (177 lines) | stat: -rw-r--r-- 6,775 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
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_UI_DELEGATE_H_
#define COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_UI_DELEGATE_H_

#include <stddef.h>

#include <memory>
#include <string>
#include <vector>

#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "build/build_config.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/translate/core/browser/translate_metrics_logger.h"
#include "components/translate/core/browser/translate_ui_delegate.h"
#include "components/translate/core/common/translate_errors.h"

namespace translate {

class LanguageState;
class TranslateDriver;
class TranslateManager;
class TranslatePrefs;
class TranslateUILanguagesManager;

// The delegate for the Full Page Translate Bubble UI.
class TranslateUIDelegate {
 public:
  TranslateUIDelegate(const base::WeakPtr<TranslateManager>& translate_manager,
                      const std::string& source_language,
                      const std::string& target_language);

  TranslateUIDelegate(const TranslateUIDelegate&) = delete;
  TranslateUIDelegate& operator=(const TranslateUIDelegate&) = delete;

  ~TranslateUIDelegate();

  TranslateUILanguagesManager* translate_ui_languages_manager() {
    return translate_ui_languages_manager_.get();
  }

  // Wrappers for equivalent TranslateUILanguagesManager APIs used to add
  // Full Page Translate related logging.
  void UpdateAndRecordSourceLanguageIndex(size_t language_index);
  void UpdateAndRecordSourceLanguage(const std::string& language_code);
  void UpdateAndRecordTargetLanguageIndex(size_t language_index);
  void UpdateAndRecordTargetLanguage(const std::string& language_code);

  // Handles when an error message is shown.
  void OnErrorShown(TranslateErrors error_type);

  // Returns the LanguageState associated with this object.
  const LanguageState* GetLanguageState();

  // Translatable content languages.
  void GetContentLanguagesCodes(
      std::vector<std::string>* content_languages_codes) const;

  // Starts translating the current page.
  void Translate();

  // Reverts translation.
  void RevertTranslation();

  // Processes when the user declines translation.
  // The function name is not accurate. It only means the user did not take
  // affirmative action after the translation ui show up. The user either
  // actively decline the translation or ignore the prompt of translation.
  //   Pass |explicitly_closed| as true if user explicityly decline the
  //     translation.
  //   Pass |explicitly_closed| as false if the translation UI is dismissed
  //     implicit by some user actions which ignore the translation UI,
  //     such as switch to a new tab/window or navigate to another page by
  //     click a link.
  void TranslationDeclined(bool explicitly_closed);

  // Returns true if the current language is blocked.
  bool IsLanguageBlocked() const;

  // Sets the value if the current language is blocked.
  void SetLanguageBlocked(bool value);

  // Returns true if the current webpage should never be prompted for
  // translation.
  bool IsSiteOnNeverPromptList() const;

  // Returns true if the site of the current webpage can be put on the never
  // prompt list.
  bool CanAddSiteToNeverPromptList() const;

  // Sets the never-prompt state for the host of the current page. If
  // value is true, the current host will be blocklisted and translation
  // prompts will not show for that site.
  void SetNeverPromptSite(bool value);

  // Returns true if the webpage in the current source language should be
  // translated into the current target language automatically.
  bool ShouldAlwaysTranslate() const;

  // Sets the value if the webpage in the current source language should be
  // translated into the current target language automatically.
  void SetAlwaysTranslate(bool value);

  // Returns true if the Always Translate checkbox should be checked by default.
  bool ShouldAlwaysTranslateBeCheckedByDefault() const;

  // Returns true if the UI should offer the user a shortcut to always translate
  // the language, when we think the user wants that functionality.
  bool ShouldShowAlwaysTranslateShortcut() const;

  // Returns true if the UI should offer the user a shortcut to never translate
  // the language, when we think the user wants that functionality.
  bool ShouldShowNeverTranslateShortcut() const;

  // Updates metrics when a user's action closes the translate UI. This includes
  // when: the user presses the 'x' button, the user selects to never translate
  // this site, and the user selects to never translate this language.
  void OnUIClosedByUser();

  // Records a high level UI interaction.
  void ReportUIInteraction(UIInteraction ui_interaction);

  // Updates TranslateMetricsLogger state of whether Translate UI is currently
  // shown.
  void ReportUIChange(bool is_ui_shown);

  // If kContentLanguagesinLanguagePicker is on, build a vector of content
  // languages data.
  void MaybeSetContentLanguages();

  // Returns whether or not the current session is off-the-record.
  bool IsIncognito() const;

#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
  // Returns whether "Always Translate Language" should automatically trigger.
  // If true, this method has the side effect of mutating some prefs.
  bool ShouldAutoAlwaysTranslate();

  // Returns whether "Never Translate Language" should automatically trigger.
  // If true, this method has the side effect of mutating some prefs.
  bool ShouldAutoNeverTranslate();
#endif  // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)

 private:
  FRIEND_TEST_ALL_PREFIXES(TranslateUIDelegateTest, GetPageHost);
  FRIEND_TEST_ALL_PREFIXES(TranslateUIDelegateTest, MaybeSetContentLanguages);

  // Gets the host of the page being translated, or an empty string if no URL is
  // associated with the current page.
  std::string GetPageHost() const;

  const TranslateDriver* GetTranslateDriver() const;

  base::WeakPtr<TranslateManager> translate_manager_;

  // Manages the Translate UI language list related APIs.
  std::unique_ptr<TranslateUILanguagesManager> translate_ui_languages_manager_;

  // The list of language codes representing translatable user's setting
  // languages. The languages are in order defined by the user.
  std::vector<std::string> translatable_content_languages_codes_;

  // Translate related preferences.
  std::unique_ptr<TranslatePrefs> prefs_;

  // Listens to accept languages changes.
  PrefChangeRegistrar pref_change_registrar_;
};

}  // namespace translate

#endif  // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_UI_DELEGATE_H_