1 // Copyright 2018 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 ASH_KEYBOARD_KEYBOARD_CONTROLLER_IMPL_H_
6 #define ASH_KEYBOARD_KEYBOARD_CONTROLLER_IMPL_H_
7 
8 #include <memory>
9 #include <set>
10 #include <vector>
11 
12 #include "ash/ash_export.h"
13 #include "ash/keyboard/ui/keyboard_layout_delegate.h"
14 #include "ash/keyboard/ui/keyboard_ui_controller.h"
15 #include "ash/public/cpp/keyboard/keyboard_controller.h"
16 #include "ash/public/cpp/keyboard/keyboard_controller_observer.h"
17 #include "ash/public/cpp/session/session_observer.h"
18 #include "base/macros.h"
19 #include "base/optional.h"
20 #include "base/time/time.h"
21 
22 class PrefChangeRegistrar;
23 class PrefRegistrySimple;
24 class PrefService;
25 
26 namespace gfx {
27 class Rect;
28 }
29 
30 namespace keyboard {
31 class KeyboardUIController;
32 class KeyboardUIFactory;
33 }  // namespace keyboard
34 
35 namespace ash {
36 
37 class SessionControllerImpl;
38 class VirtualKeyboardController;
39 struct KeyRepeatSettings;
40 
41 // Contains and observes a keyboard::KeyboardUIController instance. Ash specific
42 // behavior, including implementing the public interface, is implemented in this
43 // class.
44 class ASH_EXPORT KeyboardControllerImpl
45     : public KeyboardController,
46       public keyboard::KeyboardLayoutDelegate,
47       public KeyboardControllerObserver,
48       public SessionObserver {
49  public:
50   // |session_controller| is expected to outlive KeyboardControllerImpl.
51   explicit KeyboardControllerImpl(SessionControllerImpl* session_controller);
52   ~KeyboardControllerImpl() override;
53 
54   static void RegisterProfilePrefs(PrefRegistrySimple* registry);
55 
56   // Create or destroy the virtual keyboard. Called from Shell. TODO(stevenjb):
57   // Fix dependencies so that the virtual keyboard can be created with the
58   // keyboard controller.
59   void CreateVirtualKeyboard(
60       std::unique_ptr<keyboard::KeyboardUIFactory> keyboard_ui_factory);
61   void DestroyVirtualKeyboard();
62 
63   // Forwards events to observers.
64   void SendOnKeyboardVisibleBoundsChanged(const gfx::Rect& screen_bounds);
65   void SendOnKeyboardUIDestroyed();
66 
67   // ash::KeyboardController:
68   keyboard::KeyboardConfig GetKeyboardConfig() override;
69   void SetKeyboardConfig(
70       const keyboard::KeyboardConfig& keyboard_config) override;
71   bool IsKeyboardEnabled() override;
72   void SetEnableFlag(keyboard::KeyboardEnableFlag flag) override;
73   void ClearEnableFlag(keyboard::KeyboardEnableFlag flag) override;
74   const std::set<keyboard::KeyboardEnableFlag>& GetEnableFlags() override;
75   void ReloadKeyboardIfNeeded() override;
76   void RebuildKeyboardIfEnabled() override;
77   bool IsKeyboardVisible() override;
78   void ShowKeyboard() override;
79   void HideKeyboard(HideReason reason) override;
80   void SetContainerType(keyboard::ContainerType container_type,
81                         const gfx::Rect& target_bounds,
82                         SetContainerTypeCallback callback) override;
83   void SetKeyboardLocked(bool locked) override;
84   void SetOccludedBounds(const std::vector<gfx::Rect>& bounds) override;
85   void SetHitTestBounds(const std::vector<gfx::Rect>& bounds) override;
86   bool SetAreaToRemainOnScreen(const gfx::Rect& bounds) override;
87   void SetDraggableArea(const gfx::Rect& bounds) override;
88   bool SetWindowBoundsInScreen(const gfx::Rect& bounds_in_screen) override;
89   void SetKeyboardConfigFromPref(bool enabled) override;
90   bool ShouldOverscroll() override;
91   void AddObserver(KeyboardControllerObserver* observer) override;
92   void RemoveObserver(KeyboardControllerObserver* observer) override;
93   KeyRepeatSettings GetKeyRepeatSettings() override;
94 
95   // keyboard::KeyboardLayoutDelegate:
96   aura::Window* GetContainerForDefaultDisplay() override;
97   aura::Window* GetContainerForDisplay(
98       const display::Display& display) override;
99   void TransferGestureEventToShelf(const ui::GestureEvent& e) override;
100   // SessionObserver:
101   void OnSessionStateChanged(session_manager::SessionState state) override;
102   void OnSigninScreenPrefServiceInitialized(PrefService* prefs) override;
103   void OnActiveUserPrefServiceChanged(PrefService* prefs) override;
104 
keyboard_ui_controller()105   keyboard::KeyboardUIController* keyboard_ui_controller() {
106     return keyboard_ui_controller_.get();
107   }
108 
virtual_keyboard_controller()109   VirtualKeyboardController* virtual_keyboard_controller() {
110     return virtual_keyboard_controller_.get();
111   }
112 
113   // Called whenever a root window is closing.
114   // If the root window contains the virtual keyboard window, deactivates
115   // the keyboard so that its window doesn't get destroyed as well.
116   void OnRootWindowClosing(aura::Window* root_window);
117 
118  private:
119   // KeyboardControllerObserver:
120   void OnKeyboardConfigChanged(const keyboard::KeyboardConfig& config) override;
121   void OnKeyRepeatSettingsChanged(const KeyRepeatSettings& settings) override;
122   void OnKeyboardVisibilityChanged(bool is_visible) override;
123   void OnKeyboardVisibleBoundsChanged(const gfx::Rect& screen_bounds) override;
124   void OnKeyboardOccludedBoundsChanged(const gfx::Rect& screen_bounds) override;
125   void OnKeyboardEnableFlagsChanged(
126       const std::set<keyboard::KeyboardEnableFlag>& flags) override;
127   void OnKeyboardEnabledChanged(bool is_enabled) override;
128 
129   void ObservePrefs(PrefService* prefs);
130   void SendKeyRepeatUpdate();
131   void SendKeyboardConfigUpdate();
132 
133   std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_;
134   SessionControllerImpl* session_controller_;  // unowned
135   std::unique_ptr<keyboard::KeyboardUIController> keyboard_ui_controller_;
136   std::unique_ptr<VirtualKeyboardController> virtual_keyboard_controller_;
137   base::ObserverList<KeyboardControllerObserver>::Unchecked observers_;
138 
139   // This flag controls if the keyboard config is set from the policy settings.
140   // Note: the flag value cannot be changed from 'true' to 'false' because
141   // original config is not stored.
142   bool keyboard_config_from_pref_enabled_ = false;
143 
144   DISALLOW_COPY_AND_ASSIGN(KeyboardControllerImpl);
145 };
146 
147 }  // namespace ash
148 
149 #endif  // ASH_KEYBOARD_KEYBOARD_CONTROLLER_IMPL_H_
150