Commit f42e6e2d authored by jrummell@chromium.org's avatar jrummell@chromium.org

Move SessionIdAdapter out of WebContentDecryptionModuleImpl

In order to manage the lifetime of the CDM, have WebContentDecryptionModuleImpl
and WebContentDecryptionModuleSessionImpl both keep RefPtrs to SessionIdAdapter
(now renamed CdmSessionAdapter). CdmSessionAdapter owns the MediaKeys object,
so the CDM gets destroyed when there is no more need for it.

BUG=341567
TEST=EME content tests pass

Review URL: https://codereview.chromium.org/171073002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@252226 0039d316-1c4b-4281-b951-d872f2087c98
parent 8a9990c9
......@@ -225,6 +225,8 @@
'renderer/media/buffered_resource_loader.h',
'renderer/media/cache_util.cc',
'renderer/media/cache_util.h',
'renderer/media/cdm_session_adapter.cc',
'renderer/media/cdm_session_adapter.h',
'renderer/media/crypto/content_decryption_module_factory.cc',
'renderer/media/crypto/content_decryption_module_factory.h',
'renderer/media/crypto/key_systems.cc',
......
// Copyright 2014 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 "content/renderer/media/cdm_session_adapter.h"
#include "base/bind.h"
#include "base/logging.h"
#include "base/memory/weak_ptr.h"
#include "content/renderer/media/crypto/content_decryption_module_factory.h"
#include "content/renderer/media/webcontentdecryptionmodulesession_impl.h"
#include "media/base/media_keys.h"
#include "url/gurl.h"
namespace content {
const uint32 kStartingSessionId = 1;
uint32 CdmSessionAdapter::next_session_id_ = kStartingSessionId;
COMPILE_ASSERT(kStartingSessionId > media::MediaKeys::kInvalidSessionId,
invalid_starting_value);
CdmSessionAdapter::CdmSessionAdapter() : weak_ptr_factory_(this) {}
CdmSessionAdapter::~CdmSessionAdapter() {}
bool CdmSessionAdapter::Initialize(const std::string& key_system) {
base::WeakPtr<CdmSessionAdapter> weak_this = weak_ptr_factory_.GetWeakPtr();
media_keys_ =
ContentDecryptionModuleFactory::Create(
// TODO(ddorwin): Address lower in the stack: http://crbug.com/252065
"webkit-" + key_system,
#if defined(ENABLE_PEPPER_CDMS)
// TODO(ddorwin): Support Pepper-based CDMs: http://crbug.com/250049
NULL,
NULL,
base::Closure(),
#elif defined(OS_ANDROID)
// TODO(xhwang): Support Android.
NULL,
0,
// TODO(ddorwin): Get the URL for the frame containing the MediaKeys.
GURL(),
#endif // defined(ENABLE_PEPPER_CDMS)
base::Bind(&CdmSessionAdapter::OnSessionCreated, weak_this),
base::Bind(&CdmSessionAdapter::OnSessionMessage, weak_this),
base::Bind(&CdmSessionAdapter::OnSessionReady, weak_this),
base::Bind(&CdmSessionAdapter::OnSessionClosed, weak_this),
base::Bind(&CdmSessionAdapter::OnSessionError, weak_this));
// Success if |media_keys_| created.
return media_keys_;
}
WebContentDecryptionModuleSessionImpl* CdmSessionAdapter::CreateSession(
blink::WebContentDecryptionModuleSession::Client* client) {
// Generate a unique internal session id for the new session.
uint32 session_id = next_session_id_++;
DCHECK(sessions_.find(session_id) == sessions_.end());
WebContentDecryptionModuleSessionImpl* session =
new WebContentDecryptionModuleSessionImpl(session_id, client, this);
sessions_[session_id] = session;
return session;
}
void CdmSessionAdapter::RemoveSession(uint32 session_id) {
DCHECK(sessions_.find(session_id) != sessions_.end());
sessions_.erase(session_id);
}
void CdmSessionAdapter::InitializeNewSession(uint32 session_id,
const std::string& content_type,
const uint8* init_data,
int init_data_length) {
DCHECK(sessions_.find(session_id) != sessions_.end());
media_keys_->CreateSession(
session_id, content_type, init_data, init_data_length);
}
void CdmSessionAdapter::UpdateSession(uint32 session_id,
const uint8* response,
int response_length) {
DCHECK(sessions_.find(session_id) != sessions_.end());
media_keys_->UpdateSession(session_id, response, response_length);
}
void CdmSessionAdapter::ReleaseSession(uint32 session_id) {
DCHECK(sessions_.find(session_id) != sessions_.end());
media_keys_->ReleaseSession(session_id);
}
media::Decryptor* CdmSessionAdapter::GetDecryptor() {
return media_keys_->GetDecryptor();
}
void CdmSessionAdapter::OnSessionCreated(uint32 session_id,
const std::string& web_session_id) {
WebContentDecryptionModuleSessionImpl* session = GetSession(session_id);
DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session "
<< session_id;
if (session)
session->OnSessionCreated(web_session_id);
}
void CdmSessionAdapter::OnSessionMessage(uint32 session_id,
const std::vector<uint8>& message,
const std::string& destination_url) {
WebContentDecryptionModuleSessionImpl* session = GetSession(session_id);
DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session "
<< session_id;
if (session)
session->OnSessionMessage(message, destination_url);
}
void CdmSessionAdapter::OnSessionReady(uint32 session_id) {
WebContentDecryptionModuleSessionImpl* session = GetSession(session_id);
DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session "
<< session_id;
if (session)
session->OnSessionReady();
}
void CdmSessionAdapter::OnSessionClosed(uint32 session_id) {
WebContentDecryptionModuleSessionImpl* session = GetSession(session_id);
DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session "
<< session_id;
if (session)
session->OnSessionClosed();
}
void CdmSessionAdapter::OnSessionError(uint32 session_id,
media::MediaKeys::KeyError error_code,
int system_code) {
WebContentDecryptionModuleSessionImpl* session = GetSession(session_id);
DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session "
<< session_id;
if (session)
session->OnSessionError(error_code, system_code);
}
WebContentDecryptionModuleSessionImpl* CdmSessionAdapter::GetSession(
uint32 session_id) {
// Since session objects may get garbage collected, it is possible that there
// are events coming back from the CDM and the session has been unregistered.
// We can not tell if the CDM is firing events at sessions that never existed.
SessionMap::iterator session = sessions_.find(session_id);
return (session != sessions_.end()) ? session->second : NULL;
}
} // namespace content
// Copyright 2014 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.
#ifndef CONTENT_RENDERER_MEDIA_CDM_SESSION_ADAPTER_H_
#define CONTENT_RENDERER_MEDIA_CDM_SESSION_ADAPTER_H_
#include <map>
#include "base/basictypes.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "media/base/media_keys.h"
#include "third_party/WebKit/public/platform/WebContentDecryptionModuleSession.h"
namespace content {
class WebContentDecryptionModuleSessionImpl;
// Owns the CDM instance and makes calls from session objects to the CDM.
// Forwards the session ID-based callbacks of the MediaKeys interface to the
// appropriate session object. Callers should hold references to this class
// as long as they need the CDM instance.
class CdmSessionAdapter : public base::RefCounted<CdmSessionAdapter> {
public:
CdmSessionAdapter();
// Returns true on success.
bool Initialize(const std::string& key_system);
// Creates a new session and adds it to the internal map. The caller owns the
// created session. RemoveSession() must be called when destroying it.
WebContentDecryptionModuleSessionImpl* CreateSession(
blink::WebContentDecryptionModuleSession::Client* client);
// Removes a session from the internal map.
void RemoveSession(uint32 session_id);
// Initializes the session specified by |session_id| with the |content_type|
// and |init_data| provided.
void InitializeNewSession(uint32 session_id,
const std::string& content_type,
const uint8* init_data,
int init_data_length);
// Updates the session specified by |session_id| with |response|.
void UpdateSession(uint32 session_id,
const uint8* response,
int response_length);
// Releases the session specified by |session_id|.
void ReleaseSession(uint32 session_id);
// Returns the Decryptor associated with this CDM. May be NULL if no
// Decryptor is associated with the MediaKeys object.
// TODO(jrummell): Figure out lifetimes, as WMPI may still use the decryptor
// after WebContentDecryptionModule is freed. http://crbug.com/330324
media::Decryptor* GetDecryptor();
private:
friend class base::RefCounted<CdmSessionAdapter>;
typedef std::map<uint32, WebContentDecryptionModuleSessionImpl*> SessionMap;
~CdmSessionAdapter();
// Callbacks for firing session events.
void OnSessionCreated(uint32 session_id, const std::string& web_session_id);
void OnSessionMessage(uint32 session_id,
const std::vector<uint8>& message,
const std::string& destination_url);
void OnSessionReady(uint32 session_id);
void OnSessionClosed(uint32 session_id);
void OnSessionError(uint32 session_id,
media::MediaKeys::KeyError error_code,
int system_code);
// Helper function of the callbacks.
WebContentDecryptionModuleSessionImpl* GetSession(uint32 session_id);
scoped_ptr<media::MediaKeys> media_keys_;
base::WeakPtrFactory<CdmSessionAdapter> weak_ptr_factory_;
SessionMap sessions_;
// Session ID should be unique per renderer process for debugging purposes.
static uint32 next_session_id_;
DISALLOW_COPY_AND_ASSIGN(CdmSessionAdapter);
};
} // namespace content
#endif // CONTENT_RENDERER_MEDIA_CDM_SESSION_ADAPTER_H_
......@@ -8,163 +8,16 @@
#include <vector>
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/logging.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/string_util.h"
#include "content/renderer/media/crypto/content_decryption_module_factory.h"
#include "content/renderer/media/cdm_session_adapter.h"
#include "content/renderer/media/webcontentdecryptionmodulesession_impl.h"
#include "media/base/media_keys.h"
#include "url/gurl.h"
namespace content {
// Forwards the session ID-based callbacks of the MediaKeys interface to the
// appropriate session object.
class SessionIdAdapter {
public:
SessionIdAdapter();
~SessionIdAdapter();
// On success, creates a MediaKeys, returns it in |media_keys|, returns true.
bool Initialize(const std::string& key_system,
scoped_ptr<media::MediaKeys>* media_keys);
// Generates a unique internal session id.
uint32 GenerateSessionId();
// Adds a session to the internal map. Does not take ownership of the session.
void AddSession(uint32 session_id,
WebContentDecryptionModuleSessionImpl* session);
// Removes a session from the internal map.
void RemoveSession(uint32 session_id);
private:
typedef std::map<uint32, WebContentDecryptionModuleSessionImpl*> SessionMap;
// Callbacks for firing session events.
void OnSessionCreated(uint32 session_id, const std::string& web_session_id);
void OnSessionMessage(uint32 session_id,
const std::vector<uint8>& message,
const std::string& destination_url);
void OnSessionReady(uint32 session_id);
void OnSessionClosed(uint32 session_id);
void OnSessionError(uint32 session_id,
media::MediaKeys::KeyError error_code,
int system_code);
// Helper function of the callbacks.
WebContentDecryptionModuleSessionImpl* GetSession(uint32 session_id);
base::WeakPtrFactory<SessionIdAdapter> weak_ptr_factory_;
SessionMap sessions_;
// Session ID should be unique per renderer process for debugging purposes.
static uint32 next_session_id_;
DISALLOW_COPY_AND_ASSIGN(SessionIdAdapter);
};
const uint32 kStartingSessionId = 1;
uint32 SessionIdAdapter::next_session_id_ = kStartingSessionId;
COMPILE_ASSERT(kStartingSessionId > media::MediaKeys::kInvalidSessionId,
invalid_starting_value);
SessionIdAdapter::SessionIdAdapter()
: weak_ptr_factory_(this) {
}
SessionIdAdapter::~SessionIdAdapter() {
}
bool SessionIdAdapter::Initialize(const std::string& key_system,
scoped_ptr<media::MediaKeys>* media_keys) {
DCHECK(media_keys);
DCHECK(!*media_keys);
base::WeakPtr<SessionIdAdapter> weak_this = weak_ptr_factory_.GetWeakPtr();
scoped_ptr<media::MediaKeys> created_media_keys =
ContentDecryptionModuleFactory::Create(
// TODO(ddorwin): Address lower in the stack: http://crbug.com/252065
"webkit-" + key_system,
#if defined(ENABLE_PEPPER_CDMS)
// TODO(ddorwin): Support Pepper-based CDMs: http://crbug.com/250049
NULL,
NULL,
base::Closure(),
#elif defined(OS_ANDROID)
// TODO(xhwang): Support Android.
NULL,
0,
// TODO(ddorwin): Get the URL for the frame containing the MediaKeys.
GURL(),
#endif // defined(ENABLE_PEPPER_CDMS)
base::Bind(&SessionIdAdapter::OnSessionCreated, weak_this),
base::Bind(&SessionIdAdapter::OnSessionMessage, weak_this),
base::Bind(&SessionIdAdapter::OnSessionReady, weak_this),
base::Bind(&SessionIdAdapter::OnSessionClosed, weak_this),
base::Bind(&SessionIdAdapter::OnSessionError, weak_this));
if (!created_media_keys)
return false;
*media_keys = created_media_keys.Pass();
return true;
}
uint32 SessionIdAdapter::GenerateSessionId() {
return next_session_id_++;
}
void SessionIdAdapter::AddSession(
uint32 session_id,
WebContentDecryptionModuleSessionImpl* session) {
DCHECK(sessions_.find(session_id) == sessions_.end());
sessions_[session_id] = session;
}
void SessionIdAdapter::RemoveSession(uint32 session_id) {
DCHECK(sessions_.find(session_id) != sessions_.end());
sessions_.erase(session_id);
}
void SessionIdAdapter::OnSessionCreated(uint32 session_id,
const std::string& web_session_id) {
GetSession(session_id)->OnSessionCreated(web_session_id);
}
void SessionIdAdapter::OnSessionMessage(uint32 session_id,
const std::vector<uint8>& message,
const std::string& destination_url) {
GetSession(session_id)->OnSessionMessage(message, destination_url);
}
void SessionIdAdapter::OnSessionReady(uint32 session_id) {
GetSession(session_id)->OnSessionReady();
}
void SessionIdAdapter::OnSessionClosed(uint32 session_id) {
GetSession(session_id)->OnSessionClosed();
}
void SessionIdAdapter::OnSessionError(uint32 session_id,
media::MediaKeys::KeyError error_code,
int system_code) {
GetSession(session_id)->OnSessionError(error_code, system_code);
}
WebContentDecryptionModuleSessionImpl* SessionIdAdapter::GetSession(
uint32 session_id) {
DCHECK(sessions_.find(session_id) != sessions_.end());
return sessions_[session_id];
}
//------------------------------------------------------------------------------
WebContentDecryptionModuleImpl*
WebContentDecryptionModuleImpl::Create(const base::string16& key_system) {
WebContentDecryptionModuleImpl* WebContentDecryptionModuleImpl::Create(
const base::string16& key_system) {
// TODO(ddorwin): Guard against this in supported types check and remove this.
// Chromium only supports ASCII key systems.
if (!IsStringASCII(key_system)) {
......@@ -172,21 +25,16 @@ WebContentDecryptionModuleImpl::Create(const base::string16& key_system) {
return NULL;
}
// SessionIdAdapter creates the MediaKeys so it can provide its callbacks to
// during creation of the MediaKeys.
scoped_ptr<media::MediaKeys> media_keys;
scoped_ptr<SessionIdAdapter> adapter(new SessionIdAdapter());
if (!adapter->Initialize(UTF16ToASCII(key_system), &media_keys))
scoped_refptr<CdmSessionAdapter> adapter(new CdmSessionAdapter());
if (!adapter->Initialize(UTF16ToASCII(key_system)))
return NULL;
return new WebContentDecryptionModuleImpl(media_keys.Pass(), adapter.Pass());
return new WebContentDecryptionModuleImpl(adapter);
}
WebContentDecryptionModuleImpl::WebContentDecryptionModuleImpl(
scoped_ptr<media::MediaKeys> media_keys,
scoped_ptr<SessionIdAdapter> adapter)
: media_keys_(media_keys.Pass()),
adapter_(adapter.Pass()) {
scoped_refptr<CdmSessionAdapter> adapter)
: adapter_(adapter) {
}
WebContentDecryptionModuleImpl::~WebContentDecryptionModuleImpl() {
......@@ -196,26 +44,11 @@ WebContentDecryptionModuleImpl::~WebContentDecryptionModuleImpl() {
blink::WebContentDecryptionModuleSession*
WebContentDecryptionModuleImpl::createSession(
blink::WebContentDecryptionModuleSession::Client* client) {
DCHECK(media_keys_);
uint32 session_id = adapter_->GenerateSessionId();
WebContentDecryptionModuleSessionImpl* session =
new WebContentDecryptionModuleSessionImpl(
session_id,
media_keys_.get(),
client,
base::Bind(&WebContentDecryptionModuleImpl::OnSessionClosed,
base::Unretained(this)));
adapter_->AddSession(session_id, session);
return session;
return adapter_->CreateSession(client);
}
media::Decryptor* WebContentDecryptionModuleImpl::GetDecryptor() {
return media_keys_->GetDecryptor();
}
void WebContentDecryptionModuleImpl::OnSessionClosed(uint32 session_id) {
adapter_->RemoveSession(session_id);
return adapter_->GetDecryptor();
}
} // namespace content
......@@ -7,6 +7,7 @@
#include <string>
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/string16.h"
#include "third_party/WebKit/public/platform/WebContentDecryptionModule.h"
......@@ -18,8 +19,8 @@ class MediaKeys;
namespace content {
class CdmSessionAdapter;
class WebContentDecryptionModuleSessionImpl;
class SessionIdAdapter;
class WebContentDecryptionModuleImpl
: public blink::WebContentDecryptionModule {
......@@ -40,15 +41,10 @@ class WebContentDecryptionModuleImpl
blink::WebContentDecryptionModuleSession::Client* client);
private:
// Takes ownership of |media_keys| and |adapter|.
WebContentDecryptionModuleImpl(scoped_ptr<media::MediaKeys> media_keys,
scoped_ptr<SessionIdAdapter> adapter);
// Takes reference to |adapter|.
WebContentDecryptionModuleImpl(scoped_refptr<CdmSessionAdapter> adapter);
// Called when a WebContentDecryptionModuleSessionImpl is closed.
void OnSessionClosed(uint32 session_id);
scoped_ptr<media::MediaKeys> media_keys_;
scoped_ptr<SessionIdAdapter> adapter_;
scoped_refptr<CdmSessionAdapter> adapter_;
DISALLOW_COPY_AND_ASSIGN(WebContentDecryptionModuleImpl);
};
......
......@@ -7,24 +7,23 @@
#include "base/callback_helpers.h"
#include "base/logging.h"
#include "base/strings/string_util.h"
#include "content/renderer/media/cdm_session_adapter.h"
#include "third_party/WebKit/public/platform/WebURL.h"
namespace content {
WebContentDecryptionModuleSessionImpl::WebContentDecryptionModuleSessionImpl(
uint32 session_id,
media::MediaKeys* media_keys,
Client* client,
const SessionClosedCB& session_closed_cb)
: media_keys_(media_keys),
const scoped_refptr<CdmSessionAdapter>& adapter)
: adapter_(adapter),
client_(client),
session_closed_cb_(session_closed_cb),
session_id_(session_id) {
DCHECK(media_keys_);
}
WebContentDecryptionModuleSessionImpl::
~WebContentDecryptionModuleSessionImpl() {
~WebContentDecryptionModuleSessionImpl() {
adapter_->RemoveSession(session_id_);
}
blink::WebString WebContentDecryptionModuleSessionImpl::sessionId() const {
......@@ -42,18 +41,18 @@ void WebContentDecryptionModuleSessionImpl::initializeNewSession(
return;
}
media_keys_->CreateSession(
adapter_->InitializeNewSession(
session_id_, UTF16ToASCII(mime_type), init_data, init_data_length);
}
void WebContentDecryptionModuleSessionImpl::update(const uint8* response,
size_t response_length) {
DCHECK(response);
media_keys_->UpdateSession(session_id_, response, response_length);
adapter_->UpdateSession(session_id_, response, response_length);
}
void WebContentDecryptionModuleSessionImpl::release() {
media_keys_->ReleaseSession(session_id_);
adapter_->ReleaseSession(session_id_);
}
void WebContentDecryptionModuleSessionImpl::OnSessionCreated(
......@@ -83,8 +82,6 @@ void WebContentDecryptionModuleSessionImpl::OnSessionReady() {
void WebContentDecryptionModuleSessionImpl::OnSessionClosed() {
client_->close();
if (!session_closed_cb_.is_null())
base::ResetAndReturn(&session_closed_cb_).Run(session_id_);
}
void WebContentDecryptionModuleSessionImpl::OnSessionError(
......
......@@ -10,6 +10,7 @@
#include "base/basictypes.h"
#include "base/callback.h"
#include "base/memory/ref_counted.h"
#include "media/base/media_keys.h"
#include "third_party/WebKit/public/platform/WebContentDecryptionModuleSession.h"
#include "third_party/WebKit/public/platform/WebString.h"
......@@ -19,17 +20,15 @@ class MediaKeys;
}
namespace content {
class CdmSessionAdapter;
class WebContentDecryptionModuleSessionImpl
: public blink::WebContentDecryptionModuleSession {
public:
typedef base::Callback<void(uint32 session_id)> SessionClosedCB;
WebContentDecryptionModuleSessionImpl(
uint32 session_id,
media::MediaKeys* media_keys,
Client* client,
const SessionClosedCB& session_closed_cb);
const scoped_refptr<CdmSessionAdapter>& adapter);
virtual ~WebContentDecryptionModuleSessionImpl();
// blink::WebContentDecryptionModuleSession implementation.
......@@ -49,11 +48,10 @@ class WebContentDecryptionModuleSessionImpl
void OnSessionError(media::MediaKeys::KeyError error_code, int system_code);
private:
// Non-owned pointers.
media::MediaKeys* media_keys_;
Client* client_;
scoped_refptr<CdmSessionAdapter> adapter_;
SessionClosedCB session_closed_cb_;
// Non-owned pointer.
Client* client_;
// Web session ID is the app visible ID for this session generated by the CDM.
// This value is not set until the CDM calls OnSessionCreated().
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment