Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / media / blink / key_system_config_selector.cc
blob67a13e26ec0ab363d2b528dab09ead9a4ef51bd0
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 case EmeConfigRule::SUPPORTED:
181 return true;
183 NOTREACHED();
184 return false;
187 // Add a rule to the accumulated configuration state.
188 void AddRule(EmeConfigRule rule) {
189 DCHECK(IsRuleSupported(rule));
190 switch (rule) {
191 case EmeConfigRule::NOT_SUPPORTED:
192 NOTREACHED();
193 return;
194 case EmeConfigRule::IDENTIFIER_NOT_ALLOWED:
195 is_identifier_not_allowed_ = true;
196 return;
197 case EmeConfigRule::IDENTIFIER_REQUIRED:
198 is_identifier_required_ = true;
199 return;
200 case EmeConfigRule::IDENTIFIER_RECOMMENDED:
201 is_identifier_recommended_ = true;
202 return;
203 case EmeConfigRule::PERSISTENCE_NOT_ALLOWED:
204 is_persistence_not_allowed_ = true;
205 return;
206 case EmeConfigRule::PERSISTENCE_REQUIRED:
207 is_persistence_required_ = true;
208 return;
209 case EmeConfigRule::IDENTIFIER_AND_PERSISTENCE_REQUIRED:
210 is_identifier_required_ = true;
211 is_persistence_required_ = true;
212 return;
213 case EmeConfigRule::SUPPORTED:
214 return;
216 NOTREACHED();
219 private:
220 // Whether permission to use a distinctive identifier was requested. If set,
221 // |is_permission_granted_| represents the final decision.
222 const bool was_permission_requested_;
224 // Whether permission to use a distinctive identifier has been granted.
225 const bool is_permission_granted_;
227 // Whether a rule has been added that requires or blocks a distinctive
228 // identifier.
229 bool is_identifier_required_ = false;
230 bool is_identifier_not_allowed_ = false;
232 // Whether a rule has been added that recommends a distinctive identifier.
233 bool is_identifier_recommended_ = false;
235 // Whether a rule has been added that requires or blocks persistent state.
236 bool is_persistence_required_ = false;
237 bool is_persistence_not_allowed_ = false;
239 DISALLOW_COPY_AND_ASSIGN(ConfigState);
242 KeySystemConfigSelector::KeySystemConfigSelector(
243 const KeySystems* key_systems,
244 MediaPermission* media_permission)
245 : key_systems_(key_systems),
246 media_permission_(media_permission),
247 weak_factory_(this) {
248 DCHECK(key_systems_);
249 DCHECK(media_permission_);
252 KeySystemConfigSelector::~KeySystemConfigSelector() {
255 bool KeySystemConfigSelector::IsSupportedContentType(
256 const std::string& key_system,
257 EmeMediaType media_type,
258 const std::string& container_mime_type,
259 const std::string& codecs) {
260 // TODO(sandersd): Move contentType parsing from Blink to here so that invalid
261 // parameters can be rejected. http://crbug.com/417561
262 std::string container_lower = base::StringToLowerASCII(container_mime_type);
264 // Check that |container_mime_type| and |codecs| are supported by the CDM.
265 // This check does not handle extended codecs, so extended codec information
266 // is stripped.
267 std::vector<std::string> codec_vector;
268 net::ParseCodecString(codecs, &codec_vector, true);
269 if (!key_systems_->IsSupportedCodecCombination(
270 key_system, media_type, container_lower, codec_vector)) {
271 return false;
274 // Check that |container_mime_type| is supported by Chrome. This can only
275 // happen if the CDM declares support for a container that Chrome does not.
276 if (!net::IsSupportedMediaMimeType(container_lower)) {
277 NOTREACHED();
278 return false;
281 // Check that |codecs| are supported by Chrome. This is done primarily to
282 // validate extended codecs, but it also ensures that the CDM cannot support
283 // codecs that Chrome does not (which could complicate the robustness
284 // algorithm).
285 if (codec_vector.empty())
286 return true;
287 codec_vector.clear();
288 net::ParseCodecString(codecs, &codec_vector, false);
289 return (net::IsSupportedStrictMediaMimeType(container_lower, codec_vector) ==
290 net::IsSupported);
293 bool KeySystemConfigSelector::GetSupportedCapabilities(
294 const std::string& key_system,
295 EmeMediaType media_type,
296 const blink::WebVector<blink::WebMediaKeySystemMediaCapability>&
297 requested_media_capabilities,
298 KeySystemConfigSelector::ConfigState* config_state,
299 std::vector<blink::WebMediaKeySystemMediaCapability>*
300 supported_media_capabilities) {
301 // From
302 // https://w3c.github.io/encrypted-media/#get-supported-capabilities-for-media-type
303 // 1. Let local accumulated capabilities be a local copy of partial
304 // configuration.
305 // (Skipped as we directly update |config_state|. This is safe because we
306 // only do so when at least one requested media capability is supported.)
307 // 2. Let supported media capabilities be empty.
308 DCHECK_EQ(supported_media_capabilities->size(), 0ul);
309 // 3. For each value in requested media capabilities:
310 for (size_t i = 0; i < requested_media_capabilities.size(); i++) {
311 // 3.1. Let contentType be the value's contentType member.
312 // 3.2. Let robustness be the value's robustness member.
313 const blink::WebMediaKeySystemMediaCapability& capability =
314 requested_media_capabilities[i];
315 // 3.3. If contentType is the empty string, return null.
316 if (capability.mimeType.isEmpty()) {
317 DVLOG(2) << "Rejecting requested configuration because "
318 << "a capability contentType was empty.";
319 return false;
321 // 3.4-3.11. (Implemented by IsSupportedContentType().)
322 if (!base::IsStringASCII(capability.mimeType) ||
323 !base::IsStringASCII(capability.codecs) ||
324 !IsSupportedContentType(key_system, media_type,
325 base::UTF16ToASCII(capability.mimeType),
326 base::UTF16ToASCII(capability.codecs))) {
327 continue;
329 // 3.12. If robustness is not the empty string, run the following steps:
330 if (!capability.robustness.isEmpty()) {
331 // 3.12.1. If robustness is an unrecognized value or not supported by
332 // implementation, continue to the next iteration. String
333 // comparison is case-sensitive.
334 if (!base::IsStringASCII(capability.robustness))
335 continue;
336 EmeConfigRule robustness_rule = key_systems_->GetRobustnessConfigRule(
337 key_system, media_type, base::UTF16ToASCII(capability.robustness));
338 if (!config_state->IsRuleSupported(robustness_rule))
339 continue;
340 config_state->AddRule(robustness_rule);
341 // 3.12.2. Add robustness to configuration.
342 // (It's already added, we use capability as configuration.)
344 // 3.13. If the user agent and implementation do not support playback of
345 // encrypted media data as specified by configuration, including all
346 // media types, in combination with local accumulated capabilities,
347 // continue to the next iteration.
348 // (This is handled when adding rules to |config_state|.)
349 // 3.14. Add configuration to supported media capabilities.
350 supported_media_capabilities->push_back(capability);
351 // 3.15. Add configuration to local accumulated capabilities.
352 // (Skipped as we directly update |config_state|.)
354 // 4. If supported media capabilities is empty, return null.
355 if (supported_media_capabilities->empty()) {
356 DVLOG(2) << "Rejecting requested configuration because "
357 << "no capabilities were supported.";
358 return false;
360 // 5. Return media type capabilities.
361 return true;
364 KeySystemConfigSelector::ConfigurationSupport
365 KeySystemConfigSelector::GetSupportedConfiguration(
366 const std::string& key_system,
367 const blink::WebMediaKeySystemConfiguration& candidate,
368 ConfigState* config_state,
369 blink::WebMediaKeySystemConfiguration* accumulated_configuration) {
370 // From https://w3c.github.io/encrypted-media/#get-supported-configuration
371 // 1. Let accumulated configuration be empty. (Done by caller.)
372 // 2. If the initDataTypes member is present in candidate configuration, run
373 // the following steps:
374 if (candidate.hasInitDataTypes) {
375 // 2.1. Let supported types be empty.
376 std::vector<blink::WebEncryptedMediaInitDataType> supported_types;
378 // 2.2. For each value in candidate configuration's initDataTypes member:
379 for (size_t i = 0; i < candidate.initDataTypes.size(); i++) {
380 // 2.2.1. Let initDataType be the value.
381 blink::WebEncryptedMediaInitDataType init_data_type =
382 candidate.initDataTypes[i];
383 // 2.2.2. If the implementation supports generating requests based on
384 // initDataType, add initDataType to supported types. String
385 // comparison is case-sensitive. The empty string is never
386 // supported.
387 if (key_systems_->IsSupportedInitDataType(
388 key_system, ConvertToEmeInitDataType(init_data_type))) {
389 supported_types.push_back(init_data_type);
393 // 2.3. If supported types is empty, return null.
394 if (supported_types.empty()) {
395 DVLOG(2) << "Rejecting requested configuration because "
396 << "no initDataType values were supported.";
397 return CONFIGURATION_NOT_SUPPORTED;
400 // 2.4. Add supported types to accumulated configuration.
401 accumulated_configuration->initDataTypes = supported_types;
404 // 3. Follow the steps for the value of candidate configuration's
405 // distinctiveIdentifier member from the following list:
406 // - "required": If the implementation does not support a persistent
407 // Distinctive Identifier in combination with accumulated
408 // configuration, return null.
409 // - "optional": Continue.
410 // - "not-allowed": If the implementation requires a Distinctive
411 // Identifier in combination with accumulated configuration, return
412 // null.
413 // We also reject OPTIONAL when distinctive identifiers are ALWAYS_ENABLED and
414 // permission has already been denied. This would happen anyway at step 11.
415 EmeConfigRule di_rule = GetDistinctiveIdentifierConfigRule(
416 key_systems_->GetDistinctiveIdentifierSupport(key_system),
417 candidate.distinctiveIdentifier);
418 if (!config_state->IsRuleSupported(di_rule)) {
419 DVLOG(2) << "Rejecting requested configuration because "
420 << "the distinctiveIdentifier requirement was not supported.";
421 return CONFIGURATION_NOT_SUPPORTED;
423 config_state->AddRule(di_rule);
425 // 4. Add the value of the candidate configuration's distinctiveIdentifier
426 // member to accumulated configuration.
427 accumulated_configuration->distinctiveIdentifier =
428 candidate.distinctiveIdentifier;
430 // 5. Follow the steps for the value of candidate configuration's
431 // persistentState member from the following list:
432 // - "required": If the implementation does not support persisting state
433 // in combination with accumulated configuration, return null.
434 // - "optional": Continue.
435 // - "not-allowed": If the implementation requires persisting state in
436 // combination with accumulated configuration, return null.
437 EmeConfigRule ps_rule = GetPersistentStateConfigRule(
438 key_systems_->GetPersistentStateSupport(key_system),
439 candidate.persistentState);
440 if (!config_state->IsRuleSupported(ps_rule)) {
441 DVLOG(2) << "Rejecting requested configuration because "
442 << "the persistentState requirement was not supported.";
443 return CONFIGURATION_NOT_SUPPORTED;
445 config_state->AddRule(ps_rule);
447 // 6. Add the value of the candidate configuration's persistentState
448 // member to accumulated configuration.
449 accumulated_configuration->persistentState = candidate.persistentState;
451 // 7. Follow the steps for the first matching condition from the following
452 // list:
453 // - If the sessionTypes member is present in candidate configuration,
454 // let session types be candidate configuration's sessionTypes member.
455 // - Otherwise, let session types be [ "temporary" ].
456 blink::WebVector<blink::WebEncryptedMediaSessionType> session_types;
457 if (candidate.hasSessionTypes) {
458 session_types = candidate.sessionTypes;
459 } else {
460 std::vector<blink::WebEncryptedMediaSessionType> temporary(1);
461 temporary[0] = blink::WebEncryptedMediaSessionType::Temporary;
462 session_types = temporary;
465 // 8. For each value in session types:
466 for (size_t i = 0; i < session_types.size(); i++) {
467 // 8.1. Let session type be the value.
468 blink::WebEncryptedMediaSessionType session_type = session_types[i];
469 // 8.2. If the implementation does not support session type in combination
470 // with accumulated configuration, return null.
471 // 8.3. If session type is "persistent-license" or
472 // "persistent-release-message", follow the steps for accumulated
473 // configuration's persistentState value from the following list:
474 // - "required": Continue.
475 // - "optional": Change accumulated configuration's persistentState
476 // value to "required".
477 // - "not-allowed": Return null.
478 EmeConfigRule session_type_rule = EmeConfigRule::NOT_SUPPORTED;
479 switch (session_type) {
480 case blink::WebEncryptedMediaSessionType::Unknown:
481 DVLOG(2) << "Rejecting requested configuration because "
482 << "a required session type was not recognized.";
483 return CONFIGURATION_NOT_SUPPORTED;
484 case blink::WebEncryptedMediaSessionType::Temporary:
485 session_type_rule = EmeConfigRule::SUPPORTED;
486 break;
487 case blink::WebEncryptedMediaSessionType::PersistentLicense:
488 session_type_rule = GetSessionTypeConfigRule(
489 key_systems_->GetPersistentLicenseSessionSupport(key_system));
490 break;
491 case blink::WebEncryptedMediaSessionType::PersistentReleaseMessage:
492 session_type_rule = GetSessionTypeConfigRule(
493 key_systems_->GetPersistentReleaseMessageSessionSupport(
494 key_system));
495 break;
497 if (!config_state->IsRuleSupported(session_type_rule)) {
498 DVLOG(2) << "Rejecting requested configuration because "
499 << "a required session type was not supported.";
500 return CONFIGURATION_NOT_SUPPORTED;
502 config_state->AddRule(session_type_rule);
505 // 9. Add session types to accumulated configuration.
506 accumulated_configuration->sessionTypes = session_types;
508 // 10. If the videoCapabilities member is present in candidate configuration:
509 if (candidate.hasVideoCapabilities) {
510 // 10.1. Let video capabilities be the result of executing the Get Supported
511 // Capabilities for Media Type algorithm on Video, candidate
512 // configuration's videoCapabilities member, and accumulated
513 // configuration.
514 // 10.2. If video capabilities is null, return null.
515 std::vector<blink::WebMediaKeySystemMediaCapability> video_capabilities;
516 if (!GetSupportedCapabilities(key_system, EmeMediaType::VIDEO,
517 candidate.videoCapabilities, config_state,
518 &video_capabilities)) {
519 return CONFIGURATION_NOT_SUPPORTED;
522 // 10.3. Add video capabilities to accumulated configuration.
523 accumulated_configuration->videoCapabilities = video_capabilities;
526 // 11. If the audioCapabilities member is present in candidate configuration:
527 if (candidate.hasAudioCapabilities) {
528 // 11.1. Let audio capabilities be the result of executing the Get Supported
529 // Capabilities for Media Type algorithm on Audio, candidate
530 // configuration's audioCapabilities member, and accumulated
531 // configuration.
532 // 11.2. If audio capabilities is null, return null.
533 std::vector<blink::WebMediaKeySystemMediaCapability> audio_capabilities;
534 if (!GetSupportedCapabilities(key_system, EmeMediaType::AUDIO,
535 candidate.audioCapabilities, config_state,
536 &audio_capabilities)) {
537 return CONFIGURATION_NOT_SUPPORTED;
540 // 11.3. Add audio capabilities to accumulated configuration.
541 accumulated_configuration->audioCapabilities = audio_capabilities;
544 // 12. If accumulated configuration's distinctiveIdentifier value is
545 // "optional", follow the steps for the first matching condition from the
546 // following list:
547 // - If the implementation requires a Distinctive Identifier for any of
548 // the combinations in accumulated configuration, change accumulated
549 // configuration's distinctiveIdentifier value to "required".
550 // - Otherwise, change accumulated configuration's distinctiveIdentifier
551 // value to "not-allowed".
552 if (accumulated_configuration->distinctiveIdentifier ==
553 blink::WebMediaKeySystemConfiguration::Requirement::Optional) {
554 EmeConfigRule not_allowed_rule = GetDistinctiveIdentifierConfigRule(
555 key_systems_->GetDistinctiveIdentifierSupport(key_system),
556 EmeFeatureRequirement::NotAllowed);
557 EmeConfigRule required_rule = GetDistinctiveIdentifierConfigRule(
558 key_systems_->GetDistinctiveIdentifierSupport(key_system),
559 EmeFeatureRequirement::Required);
560 bool not_allowed_supported =
561 config_state->IsRuleSupported(not_allowed_rule);
562 bool required_supported = config_state->IsRuleSupported(required_rule);
563 // If a distinctive identifier is recommend and that is a possible outcome,
564 // prefer that.
565 if (required_supported && config_state->IsIdentifierRecommended() &&
566 config_state->IsPermissionPossible()) {
567 not_allowed_supported = false;
569 if (not_allowed_supported) {
570 accumulated_configuration->distinctiveIdentifier =
571 blink::WebMediaKeySystemConfiguration::Requirement::NotAllowed;
572 config_state->AddRule(not_allowed_rule);
573 } else if (required_supported) {
574 accumulated_configuration->distinctiveIdentifier =
575 blink::WebMediaKeySystemConfiguration::Requirement::Required;
576 config_state->AddRule(required_rule);
577 } else {
578 // We should not have passed step 3.
579 NOTREACHED();
580 return CONFIGURATION_NOT_SUPPORTED;
584 // 13. If accumulated configuration's persistentState value is "optional",
585 // follow the steps for the first matching condition from the following
586 // list:
587 // - If the implementation requires persisting state for any of the
588 // combinations in accumulated configuration, change accumulated
589 // configuration's persistentState value to "required".
590 // - Otherwise, change accumulated configuration's persistentState value
591 // to "not-allowed".
592 if (accumulated_configuration->persistentState ==
593 blink::WebMediaKeySystemConfiguration::Requirement::Optional) {
594 EmeConfigRule not_allowed_rule = GetPersistentStateConfigRule(
595 key_systems_->GetPersistentStateSupport(key_system),
596 EmeFeatureRequirement::NotAllowed);
597 EmeConfigRule required_rule = GetPersistentStateConfigRule(
598 key_systems_->GetPersistentStateSupport(key_system),
599 EmeFeatureRequirement::Required);
600 // |distinctiveIdentifier| should not be affected after it is decided.
601 DCHECK(not_allowed_rule == EmeConfigRule::NOT_SUPPORTED ||
602 not_allowed_rule == EmeConfigRule::PERSISTENCE_NOT_ALLOWED);
603 DCHECK(required_rule == EmeConfigRule::NOT_SUPPORTED ||
604 required_rule == EmeConfigRule::PERSISTENCE_REQUIRED);
605 bool not_allowed_supported =
606 config_state->IsRuleSupported(not_allowed_rule);
607 bool required_supported = config_state->IsRuleSupported(required_rule);
608 if (not_allowed_supported) {
609 accumulated_configuration->persistentState =
610 blink::WebMediaKeySystemConfiguration::Requirement::NotAllowed;
611 config_state->AddRule(not_allowed_rule);
612 } else if (required_supported) {
613 accumulated_configuration->persistentState =
614 blink::WebMediaKeySystemConfiguration::Requirement::Required;
615 config_state->AddRule(required_rule);
616 } else {
617 // We should not have passed step 5.
618 NOTREACHED();
619 return CONFIGURATION_NOT_SUPPORTED;
623 // 14. If implementation in the configuration specified by the combination of
624 // the values in accumulated configuration is not supported or not allowed
625 // in the origin, return null.
626 // 15. If accumulated configuration's distinctiveIdentifier value is
627 // "required", [prompt the user for consent].
628 if (accumulated_configuration->distinctiveIdentifier ==
629 blink::WebMediaKeySystemConfiguration::Requirement::Required) {
630 // The caller is responsible for resolving what to do if permission is
631 // required but has been denied (it should treat it as NOT_SUPPORTED).
632 if (!config_state->IsPermissionGranted())
633 return CONFIGURATION_REQUIRES_PERMISSION;
636 // 16. If the label member is present in candidate configuration, add the
637 // value of the candidate configuration's label member to accumulated
638 // configuration.
639 accumulated_configuration->label = candidate.label;
641 // 17. Return accumulated configuration.
642 return CONFIGURATION_SUPPORTED;
645 void KeySystemConfigSelector::SelectConfig(
646 const blink::WebString& key_system,
647 const blink::WebVector<blink::WebMediaKeySystemConfiguration>&
648 candidate_configurations,
649 const blink::WebSecurityOrigin& security_origin,
650 base::Callback<void(const blink::WebMediaKeySystemConfiguration&)>
651 succeeded_cb,
652 base::Callback<void(const blink::WebString&)> not_supported_cb) {
653 // Continued from requestMediaKeySystemAccess(), step 7, from
654 // https://w3c.github.io/encrypted-media/#requestmediakeysystemaccess
656 // 7.1. If keySystem is not one of the Key Systems supported by the user
657 // agent, reject promise with with a new DOMException whose name is
658 // NotSupportedError. String comparison is case-sensitive.
659 if (!base::IsStringASCII(key_system)) {
660 not_supported_cb.Run("Only ASCII keySystems are supported");
661 return;
664 std::string key_system_ascii = base::UTF16ToASCII(key_system);
665 if (!key_systems_->IsSupportedKeySystem(key_system_ascii)) {
666 not_supported_cb.Run("Unsupported keySystem");
667 return;
670 // 7.2-7.4. Implemented by OnSelectConfig().
671 // TODO(sandersd): This should be async, ideally not on the main thread.
672 scoped_ptr<SelectionRequest> request(new SelectionRequest());
673 request->key_system = key_system_ascii;
674 request->candidate_configurations = candidate_configurations;
675 request->security_origin = security_origin;
676 request->succeeded_cb = succeeded_cb;
677 request->not_supported_cb = not_supported_cb;
678 SelectConfigInternal(request.Pass());
681 void KeySystemConfigSelector::SelectConfigInternal(
682 scoped_ptr<SelectionRequest> request) {
683 // Continued from requestMediaKeySystemAccess(), step 7.1, from
684 // https://w3c.github.io/encrypted-media/#requestmediakeysystemaccess
686 // 7.2. Let implementation be the implementation of keySystem.
687 // (|key_systems_| fills this role.)
688 // 7.3. For each value in supportedConfigurations:
689 for (size_t i = 0; i < request->candidate_configurations.size(); i++) {
690 // 7.3.1. Let candidate configuration be the value.
691 // 7.3.2. Let supported configuration be the result of executing the Get
692 // Supported Configuration algorithm on implementation, candidate
693 // configuration, and origin.
694 // 7.3.3. If supported configuration is not null, [initialize and return a
695 // new MediaKeySystemAccess object.]
696 ConfigState config_state(request->was_permission_requested,
697 request->is_permission_granted);
698 blink::WebMediaKeySystemConfiguration accumulated_configuration;
699 ConfigurationSupport support = GetSupportedConfiguration(
700 request->key_system, request->candidate_configurations[i],
701 &config_state, &accumulated_configuration);
702 switch (support) {
703 case CONFIGURATION_NOT_SUPPORTED:
704 continue;
705 case CONFIGURATION_REQUIRES_PERMISSION:
706 if (request->was_permission_requested) {
707 DVLOG(2) << "Rejecting requested configuration because "
708 << "permission was denied.";
709 continue;
711 media_permission_->RequestPermission(
712 MediaPermission::PROTECTED_MEDIA_IDENTIFIER,
713 GURL(request->security_origin.toString()),
714 base::Bind(&KeySystemConfigSelector::OnPermissionResult,
715 weak_factory_.GetWeakPtr(), base::Passed(&request)));
716 return;
717 case CONFIGURATION_SUPPORTED:
718 request->succeeded_cb.Run(accumulated_configuration);
719 return;
723 // 7.4. Reject promise with a new DOMException whose name is
724 // NotSupportedError.
725 request->not_supported_cb.Run(
726 "None of the requested configurations were supported.");
729 void KeySystemConfigSelector::OnPermissionResult(
730 scoped_ptr<SelectionRequest> request,
731 bool is_permission_granted) {
732 request->was_permission_requested = true;
733 request->is_permission_granted = is_permission_granted;
734 SelectConfigInternal(request.Pass());
737 } // namespace media