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.
4
5#include "chrome/browser/ui/ash/accessibility/ax_tree_source_ash.h"
6
7#include <vector>
8
9#include "ui/views/accessibility/ax_aura_obj_cache.h"
10#include "ui/views/accessibility/ax_aura_obj_wrapper.h"
11
12using views::AXAuraObjCache;
13using views::AXAuraObjWrapper;
14
15AXTreeSourceAsh::AXTreeSourceAsh() {
16  root_.reset(
17      new AXRootObjWrapper(AXAuraObjCache::GetInstance()->GetNextID()));
18}
19
20AXTreeSourceAsh::~AXTreeSourceAsh() {
21  root_.reset();
22}
23
24void AXTreeSourceAsh::DoDefault(int32 id) {
25  AXAuraObjWrapper* obj = AXAuraObjCache::GetInstance()->Get(id);
26  CHECK(obj);
27  obj->DoDefault();
28}
29
30void AXTreeSourceAsh::Focus(int32 id) {
31  AXAuraObjWrapper* obj = AXAuraObjCache::GetInstance()->Get(id);
32  CHECK(obj);
33  obj->Focus();
34}
35
36void AXTreeSourceAsh::MakeVisible(int32 id) {
37  AXAuraObjWrapper* obj = AXAuraObjCache::GetInstance()->Get(id);
38  CHECK(obj);
39  obj->MakeVisible();
40}
41
42void AXTreeSourceAsh::SetSelection(int32 id, int32 start, int32 end) {
43  AXAuraObjWrapper* obj = AXAuraObjCache::GetInstance()->Get(id);
44  CHECK(obj);
45  obj->SetSelection(start, end);
46}
47
48AXAuraObjWrapper* AXTreeSourceAsh::GetRoot() const {
49  return root_.get();
50}
51
52AXAuraObjWrapper* AXTreeSourceAsh::GetFromId(int32 id) const {
53  if (id == root_->GetID())
54    return root_.get();
55  return AXAuraObjCache::GetInstance()->Get(id);
56}
57
58int32 AXTreeSourceAsh::GetId(AXAuraObjWrapper* node) const {
59  return node->GetID();
60}
61
62void AXTreeSourceAsh::GetChildren(AXAuraObjWrapper* node,
63    std::vector<AXAuraObjWrapper*>* out_children) const {
64  node->GetChildren(out_children);
65}
66
67AXAuraObjWrapper* AXTreeSourceAsh::GetParent(AXAuraObjWrapper* node) const {
68  AXAuraObjWrapper* parent = node->GetParent();
69  if (!parent && root_->HasChild(node))
70    parent = root_.get();
71  return parent;
72}
73
74bool AXTreeSourceAsh::IsValid(AXAuraObjWrapper* node) const {
75  return node && node->GetID() != -1;
76}
77
78bool AXTreeSourceAsh::IsEqual(AXAuraObjWrapper* node1,
79                                AXAuraObjWrapper* node2) const {
80  if (!node1 || !node2)
81    return false;
82
83  return node1->GetID() == node2->GetID() && node1->GetID() != -1;
84}
85
86AXAuraObjWrapper* AXTreeSourceAsh::GetNull() const {
87  return NULL;
88}
89
90void AXTreeSourceAsh::SerializeNode(
91    AXAuraObjWrapper* node, ui::AXNodeData* out_data) const {
92  node->Serialize(out_data);
93}
94
95std::string AXTreeSourceAsh::ToString(
96    AXAuraObjWrapper* root, std::string prefix) {
97  ui::AXNodeData data;
98  root->Serialize(&data);
99  std::string output = prefix + data.ToString() + '\n';
100
101  std::vector<AXAuraObjWrapper*> children;
102  root->GetChildren(&children);
103
104  prefix += prefix[0];
105  for (size_t i = 0; i < children.size(); ++i)
106    output += ToString(children[i], prefix);
107
108  return output;
109}
110