forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebcontentdecryptionmodule_impl.cc
175 lines (149 loc) · 6.21 KB
/
webcontentdecryptionmodule_impl.cc
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/blink/webcontentdecryptionmodule_impl.h"
#include <utility>
#include "base/bind.h"
#include "base/check.h"
#include "base/notreached.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "media/base/cdm_context.h"
#include "media/base/cdm_promise.h"
#include "media/base/content_decryption_module.h"
#include "media/base/key_systems.h"
#include "media/blink/cdm_result_promise.h"
#include "media/blink/cdm_session_adapter.h"
#include "media/blink/webcontentdecryptionmodulesession_impl.h"
#include "third_party/blink/public/platform/url_conversion.h"
#include "third_party/blink/public/platform/web_security_origin.h"
#include "third_party/blink/public/platform/web_string.h"
#include "url/origin.h"
namespace media {
namespace {
const char kCreateSessionSessionTypeUMAName[] = "CreateSession.SessionType";
const char kSetServerCertificateUMAName[] = "SetServerCertificate";
const char kGetStatusForPolicyUMAName[] = "GetStatusForPolicy";
bool ConvertHdcpVersion(const blink::WebString& hdcp_version_string,
HdcpVersion* hdcp_version) {
if (!hdcp_version_string.ContainsOnlyASCII())
return false;
std::string hdcp_version_ascii = hdcp_version_string.Ascii();
// The strings are specified in the explainer doc:
// https://github.com/WICG/hdcp-detection/blob/master/explainer.md
if (hdcp_version_ascii.empty())
*hdcp_version = HdcpVersion::kHdcpVersionNone;
else if (hdcp_version_ascii == "1.0")
*hdcp_version = HdcpVersion::kHdcpVersion1_0;
else if (hdcp_version_ascii == "1.1")
*hdcp_version = HdcpVersion::kHdcpVersion1_1;
else if (hdcp_version_ascii == "1.2")
*hdcp_version = HdcpVersion::kHdcpVersion1_2;
else if (hdcp_version_ascii == "1.3")
*hdcp_version = HdcpVersion::kHdcpVersion1_3;
else if (hdcp_version_ascii == "1.4")
*hdcp_version = HdcpVersion::kHdcpVersion1_4;
else if (hdcp_version_ascii == "2.0")
*hdcp_version = HdcpVersion::kHdcpVersion2_0;
else if (hdcp_version_ascii == "2.1")
*hdcp_version = HdcpVersion::kHdcpVersion2_1;
else if (hdcp_version_ascii == "2.2")
*hdcp_version = HdcpVersion::kHdcpVersion2_2;
else if (hdcp_version_ascii == "2.3")
*hdcp_version = HdcpVersion::kHdcpVersion2_3;
else
return false;
return true;
}
} // namespace
void WebContentDecryptionModuleImpl::Create(
media::CdmFactory* cdm_factory,
const base::string16& key_system,
const blink::WebSecurityOrigin& security_origin,
const CdmConfig& cdm_config,
WebCdmCreatedCB web_cdm_created_cb) {
DCHECK(!security_origin.IsNull());
DCHECK(!key_system.empty());
// TODO(ddorwin): Guard against this in supported types check and remove this.
// Chromium only supports ASCII key systems.
if (!base::IsStringASCII(key_system)) {
NOTREACHED();
std::move(web_cdm_created_cb).Run(nullptr, "Invalid keysystem.");
return;
}
// TODO(ddorwin): This should be a DCHECK.
std::string key_system_ascii = base::UTF16ToASCII(key_system);
if (!media::KeySystems::GetInstance()->IsSupportedKeySystem(
key_system_ascii)) {
std::string message =
"Keysystem '" + key_system_ascii + "' is not supported.";
std::move(web_cdm_created_cb).Run(nullptr, message);
return;
}
// If opaque security origin, don't try to create the CDM.
if (security_origin.IsOpaque() || security_origin.ToString() == "null") {
std::move(web_cdm_created_cb)
.Run(nullptr, "EME use is not allowed on unique origins.");
return;
}
// CdmSessionAdapter::CreateCdm() will keep a reference to |adapter|. Then
// if WebContentDecryptionModuleImpl is successfully created (returned in
// |web_cdm_created_cb|), it will keep a reference to |adapter|. Otherwise,
// |adapter| will be destructed.
scoped_refptr<CdmSessionAdapter> adapter(new CdmSessionAdapter());
adapter->CreateCdm(cdm_factory, key_system_ascii, cdm_config,
std::move(web_cdm_created_cb));
}
WebContentDecryptionModuleImpl::WebContentDecryptionModuleImpl(
scoped_refptr<CdmSessionAdapter> adapter)
: adapter_(adapter) {
}
WebContentDecryptionModuleImpl::~WebContentDecryptionModuleImpl() = default;
std::unique_ptr<blink::WebContentDecryptionModuleSession>
WebContentDecryptionModuleImpl::CreateSession(
blink::WebEncryptedMediaSessionType session_type) {
base::UmaHistogramEnumeration(
adapter_->GetKeySystemUMAPrefix() + kCreateSessionSessionTypeUMAName,
session_type);
return adapter_->CreateSession(session_type);
}
void WebContentDecryptionModuleImpl::SetServerCertificate(
const uint8_t* server_certificate,
size_t server_certificate_length,
blink::WebContentDecryptionModuleResult result) {
DCHECK(server_certificate);
adapter_->SetServerCertificate(
std::vector<uint8_t>(server_certificate,
server_certificate + server_certificate_length),
std::make_unique<CdmResultPromise<>>(result,
adapter_->GetKeySystemUMAPrefix(),
kSetServerCertificateUMAName));
}
void WebContentDecryptionModuleImpl::GetStatusForPolicy(
const blink::WebString& min_hdcp_version_string,
blink::WebContentDecryptionModuleResult result) {
HdcpVersion min_hdcp_version;
if (!ConvertHdcpVersion(min_hdcp_version_string, &min_hdcp_version)) {
result.CompleteWithError(
blink::kWebContentDecryptionModuleExceptionTypeError, 0,
"Invalid HDCP version");
return;
}
adapter_->GetStatusForPolicy(
min_hdcp_version,
std::make_unique<CdmResultPromise<CdmKeyInformation::KeyStatus>>(
result, adapter_->GetKeySystemUMAPrefix(),
kGetStatusForPolicyUMAName));
}
std::unique_ptr<CdmContextRef>
WebContentDecryptionModuleImpl::GetCdmContextRef() {
return adapter_->GetCdmContextRef();
}
std::string WebContentDecryptionModuleImpl::GetKeySystem() const {
return adapter_->GetKeySystem();
}
CdmConfig WebContentDecryptionModuleImpl::GetCdmConfig() const {
return adapter_->GetCdmConfig();
}
} // namespace media