| 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 "blimp/net/blimp_message_processor.h" | 12 #include "blimp/net/blimp_message_processor.h" |
| 13 #include "ui/base/ime/text_input_type.h" | 13 #include "ui/base/ime/text_input_type.h" |
| 14 | 14 |
| 15 namespace blimp { | 15 namespace blimp { |
| 16 namespace client { | 16 namespace client { |
| 17 | 17 |
| 18 // Handles all incoming and outgoing protobuf messages for text input of type | 18 // Handles all incoming and outgoing protobuf messages for text input of type |
| 19 // BlimpMessage::IME for blimp client. | 19 // BlimpMessage::IME for blimp client. |
| 20 // Upon receiving a text input request from the engine, the ImeFeature caches | 20 // Upon receiving a text input request from the engine, the ImeFeature |
| 21 // the |tab_id_| and |render_widget_id_| for the request and | 21 // delegates the request to the appropriate Delegate to show IME along with a |
| 22 // delegates the request to the Delegate which then opens up the IME. | 22 // Callback bound with respective tab id and render widget id. |
| 23 // After user is done typing, the text is passed back to ImeFeature, which then | |
| 24 // sends the text to the engine over network along with the same |tab_id_| and | |
| 25 // |render_widget_id_|. | |
| 26 // Any time user taps on an input text, ImeMessage::SHOW_IME message will be | 23 // Any time user taps on an input text, ImeMessage::SHOW_IME message will be |
| 27 // sent to client. Similarly, any time the text input is out of focus (e.g. if | 24 // sent to client. Similarly, any time the text input is out of focus (e.g. if |
| 28 // user navigates away from the currently page or the page loads for the first | 25 // user navigates away from the currently page or the page loads for the first |
| 29 // time), ImeMessage::HIDE_IME will be sent. | 26 // time), ImeMessage::HIDE_IME will be sent. |
| 30 | 27 |
| 31 class ImeFeature : public BlimpMessageProcessor { | 28 class ImeFeature : public BlimpMessageProcessor { |
| 32 public: | 29 public: |
| 30 // A callback to show IME. |
| 31 using ShowImeCallback = base::Callback<void(const std::string&)>; |
| 32 |
| 33 // A delegate to be notified of text input requests. | 33 // A delegate to be notified of text input requests. |
| 34 class Delegate { | 34 class Delegate { |
| 35 public: | 35 public: |
| 36 virtual ~Delegate() {} | 36 virtual ~Delegate() {} |
| 37 virtual void OnShowImeRequested(ui::TextInputType input_type, | 37 virtual void OnShowImeRequested(ui::TextInputType input_type, |
| 38 const std::string& text) = 0; | 38 const std::string& text, |
| 39 const ShowImeCallback& callback) = 0; |
| 39 virtual void OnHideImeRequested() = 0; | 40 virtual void OnHideImeRequested() = 0; |
| 40 }; | 41 }; |
| 41 | 42 |
| 42 ImeFeature(); | 43 ImeFeature(); |
| 43 ~ImeFeature() override; | 44 ~ImeFeature() override; |
| 44 | 45 |
| 45 // Set the BlimpMessageProcessor that will be used to send BlimpMessage::IME | 46 // Set the BlimpMessageProcessor that will be used to send BlimpMessage::IME |
| 46 // messages to the engine. | 47 // messages to the engine. |
| 47 void set_outgoing_message_processor( | 48 void set_outgoing_message_processor( |
| 48 std::unique_ptr<BlimpMessageProcessor> processor) { | 49 std::unique_ptr<BlimpMessageProcessor> processor) { |
| 49 outgoing_message_processor_ = std::move(processor); | 50 outgoing_message_processor_ = std::move(processor); |
| 50 } | 51 } |
| 51 | 52 |
| 52 // Sets a Delegate to be notified of all text input messages. | 53 // Sets a Delegate to be notified of all text input messages. |
| 53 // There must be a valid non-null Delegate set before routing messages | 54 // There must be a valid non-null Delegate set before routing messages |
| 54 // to the ImeFeature for processing, until the last message is processed. | 55 // to the ImeFeature for processing, until the last message is processed. |
| 55 void set_delegate(Delegate* delegate) { delegate_ = delegate; } | 56 void set_delegate(Delegate* delegate) { delegate_ = delegate; } |
| 56 | 57 |
| 57 // Sends text from IME to the blimp engine. | |
| 58 void OnImeTextEntered(const std::string& text); | |
| 59 | |
| 60 // BlimpMessageProcessor implementation. | 58 // BlimpMessageProcessor implementation. |
| 61 void ProcessMessage(std::unique_ptr<BlimpMessage> message, | 59 void ProcessMessage(std::unique_ptr<BlimpMessage> message, |
| 62 const net::CompletionCallback& callback) override; | 60 const net::CompletionCallback& callback) override; |
| 63 | 61 |
| 64 private: | 62 private: |
| 63 // Sends text from IME to the blimp engine. |
| 64 void OnImeTextEntered(int tab_id, |
| 65 int render_widget_id, |
| 66 const std::string& text); |
| 67 |
| 65 // Used to actually show or hide the IME. See notes on |set_delegate|. | 68 // Used to actually show or hide the IME. See notes on |set_delegate|. |
| 66 Delegate* delegate_ = nullptr; | 69 Delegate* delegate_ = nullptr; |
| 67 | 70 |
| 68 // Tab id and render widget id for the input field for which user input is | |
| 69 // being requested. | |
| 70 // The values are cached from the ImeMessage::SHOW_IME message and sent back | |
| 71 // to engine in the subsequent ImeMessage::SET_TEXT message. | |
| 72 // The cached values are cleared on receiving ImeMessage::HIDE_IME request. | |
| 73 int tab_id_ = -1; | |
| 74 int render_widget_id_ = 0; | |
| 75 | |
| 76 // Used to send BlimpMessage::IME messages to the engine. | 71 // Used to send BlimpMessage::IME messages to the engine. |
| 77 std::unique_ptr<BlimpMessageProcessor> outgoing_message_processor_; | 72 std::unique_ptr<BlimpMessageProcessor> outgoing_message_processor_; |
| 78 | 73 |
| 79 DISALLOW_COPY_AND_ASSIGN(ImeFeature); | 74 DISALLOW_COPY_AND_ASSIGN(ImeFeature); |
| 80 }; | 75 }; |
| 81 | 76 |
| 82 } // namespace client | 77 } // namespace client |
| 83 } // namespace blimp | 78 } // namespace blimp |
| 84 | 79 |
| 85 #endif // BLIMP_CLIENT_CORE_CONTENTS_IME_FEATURE_H_ | 80 #endif // BLIMP_CLIENT_CORE_CONTENTS_IME_FEATURE_H_ |
| OLD | NEW |