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.
4
5#include "content/renderer/media/webcontentdecryptionmodule_impl.h"
6
7#include <map>
8#include <vector>
9
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"
23#include "url/gurl.h"
24
25#if defined(ENABLE_PEPPER_CDMS)
26#include "content/renderer/media/crypto/pepper_cdm_wrapper_impl.h"
27#endif
28
29namespace content {
30
31WebContentDecryptionModuleImpl* WebContentDecryptionModuleImpl::Create(
32#if defined(ENABLE_PEPPER_CDMS)
33    blink::WebLocalFrame* frame,
34#elif defined(ENABLE_BROWSER_CDMS)
35    RendererCdmManager* manager,
36#endif
37    const blink::WebSecurityOrigin& security_origin,
38    const base::string16& key_system) {
39#if defined(ENABLE_PEPPER_CDMS)
40  DCHECK(frame);
41#elif defined(ENABLE_BROWSER_CDMS)
42  DCHECK(manager);
43#endif
44  DCHECK(!security_origin.isNull());
45  DCHECK(!key_system.empty());
46
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)) {
50    NOTREACHED();
51    return NULL;
52  }
53
54  std::string key_system_ascii = base::UTF16ToASCII(key_system);
55  if (!IsConcreteSupportedKeySystem(key_system_ascii))
56    return NULL;
57
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.";
61    return NULL;
62  }
63
64  scoped_refptr<CdmSessionAdapter> adapter(new CdmSessionAdapter());
65  GURL security_origin_as_gurl(security_origin.toString());
66
67  if (!adapter->Initialize(
68#if defined(ENABLE_PEPPER_CDMS)
69          base::Bind(&PepperCdmWrapperImpl::Create, frame),
70#elif defined(ENABLE_BROWSER_CDMS)
71          manager,
72#endif
73          key_system_ascii,
74          security_origin_as_gurl)) {
75    return NULL;
76  }
77
78  return new WebContentDecryptionModuleImpl(adapter);
79}
80
81WebContentDecryptionModuleImpl::WebContentDecryptionModuleImpl(
82    scoped_refptr<CdmSessionAdapter> adapter)
83    : adapter_(adapter) {
84}
85
86WebContentDecryptionModuleImpl::~WebContentDecryptionModuleImpl() {
87}
88
89// The caller owns the created session.
90blink::WebContentDecryptionModuleSession*
91WebContentDecryptionModuleImpl::createSession() {
92  return adapter_->CreateSession();
93}
94
95blink::WebContentDecryptionModuleSession*
96WebContentDecryptionModuleImpl::createSession(
97    blink::WebContentDecryptionModuleSession::Client* client) {
98  WebContentDecryptionModuleSessionImpl* session = adapter_->CreateSession();
99  session->setClientInterface(client);
100  return session;
101}
102
103void WebContentDecryptionModuleImpl::setServerCertificate(
104    const uint8* server_certificate,
105    size_t server_certificate_length,
106    blink::WebContentDecryptionModuleResult result) {
107  DCHECK(server_certificate);
108  adapter_->SetServerCertificate(
109      server_certificate,
110      server_certificate_length,
111      scoped_ptr<media::SimpleCdmPromise>(new SimpleCdmResultPromise(result)));
112}
113
114media::Decryptor* WebContentDecryptionModuleImpl::GetDecryptor() {
115  return adapter_->GetDecryptor();
116}
117
118#if defined(ENABLE_BROWSER_CDMS)
119int WebContentDecryptionModuleImpl::GetCdmId() const {
120  return adapter_->GetCdmId();
121}
122#endif  // defined(ENABLE_BROWSER_CDMS)
123
124}  // namespace content
125