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
3 // found in the LICENSE file.
5 #include "media/base/cdm_promise_adapter.h"
7 #include "media/base/media_keys.h"
11 CdmPromiseAdapter::CdmPromiseAdapter() : next_promise_id_(1) {
14 CdmPromiseAdapter::~CdmPromiseAdapter() {
15 DCHECK(promises_
.empty());
19 uint32_t CdmPromiseAdapter::SavePromise(scoped_ptr
<CdmPromise
> promise
) {
20 uint32_t promise_id
= next_promise_id_
++;
21 promises_
.add(promise_id
, promise
.Pass());
25 template <typename
... T
>
26 void CdmPromiseAdapter::ResolvePromise(uint32_t promise_id
,
28 scoped_ptr
<CdmPromise
> promise
= TakePromise(promise_id
);
30 NOTREACHED() << "Promise not found for " << promise_id
;
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
;
42 static_cast<CdmPromiseTemplate
<T
...>*>(promise
.get())->resolve(result
...);
45 void CdmPromiseAdapter::RejectPromise(uint32_t promise_id
,
46 MediaKeys::Exception exception_code
,
48 const std::string
& error_message
) {
49 scoped_ptr
<CdmPromise
> promise
= TakePromise(promise_id
);
51 NOTREACHED() << "No promise found for promise_id " << promise_id
;
55 promise
->reject(exception_code
, system_code
, error_message
);
58 void CdmPromiseAdapter::Clear() {
59 // Reject all outstanding promises.
60 for (auto& promise
: promises_
)
61 promise
.second
->reject(MediaKeys::UNKNOWN_ERROR
, 0, "Operation aborted.");
65 scoped_ptr
<CdmPromise
> CdmPromiseAdapter::TakePromise(uint32_t promise_id
) {
66 PromiseMap::iterator it
= promises_
.find(promise_id
);
67 if (it
== promises_
.end())
69 return promises_
.take_and_erase(it
);
72 // Explicit instantiation of function templates.
73 template MEDIA_EXPORT
void CdmPromiseAdapter::ResolvePromise(uint32_t);
74 template MEDIA_EXPORT
void CdmPromiseAdapter::ResolvePromise(