| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 CHROME_BROWSER_CHROMEOS_INPUT_METHOD_VIRTUAL_KEYBOARD_SELECTOR_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_INPUT_METHOD_VIRTUAL_KEYBOARD_SELECTOR_H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_INPUT_METHOD_VIRTUAL_KEYBOARD_SELECTOR_H_ | 6 #define CHROME_BROWSER_CHROMEOS_INPUT_METHOD_VIRTUAL_KEYBOARD_SELECTOR_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <list> | 9 #include <list> |
| 10 #include <map> |
| 10 #include <set> | 11 #include <set> |
| 11 #include <string> | 12 #include <string> |
| 12 | 13 |
| 13 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
| 14 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
| 15 | 16 |
| 16 namespace chromeos { | 17 namespace chromeos { |
| 17 namespace input_method { | 18 namespace input_method { |
| 18 | 19 |
| 19 // A class which represents a virtual keyboard extension. One virtual keyboard | 20 // A class which represents a virtual keyboard extension. One virtual keyboard |
| 20 // extension can support more than one keyboard layout. | 21 // extension can support more than one keyboard layout. |
| 21 class VirtualKeyboard { | 22 class VirtualKeyboard { |
| 22 public: | 23 public: |
| 23 VirtualKeyboard(const GURL& url, | 24 VirtualKeyboard(const GURL& url, |
| 24 const std::set<std::string>& supported_layouts, | 25 const std::set<std::string>& supported_layouts, |
| 25 bool is_system); | 26 bool is_system); |
| 26 ~VirtualKeyboard(); | 27 ~VirtualKeyboard(); |
| 27 | 28 |
| 28 // Returns URL for displaying the keyboard UI specified by |layout|. | 29 // Returns URL for displaying the keyboard UI specified by |layout|. |
| 29 // For example, when |url_| is "http://adcfj..kjhil/" and |layout| is "us", | 30 // For example, when |url_| is "http://adcfj..kjhil/" and |layout| is "us", |
| 30 // the function would return "http://adcfj..kjhil/index.html#us". When | 31 // the function would return "http://adcfj..kjhil/index.html#us". When |
| 31 // |layout| is empty, it returns |url_| as-is, which is "http://adcfj..kjhil/" | 32 // |layout| is empty, it returns |url_| as-is, which is "http://adcfj..kjhil/" |
| 32 // in this case. | 33 // in this case. |
| 33 GURL GetURLForLayout(const std::string& layout) const; | 34 GURL GetURLForLayout(const std::string& layout) const; |
| 34 | 35 |
| 36 // Returns true if the virtual keyboard extension supports the |layout|. |
| 37 bool IsLayoutSupported(const std::string& layout) const; |
| 38 |
| 35 const GURL& url() const { return url_; } | 39 const GURL& url() const { return url_; } |
| 36 const std::set<std::string>& supported_layouts() const { | 40 const std::set<std::string>& supported_layouts() const { |
| 37 return supported_layouts_; | 41 return supported_layouts_; |
| 38 } | 42 } |
| 39 bool is_system() const { return is_system_; } | 43 bool is_system() const { return is_system_; } |
| 40 | 44 |
| 41 private: | 45 private: |
| 42 const GURL url_; | 46 const GURL url_; |
| 43 const std::set<std::string> supported_layouts_; | 47 const std::set<std::string> supported_layouts_; |
| 44 const bool is_system_; | 48 const bool is_system_; |
| 45 | 49 |
| 46 DISALLOW_COPY_AND_ASSIGN(VirtualKeyboard); | 50 DISALLOW_COPY_AND_ASSIGN(VirtualKeyboard); |
| 47 }; | 51 }; |
| 48 | 52 |
| 49 // A class which holds all available virtual keyboard extensions. | 53 // A class which holds all available virtual keyboard extensions. |
| 50 class VirtualKeyboardSelector { | 54 class VirtualKeyboardSelector { |
| 51 public: | 55 public: |
| 52 VirtualKeyboardSelector(); | 56 VirtualKeyboardSelector(); |
| 53 ~VirtualKeyboardSelector(); | 57 ~VirtualKeyboardSelector(); |
| 54 | 58 |
| 55 // Adds a new virtual keyboard extension. If |keyboard.is_system_| is true, | 59 // Adds a new virtual keyboard extension. If |keyboard.is_system_| is true, |
| 56 // the virtual keyboard extension will have lower priority than non-system | 60 // the virtual keyboard extension will have lower priority than non-system |
| 57 // keyboard extensions. | 61 // keyboard extensions. Returns false if a virtual keyboard extension |
| 62 // specified by the |url| is already added. |
| 58 // TODO(yusukes): Add RemoveVirtualKeyboard() as well. | 63 // TODO(yusukes): Add RemoveVirtualKeyboard() as well. |
| 59 void AddVirtualKeyboard(const GURL& url, | 64 bool AddVirtualKeyboard(const GURL& url, |
| 60 const std::set<std::string>& supported_layouts, | 65 const std::set<std::string>& supported_layouts, |
| 61 bool is_system); | 66 bool is_system); |
| 62 | 67 |
| 63 // Selects and returns the most suitable virtual keyboard extension for the | 68 // Selects and returns the most suitable virtual keyboard extension for the |
| 64 // |layout|. Returns NULL if no virtual keyboard extension for the layout | 69 // |layout|. Returns NULL if no virtual keyboard extension for the layout |
| 65 // is found. If |current_|, which is the virtual keyboard extension currently | 70 // is found. If a specific virtual keyboard extension for the |layout| is |
| 71 // already set by SetUserPreference, the virtual keyboard extension is always |
| 72 // returned. If |current_|, which is the virtual keyboard extension currently |
| 66 // in use, supports the |layout|, the current one will be returned. Otherwise | 73 // in use, supports the |layout|, the current one will be returned. Otherwise |
| 67 // the function scans the list of |keyboards_| and then the list of | 74 // the function scans the list of |keyboards_| and then the list of |
| 68 // |system_keyboards_|. The most recently added keyboards to each list take | 75 // |system_keyboards_|. The most recently added keyboards to each list take |
| 69 // precedence. | 76 // precedence. |
| 70 // | 77 // |
| 71 // Checking the |current_| keyboard is important for the following use case: | 78 // Checking the |current_| keyboard is important for the following use case: |
| 72 // - If I have installed a VK extension that provides a US and an FR layout | 79 // - If I have installed a VK extension that provides a US and an FR layout |
| 73 // and I switch from the US layout of the extension (+ English IME) to the | 80 // and I switch from the US layout of the extension (+ English IME) to the |
| 74 // French IME, then I would like to use the FR layout of the extension I am | 81 // French IME, then I would like to use the FR layout of the extension I am |
| 75 // currently using. | 82 // currently using. |
| 76 const VirtualKeyboard* SelectVirtualKeyboard(const std::string& layout); | 83 const VirtualKeyboard* SelectVirtualKeyboard(const std::string& layout); |
| 77 | 84 |
| 78 // TODO(yusukes): Add a function something like | 85 // Sets user preferences on virtual keyboard selection so that the virtual |
| 79 // void SetUserPreference(const std::string& layout, | 86 // keyboard extension specified by the |url| is always selected for the |
| 80 // const VirtualKeyboard& keyboard); | 87 // |layout|. Returns false if a virtual keyboard extension whose address is |
| 81 // so that users could use a specific virtual keyboard extension for the | 88 // |url| is not registered, or the extension specified by the |url| does not |
| 82 // |layout|. | 89 // support the |layout|. |
| 90 bool SetUserPreference(const std::string& layout, const GURL& url); |
| 91 |
| 92 // Removes the preference for the |layout| added by SetUserPreference. |
| 93 void RemoveUserPreference(const std::string& layout); |
| 83 | 94 |
| 84 protected: | 95 protected: |
| 85 // This function neither checks |current_| nor updates the variable. The | 96 // Selects and returns the most suitable virtual keyboard extension for the |
| 86 // function is protected for testability. | 97 // |layout|. Unlike SelectVirtualKeyboard(), this function only scans |
| 87 const VirtualKeyboard* SelectVirtualKeyboardInternal( | 98 // |keyboards_| and |system_keyboards_| (in this order), and never updates |
| 99 // |current_|. The function is protected for testability. |
| 100 const VirtualKeyboard* SelectVirtualKeyboardWithoutPreferences( |
| 88 const std::string& layout); | 101 const std::string& layout); |
| 89 | 102 |
| 103 // The function is protected for testability. |
| 104 const std::map<std::string, const VirtualKeyboard*>& user_preference() const { |
| 105 return user_preference_; |
| 106 } |
| 107 |
| 90 private: | 108 private: |
| 91 // A list of third party virtual keyboard extensions. | 109 // A list of third party virtual keyboard extensions. |
| 92 std::list<const VirtualKeyboard*> keyboards_; | 110 std::list<const VirtualKeyboard*> keyboards_; |
| 93 // A list of system virtual keyboard extensions. | 111 // A list of system virtual keyboard extensions. |
| 94 std::list<const VirtualKeyboard*> system_keyboards_; | 112 std::list<const VirtualKeyboard*> system_keyboards_; |
| 95 | 113 |
| 114 // A map from layout name to virtual keyboard extension. |
| 115 std::map<std::string, const VirtualKeyboard*> user_preference_; |
| 116 |
| 117 // TODO(yusukes): Support per-site preference. e.g. always use virtual |
| 118 // keyboard ABC on https://mail.google.com/, XYZ on http://www.google.com/. |
| 119 |
| 96 // The virtual keyboard currently in use. | 120 // The virtual keyboard currently in use. |
| 97 const VirtualKeyboard* current_; | 121 const VirtualKeyboard* current_; |
| 98 | 122 |
| 123 // A map from URL to virtual keyboard extension. The map is for making |
| 124 // SetUserPreference() faster. |
| 125 std::map<GURL, const VirtualKeyboard*> url_to_keyboard_; |
| 126 |
| 99 DISALLOW_COPY_AND_ASSIGN(VirtualKeyboardSelector); | 127 DISALLOW_COPY_AND_ASSIGN(VirtualKeyboardSelector); |
| 100 }; | 128 }; |
| 101 | 129 |
| 102 } // namespace input_method | 130 } // namespace input_method |
| 103 } // namespace chromeos | 131 } // namespace chromeos |
| 104 | 132 |
| 105 #endif // CHROME_BROWSER_CHROMEOS_INPUT_METHOD_VIRTUAL_KEYBOARD_SELECTOR_H_ | 133 #endif // CHROME_BROWSER_CHROMEOS_INPUT_METHOD_VIRTUAL_KEYBOARD_SELECTOR_H_ |
| OLD | NEW |