Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / media / base / cdm_promise_adapter.cc
blob70993b580eca49910c7bee394c496f8ec65fa679
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"
9 namespace media {
11 CdmPromiseAdapter::CdmPromiseAdapter() : next_promise_id_(1) {
14 CdmPromiseAdapter::~CdmPromiseAdapter() {
15 DCHECK(promises_.empty());
16 Clear();
19 uint32_t CdmPromiseAdapter::SavePromise(scoped_ptr<CdmPromise> promise) {
20 uint32_t promise_id = next_promise_id_++;
21 promises_.add(promise_id, promise.Pass());
22 return promise_id;
25 template <typename... T>
26 void CdmPromiseAdapter::ResolvePromise(uint32_t promise_id,
27 const T&... result) {
28 scoped_ptr<CdmPromise> promise = TakePromise(promise_id);
29 if (!promise) {
30 NOTREACHED() << "Promise not found for " << promise_id;
31 return;
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;
42 static_cast<CdmPromiseTemplate<T...>*>(promise.get())->resolve(result...);
45 void CdmPromiseAdapter::RejectPromise(uint32_t promise_id,
46 MediaKeys::Exception exception_code,
47 uint32 system_code,
48 const std::string& error_message) {
49 scoped_ptr<CdmPromise> promise = TakePromise(promise_id);
50 if (!promise) {
51 NOTREACHED() << "No promise found for promise_id " << promise_id;
52 return;
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.");
62 promises_.clear();
65 scoped_ptr<CdmPromise> CdmPromiseAdapter::TakePromise(uint32_t promise_id) {
66 PromiseMap::iterator it = promises_.find(promise_id);
67 if (it == promises_.end())
68 return nullptr;
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(
75 uint32_t,
76 const std::string&);
78 } // namespace media