Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / media / blink / webcontentdecryptionmodule_impl.cc
blob7bdd9c9ee3082e929491d714fd113827bd90fb4e
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"
8 #include "base/bind.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"
21 #include "url/gurl.h"
23 namespace media {
25 void WebContentDecryptionModuleImpl::Create(
26 media::CdmFactory* cdm_factory,
27 const base::string16& key_system,
28 bool allow_distinctive_identifier,
29 bool allow_persistent_state,
30 const blink::WebSecurityOrigin& security_origin,
31 blink::WebContentDecryptionModuleResult result) {
32 DCHECK(!security_origin.isNull());
33 DCHECK(!key_system.empty());
35 // TODO(ddorwin): Guard against this in supported types check and remove this.
36 // Chromium only supports ASCII key systems.
37 if (!base::IsStringASCII(key_system)) {
38 NOTREACHED();
39 result.completeWithError(
40 blink::WebContentDecryptionModuleExceptionNotSupportedError, 0,
41 "Invalid keysystem.");
42 return;
45 // TODO(ddorwin): This should be a DCHECK.
46 std::string key_system_ascii = base::UTF16ToASCII(key_system);
47 if (!media::IsSupportedKeySystem(key_system_ascii)) {
48 std::string message =
49 "Keysystem '" + key_system_ascii + "' is not supported.";
50 result.completeWithError(
51 blink::WebContentDecryptionModuleExceptionNotSupportedError, 0,
52 blink::WebString::fromUTF8(message));
53 return;
56 // If unique security origin, don't try to create the CDM.
57 if (security_origin.isUnique() || security_origin.toString() == "null") {
58 result.completeWithError(
59 blink::WebContentDecryptionModuleExceptionNotSupportedError, 0,
60 "CDM use not allowed for unique security origin.");
61 return;
64 GURL security_origin_as_gurl(security_origin.toString());
66 // CdmSessionAdapter::CreateCdm() will keep a reference to |adapter|. Then
67 // if WebContentDecryptionModuleImpl is successfully created (returned in
68 // |result|), it will keep a reference to |adapter|. Otherwise, |adapter| will
69 // be destructed.
70 scoped_refptr<CdmSessionAdapter> adapter(new CdmSessionAdapter());
71 adapter->CreateCdm(cdm_factory, key_system_ascii,
72 allow_distinctive_identifier, allow_persistent_state,
73 security_origin_as_gurl, result);
76 WebContentDecryptionModuleImpl::WebContentDecryptionModuleImpl(
77 scoped_refptr<CdmSessionAdapter> adapter)
78 : adapter_(adapter) {
81 WebContentDecryptionModuleImpl::~WebContentDecryptionModuleImpl() {
84 // The caller owns the created session.
85 blink::WebContentDecryptionModuleSession*
86 WebContentDecryptionModuleImpl::createSession() {
87 return adapter_->CreateSession();
90 void WebContentDecryptionModuleImpl::setServerCertificate(
91 const uint8* server_certificate,
92 size_t server_certificate_length,
93 blink::WebContentDecryptionModuleResult result) {
94 DCHECK(server_certificate);
95 adapter_->SetServerCertificate(
96 server_certificate,
97 base::saturated_cast<int>(server_certificate_length),
98 scoped_ptr<SimpleCdmPromise>(
99 new CdmResultPromise<>(result, std::string())));
102 CdmContext* WebContentDecryptionModuleImpl::GetCdmContext() {
103 return adapter_->GetCdmContext();
106 } // namespace media