1 // Copyright 2013 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 "webcontentdecryptionmodule_impl.h"
7 #include "base/basictypes.h"
9 #include "base/logging.h"
10 #include "base/numerics/safe_conversions.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "media/base/cdm_promise.h"
14 #include "media/base/key_systems.h"
15 #include "media/base/media_keys.h"
16 #include "media/blink/cdm_result_promise.h"
17 #include "media/blink/cdm_session_adapter.h"
18 #include "media/blink/webcontentdecryptionmodulesession_impl.h"
19 #include "third_party/WebKit/public/platform/WebString.h"
20 #include "third_party/WebKit/public/web/WebSecurityOrigin.h"
25 // TODO(jrummell): Remove once WebContentDecryptionModuleResult always passed.
26 WebContentDecryptionModuleImpl
* WebContentDecryptionModuleImpl::Create(
27 CdmFactory
* cdm_factory
,
28 const blink::WebSecurityOrigin
& security_origin
,
29 const base::string16
& key_system
) {
30 DCHECK(!security_origin
.isNull());
31 DCHECK(!key_system
.empty());
33 // TODO(ddorwin): Guard against this in supported types check and remove this.
34 // Chromium only supports ASCII key systems.
35 if (!base::IsStringASCII(key_system
)) {
40 std::string key_system_ascii
= base::UTF16ToASCII(key_system
);
41 if (!IsConcreteSupportedKeySystem(key_system_ascii
))
44 // If unique security origin, don't try to create the CDM.
45 if (security_origin
.isUnique() || security_origin
.toString() == "null") {
46 DLOG(ERROR
) << "CDM use not allowed for unique security origin.";
50 scoped_refptr
<CdmSessionAdapter
> adapter(new CdmSessionAdapter());
51 GURL
security_origin_as_gurl(security_origin
.toString());
53 if (!adapter
->Initialize(
54 cdm_factory
, key_system_ascii
, security_origin_as_gurl
)) {
58 return new WebContentDecryptionModuleImpl(adapter
);
61 void WebContentDecryptionModuleImpl::Create(
62 media::CdmFactory
* cdm_factory
,
63 const blink::WebSecurityOrigin
& security_origin
,
64 const base::string16
& key_system
,
65 blink::WebContentDecryptionModuleResult result
) {
66 DCHECK(!security_origin
.isNull());
67 DCHECK(!key_system
.empty());
69 // TODO(ddorwin): Guard against this in supported types check and remove this.
70 // Chromium only supports ASCII key systems.
71 if (!base::IsStringASCII(key_system
)) {
73 result
.completeWithError(
74 blink::WebContentDecryptionModuleExceptionNotSupportedError
, 0,
75 "Invalid keysystem.");
79 std::string key_system_ascii
= base::UTF16ToASCII(key_system
);
80 if (!media::IsConcreteSupportedKeySystem(key_system_ascii
)) {
82 "Keysystem '" + key_system_ascii
+ "' is not supported.";
83 result
.completeWithError(
84 blink::WebContentDecryptionModuleExceptionNotSupportedError
, 0,
85 blink::WebString::fromUTF8(message
));
89 // If unique security origin, don't try to create the CDM.
90 if (security_origin
.isUnique() || security_origin
.toString() == "null") {
91 result
.completeWithError(
92 blink::WebContentDecryptionModuleExceptionNotSupportedError
, 0,
93 "CDM use not allowed for unique security origin.");
97 GURL
security_origin_as_gurl(security_origin
.toString());
98 scoped_refptr
<CdmSessionAdapter
> adapter(new CdmSessionAdapter());
100 // TODO(jrummell): Pass WebContentDecryptionModuleResult (or similar) to
101 // Initialize() so that more specific errors can be reported.
102 if (!adapter
->Initialize(cdm_factory
, key_system_ascii
,
103 security_origin_as_gurl
)) {
104 result
.completeWithError(
105 blink::WebContentDecryptionModuleExceptionNotSupportedError
, 0,
106 "Failed to initialize CDM.");
110 result
.completeWithContentDecryptionModule(
111 new WebContentDecryptionModuleImpl(adapter
));
114 WebContentDecryptionModuleImpl::WebContentDecryptionModuleImpl(
115 scoped_refptr
<CdmSessionAdapter
> adapter
)
116 : adapter_(adapter
) {
119 WebContentDecryptionModuleImpl::~WebContentDecryptionModuleImpl() {
122 // The caller owns the created session.
123 blink::WebContentDecryptionModuleSession
*
124 WebContentDecryptionModuleImpl::createSession() {
125 return adapter_
->CreateSession();
128 blink::WebContentDecryptionModuleSession
*
129 WebContentDecryptionModuleImpl::createSession(
130 blink::WebContentDecryptionModuleSession::Client
* client
) {
131 WebContentDecryptionModuleSessionImpl
* session
= adapter_
->CreateSession();
132 session
->setClientInterface(client
);
136 void WebContentDecryptionModuleImpl::setServerCertificate(
137 const uint8
* server_certificate
,
138 size_t server_certificate_length
,
139 blink::WebContentDecryptionModuleResult result
) {
140 DCHECK(server_certificate
);
141 adapter_
->SetServerCertificate(
143 base::saturated_cast
<int>(server_certificate_length
),
144 scoped_ptr
<SimpleCdmPromise
>(
145 new CdmResultPromise
<>(result
, std::string())));
148 CdmContext
* WebContentDecryptionModuleImpl::GetCdmContext() {
149 return adapter_
->GetCdmContext();