Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(527)

Side by Side Diff: content/public/browser/voice_interaction_helper.cc

Issue 2768093005: Send voice interaction structure from cros to Arc. (Closed)
Patch Set: address review comments for patchset 10 Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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 "content/public/browser/voice_interaction_helper.h"
6
7 #include "content/browser/accessibility/browser_accessibility.h"
8 #include "content/browser/web_contents/web_contents_impl.h"
9 #include "content/public/browser/web_contents.h"
10 #include "ui/accessibility/platform/ax_android_constants.h"
11
12 namespace content {
13
14 namespace {
15
16 std::string GetClassName(BrowserAccessibility* node) {
17 switch (node->GetRole()) {
18 case ui::AX_ROLE_SEARCH_BOX:
19 case ui::AX_ROLE_SPIN_BUTTON:
20 case ui::AX_ROLE_TEXT_FIELD:
21 return ui::kAXEditTextClassname;
22 case ui::AX_ROLE_SLIDER:
23 return ui::kAXSeekBarClassname;
24 case ui::AX_ROLE_COLOR_WELL:
25 case ui::AX_ROLE_COMBO_BOX:
26 case ui::AX_ROLE_DATE:
27 case ui::AX_ROLE_POP_UP_BUTTON:
28 case ui::AX_ROLE_INPUT_TIME:
29 return ui::kAXSpinnerClassname;
30 case ui::AX_ROLE_BUTTON:
31 case ui::AX_ROLE_MENU_BUTTON:
32 return ui::kAXButtonClassname;
33 case ui::AX_ROLE_CHECK_BOX:
34 case ui::AX_ROLE_SWITCH:
35 return ui::kAXCheckBoxClassname;
36 case ui::AX_ROLE_RADIO_BUTTON:
37 return ui::kAXRadioButtonClassname;
38 case ui::AX_ROLE_TOGGLE_BUTTON:
39 return ui::kAXToggleButtonClassname;
40 case ui::AX_ROLE_CANVAS:
41 case ui::AX_ROLE_IMAGE:
42 case ui::AX_ROLE_SVG_ROOT:
43 return ui::kAXImageClassname;
44 case ui::AX_ROLE_METER:
45 case ui::AX_ROLE_PROGRESS_INDICATOR:
46 return ui::kAXProgressBarClassname;
47 case ui::AX_ROLE_TAB_LIST:
48 return ui::kAXTabWidgetClassname;
49 case ui::AX_ROLE_GRID:
50 case ui::AX_ROLE_TABLE:
51 return ui::kAXGridViewClassname;
52 case ui::AX_ROLE_LIST:
53 case ui::AX_ROLE_LIST_BOX:
54 case ui::AX_ROLE_DESCRIPTION_LIST:
55 return ui::kAXListViewClassname;
56 case ui::AX_ROLE_DIALOG:
57 return ui::kAXDialogClassname;
58 case ui::AX_ROLE_ROOT_WEB_AREA:
59 return node->PlatformGetParent() == nullptr ? ui::kAXWebViewClassname
60 : ui::kAXViewClassname;
61 case ui::AX_ROLE_MENU_ITEM:
62 case ui::AX_ROLE_MENU_ITEM_CHECK_BOX:
63 case ui::AX_ROLE_MENU_ITEM_RADIO:
64 return ui::kAXMenuItemClassname;
65 default:
66 return ui::kAXViewClassname;
67 }
68 }
69
70 // TODO(muyuanli): refactor code in web_contents_android
71 // to use this function as well.
72 std::unique_ptr<AXViewStructure> WalkAXTreeDepthFirst(
73 BrowserAccessibility* node,
74 gfx::Rect rect,
75 const ui::AXTreeUpdate& update) {
76 auto result = base::MakeUnique<AXViewStructure>();
77 result->text = node->GetText();
78 result->class_name = GetClassName(node);
79
80 const int text_style = node->GetIntAttribute(ui::AX_ATTR_TEXT_STYLE);
81 result->bold = (text_style & ui::AX_TEXT_STYLE_BOLD) != 0;
82 result->italic = (text_style & ui::AX_TEXT_STYLE_ITALIC) != 0;
83 result->line_through = (text_style & ui::AX_TEXT_STYLE_LINE_THROUGH) != 0;
84 result->underline = (text_style & ui::AX_TEXT_STYLE_ITALIC) != 0;
85
86 result->text_size = node->GetFloatAttribute(ui::AX_ATTR_FONT_SIZE);
87 result->bgcolor = node->GetIntAttribute(ui::AX_ATTR_BACKGROUND_COLOR);
88
89 const gfx::Rect& absolute_rect = node->GetPageBoundsRect();
90 gfx::Rect parent_relative_rect = absolute_rect;
91 bool is_root = node->GetParent() == nullptr;
92 if (!is_root) {
93 parent_relative_rect.Offset(-rect.OffsetFromOrigin());
94 }
95 result->rect = gfx::Rect(parent_relative_rect.x(), parent_relative_rect.y(),
96 absolute_rect.width(), absolute_rect.height());
97 result->has_selection = false;
98
99 if (node->PlatformIsLeaf()) {
100 int start_selection = 0;
101 int end_selection = 0;
102 if (update.tree_data.sel_anchor_object_id == node->GetId()) {
103 start_selection = update.tree_data.sel_anchor_offset;
104 end_selection = node->GetText().length();
105 }
106 if (update.tree_data.sel_focus_object_id == node->GetId()) {
107 end_selection = update.tree_data.sel_focus_offset;
108 }
109 if (end_selection > 0) {
110 result->has_selection = true;
111 result->start_selection = start_selection;
112 result->end_selection = end_selection;
113 }
114 }
115
116 for (unsigned int i = 0; i < node->PlatformChildCount(); i++) {
117 auto child =
118 WalkAXTreeDepthFirst(node->PlatformGetChild(i), absolute_rect, update);
119 result->children.push_back(std::move(child));
120 }
121
122 return result;
123 }
124
125 void AXSnapshotCallback(
126 const base::Callback<void(std::unique_ptr<AXViewStructure>)>& callback,
127 const ui::AXTreeUpdate& ax_tree_update) {
128 auto manager = std::unique_ptr<content::BrowserAccessibilityManager>(
129 content::BrowserAccessibilityManager::Create(ax_tree_update, nullptr));
130 callback.Run(
131 WalkAXTreeDepthFirst(manager->GetRoot(), gfx::Rect(), ax_tree_update));
132 }
133
134 } // namespace
135
136 AXViewStructure::AXViewStructure() {}
Luis Héctor Chávez 2017/04/04 15:49:31 nit: = default. Same in L138.
Muyuan 2017/04/04 18:35:34 Done.
137
138 AXViewStructure::~AXViewStructure() {}
139
140 void GetAXViewStructure(
141 WebContents* web_contents,
142 const base::Callback<void(std::unique_ptr<AXViewStructure>)>& callback) {
143 WebContentsImpl* web_contents_impl =
144 static_cast<WebContentsImpl*>(web_contents);
145 web_contents_impl->RequestAXTreeSnapshot(
146 base::Bind(&AXSnapshotCallback, callback));
147 }
148
149 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698