OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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. |
| 4 |
| 5 #include "athena/virtual_keyboard/public/virtual_keyboard_bindings.h" |
| 6 |
| 7 #include "base/json/json_string_value_serializer.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/macros.h" |
| 10 #include "base/strings/stringprintf.h" |
| 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "base/values.h" |
| 13 #include "content/public/common/bindings_policy.h" |
| 14 #include "content/public/renderer/render_frame.h" |
| 15 #include "content/public/renderer/render_view.h" |
| 16 #include "content/public/renderer/render_view_observer.h" |
| 17 #include "gin/handle.h" |
| 18 #include "gin/object_template_builder.h" |
| 19 #include "gin/wrappable.h" |
| 20 #include "third_party/WebKit/public/web/WebKit.h" |
| 21 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 22 #include "ui/keyboard/keyboard_constants.h" |
| 23 |
| 24 namespace { |
| 25 |
| 26 struct VKEvent { |
| 27 std::string event_type; |
| 28 int char_value; |
| 29 int key_code; |
| 30 std::string key_name; |
| 31 int modifiers; |
| 32 }; |
| 33 |
| 34 } // namespace |
| 35 |
| 36 namespace gin { |
| 37 |
| 38 template <> |
| 39 struct Converter<VKEvent> { |
| 40 static bool FromV8(v8::Isolate* isolate, |
| 41 v8::Handle<v8::Value> val, |
| 42 VKEvent* event) { |
| 43 if (!val->IsObject()) |
| 44 return false; |
| 45 v8::Handle<v8::Object> dict(v8::Handle<v8::Object>::Cast(val)); |
| 46 |
| 47 if (!Converter<std::string>::FromV8( |
| 48 isolate, |
| 49 dict->Get(v8::String::NewFromUtf8(isolate, "type")), |
| 50 &event->event_type)) |
| 51 return false; |
| 52 if (!Converter<int32_t>::FromV8( |
| 53 isolate, |
| 54 dict->Get(v8::String::NewFromUtf8(isolate, "charValue")), |
| 55 &event->char_value)) |
| 56 return false; |
| 57 if (!Converter<int32_t>::FromV8( |
| 58 isolate, |
| 59 dict->Get(v8::String::NewFromUtf8(isolate, "keyCode")), |
| 60 &event->key_code)) |
| 61 return false; |
| 62 if (!Converter<std::string>::FromV8( |
| 63 isolate, |
| 64 dict->Get(v8::String::NewFromUtf8(isolate, "keyName")), |
| 65 &event->key_name)) |
| 66 return false; |
| 67 if (!Converter<int32_t>::FromV8( |
| 68 isolate, |
| 69 dict->Get(v8::String::NewFromUtf8(isolate, "modifiers")), |
| 70 &event->modifiers)) |
| 71 return false; |
| 72 |
| 73 return true; |
| 74 } |
| 75 }; |
| 76 |
| 77 } // namespace gin |
| 78 |
| 79 namespace athena { |
| 80 |
| 81 namespace { |
| 82 |
| 83 class VKBindings : public gin::Wrappable<VKBindings> { |
| 84 public: |
| 85 static gin::WrapperInfo kWrapperInfo; |
| 86 |
| 87 static void Install(content::RenderView* render_view) { |
| 88 blink::WebFrame* web_frame = |
| 89 render_view->GetMainRenderFrame()->GetWebFrame(); |
| 90 v8::Isolate* isolate = blink::mainThreadIsolate(); |
| 91 v8::HandleScope handle_scope(isolate); |
| 92 v8::Handle<v8::Context> context = web_frame->mainWorldScriptContext(); |
| 93 if (context.IsEmpty()) |
| 94 return; |
| 95 |
| 96 v8::Context::Scope context_scope(context); |
| 97 |
| 98 v8::Handle<v8::Object> global = context->Global(); |
| 99 v8::Handle<v8::Object> chrome = |
| 100 global->Get(gin::StringToV8(isolate, "chrome"))->ToObject(); |
| 101 CHECK(!chrome.IsEmpty()); |
| 102 |
| 103 gin::Handle<VKBindings> controller = |
| 104 gin::CreateHandle(isolate, new VKBindings(render_view)); |
| 105 if (controller.IsEmpty()) |
| 106 return; |
| 107 chrome->Set(gin::StringToSymbol(isolate, "virtualKeyboardPrivate"), |
| 108 controller.ToV8()); |
| 109 } |
| 110 |
| 111 private: |
| 112 explicit VKBindings(content::RenderView* render_view) |
| 113 : render_view_(render_view) {} |
| 114 virtual ~VKBindings() {} |
| 115 |
| 116 // gin::WrappableBase |
| 117 virtual gin::ObjectTemplateBuilder GetObjectTemplateBuilder( |
| 118 v8::Isolate* isolate) OVERRIDE { |
| 119 return gin::Wrappable<VKBindings>::GetObjectTemplateBuilder(isolate) |
| 120 .SetMethod("moveCursor", &VKBindings::NotImplemented) |
| 121 .SetMethod("sendKeyEvent", &VKBindings::SendKeyEvent) |
| 122 .SetMethod("hideKeyboard", &VKBindings::HideKeyboard) |
| 123 .SetMethod("lockKeyboard", &VKBindings::NotImplemented) |
| 124 .SetMethod("keyboardLoaded", &VKBindings::NotImplemented) |
| 125 .SetMethod("getKeyboardConfig", &VKBindings::NotImplemented); |
| 126 } |
| 127 |
| 128 void SendKeyEvent(gin::Arguments* args) { |
| 129 VKEvent event; |
| 130 if (!args->GetNext(&event)) { |
| 131 LOG(ERROR) << "Failed to get the type"; |
| 132 return; |
| 133 } |
| 134 base::ListValue params; |
| 135 params.Set(0, base::Value::CreateStringValue(event.event_type)); |
| 136 params.Set(1, base::Value::CreateIntegerValue(event.char_value)); |
| 137 params.Set(2, base::Value::CreateIntegerValue(event.key_code)); |
| 138 params.Set(3, base::Value::CreateStringValue(event.key_name)); |
| 139 params.Set(4, base::Value::CreateIntegerValue(event.modifiers)); |
| 140 |
| 141 std::string params_json; |
| 142 JSONStringValueSerializer serializer(¶ms_json); |
| 143 if (!serializer.Serialize(params)) |
| 144 return; |
| 145 |
| 146 render_view_->GetMainRenderFrame()->ExecuteJavaScript( |
| 147 base::UTF8ToUTF16("chrome.send('sendKeyEvent', " + params_json + ")")); |
| 148 } |
| 149 |
| 150 void HideKeyboard() { |
| 151 render_view_->GetMainRenderFrame()->ExecuteJavaScript( |
| 152 base::UTF8ToUTF16("chrome.send('hideKeyboard', [])")); |
| 153 } |
| 154 |
| 155 void NotImplemented(gin::Arguments* args) { NOTIMPLEMENTED(); } |
| 156 |
| 157 content::RenderView* render_view_; |
| 158 |
| 159 DISALLOW_COPY_AND_ASSIGN(VKBindings); |
| 160 }; |
| 161 |
| 162 gin::WrapperInfo VKBindings::kWrapperInfo = {gin::kEmbedderNativeGin}; |
| 163 |
| 164 class VirtualKeyboardBindingsImpl : public VirtualKeyboardBindings, |
| 165 public content::RenderViewObserver { |
| 166 public: |
| 167 explicit VirtualKeyboardBindingsImpl(content::RenderView* render_view) |
| 168 : content::RenderViewObserver(render_view) {} |
| 169 |
| 170 virtual ~VirtualKeyboardBindingsImpl() {} |
| 171 |
| 172 private: |
| 173 // content::RenderViewObserver: |
| 174 virtual void Navigate(const GURL& url) OVERRIDE { |
| 175 bool enabled_bindings = render_view()->GetEnabledBindings(); |
| 176 if (!(enabled_bindings & content::BINDINGS_POLICY_WEB_UI)) |
| 177 return; |
| 178 if (url.GetOrigin() == GURL(keyboard::kKeyboardURL)) |
| 179 VKBindings::Install(render_view()); |
| 180 } |
| 181 DISALLOW_COPY_AND_ASSIGN(VirtualKeyboardBindingsImpl); |
| 182 }; |
| 183 |
| 184 } // namespace |
| 185 |
| 186 VirtualKeyboardBindings* VirtualKeyboardBindings::Create( |
| 187 content::RenderView* render_view) { |
| 188 return new VirtualKeyboardBindingsImpl(render_view); |
| 189 } |
| 190 |
| 191 } // namespace athena |
OLD | NEW |