Chromium Code Reviews| 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 const PointerIdManager::MappedId mouseId = 1; | |
| 14 // 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?
| |
| 15 unsigned currentId = 2; | |
| 13 | 16 |
| 14 } // namespace | 17 } // namespace |
| 15 | 18 |
| 19 const PointerIdManager::MappedId PointerIdManager::s_invalidId = 0; | |
| 20 | |
| 16 PointerIdManager::PointerIdManager() | 21 PointerIdManager::PointerIdManager() |
| 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 int id = p.second; | |
| 53 if (m_ids[type].contains(id)) | |
| 54 return m_ids[type].get(id); | |
| 55 | |
| 56 int mappedId = 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 int 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 |
| 48 // When id is absent, remove() should be a no-op. | 71 remove(m_ids[type].get(id)); |
| 49 m_ids[toInt(type)].remove(id); | |
| 50 } | |
| 51 } | 72 } |
| 52 | 73 |
| 53 bool PointerIdManager::isPrimary(WebPointerProperties::PointerType type, unsigne d id) | 74 void PointerIdManager::remove(const PointerIdManager::MappedId mappedId) |
| 54 { | 75 { |
| 55 return m_hasPrimaryId[toInt(type)] && m_ids[toInt(type)].first() == id; | 76 // Do not remove mouse pointer id as it should always be there |
| 77 if (mappedId == mouseId || !m_reverseMapping.contains(mappedId)) | |
| 78 return; | |
| 79 | |
| 80 GeneratedPointer p = m_reverseMapping.get(mappedId); | |
| 81 int type = toInt(p.first); | |
| 82 int id = p.second; | |
| 83 m_reverseMapping.remove(mappedId); | |
| 84 m_ids[type].remove(id); | |
| 85 if (m_primaryId[type] == mappedId) | |
| 86 m_primaryId[type] = PointerIdManager::s_invalidId; | |
| 56 } | 87 } |
| 57 | 88 |
| 89 bool PointerIdManager::isPrimary(PointerIdManager::MappedId mappedId) const | |
| 90 { | |
| 91 if (!m_reverseMapping.contains(mappedId)) | |
| 92 return false; | |
| 93 | |
| 94 GeneratedPointer p = m_reverseMapping.get(mappedId); | |
| 95 int type = toInt(p.first); | |
| 96 return m_primaryId[type] == mappedId; | |
| 97 } | |
| 98 | |
| 99 PointerIdManager::MappedId PointerIdManager::getPrimaryId(const WebPointerProper ties::PointerType type) const | |
| 100 { | |
| 101 return m_primaryId[toInt(type)]; | |
| 102 } | |
| 103 | |
| 104 WebPointerProperties::PointerType PointerIdManager::getType(const PointerIdManag er::MappedId mappedId) const | |
| 105 { | |
| 106 if (!m_reverseMapping.contains(mappedId)) | |
| 107 return WebPointerProperties::PointerType::Unknown; | |
| 108 | |
| 109 return m_reverseMapping.get(mappedId).first; | |
| 110 } | |
| 111 | |
| 112 | |
| 58 } // namespace blink | 113 } // namespace blink |
| OLD | NEW |