@@ -82,6 +82,15 @@ Vector<CDMProxyFactory*> CDMProxyFactory::platformRegisterFactories()
8282}
8383#endif
8484
85+ bool KeyHandle::takeValueIfDifferent (KeyHandleValueVariant&& value)
86+ {
87+ if (m_value != value) {
88+ m_value = WTFMove (value);
89+ return true ;
90+ }
91+ return false ;
92+ }
93+
8594namespace {
8695
8796static String vectorToHexString (const Vector<uint8_t >& vec)
@@ -99,135 +108,38 @@ String KeyHandle::idAsString() const
99108 return makeString (" [" , vectorToHexString (m_id), " ]" );
100109}
101110
102- bool KeyHandle::takeValueIfDifferent (KeyHandleValueVariant&& value)
103- {
104- if (m_value != value) {
105- m_value = WTFMove (value);
106- return true ;
107- }
108- return false ;
109- }
110-
111- bool KeyStore::containsKeyID (const KeyIDType& keyID) const
112- {
113- return m_keys.findIf ([&](const RefPtr<KeyHandle>& storedKey) {
114- return *storedKey == keyID;
115- }) != notFound;
116- }
117-
118- void KeyStore::merge (const KeyStore& other)
111+ KeyStoreIDType keyStoreBaseNextID ()
119112{
113+ static KeyStoreIDType nextID = 1 ;
120114 ASSERT (isMainThread ());
121- LOG (EME, " EME - CDMProxy - merging %u new keys into a key store of %u keys" , other.numKeys (), numKeys ());
122- for (const auto & key : other)
123- add (key.copyRef ());
124-
125- #if !LOG_DISABLED
126- LOG (EME, " EME - CDMProxy - key store now has %u keys" , numKeys ());
127- for (const auto & key : m_keys)
128- LOG (EME, " \t EME - CDMProxy - Key ID: %s" , key->idAsString ().ascii ().data ());
129- #endif // !LOG_DISABLED
130- }
131-
132- CDMInstanceSession::KeyStatusVector KeyStore::allKeysAs (CDMInstanceSession::KeyStatus status) const
133- {
134- CDMInstanceSession::KeyStatusVector keyStatusVector = convertToJSKeyStatusVector ();
135- for (auto & keyStatus : keyStatusVector)
136- keyStatus.second = status;
137- return keyStatusVector;
115+ return nextID++;
138116}
139117
140- bool KeyStore::addKeys (Vector<RefPtr<KeyHandle>>&& newKeys )
118+ void ReferenceAwareKeyStore::unrefAllKeysFrom ( const KeyStore& otherStore )
141119{
142- bool didKeyStoreChange = false ;
143- for (auto & key : newKeys) {
144- if (add (WTFMove (key)))
145- didKeyStoreChange = true ;
120+ for (const auto & otherKey : otherStore.values ()) {
121+ auto findingResult = m_keys.find (otherKey->id ());
122+ if (findingResult == m_keys.end ())
123+ continue ;
124+ const RefPtr<ReferenceAwareKeyHandle>& key = findingResult->value ;
125+ RELEASE_ASSERT (key);
126+ key->removeReference (otherStore.id ());
127+ if (!key->hasReferences ())
128+ remove (key);
146129 }
147- return didKeyStoreChange;
148130}
149131
150- bool KeyStore::add (RefPtr<KeyHandle>&& key )
132+ void ReferenceAwareKeyStore::merge ( const KeyStore& otherStore )
151133{
152- bool didStoreChange = false ;
153- size_t keyWithMatchingKeyIDIndex = m_keys.findIf ([&] (const RefPtr<KeyHandle>& storedKey) {
154- return *key == *storedKey;
155- });
156-
157- addSessionReferenceTo (key);
158- if (keyWithMatchingKeyIDIndex != notFound) {
159- auto & keyWithMatchingKeyID = m_keys[keyWithMatchingKeyIDIndex];
160- didStoreChange = keyWithMatchingKeyID != key;
161- if (didStoreChange)
162- keyWithMatchingKeyID->mergeKeyInto (WTFMove (key));
163- } else {
164- LOG (EME, " EME - ClearKey - New key with ID %s getting added to key store" , key->idAsString ().ascii ().data ());
165- m_keys.append (WTFMove (key));
166- didStoreChange = true ;
167- }
168-
169- if (didStoreChange) {
170- // Sort the keys lexicographically.
171- // NOTE: This is not as pathological as it may seem, for all
172- // practical purposes the store has a maximum of 2 keys.
173- std::sort (m_keys.begin (), m_keys.end (),
174- [](const RefPtr<KeyHandle>& a, const RefPtr<KeyHandle>& b) {
175- return *a < *b;
176- });
177- }
178-
179- return didStoreChange;
180- }
181-
182- void KeyStore::unrefAllKeysFrom (const KeyStore& other)
183- {
184- for (const auto & key : other)
185- unref (key);
186- }
187-
188- void KeyStore::unrefAllKeys ()
189- {
190- KeyStore store (*this );
191- unrefAllKeysFrom (store);
192- }
193-
194- bool KeyStore::unref (const RefPtr<KeyHandle>& key)
195- {
196- bool storeChanged = false ;
197-
198- size_t keyWithMatchingKeyIDIndex = m_keys.find (key);
199- LOG (EME, " EME - ClearKey - requested to unref key with ID %s and %d session references" , key->idAsString ().ascii ().data (), key->numSessionReferences ());
200-
201- if (keyWithMatchingKeyIDIndex != notFound) {
202- auto & keyWithMatchingKeyID = m_keys[keyWithMatchingKeyIDIndex];
203- removeSessionReferenceFrom (keyWithMatchingKeyID);
204- if (!keyWithMatchingKeyID->hasReferences ()) {
205- LOG (EME, " EME - ClearKey - unref key with ID %s" , keyWithMatchingKeyID->idAsString ().ascii ().data ());
206- m_keys.remove (keyWithMatchingKeyIDIndex);
207- storeChanged = true ;
208- }
209- } else
210- LOG (EME, " EME - ClearKey - attempt to unref key with ID %s ignored, does not exist" , key->idAsString ().ascii ().data ());
211-
212- return storeChanged;
213- }
214-
215- const RefPtr<KeyHandle>& KeyStore::keyHandle (const KeyIDType& keyID) const
216- {
217- for (const auto & key : m_keys) {
218- if (*key == keyID)
219- return key;
134+ ASSERT (isMainThread ());
135+ for (const auto & otherKey : otherStore.values ()) {
136+ RefPtr<ReferenceAwareKeyHandle> key = keyHandle (otherKey->id ());
137+ auto otherReferenceAwareKey = ReferenceAwareKeyHandle::createFrom (otherKey, otherStore.id ());
138+ if (key)
139+ key->updateKeyFrom (WTFMove (otherReferenceAwareKey));
140+ else
141+ add (WTFMove (otherReferenceAwareKey));
220142 }
221-
222- RELEASE_ASSERT (false && " key must exist to call this method" );
223- UNREACHABLE ();
224- }
225-
226- CDMInstanceSession::KeyStatusVector KeyStore::convertToJSKeyStatusVector () const
227- {
228- return m_keys.map ([](auto & key) {
229- return std::pair { key->idAsSharedBuffer (), key->status () };
230- });
231143}
232144
233145void CDMProxy::updateKeyStore (const KeyStore& newKeyStore)
@@ -305,7 +217,7 @@ std::optional<Ref<KeyHandle>> CDMProxy::tryWaitForKeyHandle(const KeyIDType& key
305217 assertIsHeld (m_keysLock);
306218 if (!client || client->isAborting ())
307219 return true ;
308- wasKeyAvailable = keyAvailableUnlocked (keyID);
220+ wasKeyAvailable = isKeyAvailableUnlocked (keyID);
309221 return wasKeyAvailable;
310222 });
311223 }
@@ -320,20 +232,20 @@ std::optional<Ref<KeyHandle>> CDMProxy::tryWaitForKeyHandle(const KeyIDType& key
320232 return std::nullopt ;
321233}
322234
323- bool CDMProxy::keyAvailableUnlocked (const KeyIDType& keyID) const
235+ bool CDMProxy::isKeyAvailableUnlocked (const KeyIDType& keyID) const
324236{
325237 return m_keyStore.containsKeyID (keyID);
326238}
327239
328- bool CDMProxy::keyAvailable (const KeyIDType& keyID) const
240+ bool CDMProxy::isKeyAvailable (const KeyIDType& keyID) const
329241{
330242 Locker locker { m_keysLock };
331- return keyAvailableUnlocked (keyID);
243+ return isKeyAvailableUnlocked (keyID);
332244}
333245
334246std::optional<Ref<KeyHandle>> CDMProxy::getOrWaitForKeyHandle (const KeyIDType& keyID, WeakPtr<CDMProxyDecryptionClient>&& client) const
335247{
336- if (!keyAvailable (keyID)) {
248+ if (!isKeyAvailable (keyID)) {
337249 LOG (EME, " EME - CDMProxy key cache does not contain key ID %s" , vectorToHexString (keyID).ascii ().data ());
338250 return tryWaitForKeyHandle (keyID, WTFMove (client));
339251 }
0 commit comments