summaryrefslogtreecommitdiffstats
path: root/content/common/ax_content_node_data.cc
blob: 677503c2285697231b0ef1c07713ba0adb86c087 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/strings/string_number_conversions.h"
#include "content/common/ax_content_node_data.h"

using base::IntToString;

namespace content {

namespace {

// Helper function that finds a key in a vector of pairs by matching on the
// first value, and returns an iterator.
template<typename FirstType, typename SecondType>
typename std::vector<std::pair<FirstType, SecondType>>::const_iterator
    FindInVectorOfPairs(
        FirstType first,
        const std::vector<std::pair<FirstType, SecondType>>& vector) {
  return std::find_if(vector.begin(),
                      vector.end(),
                      [first](std::pair<FirstType, SecondType> const& p) {
                        return p.first == first;
                      });
}

}  // namespace

AXContentNodeData::AXContentNodeData() {
}

AXContentNodeData::~AXContentNodeData() {
}

bool AXContentNodeData::HasContentIntAttribute(
    AXContentIntAttribute attribute) const {
  auto iter = FindInVectorOfPairs(attribute, content_int_attributes);
  return iter != content_int_attributes.end();
}

int AXContentNodeData::GetContentIntAttribute(
    AXContentIntAttribute attribute) const {
  int result;
  if (GetContentIntAttribute(attribute, &result))
    return result;
  return 0;
}

bool AXContentNodeData::GetContentIntAttribute(
    AXContentIntAttribute attribute, int* value) const {
  auto iter = FindInVectorOfPairs(attribute, content_int_attributes);
  if (iter != content_int_attributes.end()) {
    *value = iter->second;
    return true;
  }

  return false;
}

void AXContentNodeData::AddContentIntAttribute(
    AXContentIntAttribute attribute, int32 value) {
  content_int_attributes.push_back(std::make_pair(attribute, value));
}

std::string AXContentNodeData::ToString() const {
  std::string result = AXNodeData::ToString();

  for (auto iter : content_int_attributes) {
    std::string value = IntToString(iter.second);
    switch (iter.first) {
      case AX_CONTENT_ATTR_ROUTING_ID:
        result += " routing_id=" + value;
        break;
      case AX_CONTENT_ATTR_PARENT_ROUTING_ID:
        result += " parent_routing_id=" + value;
        break;
      case AX_CONTENT_ATTR_CHILD_ROUTING_ID:
        result += " child_routing_id=" + value;
        break;
      case AX_CONTENT_ATTR_CHILD_BROWSER_PLUGIN_INSTANCE_ID:
        result += " child_browser_plugin_instance_id=" + value;
        break;
      case AX_CONTENT_INT_ATTRIBUTE_LAST:
        NOTREACHED();
        break;
    }
  }

  return result;
}

}  // namespace ui