Home | History | Annotate | Download | only in input_method
      1 // Copyright (c) 2012 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_INPUT_METHOD_INPUT_METHOD_UTIL_H_
      6 #define CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_UTIL_H_
      7 
      8 #include <cstddef>
      9 #include <map>
     10 #include <string>
     11 #include <vector>
     12 
     13 #include "base/containers/hash_tables.h"
     14 #include "base/memory/scoped_ptr.h"
     15 #include "base/strings/string16.h"
     16 #include "chromeos/ime/input_method_descriptor.h"
     17 
     18 namespace chromeos {
     19 namespace input_method {
     20 
     21 class InputMethodDelegate;
     22 
     23 enum InputMethodType {
     24   kKeyboardLayoutsOnly,
     25   kAllInputMethods,
     26 };
     27 
     28 // A class which provides miscellaneous input method utility functions.
     29 class InputMethodUtil {
     30  public:
     31   // |supported_input_methods| is a list of all input methods supported,
     32   // including ones not active. The list is used to initialize member variables
     33   // in this class.
     34   InputMethodUtil(InputMethodDelegate* delegate,
     35                   scoped_ptr<InputMethodDescriptors> supported_input_methods);
     36   ~InputMethodUtil();
     37 
     38   // Converts a string sent from IBus IME engines, which is written in English,
     39   // into Chrome's string ID, then pulls internationalized resource string from
     40   // the resource bundle and returns it. These functions are not thread-safe.
     41   // Non-UI threads are not allowed to call them.
     42   base::string16 TranslateString(const std::string& english_string) const;
     43 
     44   // Converts an input method ID to a language code of the IME. Returns "Eng"
     45   // when |input_method_id| is unknown.
     46   // Example: "hangul" => "ko"
     47   std::string GetLanguageCodeFromInputMethodId(
     48       const std::string& input_method_id) const;
     49 
     50   // Converts an input method ID to a display name of the IME. Returns
     51   // an empty strng when |input_method_id| is unknown.
     52   // Examples: "pinyin" => "Pinyin"
     53   std::string GetInputMethodDisplayNameFromId(
     54       const std::string& input_method_id) const;
     55 
     56   base::string16 GetInputMethodShortName(
     57       const InputMethodDescriptor& input_method) const;
     58   base::string16 GetInputMethodMediumName(
     59       const InputMethodDescriptor& input_method) const;
     60   base::string16 GetInputMethodLongName(
     61       const InputMethodDescriptor& input_method) const;
     62 
     63   // Converts an input method ID to an input method descriptor. Returns NULL
     64   // when |input_method_id| is unknown.
     65   // Example: "pinyin" => { id: "pinyin", display_name: "Pinyin",
     66   //                        keyboard_layout: "us", language_code: "zh" }
     67   const InputMethodDescriptor* GetInputMethodDescriptorFromId(
     68       const std::string& input_method_id) const;
     69 
     70   // Gets input method IDs that belong to |language_code|.
     71   // If |type| is |kKeyboardLayoutsOnly|, the function does not return input
     72   // methods that are not for keybord layout switching. Returns true on success.
     73   // Note that the function might return false or |language_code| is unknown.
     74   //
     75   // The retured input method IDs are sorted by populalirty per
     76   // chromeos/platform/assets/input_methods/whitelist.txt.
     77   bool GetInputMethodIdsFromLanguageCode(
     78       const std::string& language_code,
     79       InputMethodType type,
     80       std::vector<std::string>* out_input_method_ids) const;
     81 
     82   // Gets the input method IDs suitable for the first user login, based on
     83   // the given language code (UI language), and the descriptor of the
     84   // current input method.
     85   void GetFirstLoginInputMethodIds(
     86       const std::string& language_code,
     87       const InputMethodDescriptor& current_input_method,
     88       std::vector<std::string>* out_input_method_ids) const;
     89 
     90   // Gets the language codes associated with the given input method IDs.
     91   // The returned language codes won't have duplicates.
     92   void GetLanguageCodesFromInputMethodIds(
     93       const std::vector<std::string>& input_method_ids,
     94       std::vector<std::string>* out_language_codes) const;
     95 
     96   // Returns the input method ID of the hardware keyboard. e.g. "xkb:us::eng"
     97   // for the US Qwerty keyboard.
     98   std::string GetHardwareInputMethodId() const;
     99 
    100   // Returns true if the given input method id is supported.
    101   bool IsValidInputMethodId(const std::string& input_method_id) const;
    102 
    103   // Returns true if the given input method id is for a keyboard layout.
    104   static bool IsKeyboardLayout(const std::string& input_method_id);
    105 
    106   // Sets the list of component extension IMEs.
    107   void SetComponentExtensions(const InputMethodDescriptors& imes);
    108 
    109   // Returns the fallback input method descriptor (the very basic US
    110   // keyboard). This function is mostly used for testing, but may be used
    111   // as the fallback, when there is no other choice.
    112   static InputMethodDescriptor GetFallbackInputMethodDescriptor();
    113 
    114  protected:
    115   // protected: for unit testing as well.
    116   bool GetInputMethodIdsFromLanguageCodeInternal(
    117       const std::multimap<std::string, std::string>& language_code_to_ids,
    118       const std::string& normalized_language_code,
    119       InputMethodType type,
    120       std::vector<std::string>* out_input_method_ids) const;
    121 
    122   // protected: for unit testing as well.
    123   void ReloadInternalMaps();
    124 
    125   // All input methods that are supported, including ones not active.
    126   // protected: for testing.
    127   scoped_ptr<InputMethodDescriptors> supported_input_methods_;
    128 
    129   // Gets the keyboard layout name from the given input method ID.
    130   // If the ID is invalid, an empty string will be returned.
    131   // This function only supports xkb layouts.
    132   //
    133   // Examples:
    134   //
    135   // "xkb:us::eng"       => "us"
    136   // "xkb:us:dvorak:eng" => "us(dvorak)"
    137   // "xkb:gb::eng"       => "gb"
    138   // "pinyin"            => "us" (because Pinyin uses US keyboard layout)
    139   std::string GetKeyboardLayoutName(const std::string& input_method_id) const;
    140 
    141  private:
    142   bool TranslateStringInternal(const std::string& english_string,
    143                                base::string16 *out_string) const;
    144 
    145   // Map from language code to associated input method IDs, etc.
    146   typedef std::multimap<std::string, std::string> LanguageCodeToIdsMap;
    147   // Map from input method ID to associated input method descriptor.
    148   typedef std::map<
    149     std::string, InputMethodDescriptor> InputMethodIdToDescriptorMap;
    150   // Map from XKB layout ID to associated input method descriptor.
    151   typedef std::map<std::string, InputMethodDescriptor> XkbIdToDescriptorMap;
    152   // Map from component extention IME id to associated input method descriptor.
    153   typedef std::map<std::string, InputMethodDescriptor> ComponentExtIMEMap;
    154 
    155   LanguageCodeToIdsMap language_code_to_ids_;
    156   std::map<std::string, std::string> id_to_language_code_;
    157   InputMethodIdToDescriptorMap id_to_descriptor_;
    158   XkbIdToDescriptorMap xkb_id_to_descriptor_;
    159   ComponentExtIMEMap component_extension_ime_id_to_descriptor_;
    160 
    161   typedef base::hash_map<std::string, int> HashType;
    162   HashType english_to_resource_id_;
    163 
    164   InputMethodDelegate* delegate_;
    165 
    166   DISALLOW_COPY_AND_ASSIGN(InputMethodUtil);
    167 };
    168 
    169 }  // namespace input_method
    170 }  // namespace chromeos
    171 
    172 #endif  // CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_UTIL_H_
    173