Index: chrome/browser/chromeos/input_method/virtual_keyboard_selector.cc |
diff --git a/chrome/browser/chromeos/input_method/virtual_keyboard_selector.cc b/chrome/browser/chromeos/input_method/virtual_keyboard_selector.cc |
index 1e427ad3acd4c2c14468f1fc9a22bd713846bf5d..fb04abaf03d8aae359c192d00891a4db59fac1c5 100644 |
--- a/chrome/browser/chromeos/input_method/virtual_keyboard_selector.cc |
+++ b/chrome/browser/chromeos/input_method/virtual_keyboard_selector.cc |
@@ -8,8 +8,26 @@ |
#include "base/stl_util.h" |
namespace { |
+ |
const char kDefaultURLPath[] = "index.html"; |
const size_t kDefaultURLPathLen = arraysize(kDefaultURLPath) - 1; |
+ |
+namespace ime = ::chromeos::input_method; |
+ |
+// Selects and returns a virtual keyboard extension from |keyboards| which |
+// supports the |layout|. |
+const ime::VirtualKeyboard* SelectVirtualKeyboardInternal( |
+ const std::list<const ime::VirtualKeyboard*>& keyboards, |
+ const std::string& layout) { |
+ for (std::list<const ime::VirtualKeyboard*>::const_iterator iter = |
+ keyboards.begin(); iter != keyboards.end(); ++iter) { |
+ const ime::VirtualKeyboard* keyboard = *iter; |
+ if (keyboard->IsLayoutSupported(layout)) |
+ return keyboard; |
+ } |
+ return NULL; |
+} |
+ |
} // namespace |
namespace chromeos { |
@@ -38,6 +56,10 @@ GURL VirtualKeyboard::GetURLForLayout(const std::string& layout) const { |
return url_.ReplaceComponents(replacements); |
} |
+bool VirtualKeyboard::IsLayoutSupported(const std::string& layout) const { |
+ return supported_layouts_.count(layout) > 0; |
+} |
+ |
VirtualKeyboardSelector::VirtualKeyboardSelector() |
: current_(NULL) { |
} |
@@ -47,10 +69,13 @@ VirtualKeyboardSelector::~VirtualKeyboardSelector() { |
STLDeleteElements(&system_keyboards_); |
} |
-void VirtualKeyboardSelector::AddVirtualKeyboard( |
+bool VirtualKeyboardSelector::AddVirtualKeyboard( |
const GURL& url, |
const std::set<std::string>& supported_layouts, |
bool is_system) { |
+ if (url_to_keyboard_.count(url)) |
+ return false; // the URL is already in use. |
+ |
const VirtualKeyboard* new_keyboard = new VirtualKeyboard(url, |
supported_layouts, |
is_system); |
@@ -59,6 +84,9 @@ void VirtualKeyboardSelector::AddVirtualKeyboard( |
} else { |
keyboards_.push_front(new_keyboard); |
} |
+ |
+ url_to_keyboard_.insert(std::make_pair(url, new_keyboard)); |
+ return true; |
} |
const VirtualKeyboard* VirtualKeyboardSelector::SelectVirtualKeyboard( |
@@ -68,12 +96,22 @@ const VirtualKeyboard* VirtualKeyboardSelector::SelectVirtualKeyboard( |
return NULL; |
} |
- // First, check whether the current keyboard supports the layout. |
- if (current_ && current_->supported_layouts().count(layout) > 0) { |
+ // First, check the user pref. |
+ std::map<std::string, const VirtualKeyboard*>::const_iterator iter = |
+ user_preference_.find(layout); |
+ if (iter != user_preference_.end() && |
+ iter->second->IsLayoutSupported(layout)) { |
+ current_ = iter->second; |
return current_; |
} |
- const VirtualKeyboard* keyboard = SelectVirtualKeyboardInternal(layout); |
+ // Second, check whether the current keyboard supports the layout. |
+ if (current_ && current_->IsLayoutSupported(layout)) { |
+ return current_; |
+ } |
+ |
+ const VirtualKeyboard* keyboard = |
+ SelectVirtualKeyboardWithoutPreferences(layout); |
if (!keyboard) { |
VLOG(1) << "No virtual keyboard for " << layout << " is found"; |
return NULL; |
@@ -83,21 +121,38 @@ const VirtualKeyboard* VirtualKeyboardSelector::SelectVirtualKeyboard( |
return keyboard; |
} |
-const VirtualKeyboard* VirtualKeyboardSelector::SelectVirtualKeyboardInternal( |
- const std::string& layout) { |
- std::list<const VirtualKeyboard*>::const_iterator iter; |
- for (iter = keyboards_.begin(); iter != keyboards_.end(); ++iter) { |
- if ((*iter)->supported_layouts().count(layout) > 0) { |
- return *iter; |
- } |
+bool VirtualKeyboardSelector::SetUserPreference( |
+ const std::string& layout, const GURL& url) { |
+ std::map<GURL, const VirtualKeyboard*>::const_iterator iter = |
+ url_to_keyboard_.find(url); |
+ if (iter == url_to_keyboard_.end()) { |
+ VLOG(1) << "Can't set user preference: unknown URL"; |
+ return false; |
} |
- for (iter = system_keyboards_.begin(); |
- iter != system_keyboards_.end(); ++iter) { |
- if ((*iter)->supported_layouts().count(layout) > 0) { |
- return *iter; |
- } |
+ |
+ const VirtualKeyboard* keyboard = iter->second; |
+ if (!keyboard->IsLayoutSupported(layout)) { |
+ VLOG(1) << "Requested layout is not supported by requested URL"; |
+ return false; |
} |
- return NULL; |
+ |
+ RemoveUserPreference(layout); |
+ user_preference_.insert(std::make_pair(layout, keyboard)); |
+ return true; |
+} |
+ |
+void VirtualKeyboardSelector::RemoveUserPreference(const std::string& layout) { |
+ user_preference_.erase(layout); |
+} |
+ |
+const VirtualKeyboard* |
+VirtualKeyboardSelector::SelectVirtualKeyboardWithoutPreferences( |
+ const std::string& layout) { |
+ const VirtualKeyboard* keyboard = |
+ SelectVirtualKeyboardInternal(keyboards_, layout); |
+ if (!keyboard) |
+ keyboard = SelectVirtualKeyboardInternal(system_keyboards_, layout); |
+ return keyboard; |
} |
} // namespace input_method |