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(WebView
* web_view
) : web_view_(web_view
) {
26 void AxProviderImpl::GetTree(
27 const mojo::Callback
<void(Array
<AxNodePtr
> nodes
)>& callback
) {
28 web_view_
->settings()->setAccessibilityEnabled(true);
29 web_view_
->settings()->setInlineTextBoxAccessibilityEnabled(true);
31 Array
<AxNodePtr
> result
;
32 Populate(web_view_
->accessibilityObject(), 0, 0, &result
);
33 callback
.Run(result
.Pass());
36 int AxProviderImpl::Populate(const WebAXObject
& ax_object
,
39 Array
<AxNodePtr
>* result
) {
40 AxNodePtr
ax_node(ConvertAxNode(ax_object
, parent_id
, next_sibling_id
));
41 int ax_node_id
= ax_node
->id
;
42 if (ax_node
.is_null())
45 result
->push_back(ax_node
.Pass());
47 unsigned num_children
= ax_object
.childCount();
49 for (unsigned i
= 0; i
< num_children
; ++i
) {
50 int new_id
= Populate(ax_object
.childAt(num_children
- i
- 1),
55 next_sibling_id
= new_id
;
61 AxNodePtr
AxProviderImpl::ConvertAxNode(const WebAXObject
& ax_object
,
63 int next_sibling_id
) {
65 if (!const_cast<WebAXObject
&>(ax_object
).updateLayoutAndCheckValidity())
68 result
= mojo::AxNode::New();
69 result
->id
= static_cast<int>(ax_object
.axID());
70 result
->parent_id
= parent_id
;
71 result
->next_sibling_id
= next_sibling_id
;
72 result
->bounds
= mojo::Rect::From(ax_object
.boundingBoxRect());
74 if (ax_object
.isAnchor()) {
75 result
->link
= mojo::AxLink::New();
76 result
->link
->url
= String::From(ax_object
.url().string());
77 } else if (ax_object
.childCount() == 0 &&
78 !ax_object
.stringValue().isEmpty()) {
79 result
->text
= mojo::AxText::New();
80 result
->text
->content
= String::From(ax_object
.stringValue());
86 } // namespace html_viewer