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