Inline NetLog IPv6 reachability events.
[chromium-blink-merge.git] / components / html_viewer / ax_provider_impl.cc
blobbc7dc3cc54ea1a4b159481a143dbc77fb2330608
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;
14 using blink::WebURL;
15 using blink::WebView;
17 using mojo::Array;
18 using mojo::AxNodePtr;
19 using mojo::String;
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,
37 int parent_id,
38 int next_sibling_id,
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())
43 return 0;
45 result->push_back(ax_node.Pass());
47 unsigned num_children = ax_object.childCount();
48 next_sibling_id = 0;
49 for (unsigned i = 0; i < num_children; ++i) {
50 int new_id = Populate(ax_object.childAt(num_children - i - 1),
51 ax_node_id,
52 next_sibling_id,
53 result);
54 if (new_id != 0)
55 next_sibling_id = new_id;
58 return ax_node_id;
61 AxNodePtr AxProviderImpl::ConvertAxNode(const WebAXObject& ax_object,
62 int parent_id,
63 int next_sibling_id) {
64 AxNodePtr result;
65 if (!const_cast<WebAXObject&>(ax_object).updateLayoutAndCheckValidity())
66 return result.Pass();
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());
83 return result.Pass();
86 } // namespace html_viewer