Bug 1119609 part.3 Implement converting methods from key/code value to key/code name index r=smaug

This commit is contained in:
Masayuki Nakano
2015-02-19 15:50:19 +09:00
parent f478c85fd8
commit f1536d537d
11 changed files with 131 additions and 74 deletions

View File

@@ -267,10 +267,16 @@ KeyboardEvent::InitWithKeyboardEventInit(EventTarget* aOwner,
internalEvent->location = aParam.mLocation;
internalEvent->mIsRepeat = aParam.mRepeat;
internalEvent->mIsComposing = aParam.mIsComposing;
internalEvent->mKeyNameIndex = KEY_NAME_INDEX_USE_STRING;
internalEvent->mCodeNameIndex = CODE_NAME_INDEX_USE_STRING;
internalEvent->mKeyValue = aParam.mKey;
internalEvent->mCodeValue = aParam.mCode;
internalEvent->mKeyNameIndex =
WidgetKeyboardEvent::GetKeyNameIndex(aParam.mKey);
if (internalEvent->mKeyNameIndex == KEY_NAME_INDEX_USE_STRING) {
internalEvent->mKeyValue = aParam.mKey;
}
internalEvent->mCodeNameIndex =
WidgetKeyboardEvent::GetCodeNameIndex(aParam.mCode);
if (internalEvent->mCodeNameIndex == CODE_NAME_INDEX_USE_STRING) {
internalEvent->mCodeValue = aParam.mCode;
}
}
NS_IMETHODIMP

View File

@@ -16,7 +16,14 @@
namespace mozilla {
namespace widget {
//static
// static
void
WidgetUtils::Shutdown()
{
WidgetKeyboardEvent::Shutdown();
}
// static
already_AddRefed<nsIWidget>
WidgetUtils::DOMWindowToWidget(nsIDOMWindow *aDOMWindow)
{

View File

@@ -21,6 +21,9 @@
#include "nsTArray.h"
#include "WritingModes.h"
class nsStringHashKey;
template<class, class> class nsDataHashtable;
/******************************************************************************
* virtual keycode values
******************************************************************************/
@@ -185,6 +188,8 @@ public:
GetDOMCodeName(mCodeNameIndex, aCodeName);
}
static void Shutdown();
static uint32_t ComputeLocationFromCodeValue(CodeNameIndex aCodeNameIndex);
static void GetDOMKeyName(KeyNameIndex aKeyNameIndex,
@@ -192,6 +197,9 @@ public:
static void GetDOMCodeName(CodeNameIndex aCodeNameIndex,
nsAString& aCodeName);
static KeyNameIndex GetKeyNameIndex(const nsAString& aKeyValue);
static CodeNameIndex GetCodeNameIndex(const nsAString& aCodeValue);
static const char* GetCommandStr(Command aCommand);
void AssignKeyEventData(const WidgetKeyboardEvent& aEvent, bool aCopyTargets)
@@ -214,6 +222,16 @@ public:
mNativeKeyEvent = nullptr;
mUniqueId = aEvent.mUniqueId;
}
private:
static const char16_t* kKeyNames[];
static const char16_t* kCodeNames[];
typedef nsDataHashtable<nsStringHashKey,
KeyNameIndex> KeyNameIndexHashtable;
typedef nsDataHashtable<nsStringHashKey,
CodeNameIndex> CodeNameIndexHashtable;
static KeyNameIndexHashtable* sKeyNameIndexHashtable;
static CodeNameIndexHashtable* sCodeNameIndexHashtable;
};

View File

@@ -289,6 +289,24 @@ WidgetInputEvent::AccelModifier()
* mozilla::WidgetKeyboardEvent (TextEvents.h)
******************************************************************************/
#define NS_DEFINE_KEYNAME(aCPPName, aDOMKeyName) MOZ_UTF16(aDOMKeyName),
const char16_t* WidgetKeyboardEvent::kKeyNames[] = {
#include "mozilla/KeyNameList.h"
};
#undef NS_DEFINE_KEYNAME
#define NS_DEFINE_PHYSICAL_KEY_CODE_NAME(aCPPName, aDOMCodeName) \
MOZ_UTF16(aDOMCodeName),
const char16_t* WidgetKeyboardEvent::kCodeNames[] = {
#include "mozilla/PhysicalKeyCodeNameList.h"
};
#undef NS_DEFINE_PHYSICAL_KEY_CODE_NAME
WidgetKeyboardEvent::KeyNameIndexHashtable*
WidgetKeyboardEvent::sKeyNameIndexHashtable = nullptr;
WidgetKeyboardEvent::CodeNameIndexHashtable*
WidgetKeyboardEvent::sCodeNameIndexHashtable = nullptr;
bool
WidgetKeyboardEvent::ShouldCauseKeypressEvents() const
{
@@ -315,73 +333,31 @@ WidgetKeyboardEvent::ShouldCauseKeypressEvents() const
}
}
/*static*/ void
/* static */ void
WidgetKeyboardEvent::Shutdown()
{
delete sKeyNameIndexHashtable;
sKeyNameIndexHashtable = nullptr;
delete sCodeNameIndexHashtable;
sCodeNameIndexHashtable = nullptr;
}
/* static */ void
WidgetKeyboardEvent::GetDOMKeyName(KeyNameIndex aKeyNameIndex,
nsAString& aKeyName)
{
// The expected way to implement this function would be to use a
// switch statement. By using a table-based implementation, below, we
// ensure that this function executes in constant time in cases where
// compilers wouldn't be able to convert the switch statement to a
// jump table. This table-based implementation also minimizes the
// space required by the code and data.
#define KEY_STR_NUM_INTERNAL(line) key##line
#define KEY_STR_NUM(line) KEY_STR_NUM_INTERNAL(line)
// Catch non-ASCII DOM key names in our key name list.
#define NS_DEFINE_KEYNAME(aCPPName, aDOMKeyName) \
static_assert(sizeof(aDOMKeyName) == MOZ_ARRAY_LENGTH(aDOMKeyName), \
"Invalid DOM key name");
#include "mozilla/KeyNameList.h"
#undef NS_DEFINE_KEYNAME
struct KeyNameTable
{
#define NS_DEFINE_KEYNAME(aCPPName, aDOMKeyName) \
char16_t KEY_STR_NUM(__LINE__)[sizeof(aDOMKeyName)];
#include "mozilla/KeyNameList.h"
#undef NS_DEFINE_KEYNAME
};
static const KeyNameTable kKeyNameTable = {
#define NS_DEFINE_KEYNAME(aCPPName, aDOMKeyName) MOZ_UTF16(aDOMKeyName),
#include "mozilla/KeyNameList.h"
#undef NS_DEFINE_KEYNAME
};
static const uint16_t kKeyNameOffsets[] = {
#define NS_DEFINE_KEYNAME(aCPPName, aDOMKeyName) \
offsetof(struct KeyNameTable, KEY_STR_NUM(__LINE__)) / sizeof(char16_t),
#include "mozilla/KeyNameList.h"
#undef NS_DEFINE_KEYNAME
// Include this entry so we can compute lengths easily.
sizeof(kKeyNameTable)
};
// Use the sizeof trick rather than MOZ_ARRAY_LENGTH to avoid problems
// with constexpr functions called inside static_assert with some
// compilers.
static_assert(KEY_NAME_INDEX_USE_STRING ==
(sizeof(kKeyNameOffsets)/sizeof(kKeyNameOffsets[0])) - 1,
"Invalid enumeration values!");
if (aKeyNameIndex >= KEY_NAME_INDEX_USE_STRING) {
aKeyName.Truncate();
return;
}
uint16_t offset = kKeyNameOffsets[aKeyNameIndex];
uint16_t nextOffset = kKeyNameOffsets[aKeyNameIndex + 1];
const char16_t* table = reinterpret_cast<const char16_t*>(&kKeyNameTable);
// Subtract off 1 for the null terminator.
aKeyName.Assign(table + offset, nextOffset - offset - 1);
#undef KEY_STR_NUM
#undef KEY_STR_NUM_INTERNAL
MOZ_RELEASE_ASSERT(static_cast<size_t>(aKeyNameIndex) <
ArrayLength(kKeyNames),
"Illegal key enumeration value");
aKeyName = kKeyNames[aKeyNameIndex];
}
/*static*/ void
/* static */ void
WidgetKeyboardEvent::GetDOMCodeName(CodeNameIndex aCodeNameIndex,
nsAString& aCodeName)
{
@@ -390,20 +366,44 @@ WidgetKeyboardEvent::GetDOMCodeName(CodeNameIndex aCodeNameIndex,
return;
}
#define NS_DEFINE_PHYSICAL_KEY_CODE_NAME(aCPPName, aDOMCodeName) \
MOZ_UTF16(aDOMCodeName),
static const char16_t* kCodeNames[] = {
#include "mozilla/PhysicalKeyCodeNameList.h"
MOZ_UTF16("")
};
#undef NS_DEFINE_PHYSICAL_KEY_CODE_NAME
MOZ_RELEASE_ASSERT(static_cast<size_t>(aCodeNameIndex) <
ArrayLength(kCodeNames),
"Illegal physical code enumeration value");
aCodeName = kCodeNames[aCodeNameIndex];
}
/* static */ KeyNameIndex
WidgetKeyboardEvent::GetKeyNameIndex(const nsAString& aKeyValue)
{
if (!sKeyNameIndexHashtable) {
sKeyNameIndexHashtable =
new KeyNameIndexHashtable(ArrayLength(kKeyNames));
for (size_t i = 0; i < ArrayLength(kKeyNames); i++) {
sKeyNameIndexHashtable->Put(nsDependentString(kKeyNames[i]),
static_cast<KeyNameIndex>(i));
}
}
KeyNameIndex result = KEY_NAME_INDEX_USE_STRING;
sKeyNameIndexHashtable->Get(aKeyValue, &result);
return result;
}
/* static */ CodeNameIndex
WidgetKeyboardEvent::GetCodeNameIndex(const nsAString& aCodeValue)
{
if (!sCodeNameIndexHashtable) {
sCodeNameIndexHashtable =
new CodeNameIndexHashtable(ArrayLength(kCodeNames));
for (size_t i = 0; i < ArrayLength(kCodeNames); i++) {
sCodeNameIndexHashtable->Put(nsDependentString(kCodeNames[i]),
static_cast<CodeNameIndex>(i));
}
}
CodeNameIndex result = CODE_NAME_INDEX_USE_STRING;
sCodeNameIndexHashtable->Get(aCodeValue, &result);
return result;
}
/* static */ const char*
WidgetKeyboardEvent::GetCommandStr(Command aCommand)
{

View File

@@ -43,6 +43,12 @@ namespace widget {
class WidgetUtils
{
public:
/**
* Shutdown() is called when "xpcom-will-shutdown" is notified. This is
* useful when you need to observe the notification in XP level code under
* widget.
*/
static void Shutdown();
/**
* Starting at the docshell item for the passed in DOM window this looks up

View File

@@ -4,6 +4,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/ModuleUtils.h"
#include "mozilla/WidgetUtils.h"
#include "nsCOMPtr.h"
#include "nsWidgetsCID.h"
@@ -109,6 +110,9 @@ static const mozilla::Module::ContractIDEntry kWidgetContracts[] = {
static void
nsWidgetAndroidModuleDtor()
{
// Shutdown all XP level widget classes.
mozilla::widget::WidgetUtils::Shutdown();
nsLookAndFeel::Shutdown();
nsAppShellShutdown();
}

View File

@@ -7,6 +7,7 @@
#include "nsISupports.h"
#include "nsIComponentManager.h"
#include "mozilla/ModuleUtils.h"
#include "mozilla/WidgetUtils.h"
#include "nsWidgetsCID.h"
@@ -38,8 +39,6 @@
#include "nsPrintSession.h"
#include "nsToolkitCompsCID.h"
#include "mozilla/Module.h"
using namespace mozilla;
using namespace mozilla::widget;
@@ -195,6 +194,9 @@ static const mozilla::Module::ContractIDEntry kWidgetContracts[] = {
static void
nsWidgetCocoaModuleDtor()
{
// Shutdown all XP level widget classes.
WidgetUtils::Shutdown();
NativeKeyBindings::Shutdown();
nsLookAndFeel::Shutdown();
nsToolkit::Shutdown();

View File

@@ -17,6 +17,7 @@
#include "base/basictypes.h"
#include "mozilla/ModuleUtils.h"
#include "mozilla/WidgetUtils.h"
#include "nsCOMPtr.h"
#include "nsWidgetsCID.h"
@@ -107,6 +108,9 @@ static const mozilla::Module::ContractIDEntry kWidgetContracts[] = {
static void
nsWidgetGonkModuleDtor()
{
// Shutdown all XP level widget classes.
WidgetUtils::Shutdown();
nsLookAndFeel::Shutdown();
nsAppShellShutdown();
}

View File

@@ -6,6 +6,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/ModuleUtils.h"
#include "mozilla/WidgetUtils.h"
#include "NativeKeyBindings.h"
#include "nsWidgetsCID.h"
#include "nsAppShell.h"
@@ -268,6 +269,9 @@ static const mozilla::Module::ContractIDEntry kWidgetContracts[] = {
static void
nsWidgetGtk2ModuleDtor()
{
// Shutdown all XP level widget classes.
WidgetUtils::Shutdown();
NativeKeyBindings::Shutdown();
nsLookAndFeel::Shutdown();
nsFilePicker::Shutdown();

View File

@@ -17,6 +17,7 @@
#include "base/basictypes.h"
#include "mozilla/ModuleUtils.h"
#include "mozilla/WidgetUtils.h"
#include "nsCOMPtr.h"
#include "nsWidgetsCID.h"
@@ -147,6 +148,9 @@ static const mozilla::Module::ContractIDEntry kWidgetContracts[] = {
static void
nsWidgetQtModuleDtor()
{
// Shutdown all XP level widget classes.
WidgetUtils::Shutdown();
nsLookAndFeel::Shutdown();
nsAppShellShutdown();
}

View File

@@ -11,6 +11,7 @@
#include "nsAppShell.h"
#include "nsAppShellSingleton.h"
#include "mozilla/ModuleUtils.h"
#include "mozilla/WidgetUtils.h"
#include "nsIServiceManager.h"
#include "nsIdleServiceWin.h"
#include "nsLookAndFeel.h"
@@ -56,8 +57,6 @@
#include "nsPrintSession.h"
#endif
#include "mozilla/Module.h"
using namespace mozilla;
using namespace mozilla::widget;
@@ -289,6 +288,9 @@ static const mozilla::Module::ContractIDEntry kWidgetContracts[] = {
static void
nsWidgetWindowsModuleDtor()
{
// Shutdown all XP level widget classes.
WidgetUtils::Shutdown();
KeyboardLayout::Shutdown();
MouseScrollHandler::Shutdown();
nsLookAndFeel::Shutdown();