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 "content/renderer/media/webcontentdecryptionmodulesession_impl.h"
8 #include "base/callback_helpers.h"
9 #include "base/logging.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "content/renderer/media/cdm_session_adapter.h"
13 #include "media/base/cdm_promise.h"
14 #include "third_party/WebKit/public/platform/WebURL.h"
18 WebContentDecryptionModuleSessionImpl::WebContentDecryptionModuleSessionImpl(
20 const scoped_refptr<CdmSessionAdapter>& adapter)
24 weak_ptr_factory_(this) {
27 WebContentDecryptionModuleSessionImpl::
28 ~WebContentDecryptionModuleSessionImpl() {
29 if (!web_session_id_.empty())
30 adapter_->RemoveSession(web_session_id_);
33 blink::WebString WebContentDecryptionModuleSessionImpl::sessionId() const {
34 return blink::WebString::fromUTF8(web_session_id_);
37 void WebContentDecryptionModuleSessionImpl::initializeNewSession(
38 const blink::WebString& init_data_type,
39 const uint8* init_data,
40 size_t init_data_length) {
41 // TODO(ddorwin): Guard against this in supported types check and remove this.
42 // Chromium only supports ASCII MIME types.
43 if (!base::IsStringASCII(init_data_type)) {
45 OnSessionError(media::MediaKeys::NOT_SUPPORTED_ERROR,
47 "The initialization data type " + init_data_type.utf8() +
48 " is not supported by the key system.");
52 std::string init_data_type_as_ascii = base::UTF16ToASCII(init_data_type);
53 DLOG_IF(WARNING, init_data_type_as_ascii.find('/') != std::string::npos)
54 << "init_data_type '" << init_data_type_as_ascii
55 << "' may be a MIME type";
57 scoped_ptr<media::NewSessionCdmPromise> promise(
58 new media::NewSessionCdmPromise(
59 base::Bind(&WebContentDecryptionModuleSessionImpl::SessionCreated,
60 weak_ptr_factory_.GetWeakPtr()),
61 base::Bind(&WebContentDecryptionModuleSessionImpl::OnSessionError,
62 weak_ptr_factory_.GetWeakPtr())));
63 adapter_->InitializeNewSession(init_data_type_as_ascii,
66 media::MediaKeys::TEMPORARY_SESSION,
70 void WebContentDecryptionModuleSessionImpl::update(const uint8* response,
71 size_t response_length) {
73 scoped_ptr<media::SimpleCdmPromise> promise(new media::SimpleCdmPromise(
74 base::Bind(&WebContentDecryptionModuleSessionImpl::OnSessionReady,
75 weak_ptr_factory_.GetWeakPtr()),
76 base::Bind(&WebContentDecryptionModuleSessionImpl::OnSessionError,
77 weak_ptr_factory_.GetWeakPtr())));
78 adapter_->UpdateSession(
79 web_session_id_, response, response_length, promise.Pass());
82 void WebContentDecryptionModuleSessionImpl::release() {
83 scoped_ptr<media::SimpleCdmPromise> promise(new media::SimpleCdmPromise(
84 base::Bind(&WebContentDecryptionModuleSessionImpl::OnSessionClosed,
85 weak_ptr_factory_.GetWeakPtr()),
86 base::Bind(&WebContentDecryptionModuleSessionImpl::OnSessionError,
87 weak_ptr_factory_.GetWeakPtr())));
88 adapter_->ReleaseSession(web_session_id_, promise.Pass());
91 void WebContentDecryptionModuleSessionImpl::OnSessionMessage(
92 const std::vector<uint8>& message,
93 const GURL& destination_url) {
95 message.empty() ? NULL : &message[0], message.size(), destination_url);
98 void WebContentDecryptionModuleSessionImpl::OnSessionReady() {
102 void WebContentDecryptionModuleSessionImpl::OnSessionClosed() {
109 void WebContentDecryptionModuleSessionImpl::OnSessionError(
110 media::MediaKeys::Exception exception_code,
112 const std::string& error_message) {
113 // Convert |exception_code| back to MediaKeyErrorCode if possible.
114 // TODO(jrummell): Update this conversion when promises flow
115 // back into blink:: (as blink:: will have its own error definition).
116 switch (exception_code) {
117 case media::MediaKeys::CLIENT_ERROR:
118 client_->error(Client::MediaKeyErrorCodeClient, system_code);
121 // This will include all other CDM4 errors and any error generated
123 client_->error(Client::MediaKeyErrorCodeUnknown, system_code);
128 void WebContentDecryptionModuleSessionImpl::SessionCreated(
129 const std::string& web_session_id) {
130 DCHECK(web_session_id_.empty()) << "Session ID may not be changed once set.";
131 web_session_id_ = web_session_id;
132 adapter_->RegisterSession(web_session_id_, weak_ptr_factory_.GetWeakPtr());
135 } // namespace content