1 // Copyright (c) 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CHROME_BROWSER_CHROMEOS_INPUT_METHOD_ASSISTIVE_WINDOW_CONTROLLER_H_
6 #define CHROME_BROWSER_CHROMEOS_INPUT_METHOD_ASSISTIVE_WINDOW_CONTROLLER_H_
7 
8 #include <memory>
9 
10 #include "base/macros.h"
11 #include "chrome/browser/chromeos/input_method/assistive_window_properties.h"
12 #include "chrome/browser/chromeos/input_method/ui/assistive_delegate.h"
13 #include "chrome/browser/chromeos/input_method/ui/suggestion_window_view.h"
14 #include "chrome/browser/chromeos/input_method/ui/undo_window.h"
15 #include "content/public/browser/tts_controller.h"
16 #include "ui/base/ime/chromeos/ime_assistive_window_handler_interface.h"
17 #include "ui/gfx/native_widget_types.h"
18 
19 class Profile;
20 
21 namespace views {
22 class Widget;
23 }  // namespace views
24 
25 namespace chromeos {
26 
27 namespace input_method {
28 
29 class TtsHandler : public content::UtteranceEventDelegate {
30  public:
31   explicit TtsHandler(Profile* profile);
32   ~TtsHandler() override;
33 
34   // Announce |text| after some |delay|. The delay is to avoid conflict with
35   // other ChromeVox announcements. This should be no-op if ChromeVox is not
36   // enabled.
37   void Announce(const std::string& text,
38                 const base::TimeDelta delay = base::TimeDelta());
39 
40   // UtteranceEventDelegate implementation.
41   void OnTtsEvent(content::TtsUtterance* utterance,
42                   content::TtsEventType event_type,
43                   int char_index,
44                   int length,
45                   const std::string& error_message) override;
46 
47  private:
48   virtual void Speak(const std::string& text);
49 
50   Profile* const profile_;
51   std::unique_ptr<base::OneShotTimer> delay_timer_;
52 };
53 
54 class AssistiveWindowControllerDelegate;
55 
56 // AssistiveWindowController controls different assistive windows.
57 class AssistiveWindowController : public views::WidgetObserver,
58                                   public IMEAssistiveWindowHandlerInterface,
59                                   public ui::ime::AssistiveDelegate {
60  public:
61   explicit AssistiveWindowController(
62       AssistiveWindowControllerDelegate* delegate,
63       Profile* profile,
64       std::unique_ptr<TtsHandler> tts_handler = nullptr);
65   ~AssistiveWindowController() override;
66 
67   ui::ime::SuggestionWindowView* GetSuggestionWindowViewForTesting();
68   ui::ime::UndoWindow* GetUndoWindowForTesting() const;
69 
70  private:
71   // IMEAssistiveWindowHandlerInterface implementation.
72   void SetBounds(const Bounds& bounds) override;
73   void SetAssistiveWindowProperties(
74       const AssistiveWindowProperties& window) override;
75   void ShowSuggestion(const ui::ime::SuggestionDetails& details) override;
76   void SetButtonHighlighted(const ui::ime::AssistiveWindowButton& button,
77                             bool highlighted) override;
78   void AcceptSuggestion(const base::string16& suggestion) override;
79   void HideSuggestion() override;
80   base::string16 GetSuggestionText() const override;
81   size_t GetConfirmedLength() const override;
82   void FocusStateChanged() override;
83   void OnWidgetClosing(views::Widget* widget) override;
84 
85   // ui::ime::AssistiveDelegate implementation.
86   void AssistiveWindowButtonClicked(
87       const ui::ime::AssistiveWindowButton& button) const override;
88 
89   void InitSuggestionWindow();
90   void InitUndoWindow();
91 
92   const AssistiveWindowControllerDelegate* delegate_;
93   // The handler to handle Text-to-Speech (TTS) request.
94   std::unique_ptr<TtsHandler> const tts_handler_;
95   AssistiveWindowProperties window_;
96   ui::ime::SuggestionWindowView* suggestion_window_view_ = nullptr;
97   ui::ime::UndoWindow* undo_window_ = nullptr;
98   base::string16 suggestion_text_;
99   size_t confirmed_length_ = 0;
100   Bounds bounds_;
101 
102   DISALLOW_COPY_AND_ASSIGN(AssistiveWindowController);
103 };
104 
105 }  // namespace input_method
106 }  // namespace chromeos
107 
108 #endif  // CHROME_BROWSER_CHROMEOS_INPUT_METHOD_ASSISTIVE_WINDOW_CONTROLLER_H_
109