| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "media/base/cdm_promise_adapter.h" | 5 #include "media/base/cdm_promise_adapter.h" |
| 6 | 6 |
| 7 #include "media/base/media_keys.h" | 7 #include "media/base/media_keys.h" |
| 8 | 8 |
| 9 namespace media { | 9 namespace media { |
| 10 | 10 |
| 11 CdmPromiseAdapter::CdmPromiseAdapter() : next_promise_id_(1) { | 11 CdmPromiseAdapter::CdmPromiseAdapter() : next_promise_id_(1) { |
| 12 } | 12 } |
| 13 | 13 |
| 14 CdmPromiseAdapter::~CdmPromiseAdapter() { | 14 CdmPromiseAdapter::~CdmPromiseAdapter() { |
| 15 DCHECK(promises_.empty()); | 15 DCHECK(promises_.empty()); |
| 16 Clear(); | 16 Clear(); |
| 17 } | 17 } |
| 18 | 18 |
| 19 uint32_t CdmPromiseAdapter::SavePromise(scoped_ptr<CdmPromise> promise) { | 19 uint32_t CdmPromiseAdapter::SavePromise(scoped_ptr<CdmPromise> promise) { |
| 20 DVLOG(1) << "CdmPromiseAdapter::" << __FUNCTION__; |
| 21 base::AutoLock lock(lock_); |
| 20 uint32_t promise_id = next_promise_id_++; | 22 uint32_t promise_id = next_promise_id_++; |
| 21 promises_.add(promise_id, promise.Pass()); | 23 promises_.add(promise_id, promise.Pass()); |
| 22 return promise_id; | 24 return promise_id; |
| 23 } | 25 } |
| 24 | 26 |
| 25 template <typename... T> | 27 template <typename... T> |
| 26 void CdmPromiseAdapter::ResolvePromise(uint32_t promise_id, | 28 void CdmPromiseAdapter::ResolvePromise(uint32_t promise_id, |
| 27 const T&... result) { | 29 const T&... result) { |
| 30 DVLOG(1) << "CdmPromiseAdapter::" << __FUNCTION__; |
| 31 base::AutoLock lock(lock_); |
| 28 scoped_ptr<CdmPromise> promise = TakePromise(promise_id); | 32 scoped_ptr<CdmPromise> promise = TakePromise(promise_id); |
| 29 if (!promise) { | 33 if (!promise) { |
| 30 NOTREACHED() << "Promise not found for " << promise_id; | 34 NOTREACHED() << "Promise not found for " << promise_id; |
| 31 return; | 35 return; |
| 32 } | 36 } |
| 33 | 37 |
| 34 // Sanity check the type before we do static_cast. | 38 // Sanity check the type before we do static_cast. |
| 35 CdmPromise::ResolveParameterType type = promise->GetResolveParameterType(); | 39 CdmPromise::ResolveParameterType type = promise->GetResolveParameterType(); |
| 36 CdmPromise::ResolveParameterType expected = CdmPromiseTraits<T...>::kType; | 40 CdmPromise::ResolveParameterType expected = CdmPromiseTraits<T...>::kType; |
| 37 if (type != expected) { | 41 if (type != expected) { |
| 38 NOTREACHED() << "Promise type mismatch: " << type << " vs " << expected; | 42 NOTREACHED() << "Promise type mismatch: " << type << " vs " << expected; |
| 39 return; | 43 return; |
| 40 } | 44 } |
| 41 | 45 |
| 42 static_cast<CdmPromiseTemplate<T...>*>(promise.get())->resolve(result...); | 46 static_cast<CdmPromiseTemplate<T...>*>(promise.get())->resolve(result...); |
| 43 } | 47 } |
| 44 | 48 |
| 45 void CdmPromiseAdapter::RejectPromise(uint32_t promise_id, | 49 void CdmPromiseAdapter::RejectPromise(uint32_t promise_id, |
| 46 MediaKeys::Exception exception_code, | 50 MediaKeys::Exception exception_code, |
| 47 uint32 system_code, | 51 uint32 system_code, |
| 48 const std::string& error_message) { | 52 const std::string& error_message) { |
| 53 DVLOG(1) << "CdmPromiseAdapter::" << __FUNCTION__; |
| 54 base::AutoLock lock(lock_); |
| 49 scoped_ptr<CdmPromise> promise = TakePromise(promise_id); | 55 scoped_ptr<CdmPromise> promise = TakePromise(promise_id); |
| 50 if (!promise) { | 56 if (!promise) { |
| 51 NOTREACHED() << "No promise found for promise_id " << promise_id; | 57 NOTREACHED() << "No promise found for promise_id " << promise_id; |
| 52 return; | 58 return; |
| 53 } | 59 } |
| 54 | 60 |
| 55 promise->reject(exception_code, system_code, error_message); | 61 promise->reject(exception_code, system_code, error_message); |
| 56 } | 62 } |
| 57 | 63 |
| 58 void CdmPromiseAdapter::Clear() { | 64 void CdmPromiseAdapter::Clear() { |
| 65 DVLOG(1) << "CdmPromiseAdapter::" << __FUNCTION__; |
| 66 base::AutoLock lock(lock_); |
| 59 // Reject all outstanding promises. | 67 // Reject all outstanding promises. |
| 60 for (auto& promise : promises_) | 68 for (auto& promise : promises_) |
| 61 promise.second->reject(MediaKeys::UNKNOWN_ERROR, 0, "Operation aborted."); | 69 promise.second->reject(MediaKeys::UNKNOWN_ERROR, 0, "Operation aborted."); |
| 62 promises_.clear(); | 70 promises_.clear(); |
| 63 } | 71 } |
| 64 | 72 |
| 65 scoped_ptr<CdmPromise> CdmPromiseAdapter::TakePromise(uint32_t promise_id) { | 73 scoped_ptr<CdmPromise> CdmPromiseAdapter::TakePromise(uint32_t promise_id) { |
| 66 PromiseMap::iterator it = promises_.find(promise_id); | 74 PromiseMap::iterator it = promises_.find(promise_id); |
| 67 if (it == promises_.end()) | 75 if (it == promises_.end()) |
| 68 return nullptr; | 76 return nullptr; |
| 69 return promises_.take_and_erase(it); | 77 return promises_.take_and_erase(it); |
| 70 } | 78 } |
| 71 | 79 |
| 72 // Explicit instantiation of function templates. | 80 // Explicit instantiation of function templates. |
| 73 template MEDIA_EXPORT void CdmPromiseAdapter::ResolvePromise(uint32_t); | 81 template MEDIA_EXPORT void CdmPromiseAdapter::ResolvePromise(uint32_t); |
| 74 template MEDIA_EXPORT void CdmPromiseAdapter::ResolvePromise( | 82 template MEDIA_EXPORT void CdmPromiseAdapter::ResolvePromise( |
| 75 uint32_t, | 83 uint32_t, |
| 76 const std::string&); | 84 const std::string&); |
| 77 | 85 |
| 78 } // namespace media | 86 } // namespace media |
| OLD | NEW |