Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / media / blink / webcontentdecryptionmodule_impl.cc
blob1681b86b8b6516f20b04064eac3b54b92d7b8380
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 const blink::WebSecurityOrigin& security_origin,
29 const CdmConfig& cdm_config,
30 scoped_ptr<blink::WebContentDecryptionModuleResult> result) {
31 DCHECK(!security_origin.isNull());
32 DCHECK(!key_system.empty());
34 // TODO(ddorwin): Guard against this in supported types check and remove this.
35 // Chromium only supports ASCII key systems.
36 if (!base::IsStringASCII(key_system)) {
37 NOTREACHED();
38 result->completeWithError(
39 blink::WebContentDecryptionModuleExceptionNotSupportedError, 0,
40 "Invalid keysystem.");
41 return;
44 // TODO(ddorwin): This should be a DCHECK.
45 std::string key_system_ascii = base::UTF16ToASCII(key_system);
46 if (!media::IsSupportedKeySystem(key_system_ascii)) {
47 std::string message =
48 "Keysystem '" + key_system_ascii + "' is not supported.";
49 result->completeWithError(
50 blink::WebContentDecryptionModuleExceptionNotSupportedError, 0,
51 blink::WebString::fromUTF8(message));
52 return;
55 // If unique security origin, don't try to create the CDM.
56 if (security_origin.isUnique() || security_origin.toString() == "null") {
57 result->completeWithError(
58 blink::WebContentDecryptionModuleExceptionNotSupportedError, 0,
59 "EME use is not allowed on unique origins.");
60 return;
63 GURL security_origin_as_gurl(security_origin.toString());
65 // CdmSessionAdapter::CreateCdm() will keep a reference to |adapter|. Then
66 // if WebContentDecryptionModuleImpl is successfully created (returned in
67 // |result|), it will keep a reference to |adapter|. Otherwise, |adapter| will
68 // be destructed.
69 scoped_refptr<CdmSessionAdapter> adapter(new CdmSessionAdapter());
70 adapter->CreateCdm(cdm_factory, key_system_ascii, security_origin_as_gurl,
71 cdm_config, result.Pass());
74 WebContentDecryptionModuleImpl::WebContentDecryptionModuleImpl(
75 scoped_refptr<CdmSessionAdapter> adapter)
76 : adapter_(adapter) {
79 WebContentDecryptionModuleImpl::~WebContentDecryptionModuleImpl() {
82 // The caller owns the created session.
83 blink::WebContentDecryptionModuleSession*
84 WebContentDecryptionModuleImpl::createSession() {
85 return adapter_->CreateSession();
88 void WebContentDecryptionModuleImpl::setServerCertificate(
89 const uint8* server_certificate,
90 size_t server_certificate_length,
91 blink::WebContentDecryptionModuleResult result) {
92 DCHECK(server_certificate);
93 adapter_->SetServerCertificate(
94 std::vector<uint8>(server_certificate,
95 server_certificate + server_certificate_length),
96 scoped_ptr<SimpleCdmPromise>(
97 new CdmResultPromise<>(result, std::string())));
100 CdmContext* WebContentDecryptionModuleImpl::GetCdmContext() {
101 return adapter_->GetCdmContext();
104 } // namespace media