summaryrefslogtreecommitdiffstats
path: root/media/blink/webcontentdecryptionmodule_impl.cc
blob: 9d54f682a940cc1f12f8d1df137f999f57f83e0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "webcontentdecryptionmodule_impl.h"

#include "base/basictypes.h"
#include "base/bind.h"
#include "base/logging.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "media/base/cdm_promise.h"
#include "media/base/key_systems.h"
#include "media/base/media_keys.h"
#include "media/blink/cdm_result_promise.h"
#include "media/blink/cdm_session_adapter.h"
#include "media/blink/webcontentdecryptionmodulesession_impl.h"
#include "third_party/WebKit/public/platform/WebString.h"
#include "third_party/WebKit/public/web/WebSecurityOrigin.h"
#include "url/gurl.h"

namespace media {

// TODO(jrummell): Remove once WebContentDecryptionModuleResult always passed.
WebContentDecryptionModuleImpl* WebContentDecryptionModuleImpl::Create(
    CdmFactory* cdm_factory,
    const blink::WebSecurityOrigin& security_origin,
    const base::string16& key_system) {
  DCHECK(!security_origin.isNull());
  DCHECK(!key_system.empty());

  // TODO(ddorwin): Guard against this in supported types check and remove this.
  // Chromium only supports ASCII key systems.
  if (!base::IsStringASCII(key_system)) {
    NOTREACHED();
    return NULL;
  }

  std::string key_system_ascii = base::UTF16ToASCII(key_system);
  if (!IsConcreteSupportedKeySystem(key_system_ascii))
    return NULL;

  // If unique security origin, don't try to create the CDM.
  if (security_origin.isUnique() || security_origin.toString() == "null") {
    DLOG(ERROR) << "CDM use not allowed for unique security origin.";
    return NULL;
  }

  scoped_refptr<CdmSessionAdapter> adapter(new CdmSessionAdapter());
  GURL security_origin_as_gurl(security_origin.toString());

  if (!adapter->Initialize(
          cdm_factory, key_system_ascii, security_origin_as_gurl)) {
    return NULL;
  }

  return new WebContentDecryptionModuleImpl(adapter);
}

void WebContentDecryptionModuleImpl::Create(
    media::CdmFactory* cdm_factory,
    const blink::WebSecurityOrigin& security_origin,
    const base::string16& key_system,
    blink::WebContentDecryptionModuleResult result) {
  DCHECK(!security_origin.isNull());
  DCHECK(!key_system.empty());

  // TODO(ddorwin): Guard against this in supported types check and remove this.
  // Chromium only supports ASCII key systems.
  if (!base::IsStringASCII(key_system)) {
    NOTREACHED();
    result.completeWithError(
        blink::WebContentDecryptionModuleExceptionNotSupportedError, 0,
        "Invalid keysystem.");
    return;
  }

  std::string key_system_ascii = base::UTF16ToASCII(key_system);
  if (!media::IsConcreteSupportedKeySystem(key_system_ascii)) {
    std::string message =
        "Keysystem '" + key_system_ascii + "' is not supported.";
    result.completeWithError(
        blink::WebContentDecryptionModuleExceptionNotSupportedError, 0,
        blink::WebString::fromUTF8(message));
    return;
  }

  // If unique security origin, don't try to create the CDM.
  if (security_origin.isUnique() || security_origin.toString() == "null") {
    result.completeWithError(
        blink::WebContentDecryptionModuleExceptionNotSupportedError, 0,
        "CDM use not allowed for unique security origin.");
    return;
  }

  GURL security_origin_as_gurl(security_origin.toString());
  scoped_refptr<CdmSessionAdapter> adapter(new CdmSessionAdapter());

  // TODO(jrummell): Pass WebContentDecryptionModuleResult (or similar) to
  // Initialize() so that more specific errors can be reported.
  if (!adapter->Initialize(cdm_factory, key_system_ascii,
                           security_origin_as_gurl)) {
    result.completeWithError(
        blink::WebContentDecryptionModuleExceptionNotSupportedError, 0,
        "Failed to initialize CDM.");
    return;
  }

  result.completeWithContentDecryptionModule(
      new WebContentDecryptionModuleImpl(adapter));
}

WebContentDecryptionModuleImpl::WebContentDecryptionModuleImpl(
    scoped_refptr<CdmSessionAdapter> adapter)
    : adapter_(adapter) {
}

WebContentDecryptionModuleImpl::~WebContentDecryptionModuleImpl() {
}

// The caller owns the created session.
blink::WebContentDecryptionModuleSession*
WebContentDecryptionModuleImpl::createSession() {
  return adapter_->CreateSession();
}

blink::WebContentDecryptionModuleSession*
WebContentDecryptionModuleImpl::createSession(
    blink::WebContentDecryptionModuleSession::Client* client) {
  WebContentDecryptionModuleSessionImpl* session = adapter_->CreateSession();
  session->setClientInterface(client);
  return session;
}

void WebContentDecryptionModuleImpl::setServerCertificate(
    const uint8* server_certificate,
    size_t server_certificate_length,
    blink::WebContentDecryptionModuleResult result) {
  DCHECK(server_certificate);
  adapter_->SetServerCertificate(
      server_certificate,
      base::saturated_cast<int>(server_certificate_length),
      scoped_ptr<SimpleCdmPromise>(
          new CdmResultPromise<>(result, std::string())));
}

CdmContext* WebContentDecryptionModuleImpl::GetCdmContext() {
  return adapter_->GetCdmContext();
}

}  // namespace media