Chromium Code Reviews| Index: third_party/WebKit/Source/core/events/PointerIdManager.cpp | 
| diff --git a/third_party/WebKit/Source/core/events/PointerIdManager.cpp b/third_party/WebKit/Source/core/events/PointerIdManager.cpp | 
| index a0342b72387e5550842a64cd8307099dc0d7523b..3829ca229432dde7434b7ac62ee1654bd385bb63 100644 | 
| --- a/third_party/WebKit/Source/core/events/PointerIdManager.cpp | 
| +++ b/third_party/WebKit/Source/core/events/PointerIdManager.cpp | 
| @@ -10,9 +10,14 @@ namespace blink { | 
| namespace { | 
| inline int toInt(WebPointerProperties::PointerType t) { return static_cast<int>(t); } | 
| +const PointerIdManager::MappedId mouseId = 1; | 
| +// We do not handle the overflow of currentId as it should be very rare | 
| 
 
Navid Zolghadr
2015/11/10 17:48:50
Is this okay not to handle the overflow case?
 
 | 
| +unsigned currentId = 2; | 
| } // namespace | 
| +const PointerIdManager::MappedId PointerIdManager::s_invalidId = 0; | 
| + | 
| PointerIdManager::PointerIdManager() | 
| { | 
| clear(); | 
| @@ -27,32 +32,82 @@ void PointerIdManager::clear() | 
| { | 
| for (int type = 0; type <= toInt(WebPointerProperties::PointerType::LastEntry); type++) { | 
| m_ids[type].clear(); | 
| - m_hasPrimaryId[type] = false; | 
| + m_primaryId[type] = PointerIdManager::s_invalidId; | 
| } | 
| + m_reverseMapping.clear(); | 
| + | 
| + // Always add mouse pointer in initialization and never remove it. | 
| + // No need to add it to m_ids as it is not going to be used with the existing APIs | 
| + m_primaryId[toInt(WebPointerProperties::PointerType::Mouse)] = mouseId; | 
| + m_reverseMapping.add(mouseId, GeneratedPointer(WebPointerProperties::PointerType::Mouse, 0)); | 
| } | 
| -void PointerIdManager::add(WebPointerProperties::PointerType type, unsigned id) | 
| +PointerIdManager::MappedId PointerIdManager::add(const GeneratedPointer p) | 
| { | 
| - if (m_ids[toInt(type)].isEmpty()) | 
| - m_hasPrimaryId[toInt(type)] = true; | 
| - m_ids[toInt(type)].add(id); | 
| + // Do not add extra mouse pointer as it was added in initialization | 
| + if (p.first == WebPointerProperties::PointerType::Mouse) | 
| + return mouseId; | 
| + | 
| + int type = toInt(p.first); | 
| + int id = p.second; | 
| + if (m_ids[type].contains(id)) | 
| + return m_ids[type].get(id); | 
| + | 
| + int mappedId = currentId++; | 
| + if (m_ids[type].isEmpty()) | 
| + m_primaryId[type] = mappedId; | 
| + m_ids[type].add(id, mappedId); | 
| + m_reverseMapping.add(mappedId, p); | 
| + return static_cast<PointerIdManager::MappedId>(mappedId); | 
| } | 
| -void PointerIdManager::remove(WebPointerProperties::PointerType type, unsigned id) | 
| +void PointerIdManager::remove(const GeneratedPointer p) | 
| { | 
| - if (isPrimary(type, id)) { | 
| - m_ids[toInt(type)].removeFirst(); | 
| - m_hasPrimaryId[toInt(type)] = false; | 
| - } else { | 
| - // Note that simply counting the number of ids instead of storing all of them is not enough. | 
| - // When id is absent, remove() should be a no-op. | 
| - m_ids[toInt(type)].remove(id); | 
| - } | 
| + int type = toInt(p.first); | 
| + int id = p.second; | 
| + if (!m_ids[type].contains(id)) | 
| + return; | 
| + | 
| + remove(m_ids[type].get(id)); | 
| } | 
| -bool PointerIdManager::isPrimary(WebPointerProperties::PointerType type, unsigned id) | 
| +void PointerIdManager::remove(const PointerIdManager::MappedId mappedId) | 
| { | 
| - return m_hasPrimaryId[toInt(type)] && m_ids[toInt(type)].first() == id; | 
| + // Do not remove mouse pointer id as it should always be there | 
| + if (mappedId == mouseId || !m_reverseMapping.contains(mappedId)) | 
| + return; | 
| + | 
| + GeneratedPointer p = m_reverseMapping.get(mappedId); | 
| + int type = toInt(p.first); | 
| + int id = p.second; | 
| + m_reverseMapping.remove(mappedId); | 
| + m_ids[type].remove(id); | 
| + if (m_primaryId[type] == mappedId) | 
| + m_primaryId[type] = PointerIdManager::s_invalidId; | 
| } | 
| +bool PointerIdManager::isPrimary(PointerIdManager::MappedId mappedId) const | 
| +{ | 
| + if (!m_reverseMapping.contains(mappedId)) | 
| + return false; | 
| + | 
| + GeneratedPointer p = m_reverseMapping.get(mappedId); | 
| + int type = toInt(p.first); | 
| + return m_primaryId[type] == mappedId; | 
| +} | 
| + | 
| +PointerIdManager::MappedId PointerIdManager::getPrimaryId(const WebPointerProperties::PointerType type) const | 
| +{ | 
| + return m_primaryId[toInt(type)]; | 
| +} | 
| + | 
| +WebPointerProperties::PointerType PointerIdManager::getType(const PointerIdManager::MappedId mappedId) const | 
| +{ | 
| + if (!m_reverseMapping.contains(mappedId)) | 
| + return WebPointerProperties::PointerType::Unknown; | 
| + | 
| + return m_reverseMapping.get(mappedId).first; | 
| +} | 
| + | 
| + | 
| } // namespace blink |