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 "chrome/renderer/extensions/automation_internal_custom_bindings.h"
6
7#include "base/bind.h"
8#include "base/memory/scoped_ptr.h"
9#include "base/values.h"
10#include "chrome/common/extensions/manifest_handlers/automation.h"
11#include "content/public/renderer/v8_value_converter.h"
12#include "extensions/common/extension.h"
13#include "extensions/common/manifest.h"
14#include "extensions/renderer/script_context.h"
15#include "ui/accessibility/ax_enums.h"
16
17namespace {
18
19// Helper to convert an enum to a V8 object.
20template <typename EnumType>
21v8::Local<v8::Object> ToEnumObject(v8::Isolate* isolate,
22                                   EnumType start_after,
23                                   EnumType end_at) {
24  v8::Local<v8::Object> object = v8::Object::New(isolate);
25  for (int i = start_after + 1; i <= end_at; ++i) {
26    v8::Local<v8::String> value = v8::String::NewFromUtf8(
27        isolate, ui::ToString(static_cast<EnumType>(i)).c_str());
28    object->Set(value, value);
29  }
30  return object;
31}
32
33}  // namespace
34
35namespace extensions {
36
37AutomationInternalCustomBindings::AutomationInternalCustomBindings(
38    ScriptContext* context) : ObjectBackedNativeHandler(context) {
39  RouteFunction(
40      "IsInteractPermitted",
41      base::Bind(&AutomationInternalCustomBindings::IsInteractPermitted,
42                 base::Unretained(this)));
43  RouteFunction(
44      "GetSchemaAdditions",
45      base::Bind(&AutomationInternalCustomBindings::GetSchemaAdditions,
46                 base::Unretained(this)));
47}
48
49AutomationInternalCustomBindings::~AutomationInternalCustomBindings() {
50}
51
52void AutomationInternalCustomBindings::IsInteractPermitted(
53    const v8::FunctionCallbackInfo<v8::Value>& args) {
54  const Extension* extension = context()->extension();
55  CHECK(extension);
56  const AutomationInfo* automation_info = AutomationInfo::Get(extension);
57  CHECK(automation_info);
58  args.GetReturnValue().Set(
59      v8::Boolean::New(GetIsolate(), automation_info->interact));
60}
61
62void AutomationInternalCustomBindings::GetSchemaAdditions(
63    const v8::FunctionCallbackInfo<v8::Value>& args) {
64  v8::Local<v8::Object> additions = v8::Object::New(GetIsolate());
65
66  additions->Set(
67      v8::String::NewFromUtf8(GetIsolate(), "EventType"),
68      ToEnumObject(GetIsolate(), ui::AX_EVENT_NONE, ui::AX_EVENT_LAST));
69
70  additions->Set(
71      v8::String::NewFromUtf8(GetIsolate(), "RoleType"),
72      ToEnumObject(GetIsolate(), ui::AX_ROLE_NONE, ui::AX_ROLE_LAST));
73
74  additions->Set(
75      v8::String::NewFromUtf8(GetIsolate(), "StateType"),
76      ToEnumObject(GetIsolate(), ui::AX_STATE_NONE, ui::AX_STATE_LAST));
77
78  args.GetReturnValue().Set(additions);
79}
80
81}  // namespace extensions
82