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