Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / renderer / media / cdm_session_adapter.cc
1 // Copyright 2014 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.
4
5 #include "content/renderer/media/cdm_session_adapter.h"
6
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/memory/weak_ptr.h"
10 #include "content/renderer/media/crypto/content_decryption_module_factory.h"
11 #include "content/renderer/media/webcontentdecryptionmodulesession_impl.h"
12 #include "media/base/media_keys.h"
13 #include "url/gurl.h"
14
15 namespace content {
16
17 const uint32 kStartingSessionId = 1;
18 uint32 CdmSessionAdapter::next_session_id_ = kStartingSessionId;
19 COMPILE_ASSERT(kStartingSessionId > media::MediaKeys::kInvalidSessionId,
20                invalid_starting_value);
21
22 CdmSessionAdapter::CdmSessionAdapter() :
23 #if defined(OS_ANDROID)
24     cdm_id_(0),
25 #endif
26     weak_ptr_factory_(this) {}
27
28 CdmSessionAdapter::~CdmSessionAdapter() {}
29
30 bool CdmSessionAdapter::Initialize(
31 #if defined(ENABLE_PEPPER_CDMS)
32     const CreatePepperCdmCB& create_pepper_cdm_cb,
33 #endif  // defined(ENABLE_PEPPER_CDMS)
34     const std::string& key_system,
35     const GURL& security_origin) {
36   base::WeakPtr<CdmSessionAdapter> weak_this = weak_ptr_factory_.GetWeakPtr();
37   media_keys_ = ContentDecryptionModuleFactory::Create(
38       key_system,
39       security_origin,
40 #if defined(ENABLE_PEPPER_CDMS)
41       create_pepper_cdm_cb,
42 #elif defined(OS_ANDROID)
43       // TODO(xhwang): Support Android.
44       NULL,
45       &cdm_id_,
46 #endif  // defined(ENABLE_PEPPER_CDMS)
47       base::Bind(&CdmSessionAdapter::OnSessionCreated, weak_this),
48       base::Bind(&CdmSessionAdapter::OnSessionMessage, weak_this),
49       base::Bind(&CdmSessionAdapter::OnSessionReady, weak_this),
50       base::Bind(&CdmSessionAdapter::OnSessionClosed, weak_this),
51       base::Bind(&CdmSessionAdapter::OnSessionError, weak_this));
52
53   // Success if |media_keys_| created.
54   return media_keys_;
55 }
56
57 WebContentDecryptionModuleSessionImpl* CdmSessionAdapter::CreateSession(
58     blink::WebContentDecryptionModuleSession::Client* client) {
59   // Generate a unique internal session id for the new session.
60   uint32 session_id = next_session_id_++;
61   DCHECK(sessions_.find(session_id) == sessions_.end());
62   WebContentDecryptionModuleSessionImpl* session =
63       new WebContentDecryptionModuleSessionImpl(session_id, client, this);
64   sessions_[session_id] = session;
65   return session;
66 }
67
68 void CdmSessionAdapter::RemoveSession(uint32 session_id) {
69   DCHECK(sessions_.find(session_id) != sessions_.end());
70   sessions_.erase(session_id);
71 }
72
73 void CdmSessionAdapter::InitializeNewSession(uint32 session_id,
74                                              const std::string& content_type,
75                                              const uint8* init_data,
76                                              int init_data_length) {
77   DCHECK(sessions_.find(session_id) != sessions_.end());
78   media_keys_->CreateSession(
79       session_id, content_type, init_data, init_data_length);
80 }
81
82 void CdmSessionAdapter::UpdateSession(uint32 session_id,
83                                       const uint8* response,
84                                       int response_length) {
85   DCHECK(sessions_.find(session_id) != sessions_.end());
86   media_keys_->UpdateSession(session_id, response, response_length);
87 }
88
89 void CdmSessionAdapter::ReleaseSession(uint32 session_id) {
90   DCHECK(sessions_.find(session_id) != sessions_.end());
91   media_keys_->ReleaseSession(session_id);
92 }
93
94 media::Decryptor* CdmSessionAdapter::GetDecryptor() {
95   return media_keys_->GetDecryptor();
96 }
97
98 #if defined(OS_ANDROID)
99 int CdmSessionAdapter::GetCdmId() const {
100   return cdm_id_;
101 }
102 #endif  // defined(OS_ANDROID)
103
104 void CdmSessionAdapter::OnSessionCreated(uint32 session_id,
105                                          const std::string& web_session_id) {
106   WebContentDecryptionModuleSessionImpl* session = GetSession(session_id);
107   DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session "
108                              << session_id;
109   if (session)
110     session->OnSessionCreated(web_session_id);
111 }
112
113 void CdmSessionAdapter::OnSessionMessage(uint32 session_id,
114                                          const std::vector<uint8>& message,
115                                          const std::string& destination_url) {
116   WebContentDecryptionModuleSessionImpl* session = GetSession(session_id);
117   DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session "
118                              << session_id;
119   if (session)
120     session->OnSessionMessage(message, destination_url);
121 }
122
123 void CdmSessionAdapter::OnSessionReady(uint32 session_id) {
124   WebContentDecryptionModuleSessionImpl* session = GetSession(session_id);
125   DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session "
126                              << session_id;
127   if (session)
128     session->OnSessionReady();
129 }
130
131 void CdmSessionAdapter::OnSessionClosed(uint32 session_id) {
132   WebContentDecryptionModuleSessionImpl* session = GetSession(session_id);
133   DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session "
134                              << session_id;
135   if (session)
136     session->OnSessionClosed();
137 }
138
139 void CdmSessionAdapter::OnSessionError(uint32 session_id,
140                                        media::MediaKeys::KeyError error_code,
141                                        uint32 system_code) {
142   WebContentDecryptionModuleSessionImpl* session = GetSession(session_id);
143   DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session "
144                              << session_id;
145   if (session)
146     session->OnSessionError(error_code, system_code);
147 }
148
149 WebContentDecryptionModuleSessionImpl* CdmSessionAdapter::GetSession(
150     uint32 session_id) {
151   // Since session objects may get garbage collected, it is possible that there
152   // are events coming back from the CDM and the session has been unregistered.
153   // We can not tell if the CDM is firing events at sessions that never existed.
154   SessionMap::iterator session = sessions_.find(session_id);
155   return (session != sessions_.end()) ? session->second : NULL;
156 }
157
158 }  // namespace content