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