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