diff options
author | yusukes@chromium.org <yusukes@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-09-16 07:28:00 +0000 |
---|---|---|
committer | yusukes@chromium.org <yusukes@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-09-16 07:28:00 +0000 |
commit | e6e83363954fdf5f4935f131e76fbf5af95924e7 (patch) | |
tree | b688fcdc3eba1459761d3494e714f493f538a0f2 /chrome/browser/chromeos/input_method/xkeyboard.h | |
parent | b3b534df2a90ba884b3d6360f12955382697f14c (diff) | |
download | chromium_src-e6e83363954fdf5f4935f131e76fbf5af95924e7.zip chromium_src-e6e83363954fdf5f4935f131e76fbf5af95924e7.tar.gz chromium_src-e6e83363954fdf5f4935f131e76fbf5af95924e7.tar.bz2 |
Fix memory leaks caused by chrome/browser/chromeos/input_method/*.cc (part 2)
- Don't use the XKeyboard class as a (leaky) singleton.
- Instead, add an instance of the Xkeyboard class to IBusController.
- Put all free functions in xkeyboard.h inside the XKeyboard class.
- Remove XKeyboardSets class which was also used as a singleton.
BUG=chromium:48130
TEST=ran unit_tests under valgrind (without suppressions) and confirmed that no leaks were reported.
Review URL: http://codereview.chromium.org/7888072
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@101464 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/chromeos/input_method/xkeyboard.h')
-rw-r--r-- | chrome/browser/chromeos/input_method/xkeyboard.h | 161 |
1 files changed, 94 insertions, 67 deletions
diff --git a/chrome/browser/chromeos/input_method/xkeyboard.h b/chrome/browser/chromeos/input_method/xkeyboard.h index 8451157..6c708ef 100644 --- a/chrome/browser/chromeos/input_method/xkeyboard.h +++ b/chrome/browser/chromeos/input_method/xkeyboard.h @@ -6,10 +6,13 @@ #define CHROME_BROWSER_CHROMEOS_INPUT_METHOD_XKEYBOARD_H_ #pragma once -#include <map> +#include <queue> +#include <set> #include <string> #include <vector> +#include "base/basictypes.h" + namespace chromeos { namespace input_method { @@ -42,72 +45,96 @@ struct ModifierKeyPair { }; typedef std::vector<ModifierKeyPair> ModifierMap; -// Sets the current keyboard layout to |layout_name|. This function does not -// change the current mapping of the modifier keys. Returns true on success. -bool SetCurrentKeyboardLayoutByName(const std::string& layout_name); - -// Sets the current keyboard layout again. We have to call the function every -// time when "XI_HierarchyChanged" XInput2 event is sent to Chrome. See -// xinput_hierarchy_changed_event_listener.h for details. -bool ReapplyCurrentKeyboardLayout(); - -// Remaps modifier keys. This function does not change the current keyboard -// layout. Returns true on success. -// Notice: For now, you can't remap Left Control and Left Alt keys to CapsLock. -bool RemapModifierKeys(const ModifierMap& modifier_map); - -// Turns on and off the auto-repeat of the keyboard. Returns true on success. -// Do not call the function from non-UI threads. -bool SetAutoRepeatEnabled(bool enabled); - -// Sets the auto-repeat rate of the keyboard, initial delay in ms, and repeat -// interval in ms. Returns true on success. Do not call the function from -// non-UI threads. -bool SetAutoRepeatRate(const AutoRepeatRate& rate); - -// -// WARNING: DO NOT USE FUNCTIONS/CLASSES/TYPEDEFS BELOW. They are only for -// unittests. See the definitions in chromeos_keyboard.cc for details. -// - -// Converts |key| to a modifier key name which is used in -// /usr/share/X11/xkb/symbols/chromeos. -inline std::string ModifierKeyToString(ModifierKey key) { - switch (key) { - case kSearchKey: - return "search"; - case kLeftControlKey: - return "leftcontrol"; - case kLeftAltKey: - return "leftalt"; - case kVoidKey: - return "disabled"; - case kCapsLockKey: - return "capslock"; - case kNumModifierKeys: - break; - } - return ""; -} - -// Creates a full XKB layout name like -// "gb(extd)+chromeos(leftcontrol_disabled_leftalt),us" -// from modifier key mapping and |layout_name|, such as "us", "us(dvorak)", and -// "gb(extd)". Returns an empty string on error. -std::string CreateFullXkbLayoutName(const std::string& layout_name, - const ModifierMap& modifire_map); - -// Returns true if caps lock is enabled. Do not call the function from non-UI -// threads. -bool CapsLockIsEnabled(); - -// Sets the caps lock status to |enable_caps_lock|. Do not call the function -// from non-UI threads. -void SetCapsLockEnabled(bool enabled); - -// Returns true if |key| is in |modifier_map| as replacement. -bool ContainsModifierKeyAsReplacement( - const ModifierMap& modifier_map, ModifierKey key); +class XKeyboard { + public: + XKeyboard(); + ~XKeyboard(); + + // Sets the current keyboard layout to |layout_name|. This function does not + // change the current mapping of the modifier keys. Returns true on success. + bool SetCurrentKeyboardLayoutByName(const std::string& layout_name); + + // Remaps modifier keys. This function does not change the current keyboard + // layout. Returns true on success. For now, you can't remap Left Control and + // Left Alt keys to caps lock. + bool RemapModifierKeys(const ModifierMap& modifier_map); + + // Sets the current keyboard layout again. We have to call the function every + // time when "XI_HierarchyChanged" XInput2 event is sent to Chrome. See + // xinput_hierarchy_changed_event_listener.h for details. + bool ReapplyCurrentKeyboardLayout(); + + // Turns on and off the auto-repeat of the keyboard. Returns true on success. + // Do not call the function from non-UI threads. + static bool SetAutoRepeatEnabled(bool enabled); + + // Sets the auto-repeat rate of the keyboard, initial delay in ms, and repeat + // interval in ms. Returns true on success. Do not call the function from + // non-UI threads. + static bool SetAutoRepeatRate(const AutoRepeatRate& rate); + + // Returns true if caps lock is enabled. Do not call the function from non-UI + // threads. + static bool CapsLockIsEnabled(); + + // Sets the caps lock status to |enable_caps_lock|. Do not call the function + // from non-UI threads. + static void SetCapsLockEnabled(bool enabled); + + protected: + // Creates a full XKB layout name like + // "gb(extd)+chromeos(leftcontrol_disabled_leftalt),us" + // from modifier key mapping and |layout_name|, such as "us", "us(dvorak)", + // and "gb(extd)". Returns an empty string on error. This function is + // protected: for testability. + std::string CreateFullXkbLayoutName(const std::string& layout_name, + const ModifierMap& modifire_map); + + // Returns true if |key| is in |modifier_map| as replacement. This function is + // protected: for testability. + static bool ContainsModifierKeyAsReplacement(const ModifierMap& modifier_map, + ModifierKey key); + + private: + // This function is used by SetLayout() and RemapModifierKeys(). Calls + // setxkbmap command if needed, and updates the last_full_layout_name_ cache. + bool SetLayoutInternal(const std::string& layout_name, + const ModifierMap& modifier_map, + bool force); + + // Executes 'setxkbmap -layout ...' command asynchronously using a layout name + // in the |execute_queue_|. Do nothing if the queue is empty. + // TODO(yusukes): Use libxkbfile.so instead of the command (crosbug.com/13105) + void MaybeExecuteSetLayoutCommand(); + + // Returns true if the XKB layout uses the right Alt key for special purposes + // like AltGr. + bool KeepRightAlt(const std::string& xkb_layout_name) const; + + // Returns true if the XKB layout uses the CapsLock key for special purposes. + // For example, since US Colemak layout uses the key as back space, + // KeepCapsLock("us(colemak)") would return true. + bool KeepCapsLock(const std::string& xkb_layout_name) const; + + // Converts |key| to a modifier key name which is used in + // /usr/share/X11/xkb/symbols/chromeos. + static std::string ModifierKeyToString(ModifierKey key); + + // Called when execve'd setxkbmap process exits. + static void OnSetLayoutFinish(pid_t pid, int status, XKeyboard* self); + + // The XKB layout name which we set last time like "us" and "us(dvorak)". + std::string current_layout_name_; + // The mapping of modifier keys we set last time. + ModifierMap current_modifier_map_; + // A queue for executing setxkbmap one by one. + std::queue<std::string> execute_queue_; + + std::set<std::string> keep_right_alt_xkb_layout_names_; + std::set<std::string> caps_lock_remapped_xkb_layout_names_; + + DISALLOW_COPY_AND_ASSIGN(XKeyboard); +}; } // namespace input_method } // namespace chromeos |