OLD | NEW |
| (Empty) |
1 // Copyright 2016 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 BLIMP_CLIENT_CORE_CONTENTS_IME_FEATURE_H_ | |
6 #define BLIMP_CLIENT_CORE_CONTENTS_IME_FEATURE_H_ | |
7 | |
8 #include <map> | |
9 #include <string> | |
10 | |
11 #include "base/macros.h" | |
12 #include "base/memory/weak_ptr.h" | |
13 #include "blimp/net/blimp_message_processor.h" | |
14 #include "ui/base/ime/text_input_type.h" | |
15 | |
16 namespace blimp { | |
17 namespace client { | |
18 | |
19 // Handles all incoming and outgoing protobuf messages for text input of type | |
20 // BlimpMessage::IME for blimp client. | |
21 // Upon receiving a text input request from the engine, the ImeFeature | |
22 // delegates the request to the appropriate Delegate to show IME along with a | |
23 // Callback bound with respective tab id and render widget id. | |
24 // Any time user taps on an input text, ImeMessage::SHOW_IME message will be | |
25 // sent to client. Similarly, any time the text input is out of focus (e.g. if | |
26 // user navigates away from the currently page or the page loads for the first | |
27 // time), ImeMessage::HIDE_IME will be sent. | |
28 | |
29 class ImeFeature : public BlimpMessageProcessor { | |
30 public: | |
31 struct WebInputResponse; | |
32 | |
33 // A callback to show IME. | |
34 using ShowImeCallback = base::Callback<void(const WebInputResponse&)>; | |
35 | |
36 // A bundle of params required by the client. | |
37 struct WebInputRequest { | |
38 WebInputRequest(); | |
39 ~WebInputRequest(); | |
40 | |
41 ui::TextInputType input_type; | |
42 std::string text; | |
43 ShowImeCallback show_ime_callback; | |
44 }; | |
45 | |
46 // A bundle of params to be sent to the engine. | |
47 struct WebInputResponse { | |
48 WebInputResponse(); | |
49 ~WebInputResponse() = default; | |
50 | |
51 std::string text; | |
52 bool submit; | |
53 }; | |
54 | |
55 // A delegate to be notified of text input requests. | |
56 class Delegate { | |
57 public: | |
58 virtual ~Delegate() {} | |
59 virtual void OnShowImeRequested(const WebInputRequest& request) = 0; | |
60 virtual void OnHideImeRequested() = 0; | |
61 }; | |
62 | |
63 ImeFeature(); | |
64 ~ImeFeature() override; | |
65 | |
66 // Set the BlimpMessageProcessor that will be used to send BlimpMessage::IME | |
67 // messages to the engine. | |
68 void set_outgoing_message_processor( | |
69 std::unique_ptr<BlimpMessageProcessor> processor) { | |
70 outgoing_message_processor_ = std::move(processor); | |
71 } | |
72 | |
73 // Sets a Delegate to be notified of all text input messages. | |
74 // There must be a valid non-null Delegate set before routing messages | |
75 // to the ImeFeature for processing, until the last message is processed. | |
76 void set_delegate(Delegate* delegate) { delegate_ = delegate; } | |
77 | |
78 // BlimpMessageProcessor implementation. | |
79 void ProcessMessage(std::unique_ptr<BlimpMessage> message, | |
80 const net::CompletionCallback& callback) override; | |
81 | |
82 private: | |
83 // Sends text from IME to the blimp engine. | |
84 void OnImeTextEntered(int tab_id, | |
85 int render_widget_id, | |
86 const WebInputResponse& response); | |
87 | |
88 // Used to actually show or hide the IME. See notes on |set_delegate|. | |
89 Delegate* delegate_ = nullptr; | |
90 | |
91 // Used to send BlimpMessage::IME messages to the engine. | |
92 std::unique_ptr<BlimpMessageProcessor> outgoing_message_processor_; | |
93 | |
94 base::WeakPtrFactory<ImeFeature> weak_factory_; | |
95 | |
96 DISALLOW_COPY_AND_ASSIGN(ImeFeature); | |
97 }; | |
98 | |
99 } // namespace client | |
100 } // namespace blimp | |
101 | |
102 #endif // BLIMP_CLIENT_CORE_CONTENTS_IME_FEATURE_H_ | |
OLD | NEW |