Index: chrome/browser/chromeos/events/event_rewriter_delegate_impl.cc |
diff --git a/chrome/browser/chromeos/events/event_rewriter_delegate_impl.cc b/chrome/browser/chromeos/events/event_rewriter_delegate_impl.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0e56613a315e4717b7132bc98d61148936c3afae |
--- /dev/null |
+++ b/chrome/browser/chromeos/events/event_rewriter_delegate_impl.cc |
@@ -0,0 +1,101 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/chromeos/events/event_rewriter_delegate_impl.h" |
+ |
+#include "chrome/browser/chromeos/login/ui/login_display_host.h" |
+#include "chrome/browser/extensions/extension_commands_global_registry.h" |
+#include "chrome/browser/profiles/profile_manager.h" |
+#include "chrome/browser/ui/webui/chromeos/login/oobe_ui.h" |
+#include "chrome/browser/ui/webui/chromeos/login/signin_screen_handler.h" |
+#include "chrome/common/pref_names.h" |
+#include "components/prefs/pref_service.h" |
+#include "components/user_manager/user_manager.h" |
+ |
+namespace chromeos { |
+ |
+EventRewriterDelegateImpl::EventRewriterDelegateImpl() |
+ : pref_service_for_testing_(nullptr) {} |
+ |
+EventRewriterDelegateImpl::~EventRewriterDelegateImpl() {} |
+ |
+bool EventRewriterDelegateImpl::RewriteModifierKeys() { |
+ // Do nothing if we have just logged in as guest but have not restarted chrome |
+ // process yet (so we are still on the login screen). In this situations we |
+ // have no user profile so can not do anything useful. |
+ // Note that currently, unlike other accounts, when user logs in as guest, we |
+ // restart chrome process. In future this is to be changed. |
+ // TODO(glotov): remove the following condition when we do not restart chrome |
+ // when user logs in as guest. |
+ // TODO(kpschoedel): check whether this is still necessary. |
+ if (user_manager::UserManager::Get()->IsLoggedInAsGuest() && |
+ LoginDisplayHost::default_host()) |
+ return false; |
+ return true; |
+} |
+ |
+bool EventRewriterDelegateImpl::GetKeyboardRemappedPrefValue( |
+ const std::string& pref_name, |
+ int* value) const { |
+ DCHECK(value); |
+ // If we're at the login screen, try to get the pref from the global prefs |
+ // dictionary. |
+ if (LoginDisplayHost::default_host() && |
+ LoginDisplayHost::default_host() |
+ ->GetOobeUI() |
+ ->signin_screen_handler() |
+ ->GetKeyboardRemappedPrefValue(pref_name, value)) |
+ return true; |
+ const PrefService* pref_service = GetPrefService(); |
+ if (!pref_service) |
+ return false; |
+ const PrefService::Preference* preference = |
+ pref_service->FindPreference(pref_name); |
+ if (!preference) |
+ return false; |
+ |
+ DCHECK_EQ(preference->GetType(), base::Value::Type::INTEGER); |
+ *value = preference->GetValue()->GetInt(); |
+ return true; |
+} |
+ |
+bool EventRewriterDelegateImpl::TopRowKeysAreFunctionKeys() const { |
+ const PrefService* pref_service = GetPrefService(); |
+ if (!pref_service) |
+ return false; |
+ return pref_service->GetBoolean(prefs::kLanguageSendFunctionKeys); |
+} |
+ |
+bool EventRewriterDelegateImpl::IsExtensionCommandRegistered( |
+ ui::KeyboardCode key_code, |
+ int flags) const { |
+ // Some keyboard events for ChromeOS get rewritten, such as: |
+ // Search+Shift+Left gets converted to Shift+Home (BeginDocument). |
+ // This doesn't make sense if the user has assigned that shortcut |
+ // to an extension. Because: |
+ // 1) The extension would, upon seeing a request for Ctrl+Shift+Home have |
+ // to register for Shift+Home, instead. |
+ // 2) The conversion is unnecessary, because Shift+Home (BeginDocument) isn't |
+ // going to be executed. |
+ // Therefore, we skip converting the accelerator if an extension has |
+ // registered for this shortcut. |
+ Profile* profile = ProfileManager::GetActiveUserProfile(); |
+ if (!profile || !extensions::ExtensionCommandsGlobalRegistry::Get(profile)) |
+ return false; |
+ |
+ const int modifiers = flags & (ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN | |
+ ui::EF_ALT_DOWN | ui::EF_COMMAND_DOWN); |
oshima
2017/03/16 18:51:08
nit: define mask as constexpr. you may inline modi
Peng
2017/03/16 20:33:27
Done.
|
+ ui::Accelerator accelerator(key_code, modifiers); |
+ return extensions::ExtensionCommandsGlobalRegistry::Get(profile) |
+ ->IsRegistered(accelerator); |
+} |
+ |
+const PrefService* EventRewriterDelegateImpl::GetPrefService() const { |
+ if (pref_service_for_testing_) |
+ return pref_service_for_testing_; |
+ Profile* profile = ProfileManager::GetActiveUserProfile(); |
+ return profile ? profile->GetPrefs() : nullptr; |
+} |
+ |
+} // namespace chromeos |