Update V8 to version 4.7.56.
[chromium-blink-merge.git] / media / blink / key_system_config_selector.cc
bloba727722c5a6ddbe115e00ce11265e0db43d72afa
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.
5 #include "key_system_config_selector.h"
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "media/base/cdm_config.h"
12 #include "media/base/key_systems.h"
13 #include "media/base/media_permission.h"
14 #include "media/base/mime_util.h"
15 #include "media/blink/webmediaplayer_util.h"
16 #include "third_party/WebKit/public/platform/WebMediaKeySystemConfiguration.h"
17 #include "third_party/WebKit/public/platform/WebSecurityOrigin.h"
18 #include "third_party/WebKit/public/platform/WebString.h"
19 #include "third_party/WebKit/public/platform/WebVector.h"
20 #include "url/gurl.h"
22 namespace media {
24 using EmeFeatureRequirement =
25 blink::WebMediaKeySystemConfiguration::Requirement;
27 namespace {
29 static EmeConfigRule GetSessionTypeConfigRule(EmeSessionTypeSupport support) {
30 switch (support) {
31 case EmeSessionTypeSupport::INVALID:
32 NOTREACHED();
33 return EmeConfigRule::NOT_SUPPORTED;
34 case EmeSessionTypeSupport::NOT_SUPPORTED:
35 return EmeConfigRule::NOT_SUPPORTED;
36 case EmeSessionTypeSupport::SUPPORTED_WITH_IDENTIFIER:
37 return EmeConfigRule::IDENTIFIER_AND_PERSISTENCE_REQUIRED;
38 case EmeSessionTypeSupport::SUPPORTED:
39 return EmeConfigRule::PERSISTENCE_REQUIRED;
41 NOTREACHED();
42 return EmeConfigRule::NOT_SUPPORTED;
45 static EmeConfigRule GetDistinctiveIdentifierConfigRule(
46 EmeFeatureSupport support,
47 EmeFeatureRequirement requirement) {
48 if (support == EmeFeatureSupport::INVALID) {
49 NOTREACHED();
50 return EmeConfigRule::NOT_SUPPORTED;
53 // For NOT_ALLOWED and REQUIRED, the result is as expected. For OPTIONAL, we
54 // return the most restrictive rule that is not more restrictive than for
55 // NOT_ALLOWED or REQUIRED. Those values will be checked individually when
56 // the option is resolved.
58 // NOT_ALLOWED OPTIONAL REQUIRED
59 // NOT_SUPPORTED I_NOT_ALLOWED I_NOT_ALLOWED NOT_SUPPORTED
60 // REQUESTABLE I_NOT_ALLOWED SUPPORTED I_REQUIRED
61 // ALWAYS_ENABLED NOT_SUPPORTED I_REQUIRED I_REQUIRED
62 DCHECK(support == EmeFeatureSupport::NOT_SUPPORTED ||
63 support == EmeFeatureSupport::REQUESTABLE ||
64 support == EmeFeatureSupport::ALWAYS_ENABLED);
65 DCHECK(requirement == EmeFeatureRequirement::NotAllowed ||
66 requirement == EmeFeatureRequirement::Optional ||
67 requirement == EmeFeatureRequirement::Required);
68 if ((support == EmeFeatureSupport::NOT_SUPPORTED &&
69 requirement == EmeFeatureRequirement::Required) ||
70 (support == EmeFeatureSupport::ALWAYS_ENABLED &&
71 requirement == EmeFeatureRequirement::NotAllowed)) {
72 return EmeConfigRule::NOT_SUPPORTED;
74 if (support == EmeFeatureSupport::REQUESTABLE &&
75 requirement == EmeFeatureRequirement::Optional) {
76 return EmeConfigRule::SUPPORTED;
78 if (support == EmeFeatureSupport::NOT_SUPPORTED ||
79 requirement == EmeFeatureRequirement::NotAllowed) {
80 return EmeConfigRule::IDENTIFIER_NOT_ALLOWED;
82 return EmeConfigRule::IDENTIFIER_REQUIRED;
85 static EmeConfigRule GetPersistentStateConfigRule(
86 EmeFeatureSupport support,
87 EmeFeatureRequirement requirement) {
88 if (support == EmeFeatureSupport::INVALID) {
89 NOTREACHED();
90 return EmeConfigRule::NOT_SUPPORTED;
93 // For NOT_ALLOWED and REQUIRED, the result is as expected. For OPTIONAL, we
94 // return the most restrictive rule that is not more restrictive than for
95 // NOT_ALLOWED or REQUIRED. Those values will be checked individually when
96 // the option is resolved.
98 // Note that even though a distinctive identifier can not be required for
99 // persistent state, it may still be required for persistent sessions.
101 // NOT_ALLOWED OPTIONAL REQUIRED
102 // NOT_SUPPORTED P_NOT_ALLOWED P_NOT_ALLOWED NOT_SUPPORTED
103 // REQUESTABLE P_NOT_ALLOWED SUPPORTED P_REQUIRED
104 // ALWAYS_ENABLED NOT_SUPPORTED P_REQUIRED P_REQUIRED
105 DCHECK(support == EmeFeatureSupport::NOT_SUPPORTED ||
106 support == EmeFeatureSupport::REQUESTABLE ||
107 support == EmeFeatureSupport::ALWAYS_ENABLED);
108 DCHECK(requirement == EmeFeatureRequirement::NotAllowed ||
109 requirement == EmeFeatureRequirement::Optional ||
110 requirement == EmeFeatureRequirement::Required);
111 if ((support == EmeFeatureSupport::NOT_SUPPORTED &&
112 requirement == EmeFeatureRequirement::Required) ||
113 (support == EmeFeatureSupport::ALWAYS_ENABLED &&
114 requirement == EmeFeatureRequirement::NotAllowed)) {
115 return EmeConfigRule::NOT_SUPPORTED;
117 if (support == EmeFeatureSupport::REQUESTABLE &&
118 requirement == EmeFeatureRequirement::Optional) {
119 return EmeConfigRule::SUPPORTED;
121 if (support == EmeFeatureSupport::NOT_SUPPORTED ||
122 requirement == EmeFeatureRequirement::NotAllowed) {
123 return EmeConfigRule::PERSISTENCE_NOT_ALLOWED;
125 return EmeConfigRule::PERSISTENCE_REQUIRED;
128 } // namespace
130 struct KeySystemConfigSelector::SelectionRequest {
131 std::string key_system;
132 blink::WebVector<blink::WebMediaKeySystemConfiguration>
133 candidate_configurations;
134 blink::WebSecurityOrigin security_origin;
135 base::Callback<void(const blink::WebMediaKeySystemConfiguration&,
136 const CdmConfig&)> succeeded_cb;
137 base::Callback<void(const blink::WebString&)> not_supported_cb;
138 bool was_permission_requested = false;
139 bool is_permission_granted = false;
140 bool are_secure_codecs_supported = false;
143 // Accumulates configuration rules to determine if a feature (additional
144 // configuration rule) can be added to an accumulated configuration.
145 class KeySystemConfigSelector::ConfigState {
146 public:
147 ConfigState(bool was_permission_requested, bool is_permission_granted)
148 : was_permission_requested_(was_permission_requested),
149 is_permission_granted_(is_permission_granted) {}
151 bool IsPermissionGranted() const { return is_permission_granted_; }
153 // Permission is possible if it has not been denied.
154 bool IsPermissionPossible() const {
155 return is_permission_granted_ || !was_permission_requested_;
158 bool IsIdentifierRequired() const { return is_identifier_required_; }
160 bool IsIdentifierRecommended() const { return is_identifier_recommended_; }
162 bool AreHwSecureCodecsRequired() const {
163 return are_hw_secure_codecs_required_;
166 // Checks whether a rule is compatible with all previously added rules.
167 bool IsRuleSupported(EmeConfigRule rule) const {
168 switch (rule) {
169 case EmeConfigRule::NOT_SUPPORTED:
170 return false;
171 case EmeConfigRule::IDENTIFIER_NOT_ALLOWED:
172 return !is_identifier_required_;
173 case EmeConfigRule::IDENTIFIER_REQUIRED:
174 // TODO(sandersd): Confirm if we should be refusing these rules when
175 // permission has been denied (as the spec currently says).
176 return !is_identifier_not_allowed_ && IsPermissionPossible();
177 case EmeConfigRule::IDENTIFIER_RECOMMENDED:
178 return true;
179 case EmeConfigRule::PERSISTENCE_NOT_ALLOWED:
180 return !is_persistence_required_;
181 case EmeConfigRule::PERSISTENCE_REQUIRED:
182 return !is_persistence_not_allowed_;
183 case EmeConfigRule::IDENTIFIER_AND_PERSISTENCE_REQUIRED:
184 return (!is_identifier_not_allowed_ && IsPermissionPossible() &&
185 !is_persistence_not_allowed_);
186 case EmeConfigRule::HW_SECURE_CODECS_NOT_ALLOWED:
187 return !are_hw_secure_codecs_required_;
188 case EmeConfigRule::HW_SECURE_CODECS_REQUIRED:
189 return !are_hw_secure_codecs_not_allowed_;
190 case EmeConfigRule::SUPPORTED:
191 return true;
193 NOTREACHED();
194 return false;
197 // Add a rule to the accumulated configuration state.
198 void AddRule(EmeConfigRule rule) {
199 DCHECK(IsRuleSupported(rule));
200 switch (rule) {
201 case EmeConfigRule::NOT_SUPPORTED:
202 NOTREACHED();
203 return;
204 case EmeConfigRule::IDENTIFIER_NOT_ALLOWED:
205 is_identifier_not_allowed_ = true;
206 return;
207 case EmeConfigRule::IDENTIFIER_REQUIRED:
208 is_identifier_required_ = true;
209 return;
210 case EmeConfigRule::IDENTIFIER_RECOMMENDED:
211 is_identifier_recommended_ = true;
212 return;
213 case EmeConfigRule::PERSISTENCE_NOT_ALLOWED:
214 is_persistence_not_allowed_ = true;
215 return;
216 case EmeConfigRule::PERSISTENCE_REQUIRED:
217 is_persistence_required_ = true;
218 return;
219 case EmeConfigRule::IDENTIFIER_AND_PERSISTENCE_REQUIRED:
220 is_identifier_required_ = true;
221 is_persistence_required_ = true;
222 return;
223 case EmeConfigRule::HW_SECURE_CODECS_NOT_ALLOWED:
224 are_hw_secure_codecs_not_allowed_ = true;
225 return;
226 case EmeConfigRule::HW_SECURE_CODECS_REQUIRED:
227 are_hw_secure_codecs_required_ = true;
228 return;
229 case EmeConfigRule::SUPPORTED:
230 return;
232 NOTREACHED();
235 private:
236 // Whether permission to use a distinctive identifier was requested. If set,
237 // |is_permission_granted_| represents the final decision.
238 // (Not changed by adding rules.)
239 bool was_permission_requested_;
241 // Whether permission to use a distinctive identifier has been granted.
242 // (Not changed by adding rules.)
243 bool is_permission_granted_;
245 // Whether a rule has been added that requires or blocks a distinctive
246 // identifier.
247 bool is_identifier_required_ = false;
248 bool is_identifier_not_allowed_ = false;
250 // Whether a rule has been added that recommends a distinctive identifier.
251 bool is_identifier_recommended_ = false;
253 // Whether a rule has been added that requires or blocks persistent state.
254 bool is_persistence_required_ = false;
255 bool is_persistence_not_allowed_ = false;
257 // Whether a rule has been added that requires or blocks hardware-secure
258 // codecs.
259 bool are_hw_secure_codecs_required_ = false;
260 bool are_hw_secure_codecs_not_allowed_ = false;
263 KeySystemConfigSelector::KeySystemConfigSelector(
264 const KeySystems* key_systems,
265 MediaPermission* media_permission)
266 : key_systems_(key_systems),
267 media_permission_(media_permission),
268 weak_factory_(this) {
269 DCHECK(key_systems_);
270 DCHECK(media_permission_);
273 KeySystemConfigSelector::~KeySystemConfigSelector() {
276 bool KeySystemConfigSelector::IsSupportedContentType(
277 const std::string& key_system,
278 EmeMediaType media_type,
279 const std::string& container_mime_type,
280 const std::string& codecs,
281 KeySystemConfigSelector::ConfigState* config_state) {
282 // TODO(sandersd): Move contentType parsing from Blink to here so that invalid
283 // parameters can be rejected. http://crbug.com/417561
284 std::string container_lower = base::ToLowerASCII(container_mime_type);
286 // Check that |container_mime_type| is supported by Chrome.
287 if (!media::IsSupportedMediaMimeType(container_lower))
288 return false;
290 // Check that |codecs| are supported by Chrome. This is done primarily to
291 // validate extended codecs, but it also ensures that the CDM cannot support
292 // codecs that Chrome does not (which could complicate the robustness
293 // algorithm).
294 std::vector<std::string> codec_vector;
295 media::ParseCodecString(codecs, &codec_vector, false);
296 if (!codec_vector.empty() &&
297 (media::IsSupportedStrictMediaMimeType(container_lower, codec_vector) !=
298 media::IsSupported)) {
299 return false;
302 // Check that |container_mime_type| and |codecs| are supported by the CDM.
303 // This check does not handle extended codecs, so extended codec information
304 // is stripped (extended codec information was checked above).
305 std::vector<std::string> stripped_codec_vector;
306 media::ParseCodecString(codecs, &stripped_codec_vector, true);
307 EmeConfigRule codecs_rule = key_systems_->GetContentTypeConfigRule(
308 key_system, media_type, container_lower, stripped_codec_vector);
309 if (!config_state->IsRuleSupported(codecs_rule))
310 return false;
311 config_state->AddRule(codecs_rule);
313 return true;
316 bool KeySystemConfigSelector::GetSupportedCapabilities(
317 const std::string& key_system,
318 EmeMediaType media_type,
319 const blink::WebVector<blink::WebMediaKeySystemMediaCapability>&
320 requested_media_capabilities,
321 KeySystemConfigSelector::ConfigState* config_state,
322 std::vector<blink::WebMediaKeySystemMediaCapability>*
323 supported_media_capabilities) {
324 // From
325 // https://w3c.github.io/encrypted-media/#get-supported-capabilities-for-media-type
326 // 1. Let local accumulated capabilities be a local copy of partial
327 // configuration.
328 // (Skipped as we directly update |config_state|. This is safe because we
329 // only do so when at least one requested media capability is supported.)
330 // 2. Let supported media capabilities be empty.
331 DCHECK_EQ(supported_media_capabilities->size(), 0ul);
332 // 3. For each value in requested media capabilities:
333 for (size_t i = 0; i < requested_media_capabilities.size(); i++) {
334 // 3.1. Let contentType be the value's contentType member.
335 // 3.2. Let robustness be the value's robustness member.
336 const blink::WebMediaKeySystemMediaCapability& capability =
337 requested_media_capabilities[i];
338 // 3.3. If contentType is the empty string, return null.
339 if (capability.mimeType.isEmpty()) {
340 DVLOG(2) << "Rejecting requested configuration because "
341 << "a capability contentType was empty.";
342 return false;
345 // 3.4-3.11. (Implemented by IsSupportedContentType().)
346 ConfigState proposed_config_state = *config_state;
347 if (!base::IsStringASCII(capability.mimeType) ||
348 !base::IsStringASCII(capability.codecs) ||
349 !IsSupportedContentType(key_system, media_type,
350 base::UTF16ToASCII(
351 base::StringPiece16(capability.mimeType)),
352 base::UTF16ToASCII(
353 base::StringPiece16(capability.codecs)),
354 &proposed_config_state)) {
355 continue;
357 // 3.12. If robustness is not the empty string, run the following steps:
358 if (!capability.robustness.isEmpty()) {
359 // 3.12.1. If robustness is an unrecognized value or not supported by
360 // implementation, continue to the next iteration. String
361 // comparison is case-sensitive.
362 if (!base::IsStringASCII(capability.robustness))
363 continue;
364 EmeConfigRule robustness_rule = key_systems_->GetRobustnessConfigRule(
365 key_system, media_type, base::UTF16ToASCII(
366 base::StringPiece16(capability.robustness)));
367 if (!proposed_config_state.IsRuleSupported(robustness_rule))
368 continue;
369 proposed_config_state.AddRule(robustness_rule);
370 // 3.12.2. Add robustness to configuration.
371 // (It's already added, we use capability as configuration.)
373 // 3.13. If the user agent and implementation do not support playback of
374 // encrypted media data as specified by configuration, including all
375 // media types, in combination with local accumulated capabilities,
376 // continue to the next iteration.
377 // (This is handled when adding rules to |proposed_config_state|.)
378 // 3.14. Add configuration to supported media capabilities.
379 supported_media_capabilities->push_back(capability);
380 // 3.15. Add configuration to local accumulated capabilities.
381 *config_state = proposed_config_state;
383 // 4. If supported media capabilities is empty, return null.
384 if (supported_media_capabilities->empty()) {
385 DVLOG(2) << "Rejecting requested configuration because "
386 << "no capabilities were supported.";
387 return false;
389 // 5. Return media type capabilities.
390 return true;
393 KeySystemConfigSelector::ConfigurationSupport
394 KeySystemConfigSelector::GetSupportedConfiguration(
395 const std::string& key_system,
396 const blink::WebMediaKeySystemConfiguration& candidate,
397 ConfigState* config_state,
398 blink::WebMediaKeySystemConfiguration* accumulated_configuration) {
399 // From https://w3c.github.io/encrypted-media/#get-supported-configuration
400 // 1. Let accumulated configuration be empty. (Done by caller.)
401 // 2. If the initDataTypes member is present in candidate configuration, run
402 // the following steps:
403 if (candidate.hasInitDataTypes) {
404 // 2.1. Let supported types be empty.
405 std::vector<blink::WebEncryptedMediaInitDataType> supported_types;
407 // 2.2. For each value in candidate configuration's initDataTypes member:
408 for (size_t i = 0; i < candidate.initDataTypes.size(); i++) {
409 // 2.2.1. Let initDataType be the value.
410 blink::WebEncryptedMediaInitDataType init_data_type =
411 candidate.initDataTypes[i];
412 // 2.2.2. If the implementation supports generating requests based on
413 // initDataType, add initDataType to supported types. String
414 // comparison is case-sensitive. The empty string is never
415 // supported.
416 if (key_systems_->IsSupportedInitDataType(
417 key_system, ConvertToEmeInitDataType(init_data_type))) {
418 supported_types.push_back(init_data_type);
422 // 2.3. If supported types is empty, return null.
423 if (supported_types.empty()) {
424 DVLOG(2) << "Rejecting requested configuration because "
425 << "no initDataType values were supported.";
426 return CONFIGURATION_NOT_SUPPORTED;
429 // 2.4. Add supported types to accumulated configuration.
430 accumulated_configuration->initDataTypes = supported_types;
433 // 3. Follow the steps for the value of candidate configuration's
434 // distinctiveIdentifier member from the following list:
435 // - "required": If the implementation does not support a persistent
436 // Distinctive Identifier in combination with accumulated
437 // configuration, return null.
438 // - "optional": Continue.
439 // - "not-allowed": If the implementation requires a Distinctive
440 // Identifier in combination with accumulated configuration, return
441 // null.
442 // We also reject OPTIONAL when distinctive identifiers are ALWAYS_ENABLED and
443 // permission has already been denied. This would happen anyway at step 11.
444 EmeConfigRule di_rule = GetDistinctiveIdentifierConfigRule(
445 key_systems_->GetDistinctiveIdentifierSupport(key_system),
446 candidate.distinctiveIdentifier);
447 if (!config_state->IsRuleSupported(di_rule)) {
448 DVLOG(2) << "Rejecting requested configuration because "
449 << "the distinctiveIdentifier requirement was not supported.";
450 return CONFIGURATION_NOT_SUPPORTED;
452 config_state->AddRule(di_rule);
454 // 4. Add the value of the candidate configuration's distinctiveIdentifier
455 // member to accumulated configuration.
456 accumulated_configuration->distinctiveIdentifier =
457 candidate.distinctiveIdentifier;
459 // 5. Follow the steps for the value of candidate configuration's
460 // persistentState member from the following list:
461 // - "required": If the implementation does not support persisting state
462 // in combination with accumulated configuration, return null.
463 // - "optional": Continue.
464 // - "not-allowed": If the implementation requires persisting state in
465 // combination with accumulated configuration, return null.
466 EmeConfigRule ps_rule = GetPersistentStateConfigRule(
467 key_systems_->GetPersistentStateSupport(key_system),
468 candidate.persistentState);
469 if (!config_state->IsRuleSupported(ps_rule)) {
470 DVLOG(2) << "Rejecting requested configuration because "
471 << "the persistentState requirement was not supported.";
472 return CONFIGURATION_NOT_SUPPORTED;
474 config_state->AddRule(ps_rule);
476 // 6. Add the value of the candidate configuration's persistentState
477 // member to accumulated configuration.
478 accumulated_configuration->persistentState = candidate.persistentState;
480 // 7. Follow the steps for the first matching condition from the following
481 // list:
482 // - If the sessionTypes member is present in candidate configuration,
483 // let session types be candidate configuration's sessionTypes member.
484 // - Otherwise, let session types be [ "temporary" ].
485 blink::WebVector<blink::WebEncryptedMediaSessionType> session_types;
486 if (candidate.hasSessionTypes) {
487 session_types = candidate.sessionTypes;
488 } else {
489 std::vector<blink::WebEncryptedMediaSessionType> temporary(1);
490 temporary[0] = blink::WebEncryptedMediaSessionType::Temporary;
491 session_types = temporary;
494 // 8. For each value in session types:
495 for (size_t i = 0; i < session_types.size(); i++) {
496 // 8.1. Let session type be the value.
497 blink::WebEncryptedMediaSessionType session_type = session_types[i];
498 // 8.2. If the implementation does not support session type in combination
499 // with accumulated configuration, return null.
500 // 8.3. If session type is "persistent-license" or
501 // "persistent-release-message", follow the steps for accumulated
502 // configuration's persistentState value from the following list:
503 // - "required": Continue.
504 // - "optional": Change accumulated configuration's persistentState
505 // value to "required".
506 // - "not-allowed": Return null.
507 EmeConfigRule session_type_rule = EmeConfigRule::NOT_SUPPORTED;
508 switch (session_type) {
509 case blink::WebEncryptedMediaSessionType::Unknown:
510 DVLOG(2) << "Rejecting requested configuration because "
511 << "a required session type was not recognized.";
512 return CONFIGURATION_NOT_SUPPORTED;
513 case blink::WebEncryptedMediaSessionType::Temporary:
514 session_type_rule = EmeConfigRule::SUPPORTED;
515 break;
516 case blink::WebEncryptedMediaSessionType::PersistentLicense:
517 session_type_rule = GetSessionTypeConfigRule(
518 key_systems_->GetPersistentLicenseSessionSupport(key_system));
519 break;
520 case blink::WebEncryptedMediaSessionType::PersistentReleaseMessage:
521 session_type_rule = GetSessionTypeConfigRule(
522 key_systems_->GetPersistentReleaseMessageSessionSupport(
523 key_system));
524 break;
526 if (!config_state->IsRuleSupported(session_type_rule)) {
527 DVLOG(2) << "Rejecting requested configuration because "
528 << "a required session type was not supported.";
529 return CONFIGURATION_NOT_SUPPORTED;
531 config_state->AddRule(session_type_rule);
534 // 9. Add session types to accumulated configuration.
535 accumulated_configuration->sessionTypes = session_types;
537 // 10. If the videoCapabilities member is present in candidate configuration:
538 if (candidate.hasVideoCapabilities) {
539 // 10.1. Let video capabilities be the result of executing the Get Supported
540 // Capabilities for Media Type algorithm on Video, candidate
541 // configuration's videoCapabilities member, and accumulated
542 // configuration.
543 // 10.2. If video capabilities is null, return null.
544 std::vector<blink::WebMediaKeySystemMediaCapability> video_capabilities;
545 if (!GetSupportedCapabilities(key_system, EmeMediaType::VIDEO,
546 candidate.videoCapabilities, config_state,
547 &video_capabilities)) {
548 return CONFIGURATION_NOT_SUPPORTED;
551 // 10.3. Add video capabilities to accumulated configuration.
552 accumulated_configuration->videoCapabilities = video_capabilities;
555 // 11. If the audioCapabilities member is present in candidate configuration:
556 if (candidate.hasAudioCapabilities) {
557 // 11.1. Let audio capabilities be the result of executing the Get Supported
558 // Capabilities for Media Type algorithm on Audio, candidate
559 // configuration's audioCapabilities member, and accumulated
560 // configuration.
561 // 11.2. If audio capabilities is null, return null.
562 std::vector<blink::WebMediaKeySystemMediaCapability> audio_capabilities;
563 if (!GetSupportedCapabilities(key_system, EmeMediaType::AUDIO,
564 candidate.audioCapabilities, config_state,
565 &audio_capabilities)) {
566 return CONFIGURATION_NOT_SUPPORTED;
569 // 11.3. Add audio capabilities to accumulated configuration.
570 accumulated_configuration->audioCapabilities = audio_capabilities;
573 // 12. If accumulated configuration's distinctiveIdentifier value is
574 // "optional", follow the steps for the first matching condition from the
575 // following list:
576 // - If the implementation requires a Distinctive Identifier for any of
577 // the combinations in accumulated configuration, change accumulated
578 // configuration's distinctiveIdentifier value to "required".
579 // - Otherwise, change accumulated configuration's distinctiveIdentifier
580 // value to "not-allowed".
581 if (accumulated_configuration->distinctiveIdentifier ==
582 blink::WebMediaKeySystemConfiguration::Requirement::Optional) {
583 EmeConfigRule not_allowed_rule = GetDistinctiveIdentifierConfigRule(
584 key_systems_->GetDistinctiveIdentifierSupport(key_system),
585 EmeFeatureRequirement::NotAllowed);
586 EmeConfigRule required_rule = GetDistinctiveIdentifierConfigRule(
587 key_systems_->GetDistinctiveIdentifierSupport(key_system),
588 EmeFeatureRequirement::Required);
589 bool not_allowed_supported =
590 config_state->IsRuleSupported(not_allowed_rule);
591 bool required_supported = config_state->IsRuleSupported(required_rule);
592 // If a distinctive identifier is recommend and that is a possible outcome,
593 // prefer that.
594 if (required_supported && config_state->IsIdentifierRecommended() &&
595 config_state->IsPermissionPossible()) {
596 not_allowed_supported = false;
598 if (not_allowed_supported) {
599 accumulated_configuration->distinctiveIdentifier =
600 blink::WebMediaKeySystemConfiguration::Requirement::NotAllowed;
601 config_state->AddRule(not_allowed_rule);
602 } else if (required_supported) {
603 accumulated_configuration->distinctiveIdentifier =
604 blink::WebMediaKeySystemConfiguration::Requirement::Required;
605 config_state->AddRule(required_rule);
606 } else {
607 // We should not have passed step 3.
608 NOTREACHED();
609 return CONFIGURATION_NOT_SUPPORTED;
613 // 13. If accumulated configuration's persistentState value is "optional",
614 // follow the steps for the first matching condition from the following
615 // list:
616 // - If the implementation requires persisting state for any of the
617 // combinations in accumulated configuration, change accumulated
618 // configuration's persistentState value to "required".
619 // - Otherwise, change accumulated configuration's persistentState value
620 // to "not-allowed".
621 if (accumulated_configuration->persistentState ==
622 blink::WebMediaKeySystemConfiguration::Requirement::Optional) {
623 EmeConfigRule not_allowed_rule = GetPersistentStateConfigRule(
624 key_systems_->GetPersistentStateSupport(key_system),
625 EmeFeatureRequirement::NotAllowed);
626 EmeConfigRule required_rule = GetPersistentStateConfigRule(
627 key_systems_->GetPersistentStateSupport(key_system),
628 EmeFeatureRequirement::Required);
629 // |distinctiveIdentifier| should not be affected after it is decided.
630 DCHECK(not_allowed_rule == EmeConfigRule::NOT_SUPPORTED ||
631 not_allowed_rule == EmeConfigRule::PERSISTENCE_NOT_ALLOWED);
632 DCHECK(required_rule == EmeConfigRule::NOT_SUPPORTED ||
633 required_rule == EmeConfigRule::PERSISTENCE_REQUIRED);
634 bool not_allowed_supported =
635 config_state->IsRuleSupported(not_allowed_rule);
636 bool required_supported = config_state->IsRuleSupported(required_rule);
637 if (not_allowed_supported) {
638 accumulated_configuration->persistentState =
639 blink::WebMediaKeySystemConfiguration::Requirement::NotAllowed;
640 config_state->AddRule(not_allowed_rule);
641 } else if (required_supported) {
642 accumulated_configuration->persistentState =
643 blink::WebMediaKeySystemConfiguration::Requirement::Required;
644 config_state->AddRule(required_rule);
645 } else {
646 // We should not have passed step 5.
647 NOTREACHED();
648 return CONFIGURATION_NOT_SUPPORTED;
652 // 14. If implementation in the configuration specified by the combination of
653 // the values in accumulated configuration is not supported or not allowed
654 // in the origin, return null.
655 // 15. If accumulated configuration's distinctiveIdentifier value is
656 // "required", [prompt the user for consent].
657 if (accumulated_configuration->distinctiveIdentifier ==
658 blink::WebMediaKeySystemConfiguration::Requirement::Required) {
659 // The caller is responsible for resolving what to do if permission is
660 // required but has been denied (it should treat it as NOT_SUPPORTED).
661 if (!config_state->IsPermissionGranted())
662 return CONFIGURATION_REQUIRES_PERMISSION;
665 // 16. If the label member is present in candidate configuration, add the
666 // value of the candidate configuration's label member to accumulated
667 // configuration.
668 accumulated_configuration->label = candidate.label;
670 // 17. Return accumulated configuration.
671 return CONFIGURATION_SUPPORTED;
674 void KeySystemConfigSelector::SelectConfig(
675 const blink::WebString& key_system,
676 const blink::WebVector<blink::WebMediaKeySystemConfiguration>&
677 candidate_configurations,
678 const blink::WebSecurityOrigin& security_origin,
679 bool are_secure_codecs_supported,
680 base::Callback<void(const blink::WebMediaKeySystemConfiguration&,
681 const CdmConfig&)> succeeded_cb,
682 base::Callback<void(const blink::WebString&)> not_supported_cb) {
683 // Continued from requestMediaKeySystemAccess(), step 7, from
684 // https://w3c.github.io/encrypted-media/#requestmediakeysystemaccess
686 // 7.1. If keySystem is not one of the Key Systems supported by the user
687 // agent, reject promise with with a new DOMException whose name is
688 // NotSupportedError. String comparison is case-sensitive.
689 if (!base::IsStringASCII(key_system)) {
690 not_supported_cb.Run("Only ASCII keySystems are supported");
691 return;
694 std::string key_system_ascii =
695 base::UTF16ToASCII(base::StringPiece16(key_system));
696 if (!key_systems_->IsSupportedKeySystem(key_system_ascii)) {
697 not_supported_cb.Run("Unsupported keySystem");
698 return;
701 // 7.2-7.4. Implemented by OnSelectConfig().
702 // TODO(sandersd): This should be async, ideally not on the main thread.
703 scoped_ptr<SelectionRequest> request(new SelectionRequest());
704 request->key_system = key_system_ascii;
705 request->candidate_configurations = candidate_configurations;
706 request->security_origin = security_origin;
707 request->are_secure_codecs_supported = are_secure_codecs_supported;
708 request->succeeded_cb = succeeded_cb;
709 request->not_supported_cb = not_supported_cb;
710 SelectConfigInternal(request.Pass());
713 void KeySystemConfigSelector::SelectConfigInternal(
714 scoped_ptr<SelectionRequest> request) {
715 // Continued from requestMediaKeySystemAccess(), step 7.1, from
716 // https://w3c.github.io/encrypted-media/#requestmediakeysystemaccess
718 // 7.2. Let implementation be the implementation of keySystem.
719 // (|key_systems_| fills this role.)
720 // 7.3. For each value in supportedConfigurations:
721 for (size_t i = 0; i < request->candidate_configurations.size(); i++) {
722 // 7.3.1. Let candidate configuration be the value.
723 // 7.3.2. Let supported configuration be the result of executing the Get
724 // Supported Configuration algorithm on implementation, candidate
725 // configuration, and origin.
726 // 7.3.3. If supported configuration is not null, [initialize and return a
727 // new MediaKeySystemAccess object.]
728 ConfigState config_state(request->was_permission_requested,
729 request->is_permission_granted);
730 DCHECK(config_state.IsRuleSupported(
731 EmeConfigRule::HW_SECURE_CODECS_NOT_ALLOWED));
732 if (!request->are_secure_codecs_supported)
733 config_state.AddRule(EmeConfigRule::HW_SECURE_CODECS_NOT_ALLOWED);
734 blink::WebMediaKeySystemConfiguration accumulated_configuration;
735 CdmConfig cdm_config;
736 ConfigurationSupport support = GetSupportedConfiguration(
737 request->key_system, request->candidate_configurations[i],
738 &config_state, &accumulated_configuration);
739 switch (support) {
740 case CONFIGURATION_NOT_SUPPORTED:
741 continue;
742 case CONFIGURATION_REQUIRES_PERMISSION:
743 if (request->was_permission_requested) {
744 DVLOG(2) << "Rejecting requested configuration because "
745 << "permission was denied.";
746 continue;
749 // Note: the GURL must not be constructed inline because
750 // base::Passed(&request) sets |request| to null.
751 GURL security_origin(request->security_origin.toString());
752 media_permission_->RequestPermission(
753 MediaPermission::PROTECTED_MEDIA_IDENTIFIER, security_origin,
754 base::Bind(&KeySystemConfigSelector::OnPermissionResult,
755 weak_factory_.GetWeakPtr(), base::Passed(&request)));
757 return;
758 case CONFIGURATION_SUPPORTED:
759 cdm_config.allow_distinctive_identifier =
760 (accumulated_configuration.distinctiveIdentifier ==
761 blink::WebMediaKeySystemConfiguration::Requirement::Required);
762 cdm_config.allow_persistent_state =
763 (accumulated_configuration.persistentState ==
764 blink::WebMediaKeySystemConfiguration::Requirement::Required);
765 cdm_config.use_hw_secure_codecs =
766 config_state.AreHwSecureCodecsRequired();
767 request->succeeded_cb.Run(accumulated_configuration, cdm_config);
768 return;
772 // 7.4. Reject promise with a new DOMException whose name is
773 // NotSupportedError.
774 request->not_supported_cb.Run(
775 "None of the requested configurations were supported.");
778 void KeySystemConfigSelector::OnPermissionResult(
779 scoped_ptr<SelectionRequest> request,
780 bool is_permission_granted) {
781 request->was_permission_requested = true;
782 request->is_permission_granted = is_permission_granted;
783 SelectConfigInternal(request.Pass());
786 } // namespace media