Allow the externalfile scheme to be whitelisted as an allowed scheme for component...
[chromium-blink-merge.git] / media / blink / webcontentdecryptionmodule_impl.cc
blob9631dceaf7231793f8e3ffea3b80cd73772ef849
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 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)) {
36 NOTREACHED();
37 result.completeWithError(
38 blink::WebContentDecryptionModuleExceptionNotSupportedError, 0,
39 "Invalid keysystem.");
40 return;
43 std::string key_system_ascii = base::UTF16ToASCII(key_system);
44 if (!media::IsConcreteSupportedKeySystem(key_system_ascii)) {
45 std::string message =
46 "Keysystem '" + key_system_ascii + "' is not supported.";
47 result.completeWithError(
48 blink::WebContentDecryptionModuleExceptionNotSupportedError, 0,
49 blink::WebString::fromUTF8(message));
50 return;
53 // If unique security origin, don't try to create the CDM.
54 if (security_origin.isUnique() || security_origin.toString() == "null") {
55 result.completeWithError(
56 blink::WebContentDecryptionModuleExceptionNotSupportedError, 0,
57 "CDM use not allowed for unique security origin.");
58 return;
61 GURL security_origin_as_gurl(security_origin.toString());
62 scoped_refptr<CdmSessionAdapter> adapter(new CdmSessionAdapter());
64 // TODO(jrummell): Pass WebContentDecryptionModuleResult (or similar) to
65 // Initialize() so that more specific errors can be reported.
66 if (!adapter->Initialize(cdm_factory, key_system_ascii,
67 security_origin_as_gurl)) {
68 result.completeWithError(
69 blink::WebContentDecryptionModuleExceptionNotSupportedError, 0,
70 "Failed to initialize CDM.");
71 return;
74 result.completeWithContentDecryptionModule(
75 new WebContentDecryptionModuleImpl(adapter));
78 WebContentDecryptionModuleImpl::WebContentDecryptionModuleImpl(
79 scoped_refptr<CdmSessionAdapter> adapter)
80 : adapter_(adapter) {
83 WebContentDecryptionModuleImpl::~WebContentDecryptionModuleImpl() {
86 // The caller owns the created session.
87 blink::WebContentDecryptionModuleSession*
88 WebContentDecryptionModuleImpl::createSession() {
89 return adapter_->CreateSession();
92 void WebContentDecryptionModuleImpl::setServerCertificate(
93 const uint8* server_certificate,
94 size_t server_certificate_length,
95 blink::WebContentDecryptionModuleResult result) {
96 DCHECK(server_certificate);
97 adapter_->SetServerCertificate(
98 server_certificate,
99 base::saturated_cast<int>(server_certificate_length),
100 scoped_ptr<SimpleCdmPromise>(
101 new CdmResultPromise<>(result, std::string())));
104 CdmContext* WebContentDecryptionModuleImpl::GetCdmContext() {
105 return adapter_->GetCdmContext();
108 } // namespace media