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 void WebContentDecryptionModuleImpl::Create(
26 media::CdmFactory
* cdm_factory
,
27 const base::string16
& key_system
,
28 bool allow_distinctive_identifier
,
29 bool allow_persistent_state
,
30 const blink::WebSecurityOrigin
& security_origin
,
31 blink::WebContentDecryptionModuleResult result
) {
32 DCHECK(!security_origin
.isNull());
33 DCHECK(!key_system
.empty());
35 // TODO(ddorwin): Guard against this in supported types check and remove this.
36 // Chromium only supports ASCII key systems.
37 if (!base::IsStringASCII(key_system
)) {
39 result
.completeWithError(
40 blink::WebContentDecryptionModuleExceptionNotSupportedError
, 0,
41 "Invalid keysystem.");
45 // TODO(ddorwin): This should be a DCHECK.
46 std::string key_system_ascii
= base::UTF16ToASCII(key_system
);
47 if (!media::IsSupportedKeySystem(key_system_ascii
)) {
49 "Keysystem '" + key_system_ascii
+ "' is not supported.";
50 result
.completeWithError(
51 blink::WebContentDecryptionModuleExceptionNotSupportedError
, 0,
52 blink::WebString::fromUTF8(message
));
56 // If unique security origin, don't try to create the CDM.
57 if (security_origin
.isUnique() || security_origin
.toString() == "null") {
58 result
.completeWithError(
59 blink::WebContentDecryptionModuleExceptionNotSupportedError
, 0,
60 "CDM use not allowed for unique security origin.");
64 GURL
security_origin_as_gurl(security_origin
.toString());
66 // CdmSessionAdapter::CreateCdm() will keep a reference to |adapter|. Then
67 // if WebContentDecryptionModuleImpl is successfully created (returned in
68 // |result|), it will keep a reference to |adapter|. Otherwise, |adapter| will
70 scoped_refptr
<CdmSessionAdapter
> adapter(new CdmSessionAdapter());
71 adapter
->CreateCdm(cdm_factory
, key_system_ascii
,
72 allow_distinctive_identifier
, allow_persistent_state
,
73 security_origin_as_gurl
, result
);
76 WebContentDecryptionModuleImpl::WebContentDecryptionModuleImpl(
77 scoped_refptr
<CdmSessionAdapter
> adapter
)
81 WebContentDecryptionModuleImpl::~WebContentDecryptionModuleImpl() {
84 // The caller owns the created session.
85 blink::WebContentDecryptionModuleSession
*
86 WebContentDecryptionModuleImpl::createSession() {
87 return adapter_
->CreateSession();
90 void WebContentDecryptionModuleImpl::setServerCertificate(
91 const uint8
* server_certificate
,
92 size_t server_certificate_length
,
93 blink::WebContentDecryptionModuleResult result
) {
94 DCHECK(server_certificate
);
95 adapter_
->SetServerCertificate(
97 base::saturated_cast
<int>(server_certificate_length
),
98 scoped_ptr
<SimpleCdmPromise
>(
99 new CdmResultPromise
<>(result
, std::string())));
102 CdmContext
* WebContentDecryptionModuleImpl::GetCdmContext() {
103 return adapter_
->GetCdmContext();