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