| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "config.h" | 5 #include "config.h" |
| 6 #include "core/events/PointerIdManager.h" | 6 #include "core/events/PointerIdManager.h" |
| 7 | 7 |
| 8 namespace blink { | 8 namespace blink { |
| 9 | 9 |
| 10 namespace { | 10 namespace { |
| 11 | 11 |
| 12 inline int toInt(WebPointerProperties::PointerType t) { return static_cast<int>(
t); } | 12 inline int toInt(WebPointerProperties::PointerType t) { return static_cast<int>(
t); } |
| 13 // Mouse id is 1 to behave the same as MS Edge for compatibility reasons. |
| 14 const PointerIdManager::MappedId mouseId = 1; |
| 13 | 15 |
| 14 } // namespace | 16 } // namespace |
| 15 | 17 |
| 18 const PointerIdManager::MappedId PointerIdManager::s_invalidId = 0; |
| 19 |
| 16 PointerIdManager::PointerIdManager() | 20 PointerIdManager::PointerIdManager() |
| 21 : m_currentId(2) |
| 17 { | 22 { |
| 18 clear(); | 23 clear(); |
| 19 } | 24 } |
| 20 | 25 |
| 21 PointerIdManager::~PointerIdManager() | 26 PointerIdManager::~PointerIdManager() |
| 22 { | 27 { |
| 23 clear(); | 28 clear(); |
| 24 } | 29 } |
| 25 | 30 |
| 26 void PointerIdManager::clear() | 31 void PointerIdManager::clear() |
| 27 { | 32 { |
| 28 for (int type = 0; type <= toInt(WebPointerProperties::PointerType::LastEntr
y); type++) { | 33 for (int type = 0; type <= toInt(WebPointerProperties::PointerType::LastEntr
y); type++) { |
| 29 m_ids[type].clear(); | 34 m_ids[type].clear(); |
| 30 m_hasPrimaryId[type] = false; | 35 m_primaryId[type] = PointerIdManager::s_invalidId; |
| 31 } | 36 } |
| 37 m_reverseMapping.clear(); |
| 38 |
| 39 // Always add mouse pointer in initialization and never remove it. |
| 40 // No need to add it to m_ids as it is not going to be used with the existin
g APIs |
| 41 m_primaryId[toInt(WebPointerProperties::PointerType::Mouse)] = mouseId; |
| 42 m_reverseMapping.add(mouseId, GeneratedPointer(WebPointerProperties::Pointer
Type::Mouse, 0)); |
| 32 } | 43 } |
| 33 | 44 |
| 34 void PointerIdManager::add(WebPointerProperties::PointerType type, unsigned id) | 45 PointerIdManager::MappedId PointerIdManager::add(const GeneratedPointer p) |
| 35 { | 46 { |
| 36 if (m_ids[toInt(type)].isEmpty()) | 47 // Do not add extra mouse pointer as it was added in initialization |
| 37 m_hasPrimaryId[toInt(type)] = true; | 48 if (p.first == WebPointerProperties::PointerType::Mouse) |
| 38 m_ids[toInt(type)].add(id); | 49 return mouseId; |
| 50 |
| 51 int type = toInt(p.first); |
| 52 unsigned id = p.second; |
| 53 if (m_ids[type].contains(id)) |
| 54 return m_ids[type].get(id); |
| 55 // We do not handle the overflow of m_currentId as it should be very rare |
| 56 MappedId mappedId = m_currentId++; |
| 57 if (m_ids[type].isEmpty()) |
| 58 m_primaryId[type] = mappedId; |
| 59 m_ids[type].add(id, mappedId); |
| 60 m_reverseMapping.add(mappedId, p); |
| 61 return static_cast<PointerIdManager::MappedId>(mappedId); |
| 39 } | 62 } |
| 40 | 63 |
| 41 void PointerIdManager::remove(WebPointerProperties::PointerType type, unsigned i
d) | 64 void PointerIdManager::remove(const GeneratedPointer p) |
| 42 { | 65 { |
| 43 if (isPrimary(type, id)) { | 66 int type = toInt(p.first); |
| 44 m_ids[toInt(type)].removeFirst(); | 67 unsigned id = p.second; |
| 45 m_hasPrimaryId[toInt(type)] = false; | 68 if (!m_ids[type].contains(id)) |
| 46 } else { | 69 return; |
| 47 // Note that simply counting the number of ids instead of storing all of
them is not enough. | 70 remove(m_ids[type].get(id)); |
| 48 // When id is absent, remove() should be a no-op. | |
| 49 m_ids[toInt(type)].remove(id); | |
| 50 } | |
| 51 } | 71 } |
| 52 | 72 |
| 53 bool PointerIdManager::isPrimary(WebPointerProperties::PointerType type, unsigne
d id) | 73 void PointerIdManager::remove(const PointerIdManager::MappedId mappedId) |
| 54 { | 74 { |
| 55 return m_hasPrimaryId[toInt(type)] && m_ids[toInt(type)].first() == id; | 75 // Do not remove mouse pointer id as it should always be there |
| 76 if (mappedId == mouseId || !m_reverseMapping.contains(mappedId)) |
| 77 return; |
| 78 |
| 79 GeneratedPointer p = m_reverseMapping.get(mappedId); |
| 80 int type = toInt(p.first); |
| 81 unsigned id = p.second; |
| 82 m_reverseMapping.remove(mappedId); |
| 83 m_ids[type].remove(id); |
| 84 if (m_primaryId[type] == mappedId) |
| 85 m_primaryId[type] = PointerIdManager::s_invalidId; |
| 56 } | 86 } |
| 57 | 87 |
| 88 bool PointerIdManager::isPrimary(PointerIdManager::MappedId mappedId) const |
| 89 { |
| 90 if (!m_reverseMapping.contains(mappedId)) |
| 91 return false; |
| 92 |
| 93 GeneratedPointer p = m_reverseMapping.get(mappedId); |
| 94 int type = toInt(p.first); |
| 95 return m_primaryId[type] == mappedId; |
| 96 } |
| 97 |
| 98 PointerIdManager::MappedId PointerIdManager::getPrimaryId(const WebPointerProper
ties::PointerType type) const |
| 99 { |
| 100 return m_primaryId[toInt(type)]; |
| 101 } |
| 102 |
| 103 WebPointerProperties::PointerType PointerIdManager::getType(const PointerIdManag
er::MappedId mappedId) const |
| 104 { |
| 105 if (!m_reverseMapping.contains(mappedId)) |
| 106 return WebPointerProperties::PointerType::Unknown; |
| 107 |
| 108 return m_reverseMapping.get(mappedId).first; |
| 109 } |
| 110 |
| 111 |
| 58 } // namespace blink | 112 } // namespace blink |
| OLD | NEW |