2015-09-18 03:06:38 +00:00
|
|
|
// Copyright (c) 2015 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "atom/common/native_mate_converters/blink_converter.h"
|
|
|
|
|
2015-09-18 03:10:32 +00:00
|
|
|
#include <string>
|
2015-09-18 05:33:06 +00:00
|
|
|
#include <vector>
|
2015-09-18 03:10:32 +00:00
|
|
|
|
2015-09-18 05:33:06 +00:00
|
|
|
#include "atom/common/keyboad_util.h"
|
2015-09-18 03:06:38 +00:00
|
|
|
#include "base/strings/string_util.h"
|
2015-09-28 00:41:06 +00:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2015-09-18 05:33:06 +00:00
|
|
|
#include "content/public/browser/native_web_keyboard_event.h"
|
2015-09-18 03:06:38 +00:00
|
|
|
#include "native_mate/dictionary.h"
|
|
|
|
#include "third_party/WebKit/public/web/WebDeviceEmulationParams.h"
|
2015-09-18 05:33:06 +00:00
|
|
|
#include "third_party/WebKit/public/web/WebInputEvent.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
int VectorToBitArray(const std::vector<T>& vec) {
|
|
|
|
int bits = 0;
|
|
|
|
for (const T& item : vec)
|
|
|
|
bits |= item;
|
|
|
|
return bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
2015-09-18 03:06:38 +00:00
|
|
|
|
|
|
|
namespace mate {
|
|
|
|
|
2015-09-18 05:33:06 +00:00
|
|
|
template<>
|
2015-09-28 00:41:06 +00:00
|
|
|
struct Converter<base::char16> {
|
2015-09-18 05:33:06 +00:00
|
|
|
static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val,
|
2015-09-28 00:41:06 +00:00
|
|
|
base::char16* out) {
|
|
|
|
base::string16 code = base::UTF8ToUTF16(V8ToString(val));
|
2015-09-18 05:33:06 +00:00
|
|
|
if (code.length() != 1)
|
|
|
|
return false;
|
|
|
|
*out = code[0];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct Converter<blink::WebInputEvent::Type> {
|
|
|
|
static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val,
|
|
|
|
blink::WebInputEvent::Type* out) {
|
2015-12-07 11:56:23 +00:00
|
|
|
std::string type = base::ToLowerASCII(V8ToString(val));
|
2015-09-18 05:33:06 +00:00
|
|
|
if (type == "mousedown")
|
|
|
|
*out = blink::WebInputEvent::MouseDown;
|
|
|
|
else if (type == "mouseup")
|
|
|
|
*out = blink::WebInputEvent::MouseUp;
|
|
|
|
else if (type == "mousemove")
|
|
|
|
*out = blink::WebInputEvent::MouseMove;
|
|
|
|
else if (type == "mouseenter")
|
|
|
|
*out = blink::WebInputEvent::MouseEnter;
|
|
|
|
else if (type == "mouseleave")
|
|
|
|
*out = blink::WebInputEvent::MouseLeave;
|
|
|
|
else if (type == "contextmenu")
|
|
|
|
*out = blink::WebInputEvent::ContextMenu;
|
|
|
|
else if (type == "mousewheel")
|
|
|
|
*out = blink::WebInputEvent::MouseWheel;
|
|
|
|
else if (type == "keydown")
|
2015-10-05 17:11:20 +00:00
|
|
|
*out = blink::WebInputEvent::RawKeyDown;
|
2015-09-18 05:33:06 +00:00
|
|
|
else if (type == "keyup")
|
|
|
|
*out = blink::WebInputEvent::KeyUp;
|
|
|
|
else if (type == "char")
|
|
|
|
*out = blink::WebInputEvent::Char;
|
|
|
|
else if (type == "touchstart")
|
|
|
|
*out = blink::WebInputEvent::TouchStart;
|
|
|
|
else if (type == "touchmove")
|
|
|
|
*out = blink::WebInputEvent::TouchMove;
|
|
|
|
else if (type == "touchend")
|
|
|
|
*out = blink::WebInputEvent::TouchEnd;
|
|
|
|
else if (type == "touchcancel")
|
|
|
|
*out = blink::WebInputEvent::TouchCancel;
|
2015-09-18 10:21:51 +00:00
|
|
|
return true;
|
2015-09-18 05:33:06 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-09-25 11:01:52 +00:00
|
|
|
template<>
|
|
|
|
struct Converter<blink::WebMouseEvent::Button> {
|
|
|
|
static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val,
|
|
|
|
blink::WebMouseEvent::Button* out) {
|
2015-12-07 11:56:23 +00:00
|
|
|
std::string button = base::ToLowerASCII(V8ToString(val));
|
2015-09-25 11:01:52 +00:00
|
|
|
if (button == "left")
|
|
|
|
*out = blink::WebMouseEvent::Button::ButtonLeft;
|
|
|
|
else if (button == "middle")
|
|
|
|
*out = blink::WebMouseEvent::Button::ButtonMiddle;
|
|
|
|
else if (button == "right")
|
|
|
|
*out = blink::WebMouseEvent::Button::ButtonRight;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-09-18 05:33:06 +00:00
|
|
|
template<>
|
|
|
|
struct Converter<blink::WebInputEvent::Modifiers> {
|
|
|
|
static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val,
|
|
|
|
blink::WebInputEvent::Modifiers* out) {
|
2015-12-07 11:56:23 +00:00
|
|
|
std::string modifier = base::ToLowerASCII(V8ToString(val));
|
2015-09-18 05:33:06 +00:00
|
|
|
if (modifier == "shift")
|
|
|
|
*out = blink::WebInputEvent::ShiftKey;
|
|
|
|
else if (modifier == "control" || modifier == "ctrl")
|
|
|
|
*out = blink::WebInputEvent::ControlKey;
|
|
|
|
else if (modifier == "alt")
|
|
|
|
*out = blink::WebInputEvent::AltKey;
|
|
|
|
else if (modifier == "meta" || modifier == "command" || modifier == "cmd")
|
|
|
|
*out = blink::WebInputEvent::MetaKey;
|
|
|
|
else if (modifier == "iskeypad")
|
|
|
|
*out = blink::WebInputEvent::IsKeyPad;
|
|
|
|
else if (modifier == "isautorepeat")
|
|
|
|
*out = blink::WebInputEvent::IsAutoRepeat;
|
|
|
|
else if (modifier == "leftbuttondown")
|
|
|
|
*out = blink::WebInputEvent::LeftButtonDown;
|
|
|
|
else if (modifier == "middlebuttondown")
|
|
|
|
*out = blink::WebInputEvent::MiddleButtonDown;
|
|
|
|
else if (modifier == "rightbuttondown")
|
|
|
|
*out = blink::WebInputEvent::RightButtonDown;
|
|
|
|
else if (modifier == "capslock")
|
|
|
|
*out = blink::WebInputEvent::CapsLockOn;
|
|
|
|
else if (modifier == "numlock")
|
|
|
|
*out = blink::WebInputEvent::NumLockOn;
|
|
|
|
else if (modifier == "left")
|
|
|
|
*out = blink::WebInputEvent::IsLeft;
|
|
|
|
else if (modifier == "right")
|
|
|
|
*out = blink::WebInputEvent::IsRight;
|
2015-09-18 10:21:51 +00:00
|
|
|
return true;
|
2015-09-18 05:33:06 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-09-18 06:09:31 +00:00
|
|
|
int GetWebInputEventType(v8::Isolate* isolate, v8::Local<v8::Value> val) {
|
2015-09-18 10:21:51 +00:00
|
|
|
blink::WebInputEvent::Type type = blink::WebInputEvent::Undefined;
|
2015-09-18 06:09:31 +00:00
|
|
|
mate::Dictionary dict;
|
|
|
|
ConvertFromV8(isolate, val, &dict) && dict.Get("type", &type);
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2015-09-18 05:33:06 +00:00
|
|
|
bool Converter<blink::WebInputEvent>::FromV8(
|
|
|
|
v8::Isolate* isolate, v8::Local<v8::Value> val,
|
|
|
|
blink::WebInputEvent* out) {
|
|
|
|
mate::Dictionary dict;
|
|
|
|
if (!ConvertFromV8(isolate, val, &dict))
|
|
|
|
return false;
|
|
|
|
if (!dict.Get("type", &out->type))
|
|
|
|
return false;
|
|
|
|
std::vector<blink::WebInputEvent::Modifiers> modifiers;
|
|
|
|
if (dict.Get("modifiers", &modifiers))
|
|
|
|
out->modifiers = VectorToBitArray(modifiers);
|
|
|
|
out->timeStampSeconds = base::Time::Now().ToDoubleT();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Converter<blink::WebKeyboardEvent>::FromV8(
|
|
|
|
v8::Isolate* isolate, v8::Local<v8::Value> val,
|
|
|
|
blink::WebKeyboardEvent* out) {
|
|
|
|
mate::Dictionary dict;
|
|
|
|
if (!ConvertFromV8(isolate, val, &dict))
|
|
|
|
return false;
|
|
|
|
if (!ConvertFromV8(isolate, val, static_cast<blink::WebInputEvent*>(out)))
|
|
|
|
return false;
|
2015-09-28 00:41:06 +00:00
|
|
|
base::char16 code;
|
2015-11-16 03:15:13 +00:00
|
|
|
std::string identifier;
|
2015-09-18 05:33:06 +00:00
|
|
|
bool shifted = false;
|
2015-11-16 03:15:13 +00:00
|
|
|
|
|
|
|
if (dict.Get("keyCode", &code))
|
|
|
|
out->windowsKeyCode = atom::KeyboardCodeFromCharCode(code, &shifted);
|
2015-11-16 13:51:26 +00:00
|
|
|
else if (dict.Get("keyCode", &identifier))
|
2015-11-16 03:15:13 +00:00
|
|
|
out->windowsKeyCode = atom::KeyboardCodeFromKeyIdentifier(
|
2015-12-07 11:56:23 +00:00
|
|
|
base::ToLowerASCII(identifier));
|
2015-11-16 03:15:13 +00:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
2015-09-18 05:33:06 +00:00
|
|
|
if (shifted)
|
|
|
|
out->modifiers |= blink::WebInputEvent::ShiftKey;
|
|
|
|
out->setKeyIdentifierFromWindowsKeyCode();
|
2015-09-28 04:48:33 +00:00
|
|
|
if (out->type == blink::WebInputEvent::Char ||
|
|
|
|
out->type == blink::WebInputEvent::RawKeyDown) {
|
2015-09-28 00:41:06 +00:00
|
|
|
out->text[0] = code;
|
|
|
|
out->unmodifiedText[0] = code;
|
|
|
|
}
|
2015-09-18 10:21:51 +00:00
|
|
|
return true;
|
2015-09-18 05:33:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Converter<content::NativeWebKeyboardEvent>::FromV8(
|
|
|
|
v8::Isolate* isolate, v8::Local<v8::Value> val,
|
|
|
|
content::NativeWebKeyboardEvent* out) {
|
|
|
|
mate::Dictionary dict;
|
|
|
|
if (!ConvertFromV8(isolate, val, &dict))
|
|
|
|
return false;
|
|
|
|
if (!ConvertFromV8(isolate, val, static_cast<blink::WebKeyboardEvent*>(out)))
|
|
|
|
return false;
|
|
|
|
dict.Get("skipInBrowser", &out->skip_in_browser);
|
2015-09-18 10:21:51 +00:00
|
|
|
return true;
|
2015-09-18 05:33:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Converter<blink::WebMouseEvent>::FromV8(
|
|
|
|
v8::Isolate* isolate, v8::Local<v8::Value> val, blink::WebMouseEvent* out) {
|
|
|
|
mate::Dictionary dict;
|
|
|
|
if (!ConvertFromV8(isolate, val, &dict))
|
|
|
|
return false;
|
|
|
|
if (!ConvertFromV8(isolate, val, static_cast<blink::WebInputEvent*>(out)))
|
|
|
|
return false;
|
|
|
|
if (!dict.Get("x", &out->x) || !dict.Get("y", &out->y))
|
|
|
|
return false;
|
2015-09-25 11:01:52 +00:00
|
|
|
dict.Get("button", &out->button);
|
2015-09-18 05:33:06 +00:00
|
|
|
dict.Get("globalX", &out->globalX);
|
|
|
|
dict.Get("globalY", &out->globalY);
|
|
|
|
dict.Get("movementX", &out->movementX);
|
|
|
|
dict.Get("movementY", &out->movementY);
|
|
|
|
dict.Get("clickCount", &out->clickCount);
|
2015-09-18 10:21:51 +00:00
|
|
|
return true;
|
2015-09-18 05:33:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Converter<blink::WebMouseWheelEvent>::FromV8(
|
|
|
|
v8::Isolate* isolate, v8::Local<v8::Value> val,
|
|
|
|
blink::WebMouseWheelEvent* out) {
|
|
|
|
mate::Dictionary dict;
|
|
|
|
if (!ConvertFromV8(isolate, val, &dict))
|
|
|
|
return false;
|
|
|
|
if (!ConvertFromV8(isolate, val, static_cast<blink::WebMouseEvent*>(out)))
|
|
|
|
return false;
|
|
|
|
dict.Get("deltaX", &out->deltaX);
|
|
|
|
dict.Get("deltaY", &out->deltaY);
|
|
|
|
dict.Get("wheelTicksX", &out->wheelTicksX);
|
|
|
|
dict.Get("wheelTicksY", &out->wheelTicksY);
|
|
|
|
dict.Get("accelerationRatioX", &out->accelerationRatioX);
|
|
|
|
dict.Get("accelerationRatioY", &out->accelerationRatioY);
|
|
|
|
dict.Get("hasPreciseScrollingDeltas", &out->hasPreciseScrollingDeltas);
|
|
|
|
dict.Get("canScroll", &out->canScroll);
|
2015-09-18 10:21:51 +00:00
|
|
|
return true;
|
2015-09-18 05:33:06 +00:00
|
|
|
}
|
|
|
|
|
2015-09-18 03:06:38 +00:00
|
|
|
bool Converter<blink::WebFloatPoint>::FromV8(
|
|
|
|
v8::Isolate* isolate, v8::Local<v8::Value> val, blink::WebFloatPoint* out) {
|
|
|
|
mate::Dictionary dict;
|
|
|
|
if (!ConvertFromV8(isolate, val, &dict))
|
|
|
|
return false;
|
|
|
|
return dict.Get("x", &out->x) && dict.Get("y", &out->y);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Converter<blink::WebPoint>::FromV8(
|
|
|
|
v8::Isolate* isolate, v8::Local<v8::Value> val, blink::WebPoint* out) {
|
|
|
|
mate::Dictionary dict;
|
|
|
|
if (!ConvertFromV8(isolate, val, &dict))
|
|
|
|
return false;
|
|
|
|
return dict.Get("x", &out->x) && dict.Get("y", &out->y);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Converter<blink::WebSize>::FromV8(
|
|
|
|
v8::Isolate* isolate, v8::Local<v8::Value> val, blink::WebSize* out) {
|
|
|
|
mate::Dictionary dict;
|
|
|
|
if (!ConvertFromV8(isolate, val, &dict))
|
|
|
|
return false;
|
|
|
|
return dict.Get("width", &out->width) && dict.Get("height", &out->height);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Converter<blink::WebDeviceEmulationParams>::FromV8(
|
|
|
|
v8::Isolate* isolate, v8::Local<v8::Value> val,
|
|
|
|
blink::WebDeviceEmulationParams* out) {
|
|
|
|
mate::Dictionary dict;
|
|
|
|
if (!ConvertFromV8(isolate, val, &dict))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
std::string screen_position;
|
|
|
|
if (dict.Get("screenPosition", &screen_position)) {
|
2015-12-07 11:56:23 +00:00
|
|
|
screen_position = base::ToLowerASCII(screen_position);
|
2015-09-18 03:06:38 +00:00
|
|
|
if (screen_position == "mobile")
|
|
|
|
out->screenPosition = blink::WebDeviceEmulationParams::Mobile;
|
|
|
|
else if (screen_position == "desktop")
|
|
|
|
out->screenPosition = blink::WebDeviceEmulationParams::Desktop;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
dict.Get("screenSize", &out->screenSize);
|
|
|
|
dict.Get("viewPosition", &out->viewPosition);
|
|
|
|
dict.Get("deviceScaleFactor", &out->deviceScaleFactor);
|
|
|
|
dict.Get("viewSize", &out->viewSize);
|
|
|
|
dict.Get("fitToView", &out->fitToView);
|
|
|
|
dict.Get("offset", &out->offset);
|
|
|
|
dict.Get("scale", &out->scale);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-17 17:27:56 +00:00
|
|
|
bool Converter<blink::WebFindOptions>::FromV8(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> val,
|
|
|
|
blink::WebFindOptions* out) {
|
|
|
|
mate::Dictionary dict;
|
|
|
|
if (!ConvertFromV8(isolate, val, &dict))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
dict.Get("forward", &out->forward);
|
|
|
|
dict.Get("matchCase", &out->matchCase);
|
|
|
|
dict.Get("findNext", &out->findNext);
|
|
|
|
dict.Get("wordStart", &out->wordStart);
|
|
|
|
dict.Get("medialCapitalAsWordStart", &out->medialCapitalAsWordStart);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-09-18 03:06:38 +00:00
|
|
|
} // namespace mate
|