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