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 "content/renderer/media/webcontentdecryptionmodule_impl.h"
10 #include "base/basictypes.h"
11 #include "base/bind.h"
12 #include "base/logging.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "content/renderer/media/cdm_result_promise.h"
16 #include "content/renderer/media/cdm_session_adapter.h"
17 #include "content/renderer/media/crypto/key_systems.h"
18 #include "content/renderer/media/webcontentdecryptionmodulesession_impl.h"
19 #include "media/base/cdm_promise.h"
20 #include "media/base/media_keys.h"
21 #include "third_party/WebKit/public/platform/WebString.h"
22 #include "third_party/WebKit/public/web/WebSecurityOrigin.h"
25 #if defined(ENABLE_PEPPER_CDMS)
26 #include "content/renderer/media/crypto/pepper_cdm_wrapper_impl.h"
31 WebContentDecryptionModuleImpl* WebContentDecryptionModuleImpl::Create(
32 #if defined(ENABLE_PEPPER_CDMS)
33 blink::WebLocalFrame* frame,
34 #elif defined(ENABLE_BROWSER_CDMS)
35 RendererCdmManager* manager,
37 const blink::WebSecurityOrigin& security_origin,
38 const base::string16& key_system) {
39 #if defined(ENABLE_PEPPER_CDMS)
41 #elif defined(ENABLE_BROWSER_CDMS)
44 DCHECK(!security_origin.isNull());
45 DCHECK(!key_system.empty());
47 // TODO(ddorwin): Guard against this in supported types check and remove this.
48 // Chromium only supports ASCII key systems.
49 if (!base::IsStringASCII(key_system)) {
54 std::string key_system_ascii = base::UTF16ToASCII(key_system);
55 if (!IsConcreteSupportedKeySystem(key_system_ascii))
58 // If unique security origin, don't try to create the CDM.
59 if (security_origin.isUnique() || security_origin.toString() == "null") {
60 DLOG(ERROR) << "CDM use not allowed for unique security origin.";
64 scoped_refptr<CdmSessionAdapter> adapter(new CdmSessionAdapter());
65 GURL security_origin_as_gurl(security_origin.toString());
67 if (!adapter->Initialize(
68 #if defined(ENABLE_PEPPER_CDMS)
69 base::Bind(&PepperCdmWrapperImpl::Create, frame),
70 #elif defined(ENABLE_BROWSER_CDMS)
74 security_origin_as_gurl)) {
78 return new WebContentDecryptionModuleImpl(adapter);
81 WebContentDecryptionModuleImpl::WebContentDecryptionModuleImpl(
82 scoped_refptr<CdmSessionAdapter> adapter)
86 WebContentDecryptionModuleImpl::~WebContentDecryptionModuleImpl() {
89 // The caller owns the created session.
90 blink::WebContentDecryptionModuleSession*
91 WebContentDecryptionModuleImpl::createSession() {
92 return adapter_->CreateSession();
95 blink::WebContentDecryptionModuleSession*
96 WebContentDecryptionModuleImpl::createSession(
97 blink::WebContentDecryptionModuleSession::Client* client) {
98 WebContentDecryptionModuleSessionImpl* session = adapter_->CreateSession();
99 session->setClientInterface(client);
103 void WebContentDecryptionModuleImpl::setServerCertificate(
104 const uint8* server_certificate,
105 size_t server_certificate_length,
106 blink::WebContentDecryptionModuleResult result) {
107 DCHECK(server_certificate);
108 adapter_->SetServerCertificate(
110 server_certificate_length,
111 scoped_ptr<media::SimpleCdmPromise>(new SimpleCdmResultPromise(result)));
114 media::Decryptor* WebContentDecryptionModuleImpl::GetDecryptor() {
115 return adapter_->GetDecryptor();
118 #if defined(ENABLE_BROWSER_CDMS)
119 int WebContentDecryptionModuleImpl::GetCdmId() const {
120 return adapter_->GetCdmId();
122 #endif // defined(ENABLE_BROWSER_CDMS)
124 } // namespace content