OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/media/webcontentdecryptionmodule_impl.h" | 5 #include "content/renderer/media/webcontentdecryptionmodule_impl.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/callback_helpers.h" | 12 #include "base/callback_helpers.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
16 #include "content/renderer/media/crypto/content_decryption_module_factory.h" | 16 #include "content/renderer/media/crypto/content_decryption_module_factory.h" |
17 #include "content/renderer/media/webcontentdecryptionmodulesession_impl.h" | 17 #include "content/renderer/media/webcontentdecryptionmodulesession_impl.h" |
18 #include "media/base/media_keys.h" | 18 #include "media/base/media_keys.h" |
19 #include "url/gurl.h" | 19 #include "url/gurl.h" |
20 | 20 |
21 namespace content { | 21 namespace content { |
22 | 22 |
23 // Forwards the reference ID-based callbacks of the MediaKeys interface to the | 23 // Forwards the session ID-based callbacks of the MediaKeys interface to the |
24 // appropriate session object. | 24 // appropriate session object. |
25 class ReferenceIdAdapter { | 25 class SessionIdAdapter { |
26 public: | 26 public: |
27 ReferenceIdAdapter(); | 27 SessionIdAdapter(); |
28 ~ReferenceIdAdapter(); | 28 ~SessionIdAdapter(); |
29 | 29 |
30 // On success, creates a MediaKeys, returns it in |media_keys|, returns true. | 30 // On success, creates a MediaKeys, returns it in |media_keys|, returns true. |
31 bool Initialize(const std::string& key_system, | 31 bool Initialize(const std::string& key_system, |
32 scoped_ptr<media::MediaKeys>* media_keys); | 32 scoped_ptr<media::MediaKeys>* media_keys); |
33 | 33 |
| 34 // Generates a unique internal session id. |
| 35 uint32 GenerateSessionId(); |
| 36 |
34 // Adds a session to the internal map. Does not take ownership of the session. | 37 // Adds a session to the internal map. Does not take ownership of the session. |
35 void AddSession(uint32 reference_id, | 38 void AddSession(uint32 session_id, |
36 WebContentDecryptionModuleSessionImpl* session); | 39 WebContentDecryptionModuleSessionImpl* session); |
37 | 40 |
38 // Removes a session from the internal map. | 41 // Removes a session from the internal map. |
39 void RemoveSession(uint32 reference_id); | 42 void RemoveSession(uint32 session_id); |
40 | 43 |
41 private: | 44 private: |
42 typedef std::map<uint32, WebContentDecryptionModuleSessionImpl*> SessionMap; | 45 typedef std::map<uint32, WebContentDecryptionModuleSessionImpl*> SessionMap; |
43 | 46 |
44 // Callbacks for firing session events. | 47 // Callbacks for firing session events. |
45 void OnSessionCreated(uint32 reference_id, const std::string& session_id); | 48 void OnSessionCreated(uint32 session_id, const std::string& web_session_id); |
46 void OnSessionMessage(uint32 reference_id, | 49 void OnSessionMessage(uint32 session_id, |
47 const std::vector<uint8>& message, | 50 const std::vector<uint8>& message, |
48 const std::string& destination_url); | 51 const std::string& destination_url); |
49 void OnSessionReady(uint32 reference_id); | 52 void OnSessionReady(uint32 session_id); |
50 void OnSessionClosed(uint32 reference_id); | 53 void OnSessionClosed(uint32 session_id); |
51 void OnSessionError(uint32 reference_id, | 54 void OnSessionError(uint32 session_id, |
52 media::MediaKeys::KeyError error_code, | 55 media::MediaKeys::KeyError error_code, |
53 int system_code); | 56 int system_code); |
54 | 57 |
55 // Helper function of the callbacks. | 58 // Helper function of the callbacks. |
56 WebContentDecryptionModuleSessionImpl* GetSession(uint32 reference_id); | 59 WebContentDecryptionModuleSessionImpl* GetSession(uint32 session_id); |
57 | 60 |
58 base::WeakPtrFactory<ReferenceIdAdapter> weak_ptr_factory_; | 61 base::WeakPtrFactory<SessionIdAdapter> weak_ptr_factory_; |
59 | 62 |
60 SessionMap sessions_; | 63 SessionMap sessions_; |
61 | 64 |
62 DISALLOW_COPY_AND_ASSIGN(ReferenceIdAdapter); | 65 // Session ID should be unique per renderer process for debugging purposes. |
| 66 static uint32 next_session_id_; |
| 67 |
| 68 DISALLOW_COPY_AND_ASSIGN(SessionIdAdapter); |
63 }; | 69 }; |
64 | 70 |
65 ReferenceIdAdapter::ReferenceIdAdapter() | 71 const uint32 kStartingSessionId = 1; |
| 72 uint32 SessionIdAdapter::next_session_id_ = kStartingSessionId; |
| 73 COMPILE_ASSERT(kStartingSessionId > media::MediaKeys::kInvalidSessionId, |
| 74 invalid_starting_value); |
| 75 |
| 76 SessionIdAdapter::SessionIdAdapter() |
66 : weak_ptr_factory_(this) { | 77 : weak_ptr_factory_(this) { |
67 } | 78 } |
68 | 79 |
69 ReferenceIdAdapter::~ReferenceIdAdapter() { | 80 SessionIdAdapter::~SessionIdAdapter() { |
70 } | 81 } |
71 | 82 |
72 bool ReferenceIdAdapter::Initialize(const std::string& key_system, | 83 bool SessionIdAdapter::Initialize(const std::string& key_system, |
73 scoped_ptr<media::MediaKeys>* media_keys) { | 84 scoped_ptr<media::MediaKeys>* media_keys) { |
74 DCHECK(media_keys); | 85 DCHECK(media_keys); |
75 DCHECK(!*media_keys); | 86 DCHECK(!*media_keys); |
76 | 87 |
77 base::WeakPtr<ReferenceIdAdapter> weak_this = weak_ptr_factory_.GetWeakPtr(); | 88 base::WeakPtr<SessionIdAdapter> weak_this = weak_ptr_factory_.GetWeakPtr(); |
78 scoped_ptr<media::MediaKeys> created_media_keys = | 89 scoped_ptr<media::MediaKeys> created_media_keys = |
79 ContentDecryptionModuleFactory::Create( | 90 ContentDecryptionModuleFactory::Create( |
80 // TODO(ddorwin): Address lower in the stack: http://crbug.com/252065 | 91 // TODO(ddorwin): Address lower in the stack: http://crbug.com/252065 |
81 "webkit-" + key_system, | 92 "webkit-" + key_system, |
82 #if defined(ENABLE_PEPPER_CDMS) | 93 #if defined(ENABLE_PEPPER_CDMS) |
83 // TODO(ddorwin): Support Pepper-based CDMs: http://crbug.com/250049 | 94 // TODO(ddorwin): Support Pepper-based CDMs: http://crbug.com/250049 |
84 NULL, | 95 NULL, |
85 NULL, | 96 NULL, |
86 base::Closure(), | 97 base::Closure(), |
87 #elif defined(OS_ANDROID) | 98 #elif defined(OS_ANDROID) |
88 // TODO(xhwang): Support Android. | 99 // TODO(xhwang): Support Android. |
89 NULL, | 100 NULL, |
90 0, | 101 0, |
91 // TODO(ddorwin): Get the URL for the frame containing the MediaKeys. | 102 // TODO(ddorwin): Get the URL for the frame containing the MediaKeys. |
92 GURL(), | 103 GURL(), |
93 #endif // defined(ENABLE_PEPPER_CDMS) | 104 #endif // defined(ENABLE_PEPPER_CDMS) |
94 base::Bind(&ReferenceIdAdapter::OnSessionCreated, weak_this), | 105 base::Bind(&SessionIdAdapter::OnSessionCreated, weak_this), |
95 base::Bind(&ReferenceIdAdapter::OnSessionMessage, weak_this), | 106 base::Bind(&SessionIdAdapter::OnSessionMessage, weak_this), |
96 base::Bind(&ReferenceIdAdapter::OnSessionReady, weak_this), | 107 base::Bind(&SessionIdAdapter::OnSessionReady, weak_this), |
97 base::Bind(&ReferenceIdAdapter::OnSessionClosed, weak_this), | 108 base::Bind(&SessionIdAdapter::OnSessionClosed, weak_this), |
98 base::Bind(&ReferenceIdAdapter::OnSessionError, weak_this)); | 109 base::Bind(&SessionIdAdapter::OnSessionError, weak_this)); |
99 if (!created_media_keys) | 110 if (!created_media_keys) |
100 return false; | 111 return false; |
101 | 112 |
102 *media_keys = created_media_keys.Pass(); | 113 *media_keys = created_media_keys.Pass(); |
103 return true; | 114 return true; |
104 } | 115 } |
105 | 116 |
106 void ReferenceIdAdapter::AddSession( | 117 uint32 SessionIdAdapter::GenerateSessionId() { |
107 uint32 reference_id, | 118 return next_session_id_++; |
108 WebContentDecryptionModuleSessionImpl* session) { | |
109 DCHECK(sessions_.find(reference_id) == sessions_.end()); | |
110 sessions_[reference_id] = session; | |
111 } | 119 } |
112 | 120 |
113 void ReferenceIdAdapter::RemoveSession(uint32 reference_id) { | 121 void SessionIdAdapter::AddSession( |
114 DCHECK(sessions_.find(reference_id) != sessions_.end()); | 122 uint32 session_id, |
115 sessions_.erase(reference_id); | 123 WebContentDecryptionModuleSessionImpl* session) { |
| 124 DCHECK(sessions_.find(session_id) == sessions_.end()); |
| 125 sessions_[session_id] = session; |
116 } | 126 } |
117 | 127 |
118 void ReferenceIdAdapter::OnSessionCreated(uint32 reference_id, | 128 void SessionIdAdapter::RemoveSession(uint32 session_id) { |
119 const std::string& session_id) { | 129 DCHECK(sessions_.find(session_id) != sessions_.end()); |
120 GetSession(reference_id)->OnSessionCreated(session_id); | 130 sessions_.erase(session_id); |
121 } | 131 } |
122 | 132 |
123 void ReferenceIdAdapter::OnSessionMessage(uint32 reference_id, | 133 void SessionIdAdapter::OnSessionCreated(uint32 session_id, |
124 const std::vector<uint8>& message, | 134 const std::string& web_session_id) { |
125 const std::string& destination_url) { | 135 GetSession(session_id)->OnSessionCreated(web_session_id); |
126 GetSession(reference_id)->OnSessionMessage(message, destination_url); | |
127 } | 136 } |
128 | 137 |
129 void ReferenceIdAdapter::OnSessionReady(uint32 reference_id) { | 138 void SessionIdAdapter::OnSessionMessage(uint32 session_id, |
130 GetSession(reference_id)->OnSessionReady(); | 139 const std::vector<uint8>& message, |
| 140 const std::string& destination_url) { |
| 141 GetSession(session_id)->OnSessionMessage(message, destination_url); |
131 } | 142 } |
132 | 143 |
133 void ReferenceIdAdapter::OnSessionClosed(uint32 reference_id) { | 144 void SessionIdAdapter::OnSessionReady(uint32 session_id) { |
134 GetSession(reference_id)->OnSessionClosed(); | 145 GetSession(session_id)->OnSessionReady(); |
135 } | 146 } |
136 | 147 |
137 void ReferenceIdAdapter::OnSessionError(uint32 reference_id, | 148 void SessionIdAdapter::OnSessionClosed(uint32 session_id) { |
138 media::MediaKeys::KeyError error_code, | 149 GetSession(session_id)->OnSessionClosed(); |
139 int system_code) { | |
140 GetSession(reference_id)->OnSessionError(error_code, system_code); | |
141 } | 150 } |
142 | 151 |
143 WebContentDecryptionModuleSessionImpl* ReferenceIdAdapter::GetSession( | 152 void SessionIdAdapter::OnSessionError(uint32 session_id, |
144 uint32 reference_id) { | 153 media::MediaKeys::KeyError error_code, |
145 DCHECK(sessions_.find(reference_id) != sessions_.end()); | 154 int system_code) { |
146 return sessions_[reference_id]; | 155 GetSession(session_id)->OnSessionError(error_code, system_code); |
| 156 } |
| 157 |
| 158 WebContentDecryptionModuleSessionImpl* SessionIdAdapter::GetSession( |
| 159 uint32 session_id) { |
| 160 DCHECK(sessions_.find(session_id) != sessions_.end()); |
| 161 return sessions_[session_id]; |
147 } | 162 } |
148 | 163 |
149 //------------------------------------------------------------------------------ | 164 //------------------------------------------------------------------------------ |
150 | 165 |
151 WebContentDecryptionModuleImpl* | 166 WebContentDecryptionModuleImpl* |
152 WebContentDecryptionModuleImpl::Create(const base::string16& key_system) { | 167 WebContentDecryptionModuleImpl::Create(const base::string16& key_system) { |
153 // TODO(ddorwin): Guard against this in supported types check and remove this. | 168 // TODO(ddorwin): Guard against this in supported types check and remove this. |
154 // Chromium only supports ASCII key systems. | 169 // Chromium only supports ASCII key systems. |
155 if (!IsStringASCII(key_system)) { | 170 if (!IsStringASCII(key_system)) { |
156 NOTREACHED(); | 171 NOTREACHED(); |
157 return NULL; | 172 return NULL; |
158 } | 173 } |
159 | 174 |
160 // ReferenceIdAdapter creates the MediaKeys so it can provide its callbacks to | 175 // SessionIdAdapter creates the MediaKeys so it can provide its callbacks to |
161 // during creation of the MediaKeys. | 176 // during creation of the MediaKeys. |
162 scoped_ptr<media::MediaKeys> media_keys; | 177 scoped_ptr<media::MediaKeys> media_keys; |
163 scoped_ptr<ReferenceIdAdapter> adapter(new ReferenceIdAdapter()); | 178 scoped_ptr<SessionIdAdapter> adapter(new SessionIdAdapter()); |
164 if (!adapter->Initialize(UTF16ToASCII(key_system), &media_keys)) | 179 if (!adapter->Initialize(UTF16ToASCII(key_system), &media_keys)) |
165 return NULL; | 180 return NULL; |
166 | 181 |
167 return new WebContentDecryptionModuleImpl(media_keys.Pass(), adapter.Pass()); | 182 return new WebContentDecryptionModuleImpl(media_keys.Pass(), adapter.Pass()); |
168 } | 183 } |
169 | 184 |
170 WebContentDecryptionModuleImpl::WebContentDecryptionModuleImpl( | 185 WebContentDecryptionModuleImpl::WebContentDecryptionModuleImpl( |
171 scoped_ptr<media::MediaKeys> media_keys, | 186 scoped_ptr<media::MediaKeys> media_keys, |
172 scoped_ptr<ReferenceIdAdapter> adapter) | 187 scoped_ptr<SessionIdAdapter> adapter) |
173 : media_keys_(media_keys.Pass()), | 188 : media_keys_(media_keys.Pass()), |
174 adapter_(adapter.Pass()) { | 189 adapter_(adapter.Pass()) { |
175 } | 190 } |
176 | 191 |
177 WebContentDecryptionModuleImpl::~WebContentDecryptionModuleImpl() { | 192 WebContentDecryptionModuleImpl::~WebContentDecryptionModuleImpl() { |
178 } | 193 } |
179 | 194 |
180 // The caller owns the created session. | 195 // The caller owns the created session. |
181 blink::WebContentDecryptionModuleSession* | 196 blink::WebContentDecryptionModuleSession* |
182 WebContentDecryptionModuleImpl::createSession( | 197 WebContentDecryptionModuleImpl::createSession( |
183 blink::WebContentDecryptionModuleSession::Client* client) { | 198 blink::WebContentDecryptionModuleSession::Client* client) { |
184 DCHECK(media_keys_); | 199 DCHECK(media_keys_); |
| 200 uint32 session_id = adapter_->GenerateSessionId(); |
185 WebContentDecryptionModuleSessionImpl* session = | 201 WebContentDecryptionModuleSessionImpl* session = |
186 new WebContentDecryptionModuleSessionImpl( | 202 new WebContentDecryptionModuleSessionImpl( |
| 203 session_id, |
187 media_keys_.get(), | 204 media_keys_.get(), |
188 client, | 205 client, |
189 base::Bind(&WebContentDecryptionModuleImpl::OnSessionClosed, | 206 base::Bind(&WebContentDecryptionModuleImpl::OnSessionClosed, |
190 base::Unretained(this))); | 207 base::Unretained(this))); |
191 | 208 |
192 adapter_->AddSession(session->reference_id(), session); | 209 adapter_->AddSession(session_id, session); |
193 return session; | 210 return session; |
194 } | 211 } |
195 | 212 |
196 void WebContentDecryptionModuleImpl::OnSessionClosed(uint32 reference_id) { | 213 void WebContentDecryptionModuleImpl::OnSessionClosed(uint32 session_id) { |
197 adapter_->RemoveSession(reference_id); | 214 adapter_->RemoveSession(session_id); |
198 } | 215 } |
199 | 216 |
200 } // namespace content | 217 } // namespace content |
OLD | NEW |