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 "mojo/services/html_viewer/ax_provider_impl.h"
7 #include "mojo/services/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
;
19 AxProviderImpl::AxProviderImpl(WebView
* web_view
) : web_view_(web_view
) {
22 void AxProviderImpl::GetTree(
23 const Callback
<void(Array
<AxNodePtr
> nodes
)>& callback
) {
24 web_view_
->settings()->setAccessibilityEnabled(true);
25 web_view_
->settings()->setInlineTextBoxAccessibilityEnabled(true);
27 Array
<AxNodePtr
> result
;
28 Populate(web_view_
->accessibilityObject(), 0, 0, &result
);
29 callback
.Run(result
.Pass());
32 int AxProviderImpl::Populate(const WebAXObject
& ax_object
,
35 Array
<AxNodePtr
>* result
) {
36 AxNodePtr
ax_node(ConvertAxNode(ax_object
, parent_id
, next_sibling_id
));
37 int ax_node_id
= ax_node
->id
;
38 if (ax_node
.is_null())
41 result
->push_back(ax_node
.Pass());
43 unsigned num_children
= ax_object
.childCount();
45 for (unsigned i
= 0; i
< num_children
; ++i
) {
46 int new_id
= Populate(ax_object
.childAt(num_children
- i
- 1),
51 next_sibling_id
= new_id
;
57 AxNodePtr
AxProviderImpl::ConvertAxNode(const WebAXObject
& ax_object
,
59 int next_sibling_id
) {
61 if (!const_cast<WebAXObject
&>(ax_object
).updateLayoutAndCheckValidity())
64 result
= AxNode::New();
65 result
->id
= static_cast<int>(ax_object
.axID());
66 result
->parent_id
= parent_id
;
67 result
->next_sibling_id
= next_sibling_id
;
68 result
->bounds
= Rect::From(ax_object
.boundingBoxRect());
70 if (ax_object
.isAnchor()) {
71 result
->link
= AxLink::New();
72 result
->link
->url
= String::From(ax_object
.url().string());
73 } else if (ax_object
.childCount() == 0 &&
74 !ax_object
.stringValue().isEmpty()) {
75 result
->text
= AxText::New();
76 result
->text
->content
= String::From(ax_object
.stringValue());