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 // TODO(ddorwin): This should be a DCHECK.
44 std::string key_system_ascii
= base::UTF16ToASCII(key_system
);
45 if (!media::IsSupportedKeySystem(key_system_ascii
)) {
47 "Keysystem '" + key_system_ascii
+ "' is not supported.";
48 result
.completeWithError(
49 blink::WebContentDecryptionModuleExceptionNotSupportedError
, 0,
50 blink::WebString::fromUTF8(message
));
54 // If unique security origin, don't try to create the CDM.
55 if (security_origin
.isUnique() || security_origin
.toString() == "null") {
56 result
.completeWithError(
57 blink::WebContentDecryptionModuleExceptionNotSupportedError
, 0,
58 "CDM use not allowed for unique security origin.");
62 GURL
security_origin_as_gurl(security_origin
.toString());
63 scoped_refptr
<CdmSessionAdapter
> adapter(new CdmSessionAdapter());
65 // TODO(jrummell): Pass WebContentDecryptionModuleResult (or similar) to
66 // Initialize() so that more specific errors can be reported.
67 if (!adapter
->Initialize(cdm_factory
, key_system_ascii
,
68 security_origin_as_gurl
)) {
69 result
.completeWithError(
70 blink::WebContentDecryptionModuleExceptionNotSupportedError
, 0,
71 "Failed to initialize CDM.");
75 result
.completeWithContentDecryptionModule(
76 new WebContentDecryptionModuleImpl(adapter
));
79 WebContentDecryptionModuleImpl::WebContentDecryptionModuleImpl(
80 scoped_refptr
<CdmSessionAdapter
> adapter
)
84 WebContentDecryptionModuleImpl::~WebContentDecryptionModuleImpl() {
87 // The caller owns the created session.
88 blink::WebContentDecryptionModuleSession
*
89 WebContentDecryptionModuleImpl::createSession() {
90 return adapter_
->CreateSession();
93 void WebContentDecryptionModuleImpl::setServerCertificate(
94 const uint8
* server_certificate
,
95 size_t server_certificate_length
,
96 blink::WebContentDecryptionModuleResult result
) {
97 DCHECK(server_certificate
);
98 adapter_
->SetServerCertificate(
100 base::saturated_cast
<int>(server_certificate_length
),
101 scoped_ptr
<SimpleCdmPromise
>(
102 new CdmResultPromise
<>(result
, std::string())));
105 CdmContext
* WebContentDecryptionModuleImpl::GetCdmContext() {
106 return adapter_
->GetCdmContext();