// Copyright (c) 2012 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 "content/browser/accessibility/accessibility_tree_formatter_utils_win.h"

#include <oleacc.h>

#include <map>
#include <string>

#include "base/macros.h"
#include "base/memory/singleton.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "third_party/iaccessible2/ia2_api_all.h"

namespace content {
namespace {

class AccessibilityEnumMap {
 public:
  static AccessibilityEnumMap* GetInstance();

  std::map<int32_t, base::string16> ia_role_string_map;
  std::map<int32_t, base::string16> ia2_role_string_map;
  std::map<int32_t, base::string16> ia_state_string_map;
  std::map<int32_t, base::string16> ia2_state_string_map;
  std::map<int32_t, base::string16> event_string_map;

 private:
  AccessibilityEnumMap();
  virtual ~AccessibilityEnumMap() {}

  friend struct base::DefaultSingletonTraits<AccessibilityEnumMap>;

  DISALLOW_COPY_AND_ASSIGN(AccessibilityEnumMap);
};

// static
AccessibilityEnumMap* AccessibilityEnumMap::GetInstance() {
  return base::Singleton<
      AccessibilityEnumMap,
      base::LeakySingletonTraits<AccessibilityEnumMap>>::get();
}

AccessibilityEnumMap::AccessibilityEnumMap() {
// Convenience macros for generating readable strings.
#define IA_ROLE_MAP(x) ia_role_string_map[x] = L###x;  \
                       ia2_role_string_map[x] = L###x;
#define IA2_ROLE_MAP(x) ia2_role_string_map[x] = L###x;
#define IA_STATE_MAP(x) ia_state_string_map[STATE_SYSTEM_##x] = L###x;
#define IA2_STATE_MAP(x) ia2_state_string_map[x] = L###x;
#define EVENT_MAP(x) event_string_map[x] = L###x;

  // MSAA / IAccessible roles. Each one of these is also a valid
  // IAccessible2 role, the IA_ROLE_MAP macro adds it to both.
  IA_ROLE_MAP(ROLE_SYSTEM_ALERT)
  IA_ROLE_MAP(ROLE_SYSTEM_ANIMATION)
  IA_ROLE_MAP(ROLE_SYSTEM_APPLICATION)
  IA_ROLE_MAP(ROLE_SYSTEM_BORDER)
  IA_ROLE_MAP(ROLE_SYS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_MAP(IA2_ROLE_ROOT_PANE)
  IA2_ROLE_MAP(IA2_ROLE_RULER)
  IA2_ROLE_MAP(IA2_ROLE_SCROLL_PANE)
  IA2_ROLE_MAP(IA2_ROLE_SECTION)
  IA2_ROLE_MAP(IA2_ROLE_SHAPE)
  IA2_ROLE_MAP(IA2_ROLE_SPLIT_PANE)
  IA2_ROLE_MAP(IA2_ROLE_TEAR_OFF_MENU)
  IA2_ROLE_MAP(IA2_ROLE_TERMINAL)
  IA2_ROLE_MAP(IA2_ROLE_TEXT_FRAME)
  IA2_ROLE_MAP(IA2_ROLE_TOGGLE_BUTTON)
  IA2_ROLE_MAP(IA2_ROLE_UNKNOWN)
  IA2_ROLE_MAP(IA2_ROLE_VIEW_PORT)

  // MSAA / IAccessible states. Unlike roles, these are not also IA2 states.
  IA_STATE_MAP(ALERT_HIGH)
  IA_STATE_MAP(ALERT_LOW)
  IA_STATE_MAP(ALERT_MEDIUM)
  IA_STATE_MAP(ANIMATED)
  IA_STATE_MAP(BUSY)
  IA_STATE_MAP(CHECKED)
  IA_STATE_MAP(COLLAPSED)
  IA_STATE_MAP(DEFAULT)
  IA_STATE_MAP(EXPANDED)
  IA_STATE_MAP(EXTSELECTABLE)
  IA_STATE_MAP(FLOATING)
  IA_STATE_MAP(FOCUSABLE)
  IA_STATE_MAP(FOCUSED)
  IA_STATE_MAP(HASPOPUP)
  IA_STATE_MAP(HOTTRACKED)
  IA_STATE_MAP(INVISIBLE)
  IA_STATE_MAP(LINKED)
  IA_STATE_MAP(MARQUEED)
  IA_STATE_MAP(MIXED)
  IA_STATE_MAP(MOVEABLE)
  IA_STATE_MAP(MULTISELECTABLE)
  IA_STATE_MAP(OFFSCREEN)
  IA_STATE_MAP(PRESSED)
  IA_STATE_MAP(PROTECTED)
  IA_STATE_MAP(READONLY)
  IA_STATE_MAP(SELECTABLE)
  IA_STATE_MAP(SELECTED)
  IA_STATE_MAP(SELFVOICING)
  IA_STATE_MAP(SIZEABLE)
  IA_STATE_MAP(TRAVERSED)
  IA_STATE_MAP(UNAVAILABLE)

  // IAccessible2 states.
  IA2_STATE_MAP(IA2_STATE_ACTIVE)
  IA2_STATE_MAP(IA2_STATE_ARMED)
  IA2_STATE_MAP(IA2_STATE_CHECKABLE)
  IA2_STATE_MAP(IA2_STATE_DEFUNCT)
  IA2_STATE_MAP(IA2_STATE_EDITABLE)
  IA2_STATE_MAP(IA2_STATE_HORIZONTAL)
  IA2_STATE_MAP(IA2_STATE_ICONIFIED)
  IA2_STATE_MAP(IA2_STATE_INVALID_ENTRY)
  IA2_STATE_MAP(IA2_STATE_MANAGES_DESCENDANTS)
  IA2_STATE_MAP(IA2_STATE_MODAL)
  IA2_STATE_MAP(IA2_STATE_MULTI_LINE)
  IA2_STATE_MAP(IA2_STATE_REQUIRED)
  IA2_STATE_MAP(IA2_STATE_SELECTABLE_TEXT)
  IA2_STATE_MAP(IA2_STATE_SINGLE_LINE)
  IA2_STATE_MAP(IA2_STATE_STALE)
  IA2_STATE_MAP(IA2_STATE_SUPPORTS_AUTOCOMPLETION)
  IA2_STATE_MAP(IA2_STATE_TRANSIENT)
  IA2_STATE_MAP(IA2_STATE_VERTICAL)

  // Untested states include those that would be repeated on nearly every node,
  // or would vary based on window size.
  // IA2_STATE_MAP(IA2_STATE_OPAQUE) // Untested.

  EVENT_MAP(EVENT_OBJECT_CREATE)
  EVENT_MAP(EVENT_OBJECT_DESTROY)
  EVENT_MAP(EVENT_OBJECT_SHOW)
  EVENT_MAP(EVENT_OBJECT_HIDE)
  EVENT_MAP(EVENT_OBJECT_REORDER)
  EVENT_MAP(EVENT_OBJECT_FOCUS)
  EVENT_MAP(EVENT_OBJECT_SELECTION)
  EVENT_MAP(EVENT_OBJECT_SELECTIONADD)
  EVENT_MAP(EVENT_OBJECT_SELECTIONREMOVE)
  EVENT_MAP(EVENT_OBJECT_SELECTIONWITHIN)
  EVENT_MAP(EVENT_OBJECT_STATECHANGE)
  EVENT_MAP(EVENT_OBJECT_LOCATIONCHANGE)
  EVENT_MAP(EVENT_OBJECT_NAMECHANGE)
  EVENT_MAP(EVENT_OBJECT_DESCRIPTIONCHANGE)
  EVENT_MAP(EVENT_OBJECT_VALUECHANGE)
  EVENT_MAP(EVENT_OBJECT_PARENTCHANGE)
  EVENT_MAP(EVENT_OBJECT_HELPCHANGE)
  EVENT_MAP(EVENT_OBJECT_DEFACTIONCHANGE)
  EVENT_MAP(EVENT_OBJECT_ACCELERATORCHANGE)
  EVENT_MAP(EVENT_OBJECT_INVOKED)
  EVENT_MAP(EVENT_OBJECT_TEXTSELECTIONCHANGED)
  EVENT_MAP(EVENT_OBJECT_CONTENTSCROLLED)
  EVENT_MAP(EVENT_OBJECT_LIVEREGIONCHANGED)
  EVENT_MAP(EVENT_OBJECT_HOSTEDOBJECTSINVALIDATED)
  EVENT_MAP(EVENT_OBJECT_DRAGSTART)
  EVENT_MAP(EVENT_OBJECT_DRAGCANCEL)
  EVENT_MAP(EVENT_OBJECT_DRAGCOMPLETE)
  EVENT_MAP(EVENT_OBJECT_DRAGENTER)
  EVENT_MAP(EVENT_OBJECT_DRAGLEAVE)
  EVENT_MAP(EVENT_OBJECT_DRAGDROPPED)
  EVENT_MAP(EVENT_SYSTEM_ALERT)
  EVENT_MAP(EVENT_SYSTEM_SCROLLINGSTART)
  EVENT_MAP(EVENT_SYSTEM_SCROLLINGEND)
  EVENT_MAP(IA2_EVENT_ACTION_CHANGED)
  EVENT_MAP(IA2_EVENT_ACTIVE_DECENDENT_CHANGED)
  EVENT_MAP(IA2_EVENT_ACTIVE_DESCENDANT_CHANGED)
  EVENT_MAP(IA2_EVENT_DOCUMENT_ATTRIBUTE_CHANGED)
  EVENT_MAP(IA2_EVENT_DOCUMENT_CONTENT_CHANGED)
  EVENT_MAP(IA2_EVENT_DOCUMENT_LOAD_COMPLETE)
  EVENT_MAP(IA2_EVENT_DOCUMENT_LOAD_STOPPED)
  EVENT_MAP(IA2_EVENT_DOCUMENT_RELOAD)
  EVENT_MAP(IA2_EVENT_HYPERLINK_END_INDEX_CHANGED)
  EVENT_MAP(IA2_EVENT_HYPERLINK_NUMBER_OF_ANCHORS_CHANGED)
  EVENT_MAP(IA2_EVENT_HYPERLINK_SELECTED_LINK_CHANGED)
  EVENT_MAP(IA2_EVENT_HYPERTEXT_LINK_ACTIVATED)
  EVENT_MAP(IA2_EVENT_HYPERTEXT_LINK_SELECTED)
  EVENT_MAP(IA2_EVENT_HYPERLINK_START_INDEX_CHANGED)
  EVENT_MAP(IA2_EVENT_HYPERTEXT_CHANGED)
  EVENT_MAP(IA2_EVENT_HYPERTEXT_NLINKS_CHANGED)
  EVENT_MAP(IA2_EVENT_OBJECT_ATTRIBUTE_CHANGED)
  EVENT_MAP(IA2_EVENT_PAGE_CHANGED)
  EVENT_MAP(IA2_EVENT_SECTION_CHANGED)
  EVENT_MAP(IA2_EVENT_TABLE_CAPTION_CHANGED)
  EVENT_MAP(IA2_EVENT_TABLE_COLUMN_DESCRIPTION_CHANGED)
  EVENT_MAP(IA2_EVENT_TABLE_COLUMN_HEADER_CHANGED)
  EVENT_MAP(IA2_EVENT_TABLE_MODEL_CHANGED)
  EVENT_MAP(IA2_EVENT_TABLE_ROW_DESCRIPTION_CHANGED)
  EVENT_MAP(IA2_EVENT_TABLE_ROW_HEADER_CHANGED)
  EVENT_MAP(IA2_EVENT_TABLE_SUMMARY_CHANGED)
  EVENT_MAP(IA2_EVENT_TEXT_ATTRIBUTE_CHANGED)
  EVENT_MAP(IA2_EVENT_TEXT_CARET_MOVED)
  EVENT_MAP(IA2_EVENT_TEXT_CHANGED)
  EVENT_MAP(IA2_EVENT_TEXT_COLUMN_CHANGED)
  EVENT_MAP(IA2_EVENT_TEXT_INSERTED)
  EVENT_MAP(IA2_EVENT_TEXT_REMOVED)
  EVENT_MAP(IA2_EVENT_TEXT_UPDATED)
  EVENT_MAP(IA2_EVENT_TEXT_SELECTION_CHANGED)
  EVENT_MAP(IA2_EVENT_VISIBLE_DATA_CHANGED)
}

}  // namespace.

base::string16 IAccessibleRoleToString(int32_t ia_role) {
  return AccessibilityEnumMap::GetInstance()->ia_role_string_map[ia_role];
}

base::string16 IAccessible2RoleToString(int32_t ia_role) {
  return AccessibilityEnumMap::GetInstance()->ia2_role_string_map[ia_role];
}

void IAccessibleStateToStringVector(int32_t ia_state,
                                    std::vector<base::string16>* result) {
  const std::map<int32_t, base::string16>& state_string_map =
      AccessibilityEnumMap::GetInstance()->ia_state_string_map;
  std::map<int32_t, base::string16>::const_iterator it;
  for (it = state_string_map.begin(); it != state_string_map.end(); ++it) {
    if (it->first & ia_state)
      result->push_back(it->second);
  }
}

base::string16 IAccessibleStateToString(int32_t ia_state) {
  std::vector<base::string16> strings;
  IAccessibleStateToStringVector(ia_state, &strings);
  return base::JoinString(strings, base::ASCIIToUTF16(","));
}

void IAccessible2StateToStringVector(int32_t ia2_state,
                                     std::vector<base::string16>* result) {
  const std::map<int32_t, base::string16>& state_string_map =
      AccessibilityEnumMap::GetInstance()->ia2_state_string_map;
  std::map<int32_t, base::string16>::const_iterator it;
  for (it = state_string_map.begin(); it != state_string_map.end(); ++it) {
    if (it->first & ia2_state)
      result->push_back(it->second);
  }
}

base::string16 IAccessible2StateToString(int32_t ia2_state) {
  std::vector<base::string16> strings;
  IAccessible2StateToStringVector(ia2_state, &strings);
  return base::JoinString(strings, base::ASCIIToUTF16(","));
}

base::string16 AccessibilityEventToString(int32_t event_id) {
  return AccessibilityEnumMap::GetInstance()->event_string_map[event_id];
}

}  // namespace content
