1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/base/cdm_promise_adapter.h"
#include <utility>
namespace media {
namespace {
CdmPromise::SystemCode ToSystemCode(CdmPromiseAdapter::ClearReason reason) {
switch (reason) {
case CdmPromiseAdapter::ClearReason::kDestruction:
return CdmPromise::SystemCode::kAborted;
case CdmPromiseAdapter::ClearReason::kConnectionError:
return CdmPromise::SystemCode::kConnectionError;
}
}
} // namespace
CdmPromiseAdapter::CdmPromiseAdapter()
: next_promise_id_(kInvalidPromiseId + 1) {}
CdmPromiseAdapter::~CdmPromiseAdapter() {
DCHECK(thread_checker_.CalledOnValidThread());
DLOG_IF(WARNING, !promises_.empty()) << "There are unfulfilled promises";
Clear(ClearReason::kDestruction);
}
uint32_t CdmPromiseAdapter::SavePromise(std::unique_ptr<CdmPromise> promise) {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK_NE(kInvalidPromiseId, next_promise_id_);
uint32_t promise_id = next_promise_id_++;
if (next_promise_id_ == kInvalidPromiseId)
next_promise_id_++;
promises_[promise_id] = std::move(promise);
return promise_id;
}
template <typename... T>
void CdmPromiseAdapter::ResolvePromise(uint32_t promise_id,
const T&... result) {
std::unique_ptr<CdmPromise> promise = TakePromise(promise_id);
if (!promise) {
LOG(ERROR) << "Promise not found for " << promise_id;
return;
}
// Sanity check the type before we do static_cast.
CdmPromise::ResolveParameterType type = promise->GetResolveParameterType();
CdmPromise::ResolveParameterType expected = CdmPromiseTraits<T...>::kType;
if (type != expected) {
LOG(ERROR) << "Promise type mismatch: " << type << " vs " << expected;
return;
}
static_cast<CdmPromiseTemplate<T...>*>(promise.get())->resolve(result...);
}
void CdmPromiseAdapter::RejectPromise(uint32_t promise_id,
CdmPromise::Exception exception_code,
uint32_t system_code,
const std::string& error_message) {
std::unique_ptr<CdmPromise> promise = TakePromise(promise_id);
if (!promise) {
LOG(ERROR) << "Promise not found for " << promise_id;
return;
}
promise->reject(exception_code, system_code, error_message);
}
void CdmPromiseAdapter::Clear(ClearReason reason) {
// Reject all outstanding promises.
DCHECK(thread_checker_.CalledOnValidThread());
for (auto& promise : promises_) {
promise.second->reject(CdmPromise::Exception::INVALID_STATE_ERROR,
ToSystemCode(reason), "Operation aborted.");
}
promises_.clear();
}
std::unique_ptr<CdmPromise> CdmPromiseAdapter::TakePromise(
uint32_t promise_id) {
DCHECK(thread_checker_.CalledOnValidThread());
auto it = promises_.find(promise_id);
if (it == promises_.end())
return nullptr;
std::unique_ptr<CdmPromise> result = std::move(it->second);
promises_.erase(it);
return result;
}
// Explicit instantiation of function templates.
template MEDIA_EXPORT void CdmPromiseAdapter::ResolvePromise(uint32_t);
template MEDIA_EXPORT void CdmPromiseAdapter::ResolvePromise(
uint32_t,
const CdmKeyInformation::KeyStatus&);
template MEDIA_EXPORT void CdmPromiseAdapter::ResolvePromise(
uint32_t,
const std::string&);
} // namespace media
|