15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#ifndef UI_BASE_IME_CHROMEOS_IME_BRIDGE_H_
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define UI_BASE_IME_CHROMEOS_IME_BRIDGE_H_
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <string>
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/basictypes.h"
10f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/callback.h"
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/strings/string16.h"
12f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "ui/base/ime/text_input_mode.h"
13f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "ui/base/ime/text_input_type.h"
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/base/ui_base_export.h"
15f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
16f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)namespace gfx {
17f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)class Rect;
18f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}  // namespace gfx
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)namespace ui {
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class CandidateWindow;
22a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)class KeyEvent;
23a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}  // namespace ui
24f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
25a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)namespace chromeos {
263551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class CompositionText;
283551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class UI_BASE_EXPORT IMEInputContextHandlerInterface {
303551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) public:
313551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Called when the engine commit a text.
324e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  virtual void CommitText(const std::string& text) = 0;
333551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Called when the engine updates composition text.
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void UpdateCompositionText(const CompositionText& text,
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                     uint32 cursor_pos,
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                     bool visible) = 0;
383551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
393551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Called when the engine request deleting surrounding string.
403551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  virtual void DeleteSurroundingText(int32 offset, uint32 length) = 0;
413551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)};
423551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
433551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
443551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// A interface to handle the engine handler method call.
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class UI_BASE_EXPORT IMEEngineHandlerInterface {
463551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) public:
473551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  typedef base::Callback<void (bool consumed)> KeyEventDoneCallback;
483551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
49f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // A information about a focused text input field.
50f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // A type of each member is based on the html spec, but InputContext can be
51f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // used to specify about a non html text field like Omnibox.
52f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  struct InputContext {
53f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    InputContext(ui::TextInputType type_, ui::TextInputMode mode_) :
54f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      type(type_), mode(mode_) {}
55f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
56f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // An attribute of the field defined at
57f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // http://www.w3.org/TR/html401/interact/forms.html#input-control-types.
58f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    ui::TextInputType type;
59f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // An attribute of the field defined at
60f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // http://www.whatwg.org/specs/web-apps/current-work/multipage/
61f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    //  association-of-controls-and-forms.html#input-modalities
62f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    //  :-the-inputmode-attribute.
63f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    ui::TextInputMode mode;
643551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  };
653551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual ~IMEEngineHandlerInterface() {}
673551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
683551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Called when the Chrome input field get the focus.
69f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual void FocusIn(const InputContext& input_context) = 0;
703551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
713551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Called when the Chrome input field lose the focus.
723551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  virtual void FocusOut() = 0;
733551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
743551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Called when the IME is enabled.
755f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  virtual void Enable(const std::string& component_id) = 0;
763551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
773551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Called when the IME is disabled.
783551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  virtual void Disable() = 0;
793551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
803551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Called when a property is activated or changed.
814e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  virtual void PropertyActivate(const std::string& property_name) = 0;
823551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
833551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Called when the IME is reset.
843551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  virtual void Reset() = 0;
853551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
86a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Called when the key event is received.
873551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Actual implementation must call |callback| after key event handling.
88a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  virtual void ProcessKeyEvent(const ui::KeyEvent& key_event,
893551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                               const KeyEventDoneCallback& callback) = 0;
903551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
913551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Called when the candidate in lookup table is clicked. The |index| is 0
92a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // based candidate index in lookup table.
93a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  virtual void CandidateClicked(uint32 index) = 0;
943551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
953551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Called when a new surrounding text is set. The |text| is surrounding text
963551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // and |cursor_pos| is 0 based index of cursor position in |text|. If there is
973551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // selection range, |anchor_pos| represents opposite index from |cursor_pos|.
983551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Otherwise |anchor_pos| is equal to |cursor_pos|.
993551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  virtual void SetSurroundingText(const std::string& text, uint32 cursor_pos,
1003551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                                  uint32 anchor_pos) = 0;
1013551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1023551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) protected:
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  IMEEngineHandlerInterface() {}
1043551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)};
1053551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1063551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// A interface to handle the candidate window related method call.
107a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class UI_BASE_EXPORT IMECandidateWindowHandlerInterface {
1083551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) public:
109a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual ~IMECandidateWindowHandlerInterface() {}
1103551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1113551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Called when the IME updates the lookup table.
1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void UpdateLookupTable(const ui::CandidateWindow& candidate_window,
1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                 bool visible) = 0;
1143551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1153551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Called when the IME updates the preedit text. The |text| is given in
1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // UTF-16 encoding.
1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void UpdatePreeditText(const base::string16& text,
1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                 uint32 cursor_pos,
1193551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                                 bool visible) = 0;
1203551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
121f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Called when the application changes its caret bounds.
122f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual void SetCursorBounds(const gfx::Rect& cursor_bounds,
123f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                               const gfx::Rect& composition_head) = 0;
1243551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
125f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Called when the text field's focus state is changed.
126f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // |is_focused| is true when the text field gains the focus.
127f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual void FocusStateChanged(bool is_focused) {}
1283551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1293551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) protected:
130a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  IMECandidateWindowHandlerInterface() {}
1313551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)};
1323551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// IMEBridge provides access of each IME related handler. This class
1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// is used for IME implementation.
1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class UI_BASE_EXPORT IMEBridge {
1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual ~IMEBridge();
1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Allocates the global instance. Must be called before any calls to Get().
141f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  static void Initialize();
1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Releases the global instance.
144f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  static void Shutdown();
1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Returns IMEBridge global instance. Initialize() must be called first.
1475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static IMEBridge* Get();
1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns current InputContextHandler. This function returns NULL if input
1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // context is not ready to use.
151a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual IMEInputContextHandlerInterface* GetInputContextHandler() const = 0;
1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Updates current InputContextHandler. If there is no active input context,
1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // pass NULL for |handler|. Caller must release |handler|.
1552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void SetInputContextHandler(
156a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      IMEInputContextHandlerInterface* handler) = 0;
1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Updates current EngineHandler. If there is no active engine service, pass
1592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // NULL for |handler|. Caller must release |handler|.
1605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void SetCurrentEngineHandler(IMEEngineHandlerInterface* handler) = 0;
1612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Returns current EngineHandler. This function returns NULL if current engine
1635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // is not ready to use.
1645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual IMEEngineHandlerInterface* GetCurrentEngineHandler() const = 0;
1655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns current CandidateWindowHandler. This function returns NULL if
1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // current candidate window is not ready to use.
168a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual IMECandidateWindowHandlerInterface* GetCandidateWindowHandler()
169a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      const = 0;
1702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Updates current CandidatWindowHandler. If there is no active candidate
1722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // window service, pass NULL for |handler|. Caller must release |handler|.
1732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void SetCandidateWindowHandler(
174a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      IMECandidateWindowHandlerInterface* handler) = 0;
1752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1760529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // Updates current text input type.
1770529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  virtual void SetCurrentTextInputType(ui::TextInputType input_type) = 0;
1780529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
1790529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // Returns the current text input type.
1800529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  virtual ui::TextInputType GetCurrentTextInputType() const = 0;
1810529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
1822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) protected:
1835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  IMEBridge();
1842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
1865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(IMEBridge);
1872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
1882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace chromeos
1902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif  // UI_BASE_IME_CHROMEOS_IME_BRIDGE_H_
192