OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/accessibility/accessibility_tree_formatter.h" | 5 #include "content/browser/accessibility/accessibility_tree_formatter.h" |
6 | 6 |
7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
8 | 8 |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 | 27 |
28 const char* kPositionDictAttr = "position"; | 28 const char* kPositionDictAttr = "position"; |
29 const char* kXCoordDictAttr = "x"; | 29 const char* kXCoordDictAttr = "x"; |
30 const char* kYCoordDictAttr = "y"; | 30 const char* kYCoordDictAttr = "y"; |
31 const char* kSizeDictAttr = "size"; | 31 const char* kSizeDictAttr = "size"; |
32 const char* kWidthDictAttr = "width"; | 32 const char* kWidthDictAttr = "width"; |
33 const char* kHeightDictAttr = "height"; | 33 const char* kHeightDictAttr = "height"; |
34 const char* kRangeLocDictAttr = "loc"; | 34 const char* kRangeLocDictAttr = "loc"; |
35 const char* kRangeLenDictAttr = "len"; | 35 const char* kRangeLenDictAttr = "len"; |
36 | 36 |
37 scoped_ptr<base::DictionaryValue> PopulatePosition( | 37 std::unique_ptr<base::DictionaryValue> PopulatePosition( |
38 const BrowserAccessibility& node) { | 38 const BrowserAccessibility& node) { |
39 scoped_ptr<base::DictionaryValue> position(new base::DictionaryValue); | 39 std::unique_ptr<base::DictionaryValue> position(new base::DictionaryValue); |
40 // The NSAccessibility position of an object is in global coordinates and | 40 // The NSAccessibility position of an object is in global coordinates and |
41 // based on the lower-left corner of the object. To make this easier and less | 41 // based on the lower-left corner of the object. To make this easier and less |
42 // confusing, convert it to local window coordinates using the top-left | 42 // confusing, convert it to local window coordinates using the top-left |
43 // corner when dumping the position. | 43 // corner when dumping the position. |
44 BrowserAccessibility* root = node.manager()->GetRootManager()->GetRoot(); | 44 BrowserAccessibility* root = node.manager()->GetRootManager()->GetRoot(); |
45 BrowserAccessibilityCocoa* cocoa_root = ToBrowserAccessibilityCocoa(root); | 45 BrowserAccessibilityCocoa* cocoa_root = ToBrowserAccessibilityCocoa(root); |
46 NSPoint root_position = [[cocoa_root position] pointValue]; | 46 NSPoint root_position = [[cocoa_root position] pointValue]; |
47 NSSize root_size = [[cocoa_root size] sizeValue]; | 47 NSSize root_size = [[cocoa_root size] sizeValue]; |
48 int root_top = -static_cast<int>(root_position.y + root_size.height); | 48 int root_top = -static_cast<int>(root_position.y + root_size.height); |
49 int root_left = static_cast<int>(root_position.x); | 49 int root_left = static_cast<int>(root_position.x); |
50 | 50 |
51 BrowserAccessibilityCocoa* cocoa_node = | 51 BrowserAccessibilityCocoa* cocoa_node = |
52 ToBrowserAccessibilityCocoa(const_cast<BrowserAccessibility*>(&node)); | 52 ToBrowserAccessibilityCocoa(const_cast<BrowserAccessibility*>(&node)); |
53 NSPoint node_position = [[cocoa_node position] pointValue]; | 53 NSPoint node_position = [[cocoa_node position] pointValue]; |
54 NSSize node_size = [[cocoa_node size] sizeValue]; | 54 NSSize node_size = [[cocoa_node size] sizeValue]; |
55 | 55 |
56 position->SetInteger(kXCoordDictAttr, | 56 position->SetInteger(kXCoordDictAttr, |
57 static_cast<int>(node_position.x - root_left)); | 57 static_cast<int>(node_position.x - root_left)); |
58 position->SetInteger(kYCoordDictAttr, | 58 position->SetInteger(kYCoordDictAttr, |
59 static_cast<int>(-node_position.y - node_size.height - root_top)); | 59 static_cast<int>(-node_position.y - node_size.height - root_top)); |
60 return position; | 60 return position; |
61 } | 61 } |
62 | 62 |
63 scoped_ptr<base::DictionaryValue> | 63 std::unique_ptr<base::DictionaryValue> PopulateSize( |
64 PopulateSize(const BrowserAccessibilityCocoa* cocoa_node) { | 64 const BrowserAccessibilityCocoa* cocoa_node) { |
65 scoped_ptr<base::DictionaryValue> size(new base::DictionaryValue); | 65 std::unique_ptr<base::DictionaryValue> size(new base::DictionaryValue); |
66 NSSize node_size = [[cocoa_node size] sizeValue]; | 66 NSSize node_size = [[cocoa_node size] sizeValue]; |
67 size->SetInteger(kHeightDictAttr, static_cast<int>(node_size.height)); | 67 size->SetInteger(kHeightDictAttr, static_cast<int>(node_size.height)); |
68 size->SetInteger(kWidthDictAttr, static_cast<int>(node_size.width)); | 68 size->SetInteger(kWidthDictAttr, static_cast<int>(node_size.width)); |
69 return size; | 69 return size; |
70 } | 70 } |
71 | 71 |
72 scoped_ptr<base::DictionaryValue> PopulateRange(NSRange range) { | 72 std::unique_ptr<base::DictionaryValue> PopulateRange(NSRange range) { |
73 scoped_ptr<base::DictionaryValue> rangeDict(new base::DictionaryValue); | 73 std::unique_ptr<base::DictionaryValue> rangeDict(new base::DictionaryValue); |
74 rangeDict->SetInteger(kRangeLocDictAttr, static_cast<int>(range.location)); | 74 rangeDict->SetInteger(kRangeLocDictAttr, static_cast<int>(range.location)); |
75 rangeDict->SetInteger(kRangeLenDictAttr, static_cast<int>(range.length)); | 75 rangeDict->SetInteger(kRangeLenDictAttr, static_cast<int>(range.length)); |
76 return rangeDict; | 76 return rangeDict; |
77 } | 77 } |
78 | 78 |
79 // Returns true if |value| is an NSValue containing a NSRange. | 79 // Returns true if |value| is an NSValue containing a NSRange. |
80 bool IsRangeValue(id value) { | 80 bool IsRangeValue(id value) { |
81 if (![value isKindOfClass:[NSValue class]]) | 81 if (![value isKindOfClass:[NSValue class]]) |
82 return false; | 82 return false; |
83 return 0 == strcmp([value objCType], @encode(NSRange)); | 83 return 0 == strcmp([value objCType], @encode(NSRange)); |
84 } | 84 } |
85 | 85 |
86 scoped_ptr<base::Value> PopulateObject(id value); | 86 std::unique_ptr<base::Value> PopulateObject(id value); |
87 | 87 |
88 scoped_ptr<base::ListValue> PopulateArray(NSArray* array) { | 88 std::unique_ptr<base::ListValue> PopulateArray(NSArray* array) { |
89 scoped_ptr<base::ListValue> list(new base::ListValue); | 89 std::unique_ptr<base::ListValue> list(new base::ListValue); |
90 for (NSUInteger i = 0; i < [array count]; i++) | 90 for (NSUInteger i = 0; i < [array count]; i++) |
91 list->Append(PopulateObject([array objectAtIndex:i]).release()); | 91 list->Append(PopulateObject([array objectAtIndex:i]).release()); |
92 return list; | 92 return list; |
93 } | 93 } |
94 | 94 |
95 scoped_ptr<base::StringValue> StringForBrowserAccessibility( | 95 std::unique_ptr<base::StringValue> StringForBrowserAccessibility( |
96 BrowserAccessibilityCocoa* obj) { | 96 BrowserAccessibilityCocoa* obj) { |
97 NSMutableArray* tokens = [[NSMutableArray alloc] init]; | 97 NSMutableArray* tokens = [[NSMutableArray alloc] init]; |
98 | 98 |
99 // Always include the role | 99 // Always include the role |
100 id role = [obj role]; | 100 id role = [obj role]; |
101 [tokens addObject:role]; | 101 [tokens addObject:role]; |
102 | 102 |
103 // If the role is "group", include the role description as well. | 103 // If the role is "group", include the role description as well. |
104 id roleDescription = [obj roleDescription]; | 104 id roleDescription = [obj roleDescription]; |
105 if ([role isEqualToString:NSAccessibilityGroupRole] && | 105 if ([role isEqualToString:NSAccessibilityGroupRole] && |
106 roleDescription != nil && | 106 roleDescription != nil && |
107 ![roleDescription isEqualToString:@""] && | 107 ![roleDescription isEqualToString:@""] && |
108 ![roleDescription isEqualToString:@"group"]) { | 108 ![roleDescription isEqualToString:@"group"]) { |
109 [tokens addObject:roleDescription]; | 109 [tokens addObject:roleDescription]; |
110 } | 110 } |
111 | 111 |
112 // Include the description, title, or value - the first one not empty. | 112 // Include the description, title, or value - the first one not empty. |
113 id title = [obj title]; | 113 id title = [obj title]; |
114 id description = [obj description]; | 114 id description = [obj description]; |
115 id value = [obj value]; | 115 id value = [obj value]; |
116 if (description && ![description isEqual:@""]) { | 116 if (description && ![description isEqual:@""]) { |
117 [tokens addObject:description]; | 117 [tokens addObject:description]; |
118 } else if (title && ![title isEqual:@""]) { | 118 } else if (title && ![title isEqual:@""]) { |
119 [tokens addObject:title]; | 119 [tokens addObject:title]; |
120 } else if (value && ![value isEqual:@""]) { | 120 } else if (value && ![value isEqual:@""]) { |
121 [tokens addObject:value]; | 121 [tokens addObject:value]; |
122 } | 122 } |
123 | 123 |
124 NSString* result = [tokens componentsJoinedByString:@" "]; | 124 NSString* result = [tokens componentsJoinedByString:@" "]; |
125 return scoped_ptr<base::StringValue>( | 125 return std::unique_ptr<base::StringValue>( |
126 new base::StringValue(SysNSStringToUTF16(result))); | 126 new base::StringValue(SysNSStringToUTF16(result))); |
127 } | 127 } |
128 | 128 |
129 scoped_ptr<base::Value> PopulateObject(id value) { | 129 std::unique_ptr<base::Value> PopulateObject(id value) { |
130 if ([value isKindOfClass:[NSArray class]]) | 130 if ([value isKindOfClass:[NSArray class]]) |
131 return scoped_ptr<base::Value>(PopulateArray((NSArray*) value)); | 131 return std::unique_ptr<base::Value>(PopulateArray((NSArray*)value)); |
132 if (IsRangeValue(value)) | 132 if (IsRangeValue(value)) |
133 return scoped_ptr<base::Value>(PopulateRange([value rangeValue])); | 133 return std::unique_ptr<base::Value>(PopulateRange([value rangeValue])); |
134 if ([value isKindOfClass:[BrowserAccessibilityCocoa class]]) { | 134 if ([value isKindOfClass:[BrowserAccessibilityCocoa class]]) { |
135 std::string str; | 135 std::string str; |
136 StringForBrowserAccessibility(value)->GetAsString(&str); | 136 StringForBrowserAccessibility(value)->GetAsString(&str); |
137 return scoped_ptr<base::Value>(StringForBrowserAccessibility( | 137 return std::unique_ptr<base::Value>( |
138 (BrowserAccessibilityCocoa*) value)); | 138 StringForBrowserAccessibility((BrowserAccessibilityCocoa*)value)); |
139 } | 139 } |
140 | 140 |
141 return scoped_ptr<base::Value>(new base::StringValue( | 141 return std::unique_ptr<base::Value>(new base::StringValue( |
142 SysNSStringToUTF16([NSString stringWithFormat:@"%@", value]))); | 142 SysNSStringToUTF16([NSString stringWithFormat:@"%@", value]))); |
143 } | 143 } |
144 | 144 |
145 NSArray* BuildAllAttributesArray() { | 145 NSArray* BuildAllAttributesArray() { |
146 NSArray* array = [NSArray arrayWithObjects: | 146 NSArray* array = [NSArray arrayWithObjects: |
147 NSAccessibilityRoleDescriptionAttribute, | 147 NSAccessibilityRoleDescriptionAttribute, |
148 NSAccessibilityTitleAttribute, | 148 NSAccessibilityTitleAttribute, |
149 NSAccessibilityValueAttribute, | 149 NSAccessibilityValueAttribute, |
150 NSAccessibilityMinValueAttribute, | 150 NSAccessibilityMinValueAttribute, |
151 NSAccessibilityMaxValueAttribute, | 151 NSAccessibilityMaxValueAttribute, |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 | 330 |
331 const string AccessibilityTreeFormatterMac::GetAllowString() { | 331 const string AccessibilityTreeFormatterMac::GetAllowString() { |
332 return "@MAC-ALLOW:"; | 332 return "@MAC-ALLOW:"; |
333 } | 333 } |
334 | 334 |
335 const string AccessibilityTreeFormatterMac::GetDenyString() { | 335 const string AccessibilityTreeFormatterMac::GetDenyString() { |
336 return "@MAC-DENY:"; | 336 return "@MAC-DENY:"; |
337 } | 337 } |
338 | 338 |
339 } // namespace content | 339 } // namespace content |
OLD | NEW |