File: grammar_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 (111 lines) | stat: -rw-r--r-- 4,171 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
// Copyright 2021 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_GRAMMAR_MANAGER_H_
#define CHROME_BROWSER_ASH_INPUT_METHOD_GRAMMAR_MANAGER_H_

#include <string>
#include <unordered_map>
#include <unordered_set>

#include "base/memory/raw_ptr.h"
#include "base/timer/timer.h"
#include "chrome/browser/ash/input_method/grammar_service_client.h"
#include "chrome/browser/ash/input_method/suggestion_handler_interface.h"
#include "chrome/browser/ash/input_method/text_utils.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/ash/input_method/assistive_delegate.h"
#include "ui/base/ime/ash/text_input_method.h"
#include "ui/events/event.h"

namespace ash {
namespace input_method {

// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused. Needs to match ImeGrammarActions
// in enums.xml.
enum class GrammarActions {
  kUnderlined = 0,
  kWindowShown = 1,
  kAccepted = 2,
  kIgnored = 3,
  kMaxValue = kIgnored,
};

// Sends grammar check requests to ml service and upon receiving the grammar
// suggestions, shows UI to indiciate the suggestions and allows users to
// accept or dismiss the suggestions.
class GrammarManager {
 public:
  GrammarManager(Profile* profile,
                 std::unique_ptr<GrammarServiceClient> grammar_client,
                 SuggestionHandlerInterface* suggestion_handler);
  GrammarManager(const GrammarManager&) = delete;
  GrammarManager& operator=(const GrammarManager&) = delete;
  ~GrammarManager();

  // Check if the chromium flag for on-device grammar check is enabled.
  bool IsOnDeviceGrammarEnabled();

  // Indicates a new text field is focused, used to save context ID.
  void OnFocus(int context_id,
               SpellcheckMode spellcheck_mode = SpellcheckMode::kUnspecified);

  // This class intercepts keystrokes when the grammar suggestion pop up is
  // displayed. Returns whether the keypress has been handled.
  bool OnKeyEvent(const ui::KeyEvent& event);

  // Sends grammar check request to ml service or display existing grammar
  // suggestion based on the surrounding text changes and cursor changes.
  void OnSurroundingTextChanged(const std::u16string& text,
                                gfx::Range selection_range);

  // Dismisses the suggestion window and replaces the incorrect grammar fragment
  // with the suggestion.
  void AcceptSuggestion();

  void IgnoreSuggestion();

 private:
  // Sends grammar check request to ml service or display existing grammar
  // suggestion based on the surrounding text changes and cursor changes.
  // Returns true is grammar suggestion window should show.
  bool HandleSurroundingTextChange(const std::u16string& text,
                                   gfx::Range selection_range);

  void Check(const Sentence& sentence);

  void OnGrammarCheckDone(const Sentence& sentence,
                          bool success,
                          const std::vector<ui::GrammarFragment>& results);

  void DismissSuggestion();

  void SetButtonHighlighted(const ui::ime::AssistiveWindowButton& button,
                            bool highlighted);

  raw_ptr<Profile> profile_;
  std::unique_ptr<GrammarServiceClient> grammar_client_;
  raw_ptr<SuggestionHandlerInterface> suggestion_handler_;
  int context_id_ = 0;
  bool new_to_context_ = true;
  std::u16string current_text_;
  base::OneShotTimer delay_timer_;
  ui::GrammarFragment current_fragment_;
  ui::ime::AssistiveWindowButton suggestion_button_;
  const ui::ime::AssistiveWindowButton ignore_button_;
  bool suggestion_shown_ = false;
  ui::ime::ButtonId highlighted_button_ = ui::ime::ButtonId::kNone;
  Sentence current_sentence_;
  Sentence last_sentence_;
  SpellcheckMode spellcheck_mode_ = SpellcheckMode::kUnspecified;
  std::unordered_map<std::u16string, std::unordered_set<uint64_t>>
      ignored_marker_hashes_;
  std::unordered_set<uint64_t> recorded_marker_hashes_;
};

}  // namespace input_method
}  // namespace ash

#endif  // CHROME_BROWSER_ASH_INPUT_METHOD_GRAMMAR_MANAGER_H_