Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / aura / accessibility / ax_tree_source_aura.cc
blobace43a5da318010b658525b53b129c1407a03f6f
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 "chrome/browser/ui/aura/accessibility/ax_tree_source_aura.h"
7 #include <vector>
9 #include "chrome/browser/ui/aura/accessibility/automation_manager_aura.h"
10 #include "content/public/browser/render_frame_host.h"
11 #include "content/public/browser/render_process_host.h"
12 #include "content/public/browser/web_contents.h"
13 #include "ui/views/accessibility/ax_aura_obj_cache.h"
14 #include "ui/views/accessibility/ax_aura_obj_wrapper.h"
15 #include "ui/views/accessibility/ax_view_obj_wrapper.h"
16 #include "ui/views/controls/webview/webview.h"
18 using views::AXAuraObjCache;
19 using views::AXAuraObjWrapper;
21 AXTreeSourceAura::AXTreeSourceAura() {
22 root_.reset(new AXRootObjWrapper(AXAuraObjCache::GetInstance()->GetNextID()));
25 AXTreeSourceAura::~AXTreeSourceAura() {
26 root_.reset();
29 void AXTreeSourceAura::DoDefault(int32 id) {
30 AXAuraObjWrapper* obj = AXAuraObjCache::GetInstance()->Get(id);
31 if (obj)
32 obj->DoDefault();
35 void AXTreeSourceAura::Focus(int32 id) {
36 AXAuraObjWrapper* obj = AXAuraObjCache::GetInstance()->Get(id);
37 if (obj)
38 obj->Focus();
41 void AXTreeSourceAura::MakeVisible(int32 id) {
42 AXAuraObjWrapper* obj = AXAuraObjCache::GetInstance()->Get(id);
43 if (obj)
44 obj->MakeVisible();
47 void AXTreeSourceAura::SetSelection(int32 id, int32 start, int32 end) {
48 AXAuraObjWrapper* obj = AXAuraObjCache::GetInstance()->Get(id);
49 if (obj)
50 obj->SetSelection(start, end);
53 void AXTreeSourceAura::ShowContextMenu(int32 id) {
54 AXAuraObjWrapper* obj = AXAuraObjCache::GetInstance()->Get(id);
55 if (obj)
56 obj->ShowContextMenu();
59 AXAuraObjWrapper* AXTreeSourceAura::GetRoot() const {
60 return root_.get();
63 AXAuraObjWrapper* AXTreeSourceAura::GetFromId(int32 id) const {
64 if (id == root_->GetID())
65 return root_.get();
66 return AXAuraObjCache::GetInstance()->Get(id);
69 int32 AXTreeSourceAura::GetId(AXAuraObjWrapper* node) const {
70 return node->GetID();
73 void AXTreeSourceAura::GetChildren(
74 AXAuraObjWrapper* node,
75 std::vector<AXAuraObjWrapper*>* out_children) const {
76 node->GetChildren(out_children);
79 AXAuraObjWrapper* AXTreeSourceAura::GetParent(AXAuraObjWrapper* node) const {
80 AXAuraObjWrapper* parent = node->GetParent();
81 if (!parent && node->GetID() != root_->GetID())
82 parent = root_.get();
83 return parent;
86 bool AXTreeSourceAura::IsValid(AXAuraObjWrapper* node) const {
87 return node && node->GetID() != -1;
90 bool AXTreeSourceAura::IsEqual(AXAuraObjWrapper* node1,
91 AXAuraObjWrapper* node2) const {
92 if (!node1 || !node2)
93 return false;
95 return node1->GetID() == node2->GetID() && node1->GetID() != -1;
98 AXAuraObjWrapper* AXTreeSourceAura::GetNull() const {
99 return NULL;
102 void AXTreeSourceAura::SerializeNode(AXAuraObjWrapper* node,
103 ui::AXNodeData* out_data) const {
104 node->Serialize(out_data);
106 if (out_data->role == ui::AX_ROLE_WEB_VIEW) {
107 views::View* view = static_cast<views::AXViewObjWrapper*>(node)->view();
108 content::WebContents* contents =
109 static_cast<views::WebView*>(view)->GetWebContents();
110 content::RenderFrameHost* rfh = contents->GetMainFrame();
111 if (rfh) {
112 int ax_tree_id = rfh->GetAXTreeID();
113 out_data->AddIntAttribute(ui::AX_ATTR_CHILD_TREE_ID, ax_tree_id);
118 std::string AXTreeSourceAura::ToString(AXAuraObjWrapper* root,
119 std::string prefix) {
120 ui::AXNodeData data;
121 root->Serialize(&data);
122 std::string output = prefix + data.ToString() + '\n';
124 std::vector<AXAuraObjWrapper*> children;
125 root->GetChildren(&children);
127 prefix += prefix[0];
128 for (size_t i = 0; i < children.size(); ++i)
129 output += ToString(children[i], prefix);
131 return output;