Re-land: C++ readability review
[chromium-blink-merge.git] / remoting / tools / javascript_key_tester / pnacl / remoting_key_tester.cc
blob408725c5e4a77b7ae7f401e79d80114736b900ef
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <sstream>
7 #include "ppapi/cpp/input_event.h"
8 #include "ppapi/cpp/instance.h"
9 #include "ppapi/cpp/module.h"
10 #include "ppapi/cpp/var.h"
11 #include "ppapi/cpp/var_dictionary.h"
13 namespace remoting {
15 class KeyTesterInstance : public pp::Instance {
16 public:
17 explicit KeyTesterInstance(PP_Instance instance) : pp::Instance(instance) {
18 RequestFilteringInputEvents(PP_INPUTEVENT_CLASS_KEYBOARD);
21 virtual ~KeyTesterInstance() {}
23 virtual bool HandleInputEvent(const pp::InputEvent& event) {
24 switch (event.GetType()) {
25 case PP_INPUTEVENT_TYPE_KEYDOWN:
26 case PP_INPUTEVENT_TYPE_KEYUP:
27 case PP_INPUTEVENT_TYPE_CHAR: {
28 HandleKeyboardEvent(pp::KeyboardInputEvent(event));
29 break;
31 default:
32 break;
34 return true;
37 private:
38 void HandleKeyboardEvent(const pp::KeyboardInputEvent& event) {
39 pp::VarDictionary out;
40 out.Set("type", EventTypeToString(event.GetType()));
41 out.Set("modifiers", (double)event.GetModifiers());
42 out.Set("keyCode", (double)event.GetKeyCode());
43 out.Set("characterText", event.GetCharacterText());
44 out.Set("code", event.GetCode());
45 PostMessage(out);
48 std::string EventTypeToString(PP_InputEvent_Type t) {
49 switch (t) {
50 case PP_INPUTEVENT_TYPE_UNDEFINED:
51 return "UNDEFINED";
52 case PP_INPUTEVENT_TYPE_MOUSEDOWN:
53 return "MOUSEDOWN";
54 case PP_INPUTEVENT_TYPE_MOUSEUP:
55 return "MOUSEUP";
56 case PP_INPUTEVENT_TYPE_MOUSEMOVE:
57 return "MOUSEMOVE";
58 case PP_INPUTEVENT_TYPE_MOUSEENTER:
59 return "MOUSEENTER";
60 case PP_INPUTEVENT_TYPE_MOUSELEAVE:
61 return "MOUSELEAVE";
62 case PP_INPUTEVENT_TYPE_WHEEL:
63 return "WHEEL";
64 case PP_INPUTEVENT_TYPE_RAWKEYDOWN:
65 return "RAWKEYDOWN";
66 case PP_INPUTEVENT_TYPE_KEYDOWN:
67 return "KEYDOWN";
68 case PP_INPUTEVENT_TYPE_KEYUP:
69 return "KEYUP";
70 case PP_INPUTEVENT_TYPE_CHAR:
71 return "CHAR";
72 case PP_INPUTEVENT_TYPE_CONTEXTMENU:
73 return "CONTEXTMENU";
74 case PP_INPUTEVENT_TYPE_IME_COMPOSITION_START:
75 return "IME_COMPOSITION_START";
76 case PP_INPUTEVENT_TYPE_IME_COMPOSITION_UPDATE:
77 return "IME_COMPOSITION_UPDATE";
78 case PP_INPUTEVENT_TYPE_IME_COMPOSITION_END:
79 return "IME_COMPOSITION_END";
80 case PP_INPUTEVENT_TYPE_IME_TEXT:
81 return "IME_TEXT";
82 case PP_INPUTEVENT_TYPE_TOUCHSTART:
83 return "TOUCHSTART";
84 case PP_INPUTEVENT_TYPE_TOUCHMOVE:
85 return "TOUCHMOVE";
86 case PP_INPUTEVENT_TYPE_TOUCHEND:
87 return "TOUCHEND";
88 case PP_INPUTEVENT_TYPE_TOUCHCANCEL:
89 return "TOUCHCANCEL";
90 default:
91 return "[UNRECOGNIZED]";
96 class KeyTesterModule : public pp::Module {
97 public:
98 KeyTesterModule() : pp::Module() {}
99 virtual ~KeyTesterModule() {}
101 virtual pp::Instance* CreateInstance(PP_Instance instance) {
102 return new KeyTesterInstance(instance);
106 } // namespace remoting
108 namespace pp {
110 Module* CreateModule() {
111 return new remoting::KeyTesterModule();
114 } // namespace pp