Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(241)

Unified Diff: ui/events/ozone/layout/xkb/xkb_keyboard_layout_engine.cc

Issue 817983002: ozone: xkb: Load keymaps on worker thread & cache them (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Figured out Michael's comments Created 5 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/events/ozone/layout/xkb/xkb_keyboard_layout_engine.cc
diff --git a/ui/events/ozone/layout/xkb/xkb_keyboard_layout_engine.cc b/ui/events/ozone/layout/xkb/xkb_keyboard_layout_engine.cc
index a756db9131a69dfdc20747c23bf5f8514021a2f6..6cfcf1e96b75f12b709a7672c19f5a4f5188c157 100644
--- a/ui/events/ozone/layout/xkb/xkb_keyboard_layout_engine.cc
+++ b/ui/events/ozone/layout/xkb/xkb_keyboard_layout_engine.cc
@@ -6,7 +6,13 @@
#include <xkbcommon/xkbcommon-names.h>
+#include "base/bind.h"
+#include "base/location.h"
#include "base/logging.h"
+#include "base/single_thread_task_runner.h"
+#include "base/task_runner.h"
+#include "base/thread_task_runner_handle.h"
+#include "base/threading/worker_pool.h"
#include "ui/events/event_constants.h"
#include "ui/events/keycodes/dom3/dom_code.h"
#include "ui/events/keycodes/dom3/dom_key.h"
@@ -16,6 +22,10 @@
namespace ui {
+typedef base::Callback<void(const std::string&,
+ scoped_ptr<xkb_keymap, XkbKeymapDeleter> keymap)>
+ LoadKeymapCallback;
Shu Chen 2015/01/04 01:43:16 Please move this to the anonymous namespace below.
FengYuan 2015/01/04 02:25:22 Done.
+
namespace {
DomKey CharacterToDomKey(base::char16 character) {
@@ -626,7 +636,7 @@ XkbKeyCodeConverter::~XkbKeyCodeConverter() {
XkbKeyboardLayoutEngine::XkbKeyboardLayoutEngine(
const XkbKeyCodeConverter& converter)
- : key_code_converter_(converter) {
+ : key_code_converter_(converter), weak_ptr_factory_(this) {
// TODO: add XKB_CONTEXT_NO_ENVIRONMENT_NAMES
xkb_context_.reset(xkb_context_new(XKB_CONTEXT_NO_DEFAULT_INCLUDES));
xkb_context_include_path_append(xkb_context_.get(),
@@ -634,6 +644,9 @@ XkbKeyboardLayoutEngine::XkbKeyboardLayoutEngine(
}
XkbKeyboardLayoutEngine::~XkbKeyboardLayoutEngine() {
+ for (const auto& entry : xkb_keymaps_) {
+ xkb_keymap_unref(entry.keymap);
+ }
}
bool XkbKeyboardLayoutEngine::CanSetCurrentLayout() const {
@@ -644,9 +657,44 @@ bool XkbKeyboardLayoutEngine::CanSetCurrentLayout() const {
#endif
}
+void LoadKeymap(const std::string& layout_name,
+ scoped_ptr<xkb_rule_names> names,
+ xkb_context* context,
+ scoped_refptr<base::SingleThreadTaskRunner> reply_runner,
+ const LoadKeymapCallback& reply_callback) {
+ scoped_ptr<xkb_keymap, XkbKeymapDeleter> keymap;
+ keymap.reset(xkb_keymap_new_from_names(context, names.get(),
+ XKB_KEYMAP_COMPILE_NO_FLAGS));
+ reply_runner->PostTask(FROM_HERE, base::Bind(reply_callback, layout_name,
+ base::Passed(&keymap)));
+}
Shu Chen 2015/01/04 01:43:17 Please move this global function to the anonymous
FengYuan 2015/01/04 02:25:22 Done.
+
bool XkbKeyboardLayoutEngine::SetCurrentLayoutByName(
const std::string& layout_name) {
#if defined(OS_CHROMEOS)
+ current_layout_name_ = layout_name;
+ for (const auto& entry : xkb_keymaps_) {
Shu Chen 2015/01/04 01:43:16 Not sure whether the Chromium building systems are
FengYuan 2015/01/04 02:25:22 It is existing in the above codes, so it should be
spang 2015/01/05 18:17:21 Correct. Here's the full reference: https://chrom
+ if (entry.layout_name == layout_name) {
+ SetKeymap(entry.keymap);
+ return true;
+ }
+ }
+ LoadKeymapCallback reply_callback = base::Bind(
+ &XkbKeyboardLayoutEngine::OnKeymapLoaded, weak_ptr_factory_.GetWeakPtr());
+ scoped_ptr<xkb_rule_names> names = GetXkbRuleNames(layout_name);
+ base::WorkerPool::PostTask(
+ FROM_HERE,
+ base::Bind(&LoadKeymap, layout_name, base::Passed(&names),
+ base::Unretained(xkb_context_.get()),
+ base::ThreadTaskRunnerHandle::Get(), reply_callback),
+ true);
+ return true;
+#endif // defined(OS_CHROMEOS)
+ return false;
+}
+
+scoped_ptr<xkb_rule_names> XkbKeyboardLayoutEngine::GetXkbRuleNames(
+ const std::string& layout_name) {
size_t dash_index = layout_name.find('-');
size_t parentheses_index = layout_name.find('(');
std::string layout_id = layout_name;
@@ -662,22 +710,23 @@ bool XkbKeyboardLayoutEngine::SetCurrentLayoutByName(
layout_id = layout_name.substr(0, dash_index);
layout_variant = layout_name.substr(dash_index + 1);
}
- struct xkb_rule_names names = {
- .rules = NULL,
- .model = "pc101",
- .layout = layout_id.c_str(),
- .variant = layout_variant.c_str(),
- .options = ""
- };
- xkb_keymap* keymap = xkb_keymap_new_from_names(xkb_context_.get(),
- &names,
- XKB_KEYMAP_COMPILE_NO_FLAGS);
+ return make_scoped_ptr<xkb_rule_names>(
+ new xkb_rule_names{.rules = NULL,
+ .model = "pc101",
+ .layout = layout_id.c_str(),
+ .variant = layout_variant.c_str(),
+ .options = ""});
+}
+
+void XkbKeyboardLayoutEngine::OnKeymapLoaded(
+ const std::string& layout_name,
+ scoped_ptr<xkb_keymap, XkbKeymapDeleter> keymap) {
if (keymap) {
Shu Chen 2015/01/04 01:43:17 keymap.get()
FengYuan 2015/01/04 02:25:22 not necessary, see same usage in XkbLookup
- SetKeymap(keymap);
- return true;
+ XkbKeymapEntry entry = {layout_name, keymap.get()};
+ xkb_keymaps_.push_back(entry);
Shu Chen 2015/01/04 01:43:16 There is risk of inserting duplicated entries here
FengYuan 2015/01/04 02:25:22 yep, duplicate entries doesn't do harm to the logi
Shu Chen 2015/01/04 02:30:59 Are you going to create further CLs to solve the d
FengYuan 2015/01/04 02:58:42 Yep, I will solve the duplicate loading in IMF sid
+ if (layout_name == current_layout_name_)
+ SetKeymap(keymap.get());
}
Shu Chen 2015/01/04 01:43:17 Suggest to do an error log here. else { LOG(ERR
FengYuan 2015/01/04 02:25:22 Done.
-#endif // defined(OS_CHROMEOS)
- return false;
}
bool XkbKeyboardLayoutEngine::UsesISOLevel5Shift() const {

Powered by Google App Engine
This is Rietveld 408576698