OLD | NEW |
| (Empty) |
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 #include "chrome/browser/ui/webui/options/chromeos/virtual_keyboard_manager_hand
ler.h" | |
6 | |
7 #include <map> | |
8 #include <set> | |
9 #include <string> | |
10 | |
11 #include "base/bind.h" | |
12 #include "base/bind_helpers.h" | |
13 #include "base/string_number_conversions.h" | |
14 #include "base/utf_string_conversions.h" | |
15 #include "base/values.h" | |
16 #include "chrome/browser/chromeos/input_method/input_method_manager.h" | |
17 #include "chrome/browser/chromeos/input_method/input_method_util.h" | |
18 #include "chrome/browser/chromeos/input_method/virtual_keyboard_selector.h" | |
19 #include "chrome/browser/chromeos/preferences.h" | |
20 #include "chrome/browser/prefs/pref_service.h" | |
21 #include "chrome/browser/prefs/scoped_user_pref_update.h" | |
22 #include "chrome/browser/profiles/profile.h" | |
23 #include "chrome/common/chrome_notification_types.h" | |
24 #include "chrome/common/pref_names.h" | |
25 #include "content/public/browser/notification_details.h" | |
26 #include "content/public/browser/notification_source.h" | |
27 #include "content/public/browser/web_ui.h" | |
28 #include "grit/generated_resources.h" | |
29 #include "ui/base/l10n/l10n_util.h" | |
30 | |
31 namespace ime = ::chromeos::input_method; | |
32 | |
33 namespace chromeos { | |
34 | |
35 VirtualKeyboardManagerHandler::VirtualKeyboardManagerHandler() { | |
36 } | |
37 | |
38 VirtualKeyboardManagerHandler::~VirtualKeyboardManagerHandler() { | |
39 } | |
40 | |
41 void VirtualKeyboardManagerHandler::GetLocalizedValues( | |
42 DictionaryValue* localized_strings) { | |
43 DCHECK(localized_strings); | |
44 | |
45 static const OptionsStringResource resources[] = { | |
46 { "virtualKeyboardLayoutColumnTitle", | |
47 IDS_OPTIONS_SETTINGS_LANGUAGES_VIRTUAL_KEYBOARD_LAYOUT_COLUMN_TITLE }, | |
48 { "virtualKeyboardKeyboardColumnTitle", | |
49 IDS_OPTIONS_SETTINGS_LANGUAGES_VIRTUAL_KEYBOARD_KEYBOARD_COLUMN_TITLE }, | |
50 { "defaultVirtualKeyboard", | |
51 IDS_OPTIONS_SETTINGS_LANGUAGES_DEFAULT_VIRTUAL_KEYBOARD }, | |
52 }; | |
53 RegisterStrings(localized_strings, resources, arraysize(resources)); | |
54 | |
55 RegisterTitle(localized_strings, "virtualKeyboardPage", | |
56 IDS_OPTIONS_SETTINGS_LANGUAGES_VIRTUAL_KEYBOARD_SETTINGS_TITLE); | |
57 | |
58 // Do not call GetVirtualKeyboardList() here since |web_ui()| is not ready | |
59 // yet. | |
60 } | |
61 | |
62 void VirtualKeyboardManagerHandler::RegisterMessages() { | |
63 // Register handler functions for chrome.send(). | |
64 web_ui()->RegisterMessageCallback("updateVirtualKeyboardList", | |
65 base::Bind(&VirtualKeyboardManagerHandler::UpdateVirtualKeyboardList, | |
66 base::Unretained(this))); | |
67 web_ui()->RegisterMessageCallback("setVirtualKeyboardPreference", | |
68 base::Bind(&VirtualKeyboardManagerHandler::SetVirtualKeyboardPreference, | |
69 base::Unretained(this))); | |
70 web_ui()->RegisterMessageCallback("clearVirtualKeyboardPreference", | |
71 base::Bind(&VirtualKeyboardManagerHandler::ClearVirtualKeyboardPreference, | |
72 base::Unretained(this))); | |
73 } | |
74 | |
75 ListValue* VirtualKeyboardManagerHandler::GetVirtualKeyboardList() { | |
76 ime::InputMethodManager* input_method = | |
77 ime::InputMethodManager::GetInstance(); | |
78 | |
79 // Get a multi map from layout name (e.g. "us(dvorak)"), to virtual keyboard | |
80 // extension. | |
81 const LayoutToKeyboard& layout_to_keyboard = | |
82 input_method->GetLayoutNameToKeyboardMapping(); | |
83 const UrlToKeyboard& url_to_keyboard = | |
84 input_method->GetUrlToKeyboardMapping(); | |
85 | |
86 // Get the current pref values. | |
87 PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs(); | |
88 DCHECK(prefs); | |
89 const DictionaryValue* virtual_keyboard_pref = | |
90 prefs->GetDictionary(prefs::kLanguagePreferredVirtualKeyboard); | |
91 | |
92 return CreateVirtualKeyboardList( | |
93 layout_to_keyboard, url_to_keyboard, virtual_keyboard_pref); | |
94 } | |
95 | |
96 void VirtualKeyboardManagerHandler::UpdateVirtualKeyboardList( | |
97 const ListValue* args) { | |
98 scoped_ptr<Value> virtual_keyboards(GetVirtualKeyboardList()); | |
99 DCHECK(virtual_keyboards.get()); | |
100 web_ui()->CallJavascriptFunction( | |
101 "VirtualKeyboardManager.updateVirtualKeyboardList", *virtual_keyboards); | |
102 } | |
103 | |
104 void VirtualKeyboardManagerHandler::SetVirtualKeyboardPreference( | |
105 const ListValue* args) { | |
106 std::string layout, url; | |
107 if (!args || !args->GetString(0, &layout) || !args->GetString(1, &url)) { | |
108 LOG(ERROR) << "SetVirtualKeyboardPreference: Invalid argument"; | |
109 return; | |
110 } | |
111 | |
112 // Validate args. | |
113 ime::InputMethodManager* input_method = | |
114 ime::InputMethodManager::GetInstance(); | |
115 if (!ValidateUrl(input_method->GetUrlToKeyboardMapping(), layout, url)) { | |
116 LOG(ERROR) << "SetVirtualKeyboardPreference: Invalid args: " | |
117 << "layout=" << layout << ", url=" << url; | |
118 return; | |
119 } | |
120 | |
121 PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs(); | |
122 DCHECK(prefs); | |
123 { | |
124 DictionaryPrefUpdate updater( | |
125 prefs, prefs::kLanguagePreferredVirtualKeyboard); | |
126 DictionaryValue* pref_value = updater.Get(); | |
127 pref_value->SetWithoutPathExpansion(layout, new StringValue(url)); | |
128 } | |
129 Preferences::UpdateVirturalKeyboardPreference(prefs); | |
130 } | |
131 | |
132 void VirtualKeyboardManagerHandler::ClearVirtualKeyboardPreference( | |
133 const ListValue* args) { | |
134 std::string layout; | |
135 if (!args || !args->GetString(0, &layout)) { | |
136 LOG(ERROR) << "ClearVirtualKeyboardPreference: Invalid argument"; | |
137 return; | |
138 } | |
139 | |
140 // Validate |layout|. | |
141 ime::InputMethodManager* input_method = | |
142 ime::InputMethodManager::GetInstance(); | |
143 if (!input_method->GetLayoutNameToKeyboardMapping().count(layout)) { | |
144 LOG(ERROR) << "ClearVirtualKeyboardPreference: Invalid layout: " << layout; | |
145 return; | |
146 } | |
147 | |
148 PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs(); | |
149 DCHECK(prefs); | |
150 { | |
151 DictionaryPrefUpdate updater( | |
152 prefs, prefs::kLanguagePreferredVirtualKeyboard); | |
153 DictionaryValue* pref_value = updater.Get(); | |
154 pref_value->RemoveWithoutPathExpansion(layout, NULL); | |
155 } | |
156 Preferences::UpdateVirturalKeyboardPreference(prefs); | |
157 } | |
158 | |
159 // static | |
160 bool VirtualKeyboardManagerHandler::ValidateUrl( | |
161 const UrlToKeyboard& url_to_keyboard, | |
162 const std::string& layout, | |
163 const std::string& url) { | |
164 UrlToKeyboard::const_iterator iter = url_to_keyboard.find(GURL(url)); | |
165 if (iter == url_to_keyboard.end() || | |
166 !iter->second->supported_layouts().count(layout)) { | |
167 return false; | |
168 } | |
169 return true; | |
170 } | |
171 | |
172 // static | |
173 ListValue* VirtualKeyboardManagerHandler::CreateVirtualKeyboardList( | |
174 const LayoutToKeyboard& layout_to_keyboard, | |
175 const UrlToKeyboard& url_to_keyboard, | |
176 const DictionaryValue* virtual_keyboard_pref) { | |
177 ListValue* layout_list = new ListValue; | |
178 | |
179 // |dictionary| points to an element in the |layout_list|. One dictionary | |
180 // element is created for one layout. | |
181 DictionaryValue* dictionary = NULL; | |
182 | |
183 LayoutToKeyboard::const_iterator i; | |
184 for (i = layout_to_keyboard.begin(); i != layout_to_keyboard.end(); ++i) { | |
185 const std::string& layout_id = i->first; | |
186 | |
187 std::string string_value; | |
188 // Check the "layout" value in the current dictionary. | |
189 if (dictionary) { | |
190 dictionary->GetString("layout", &string_value); | |
191 } | |
192 | |
193 if (string_value != layout_id) { | |
194 // New layout is found. Add the layout to |layout_list|. | |
195 dictionary = new DictionaryValue; | |
196 layout_list->Append(dictionary); | |
197 | |
198 // Set layout id as well as its human readable form. | |
199 ime::InputMethodManager* manager = ime::InputMethodManager::GetInstance(); | |
200 const ime::InputMethodDescriptor* desc = | |
201 manager->GetInputMethodUtil()->GetInputMethodDescriptorFromXkbId( | |
202 layout_id); | |
203 const std::string layout_name = desc ? | |
204 manager->GetInputMethodUtil()->GetInputMethodDisplayNameFromId( | |
205 desc->id()) : layout_id; | |
206 dictionary->SetString("layout", layout_id); | |
207 dictionary->SetString("layoutName", layout_name); | |
208 | |
209 // Check if the layout is in user pref. | |
210 if (virtual_keyboard_pref && | |
211 virtual_keyboard_pref->GetString(layout_id, &string_value) && | |
212 ValidateUrl(url_to_keyboard, layout_id, string_value)) { | |
213 dictionary->SetString("preferredKeyboard", string_value); | |
214 } | |
215 dictionary->Set("supportedKeyboards", new ListValue); | |
216 } | |
217 | |
218 ListValue* supported_keyboards = NULL; | |
219 dictionary->GetList("supportedKeyboards", &supported_keyboards); | |
220 DCHECK(supported_keyboards); | |
221 | |
222 DictionaryValue* virtual_keyboard = new DictionaryValue; | |
223 virtual_keyboard->SetString("name", i->second->name()); | |
224 virtual_keyboard->SetBoolean("isSystem", i->second->is_system()); | |
225 virtual_keyboard->SetString("url", i->second->url().spec()); | |
226 supported_keyboards->Append(virtual_keyboard); | |
227 } | |
228 | |
229 return layout_list; | |
230 } | |
231 | |
232 } // namespace chromeos | |
OLD | NEW |