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/webcontentdecryptionmodulesession_impl.h" | 5 #include "content/renderer/media/webcontentdecryptionmodulesession_impl.h" |
6 | 6 |
7 #include "base/bind.h" | |
7 #include "base/callback_helpers.h" | 8 #include "base/callback_helpers.h" |
8 #include "base/logging.h" | 9 #include "base/logging.h" |
9 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
10 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
11 #include "content/renderer/media/cdm_session_adapter.h" | 12 #include "content/renderer/media/cdm_session_adapter.h" |
13 #include "media/base/cdm_promise.h" | |
12 #include "third_party/WebKit/public/platform/WebURL.h" | 14 #include "third_party/WebKit/public/platform/WebURL.h" |
13 | 15 |
14 namespace content { | 16 namespace content { |
15 | 17 |
18 #define CreatePromise(type, resolve_method, reject_method) \ | |
ddorwin
2014/05/13 22:44:02
Macros aren't allowed. Can we use a function templ
jrummell
2014/05/15 22:38:09
Tried, but looks like the callers would need to pa
| |
19 new media::CdmPromise<type>( \ | |
20 base::Bind(&WebContentDecryptionModuleSessionImpl::resolve_method, \ | |
21 weak_ptr_factory_.GetWeakPtr()), \ | |
22 base::Bind(&WebContentDecryptionModuleSessionImpl::reject_method, \ | |
23 weak_ptr_factory_.GetWeakPtr())) | |
24 | |
16 WebContentDecryptionModuleSessionImpl::WebContentDecryptionModuleSessionImpl( | 25 WebContentDecryptionModuleSessionImpl::WebContentDecryptionModuleSessionImpl( |
17 uint32 session_id, | |
18 Client* client, | 26 Client* client, |
19 const scoped_refptr<CdmSessionAdapter>& adapter) | 27 const scoped_refptr<CdmSessionAdapter>& adapter) |
20 : adapter_(adapter), | 28 : adapter_(adapter), |
21 client_(client), | 29 client_(client), |
22 session_id_(session_id) { | 30 weak_ptr_factory_(this) { |
23 } | 31 } |
24 | 32 |
25 WebContentDecryptionModuleSessionImpl:: | 33 WebContentDecryptionModuleSessionImpl:: |
26 ~WebContentDecryptionModuleSessionImpl() { | 34 ~WebContentDecryptionModuleSessionImpl() { |
27 adapter_->RemoveSession(session_id_); | 35 if (!web_session_id_.empty()) |
36 adapter_->RemoveSession(web_session_id_); | |
28 } | 37 } |
29 | 38 |
30 blink::WebString WebContentDecryptionModuleSessionImpl::sessionId() const { | 39 blink::WebString WebContentDecryptionModuleSessionImpl::sessionId() const { |
31 return web_session_id_; | 40 return blink::WebString::fromUTF8(web_session_id_); |
32 } | 41 } |
33 | 42 |
34 void WebContentDecryptionModuleSessionImpl::initializeNewSession( | 43 void WebContentDecryptionModuleSessionImpl::initializeNewSession( |
35 const blink::WebString& mime_type, | 44 const blink::WebString& init_data_type, |
36 const uint8* init_data, size_t init_data_length) { | 45 const uint8* init_data, |
46 size_t init_data_length) { | |
37 // TODO(ddorwin): Guard against this in supported types check and remove this. | 47 // TODO(ddorwin): Guard against this in supported types check and remove this. |
38 // Chromium only supports ASCII MIME types. | 48 // Chromium only supports ASCII MIME types. |
39 if (!base::IsStringASCII(mime_type)) { | 49 if (!base::IsStringASCII(init_data_type)) { |
40 NOTREACHED(); | 50 NOTREACHED(); |
41 OnSessionError(media::MediaKeys::kUnknownError, 0); | 51 OnSessionError(media::MediaKeys::MEDIA_KEYS_EXCEPTION_NOT_SUPPORTED_ERROR, |
52 0, | |
53 "The initialization data type " + init_data_type.utf8() + | |
54 " is not supported by the key system."); | |
42 return; | 55 return; |
43 } | 56 } |
44 | 57 |
45 adapter_->InitializeNewSession( | 58 std::string init_data_type_as_ascii = base::UTF16ToASCII(init_data_type); |
46 session_id_, base::UTF16ToASCII(mime_type), init_data, init_data_length); | 59 DLOG_IF(WARNING, init_data_type_as_ascii.find('/') != std::string::npos) |
60 << "init_data_type '" << init_data_type_as_ascii | |
61 << "' may be a MIME type"; | |
62 | |
63 scoped_ptr<media::CdmNewSessionPromise> promise( | |
64 CreatePromise(std::string, SessionCreated, SessionCreateFailed)); | |
65 adapter_->InitializeNewSession(init_data_type_as_ascii, | |
66 init_data, | |
67 init_data_length, | |
68 media::MediaKeys::SESSION_TYPE_TEMPORARY, | |
69 promise.Pass()); | |
47 } | 70 } |
48 | 71 |
49 void WebContentDecryptionModuleSessionImpl::update(const uint8* response, | 72 void WebContentDecryptionModuleSessionImpl::update(const uint8* response, |
50 size_t response_length) { | 73 size_t response_length) { |
51 DCHECK(response); | 74 DCHECK(response); |
52 adapter_->UpdateSession(session_id_, response, response_length); | 75 scoped_ptr<media::CdmChangeSessionPromise> promise( |
76 CreatePromise(void, SessionUpdated, SessionUpdateFailed)); | |
77 adapter_->UpdateSession( | |
78 web_session_id_, response, response_length, promise.Pass()); | |
53 } | 79 } |
54 | 80 |
55 void WebContentDecryptionModuleSessionImpl::release() { | 81 void WebContentDecryptionModuleSessionImpl::release() { |
56 adapter_->ReleaseSession(session_id_); | 82 scoped_ptr<media::CdmChangeSessionPromise> promise( |
57 } | 83 CreatePromise(void, SessionReleased, SessionReleaseFailed)); |
58 | 84 adapter_->ReleaseSession(web_session_id_, promise.Pass()); |
59 void WebContentDecryptionModuleSessionImpl::OnSessionCreated( | |
60 const std::string& web_session_id) { | |
61 // Due to heartbeat messages, OnSessionCreated() can get called multiple | |
62 // times. | |
63 // TODO(jrummell): Once all CDMs are updated to support reference ids, | |
64 // OnSessionCreated() should only be called once, and the second check can be | |
65 // removed. | |
66 blink::WebString id = blink::WebString::fromUTF8(web_session_id); | |
67 DCHECK(web_session_id_.isEmpty() || web_session_id_ == id) | |
68 << "Session ID may not be changed once set."; | |
69 web_session_id_ = id; | |
70 } | 85 } |
71 | 86 |
72 void WebContentDecryptionModuleSessionImpl::OnSessionMessage( | 87 void WebContentDecryptionModuleSessionImpl::OnSessionMessage( |
73 const std::vector<uint8>& message, | 88 const std::vector<uint8>& message, |
74 const std::string& destination_url) { | 89 const std::string& destination_url) { |
75 client_->message(message.empty() ? NULL : &message[0], | 90 client_->message(message.empty() ? NULL : &message[0], |
76 message.size(), | 91 message.size(), |
77 GURL(destination_url)); | 92 GURL(destination_url)); |
78 } | 93 } |
79 | 94 |
80 void WebContentDecryptionModuleSessionImpl::OnSessionReady() { | 95 void WebContentDecryptionModuleSessionImpl::OnSessionReady() { |
81 client_->ready(); | 96 client_->ready(); |
82 } | 97 } |
83 | 98 |
84 void WebContentDecryptionModuleSessionImpl::OnSessionClosed() { | 99 void WebContentDecryptionModuleSessionImpl::OnSessionClosed() { |
85 client_->close(); | 100 client_->close(); |
86 } | 101 } |
87 | 102 |
88 void WebContentDecryptionModuleSessionImpl::OnSessionError( | 103 void WebContentDecryptionModuleSessionImpl::OnSessionError( |
89 media::MediaKeys::KeyError error_code, | 104 media::MediaKeys::MediaKeysException exception_code, |
90 uint32 system_code) { | 105 uint32 system_code, |
91 client_->error(static_cast<Client::MediaKeyErrorCode>(error_code), | 106 const std::string& error_message) { |
92 system_code); | 107 // Convert |exception_code| back to MediaKeyErrorCode if possible. |
108 // TODO(jrummell): Update this conversion when promises flow | |
109 // back into blink:: (as blink:: will have it's own error definition). | |
ddorwin
2014/05/13 22:44:02
nit: its
jrummell
2014/05/15 22:38:09
Done.
| |
110 switch (exception_code) { | |
111 case media::MediaKeys::MEDIA_KEYS_EXCEPTION_CLIENT_ERROR: | |
112 client_->error(Client::MediaKeyErrorCodeClient, system_code); | |
113 break; | |
114 default: | |
115 // This will include all other CDM4 errors and any error generated | |
116 // by CDM5 or later. | |
117 client_->error(Client::MediaKeyErrorCodeUnknown, system_code); | |
118 break; | |
119 } | |
120 } | |
121 | |
122 void WebContentDecryptionModuleSessionImpl::SessionCreated( | |
123 const std::string& web_session_id) { | |
124 DCHECK(web_session_id_.empty()) << "Session ID may not be changed once set."; | |
125 web_session_id_ = web_session_id; | |
126 adapter_->RegisterSession(web_session_id_, weak_ptr_factory_.GetWeakPtr()); | |
127 } | |
128 | |
129 void WebContentDecryptionModuleSessionImpl::SessionCreateFailed( | |
130 media::MediaKeys::MediaKeysException exception_code, | |
131 uint32 system_code, | |
132 const std::string& error_message) { | |
133 // For now, just report this as an error back to blink::. | |
134 OnSessionError(exception_code, system_code, error_message); | |
135 } | |
136 | |
137 void WebContentDecryptionModuleSessionImpl::SessionUpdated() { | |
138 // Pass back to blink:: as a ready event. | |
ddorwin
2014/05/13 22:44:02
All of these are "For now,".
jrummell
2014/05/15 22:38:09
Done.
| |
139 OnSessionReady(); | |
140 } | |
141 | |
142 void WebContentDecryptionModuleSessionImpl::SessionUpdateFailed( | |
143 media::MediaKeys::MediaKeysException exception_code, | |
144 uint32 system_code, | |
145 const std::string& error_message) { | |
146 // For now, just report this as an error back to blink::. | |
147 OnSessionError(exception_code, system_code, error_message); | |
ddorwin
2014/05/13 22:44:02
All the Failed functions do the same thing. Why do
jrummell
2014/05/15 22:38:09
Fixed.
| |
148 } | |
149 | |
150 void WebContentDecryptionModuleSessionImpl::SessionReleased() { | |
151 // Pass back to blink:: as a closed event. | |
152 OnSessionClosed(); | |
153 } | |
154 | |
155 void WebContentDecryptionModuleSessionImpl::SessionReleaseFailed( | |
156 media::MediaKeys::MediaKeysException exception_code, | |
157 uint32 system_code, | |
158 const std::string& error_message) { | |
159 // For now, just report this as an error back to blink::. | |
160 OnSessionError(exception_code, system_code, error_message); | |
93 } | 161 } |
94 | 162 |
95 } // namespace content | 163 } // namespace content |
OLD | NEW |