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.
5 #include "components/html_viewer/ax_provider_impl.h"
7 #include "components/html_viewer/blink_basic_type_converters.h"
8 #include "third_party/WebKit/public/platform/WebURL.h"
9 #include "third_party/WebKit/public/web/WebAXObject.h"
10 #include "third_party/WebKit/public/web/WebSettings.h"
11 #include "third_party/WebKit/public/web/WebView.h"
13 using blink::WebAXObject
;
18 using mojo::AxNodePtr
;
21 namespace html_viewer
{
23 AxProviderImpl::AxProviderImpl(
25 mojo::InterfaceRequest
<mojo::AxProvider
> request
)
26 : web_view_(web_view
),
27 binding_(this, request
.Pass()) {
30 AxProviderImpl::~AxProviderImpl() {
33 void AxProviderImpl::GetTree(
34 const mojo::Callback
<void(Array
<AxNodePtr
> nodes
)>& callback
) {
35 web_view_
->settings()->setAccessibilityEnabled(true);
36 web_view_
->settings()->setInlineTextBoxAccessibilityEnabled(true);
38 Array
<AxNodePtr
> result
;
39 Populate(web_view_
->accessibilityObject(), 0, 0, &result
);
40 callback
.Run(result
.Pass());
43 int AxProviderImpl::Populate(const WebAXObject
& ax_object
,
46 Array
<AxNodePtr
>* result
) {
47 AxNodePtr
ax_node(ConvertAxNode(ax_object
, parent_id
, next_sibling_id
));
48 int ax_node_id
= ax_node
->id
;
49 if (ax_node
.is_null())
52 result
->push_back(ax_node
.Pass());
54 unsigned num_children
= ax_object
.childCount();
56 for (unsigned i
= 0; i
< num_children
; ++i
) {
57 int new_id
= Populate(ax_object
.childAt(num_children
- i
- 1),
62 next_sibling_id
= new_id
;
68 AxNodePtr
AxProviderImpl::ConvertAxNode(const WebAXObject
& ax_object
,
70 int next_sibling_id
) {
72 if (!const_cast<WebAXObject
&>(ax_object
).updateLayoutAndCheckValidity())
75 result
= mojo::AxNode::New();
76 result
->id
= static_cast<int>(ax_object
.axID());
77 result
->parent_id
= parent_id
;
78 result
->next_sibling_id
= next_sibling_id
;
79 result
->bounds
= mojo::Rect::From(ax_object
.boundingBoxRect());
81 if (ax_object
.isAnchor()) {
82 result
->link
= mojo::AxLink::New();
83 result
->link
->url
= String::From(ax_object
.url().string());
84 } else if (ax_object
.childCount() == 0 &&
85 !ax_object
.stringValue().isEmpty()) {
86 result
->text
= mojo::AxText::New();
87 result
->text
->content
= String::From(ax_object
.stringValue());
93 } // namespace html_viewer