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 blink::WebSecurityOrigin
& security_origin
,
28 const base::string16
& key_system
,
29 blink::WebContentDecryptionModuleResult result
) {
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
)) {
37 result
.completeWithError(
38 blink::WebContentDecryptionModuleExceptionNotSupportedError
, 0,
39 "Invalid keysystem.");
43 std::string key_system_ascii
= base::UTF16ToASCII(key_system
);
44 if (!media::IsConcreteSupportedKeySystem(key_system_ascii
)) {
46 "Keysystem '" + key_system_ascii
+ "' is not supported.";
47 result
.completeWithError(
48 blink::WebContentDecryptionModuleExceptionNotSupportedError
, 0,
49 blink::WebString::fromUTF8(message
));
53 // If unique security origin, don't try to create the CDM.
54 if (security_origin
.isUnique() || security_origin
.toString() == "null") {
55 result
.completeWithError(
56 blink::WebContentDecryptionModuleExceptionNotSupportedError
, 0,
57 "CDM use not allowed for unique security origin.");
61 GURL
security_origin_as_gurl(security_origin
.toString());
62 scoped_refptr
<CdmSessionAdapter
> adapter(new CdmSessionAdapter());
64 // TODO(jrummell): Pass WebContentDecryptionModuleResult (or similar) to
65 // Initialize() so that more specific errors can be reported.
66 if (!adapter
->Initialize(cdm_factory
, key_system_ascii
,
67 security_origin_as_gurl
)) {
68 result
.completeWithError(
69 blink::WebContentDecryptionModuleExceptionNotSupportedError
, 0,
70 "Failed to initialize CDM.");
74 result
.completeWithContentDecryptionModule(
75 new WebContentDecryptionModuleImpl(adapter
));
78 WebContentDecryptionModuleImpl::WebContentDecryptionModuleImpl(
79 scoped_refptr
<CdmSessionAdapter
> adapter
)
83 WebContentDecryptionModuleImpl::~WebContentDecryptionModuleImpl() {
86 // The caller owns the created session.
87 blink::WebContentDecryptionModuleSession
*
88 WebContentDecryptionModuleImpl::createSession() {
89 return adapter_
->CreateSession();
92 void WebContentDecryptionModuleImpl::setServerCertificate(
93 const uint8
* server_certificate
,
94 size_t server_certificate_length
,
95 blink::WebContentDecryptionModuleResult result
) {
96 DCHECK(server_certificate
);
97 adapter_
->SetServerCertificate(
99 base::saturated_cast
<int>(server_certificate_length
),
100 scoped_ptr
<SimpleCdmPromise
>(
101 new CdmResultPromise
<>(result
, std::string())));
104 CdmContext
* WebContentDecryptionModuleImpl::GetCdmContext() {
105 return adapter_
->GetCdmContext();