[Android] Implement 3-way sensor fallback for Device Orientation.
[chromium-blink-merge.git] / content / common / ax_content_node_data.cc
blob677503c2285697231b0ef1c07713ba0adb86c087
1 // Copyright 2015 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 "base/strings/string_number_conversions.h"
6 #include "content/common/ax_content_node_data.h"
8 using base::IntToString;
10 namespace content {
12 namespace {
14 // Helper function that finds a key in a vector of pairs by matching on the
15 // first value, and returns an iterator.
16 template<typename FirstType, typename SecondType>
17 typename std::vector<std::pair<FirstType, SecondType>>::const_iterator
18 FindInVectorOfPairs(
19 FirstType first,
20 const std::vector<std::pair<FirstType, SecondType>>& vector) {
21 return std::find_if(vector.begin(),
22 vector.end(),
23 [first](std::pair<FirstType, SecondType> const& p) {
24 return p.first == first;
25 });
28 } // namespace
30 AXContentNodeData::AXContentNodeData() {
33 AXContentNodeData::~AXContentNodeData() {
36 bool AXContentNodeData::HasContentIntAttribute(
37 AXContentIntAttribute attribute) const {
38 auto iter = FindInVectorOfPairs(attribute, content_int_attributes);
39 return iter != content_int_attributes.end();
42 int AXContentNodeData::GetContentIntAttribute(
43 AXContentIntAttribute attribute) const {
44 int result;
45 if (GetContentIntAttribute(attribute, &result))
46 return result;
47 return 0;
50 bool AXContentNodeData::GetContentIntAttribute(
51 AXContentIntAttribute attribute, int* value) const {
52 auto iter = FindInVectorOfPairs(attribute, content_int_attributes);
53 if (iter != content_int_attributes.end()) {
54 *value = iter->second;
55 return true;
58 return false;
61 void AXContentNodeData::AddContentIntAttribute(
62 AXContentIntAttribute attribute, int32 value) {
63 content_int_attributes.push_back(std::make_pair(attribute, value));
66 std::string AXContentNodeData::ToString() const {
67 std::string result = AXNodeData::ToString();
69 for (auto iter : content_int_attributes) {
70 std::string value = IntToString(iter.second);
71 switch (iter.first) {
72 case AX_CONTENT_ATTR_ROUTING_ID:
73 result += " routing_id=" + value;
74 break;
75 case AX_CONTENT_ATTR_PARENT_ROUTING_ID:
76 result += " parent_routing_id=" + value;
77 break;
78 case AX_CONTENT_ATTR_CHILD_ROUTING_ID:
79 result += " child_routing_id=" + value;
80 break;
81 case AX_CONTENT_ATTR_CHILD_BROWSER_PLUGIN_INSTANCE_ID:
82 result += " child_browser_plugin_instance_id=" + value;
83 break;
84 case AX_CONTENT_INT_ATTRIBUTE_LAST:
85 NOTREACHED();
86 break;
90 return result;
93 } // namespace ui