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/chromeos/input_method/virtual_keyboard_selector.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/stl_util.h" | |
9 | |
10 namespace { | |
11 | |
12 const char kDefaultURLPath[] = "index.html"; | |
13 const size_t kDefaultURLPathLen = arraysize(kDefaultURLPath) - 1; | |
14 | |
15 namespace ime = ::chromeos::input_method; | |
16 | |
17 // Selects and returns a virtual keyboard extension from |keyboards| which | |
18 // supports the |layout|. | |
19 const ime::VirtualKeyboard* SelectVirtualKeyboardInternal( | |
20 const std::list<const ime::VirtualKeyboard*>& keyboards, | |
21 const std::string& layout) { | |
22 for (std::list<const ime::VirtualKeyboard*>::const_iterator iter = | |
23 keyboards.begin(); iter != keyboards.end(); ++iter) { | |
24 const ime::VirtualKeyboard* keyboard = *iter; | |
25 if (keyboard->IsLayoutSupported(layout)) | |
26 return keyboard; | |
27 } | |
28 return NULL; | |
29 } | |
30 | |
31 } // namespace | |
32 | |
33 namespace chromeos { | |
34 namespace input_method { | |
35 | |
36 VirtualKeyboard::VirtualKeyboard(const GURL& url, | |
37 const std::string& name, | |
38 const std::set<std::string>& supported_layouts, | |
39 bool is_system) | |
40 : url_(url), | |
41 name_(name), | |
42 supported_layouts_(supported_layouts), | |
43 is_system_(is_system) { | |
44 } | |
45 | |
46 VirtualKeyboard::~VirtualKeyboard() { | |
47 } | |
48 | |
49 GURL VirtualKeyboard::GetURLForLayout(const std::string& layout) const { | |
50 if (layout.empty()) { | |
51 return url_; | |
52 } | |
53 url_canon::Replacements<char> replacements; | |
54 replacements.SetPath( | |
55 kDefaultURLPath, url_parse::Component(0, kDefaultURLPathLen)); | |
56 // TODO(yusukes): would be better to URL-encode the |layout|? | |
57 replacements.SetRef(layout.c_str(), url_parse::Component(0, layout.length())); | |
58 return url_.ReplaceComponents(replacements); | |
59 } | |
60 | |
61 bool VirtualKeyboard::IsLayoutSupported(const std::string& layout) const { | |
62 return supported_layouts_.count(layout) > 0; | |
63 } | |
64 | |
65 VirtualKeyboardSelector::VirtualKeyboardSelector() | |
66 : current_(NULL) { | |
67 } | |
68 | |
69 VirtualKeyboardSelector::~VirtualKeyboardSelector() { | |
70 STLDeleteElements(&keyboards_); | |
71 STLDeleteElements(&system_keyboards_); | |
72 } | |
73 | |
74 bool VirtualKeyboardSelector::AddVirtualKeyboard( | |
75 const GURL& url, | |
76 const std::string& name, | |
77 const std::set<std::string>& supported_layouts, | |
78 bool is_system) { | |
79 if (url_to_keyboard_.count(url)) | |
80 return false; // the URL is already in use. | |
81 | |
82 const VirtualKeyboard* new_keyboard = new VirtualKeyboard(url, | |
83 name, | |
84 supported_layouts, | |
85 is_system); | |
86 if (is_system) { | |
87 system_keyboards_.push_front(new_keyboard); | |
88 } else { | |
89 keyboards_.push_front(new_keyboard); | |
90 } | |
91 | |
92 url_to_keyboard_.insert(std::make_pair(url, new_keyboard)); | |
93 std::set<std::string>::const_iterator layout_iter; | |
94 for (layout_iter = new_keyboard->supported_layouts().begin(); | |
95 layout_iter != new_keyboard->supported_layouts().end(); | |
96 ++layout_iter) { | |
97 const std::string& layout = *layout_iter; | |
98 layout_to_keyboard_.insert(std::make_pair(layout, new_keyboard)); | |
99 } | |
100 | |
101 return true; | |
102 } | |
103 | |
104 const VirtualKeyboard* VirtualKeyboardSelector::SelectVirtualKeyboard( | |
105 const std::string& layout) { | |
106 if (layout.empty()) { | |
107 DVLOG(1) << "No layout is specified"; | |
108 return NULL; | |
109 } | |
110 | |
111 // First, check the user pref. | |
112 std::map<std::string, const VirtualKeyboard*>::const_iterator iter = | |
113 user_preference_.find(layout); | |
114 if (iter != user_preference_.end() && | |
115 iter->second->IsLayoutSupported(layout)) { | |
116 current_ = iter->second; | |
117 return current_; | |
118 } | |
119 | |
120 // Second, check whether the current keyboard supports the layout. | |
121 if (current_ && current_->IsLayoutSupported(layout)) { | |
122 return current_; | |
123 } | |
124 | |
125 const VirtualKeyboard* keyboard = | |
126 SelectVirtualKeyboardWithoutPreferences(layout); | |
127 if (!keyboard) { | |
128 DVLOG(1) << "No virtual keyboard for " << layout << " is found"; | |
129 return NULL; | |
130 } | |
131 | |
132 current_ = keyboard; | |
133 return keyboard; | |
134 } | |
135 | |
136 bool VirtualKeyboardSelector::SetUserPreference( | |
137 const std::string& layout, const GURL& url) { | |
138 std::map<GURL, const VirtualKeyboard*>::const_iterator iter = | |
139 url_to_keyboard_.find(url); | |
140 if (iter == url_to_keyboard_.end()) { | |
141 DVLOG(1) << "Can't set user preference: unknown URL"; | |
142 return false; | |
143 } | |
144 | |
145 const VirtualKeyboard* keyboard = iter->second; | |
146 if (!keyboard->IsLayoutSupported(layout)) { | |
147 DVLOG(1) << "Requested layout is not supported by requested URL"; | |
148 return false; | |
149 } | |
150 | |
151 RemoveUserPreference(layout); | |
152 user_preference_.insert(std::make_pair(layout, keyboard)); | |
153 return true; | |
154 } | |
155 | |
156 void VirtualKeyboardSelector::RemoveUserPreference(const std::string& layout) { | |
157 user_preference_.erase(layout); | |
158 } | |
159 | |
160 void VirtualKeyboardSelector::ClearAllUserPreferences() { | |
161 user_preference_.clear(); | |
162 } | |
163 | |
164 const VirtualKeyboard* | |
165 VirtualKeyboardSelector::SelectVirtualKeyboardWithoutPreferences( | |
166 const std::string& layout) { | |
167 const VirtualKeyboard* keyboard = | |
168 SelectVirtualKeyboardInternal(keyboards_, layout); | |
169 if (!keyboard) | |
170 keyboard = SelectVirtualKeyboardInternal(system_keyboards_, layout); | |
171 return keyboard; | |
172 } | |
173 | |
174 } // namespace input_method | |
175 } // namespace chromeos | |
OLD | NEW |