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 CHROME_BROWSER_CHROMEOS_ARC_INPUT_METHOD_MANAGER_INPUT_CONNECTION_IMPL_H_
6 #define CHROME_BROWSER_CHROMEOS_ARC_INPUT_METHOD_MANAGER_INPUT_CONNECTION_IMPL_H_
7 
8 #include <memory>
9 #include <string>
10 
11 #include "base/macros.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/optional.h"
14 #include "base/strings/string16.h"
15 #include "base/timer/timer.h"
16 #include "chrome/browser/chromeos/arc/input_method_manager/arc_input_method_manager_bridge.h"
17 #include "chrome/browser/chromeos/input_method/input_method_engine.h"
18 #include "components/arc/mojom/input_method_manager.mojom-forward.h"
19 #include "mojo/public/cpp/bindings/pending_remote.h"
20 #include "mojo/public/cpp/bindings/receiver.h"
21 
22 namespace arc {
23 
24 // The implementation of mojom::InputConnection interface. It's generated for
25 // each text field and accepts text edit commands from the ARC container.
26 class InputConnectionImpl : public mojom::InputConnection {
27  public:
28   InputConnectionImpl(chromeos::InputMethodEngine* ime_engine,
29                       ArcInputMethodManagerBridge* imm_bridge,
30                       int input_context_id);
31   ~InputConnectionImpl() override;
32 
33   // Binds this class to a passed pending remote.
34   void Bind(mojo::PendingRemote<mojom::InputConnection>* remote);
35   // Sends current text input state to the ARC container.
36   void UpdateTextInputState(bool is_input_state_update_requested);
37   // Gets current text input state.
38   mojom::TextInputStatePtr GetTextInputState(
39       bool is_input_state_update_requested) const;
40 
41   // mojom::InputConnection overrides:
42   void CommitText(const base::string16& text, int new_cursor_pos) override;
43   void DeleteSurroundingText(int before, int after) override;
44   void FinishComposingText() override;
45   void SetComposingText(
46       const base::string16& text,
47       int new_cursor_pos,
48       const base::Optional<gfx::Range>& new_selection_range) override;
49   void RequestTextInputState(
50       mojom::InputConnection::RequestTextInputStateCallback callback) override;
51   void SetSelection(const gfx::Range& new_selection_range) override;
52   void SendKeyEvent(std::unique_ptr<ui::KeyEvent> key_event) override;
53   void SetCompositionRange(const gfx::Range& new_composition_range) override;
54 
55  private:
56   // Starts the timer to send new TextInputState.
57   // This method should be called before doing any IME operation to catch state
58   // update surely. Some implementations of TextInputClient are synchronous. If
59   // starting timer is after API call, the timer won't be cancelled.
60   void StartStateUpdateTimer();
61 
62   void SendControlKeyEvent(const base::string16& text);
63 
64   chromeos::InputMethodEngine* const ime_engine_;  // Not owned
65   ArcInputMethodManagerBridge* const imm_bridge_;  // Not owned
66   const int input_context_id_;
67 
68   mojo::Receiver<mojom::InputConnection> receiver_{this};
69 
70   base::OneShotTimer state_update_timer_;
71 
72   DISALLOW_COPY_AND_ASSIGN(InputConnectionImpl);
73 };
74 
75 }  // namespace arc
76 
77 #endif  // CHROME_BROWSER_CHROMEOS_ARC_INPUT_METHOD_MANAGER_INPUT_CONNECTION_IMPL_H_
78