Don't preload rarely seen large images
[chromium-blink-merge.git] / components / html_viewer / ax_provider_impl.cc
blobecb69b47f2743d388bf79f5d040ff8a00f688fa0
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(
24 WebView* web_view,
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,
44 int parent_id,
45 int next_sibling_id,
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())
50 return 0;
52 result->push_back(ax_node.Pass());
54 unsigned num_children = ax_object.childCount();
55 next_sibling_id = 0;
56 for (unsigned i = 0; i < num_children; ++i) {
57 int new_id = Populate(ax_object.childAt(num_children - i - 1),
58 ax_node_id,
59 next_sibling_id,
60 result);
61 if (new_id != 0)
62 next_sibling_id = new_id;
65 return ax_node_id;
68 AxNodePtr AxProviderImpl::ConvertAxNode(const WebAXObject& ax_object,
69 int parent_id,
70 int next_sibling_id) {
71 AxNodePtr result;
72 if (!const_cast<WebAXObject&>(ax_object).updateLayoutAndCheckValidity())
73 return result.Pass();
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());
90 return result.Pass();
93 } // namespace html_viewer