[Cronet] Delay StartNetLog and StopNetLog until native request context is initialized
[chromium-blink-merge.git] / media / blink / webencryptedmediaclient_impl.cc
blob7b3374974fb4ab0dae61d25908c8aa5a43bc9552
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 "webencryptedmediaclient_impl.h"
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/metrics/histogram.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "media/base/key_systems.h"
13 #include "media/base/media_permission.h"
14 #include "media/blink/webcontentdecryptionmodule_impl.h"
15 #include "media/blink/webcontentdecryptionmoduleaccess_impl.h"
16 #include "net/base/mime_util.h"
17 #include "third_party/WebKit/public/platform/WebEncryptedMediaRequest.h"
18 #include "third_party/WebKit/public/platform/WebMediaKeySystemConfiguration.h"
19 #include "third_party/WebKit/public/platform/WebString.h"
20 #include "third_party/WebKit/public/platform/WebVector.h"
22 namespace media {
24 // These names are used by UMA.
25 const char kKeySystemSupportUMAPrefix[] =
26 "Media.EME.RequestMediaKeySystemAccess.";
28 enum ConfigurationSupport {
29 CONFIGURATION_NOT_SUPPORTED,
30 CONFIGURATION_REQUIRES_PERMISSION,
31 CONFIGURATION_SUPPORTED,
34 // Accumulates configuration rules to determine if a feature (additional
35 // configuration rule) can be added to an accumulated configuration.
36 class ConfigState {
37 public:
38 ConfigState(bool was_permission_requested, bool is_permission_granted)
39 : was_permission_requested_(was_permission_requested),
40 is_permission_granted_(is_permission_granted),
41 is_identifier_required_(false),
42 is_identifier_recommended_(false){
45 bool IsPermissionGranted() const {
46 return is_permission_granted_;
49 // Permission is possible if it has not been denied.
50 bool IsPermissionPossible() const {
51 return is_permission_granted_ || !was_permission_requested_;
54 bool IsIdentifierRequired() const {
55 return is_identifier_required_;
58 bool IsIdentifierRecommended() const {
59 return is_identifier_recommended_;
62 // Checks whether a rule is compatible with all previously added rules.
63 bool IsRuleSupported(EmeConfigRule rule) const {
64 switch (rule) {
65 case EmeConfigRule::NOT_SUPPORTED:
66 return false;
67 case EmeConfigRule::IDENTIFIER_REQUIRED:
68 return IsPermissionPossible();
69 case EmeConfigRule::IDENTIFIER_RECOMMENDED:
70 return true;
71 case EmeConfigRule::SUPPORTED:
72 return true;
74 NOTREACHED();
75 return false;
78 // Checks whether a rule is compatible with all previously added rules, and
79 // can be accepted without needing to add it to the configuration state. This
80 // allows considering more rules after the configuration state is final (that
81 // is, after distinctiveIdentifier has been resolved).
82 bool IsRuleSupportedWithCurrentState(EmeConfigRule rule) const {
83 switch (rule) {
84 case EmeConfigRule::NOT_SUPPORTED:
85 return false;
86 case EmeConfigRule::IDENTIFIER_REQUIRED:
87 return is_permission_granted_;
88 case EmeConfigRule::IDENTIFIER_RECOMMENDED:
89 return true;
90 case EmeConfigRule::SUPPORTED:
91 return true;
93 NOTREACHED();
94 return false;
97 // Add a rule to the accumulated configuration state.
98 void AddRule(EmeConfigRule rule) {
99 switch (rule) {
100 case EmeConfigRule::NOT_SUPPORTED:
101 return;
102 case EmeConfigRule::IDENTIFIER_REQUIRED:
103 is_identifier_required_ = true;
104 return;
105 case EmeConfigRule::IDENTIFIER_RECOMMENDED:
106 is_identifier_recommended_ = true;
107 return;
108 case EmeConfigRule::SUPPORTED:
109 return;
111 NOTREACHED();
114 private:
115 // Whether permission to use a distinctive identifier was requested. If set,
116 // |is_permission_granted_| represents the final decision.
117 const bool was_permission_requested_;
119 // Whether permission to use a distinctive identifier has been granted.
120 const bool is_permission_granted_;
122 // Whether a rule has been added that requires a distinctive identifier.
123 bool is_identifier_required_;
125 // Whether a rule has been added that recommends a distinctive identifier.
126 bool is_identifier_recommended_;
129 static bool IsSupportedContentType(
130 const KeySystems& key_systems,
131 const std::string& key_system,
132 EmeMediaType media_type,
133 const std::string& container_mime_type,
134 const std::string& codecs) {
135 // TODO(sandersd): Move contentType parsing from Blink to here so that invalid
136 // parameters can be rejected. http://crbug.com/417561
137 std::string container_lower = base::StringToLowerASCII(container_mime_type);
139 // Check that |container_mime_type| and |codecs| are supported by the CDM.
140 // This check does not handle extended codecs, so extended codec information
141 // is stripped.
142 std::vector<std::string> codec_vector;
143 net::ParseCodecString(codecs, &codec_vector, true);
144 if (!key_systems.IsSupportedCodecCombination(
145 key_system, media_type, container_lower, codec_vector)) {
146 return false;
149 // Check that |container_mime_type| is supported by Chrome. This can only
150 // happen if the CDM declares support for a container that Chrome does not.
151 if (!net::IsSupportedMediaMimeType(container_lower)) {
152 NOTREACHED();
153 return false;
156 // Check that |codecs| are supported by Chrome. This is done primarily to
157 // validate extended codecs, but it also ensures that the CDM cannot support
158 // codecs that Chrome does not (which could complicate the robustness
159 // algorithm).
160 if (codec_vector.empty())
161 return true;
162 codec_vector.clear();
163 net::ParseCodecString(codecs, &codec_vector, false);
164 return (net::IsSupportedStrictMediaMimeType(container_lower, codec_vector) ==
165 net::IsSupported);
168 static bool GetSupportedCapabilities(
169 const KeySystems& key_systems,
170 const std::string& key_system,
171 EmeMediaType media_type,
172 const blink::WebVector<blink::WebMediaKeySystemMediaCapability>&
173 requested_media_capabilities,
174 ConfigState* config_state,
175 std::vector<blink::WebMediaKeySystemMediaCapability>*
176 supported_media_capabilities) {
177 // From
178 // https://w3c.github.io/encrypted-media/#get-supported-capabilities-for-media-type
179 // 1. Let local accumulated capabilities be a local copy of partial
180 // configuration.
181 // (Skipped as we directly update |config_state|. This is safe because we
182 // only do so when at least one requested media capability is supported.)
183 // 2. Let supported media capabilities be empty.
184 DCHECK_EQ(supported_media_capabilities->size(), 0ul);
185 // 3. For each value in requested media capabilities:
186 for (size_t i = 0; i < requested_media_capabilities.size(); i++) {
187 // 3.1. Let contentType be the value's contentType member.
188 // 3.2. Let robustness be the value's robustness member.
189 const blink::WebMediaKeySystemMediaCapability& capability =
190 requested_media_capabilities[i];
191 // 3.3. If contentType is the empty string, return null.
192 if (capability.mimeType.isEmpty()) {
193 DVLOG(2) << "Rejecting requested configuration because "
194 << "a capability contentType was empty.";
195 return false;
197 // 3.4-3.11. (Implemented by IsSupportedContentType().)
198 if (!base::IsStringASCII(capability.mimeType) ||
199 !base::IsStringASCII(capability.codecs) ||
200 !IsSupportedContentType(key_systems, key_system, media_type,
201 base::UTF16ToASCII(capability.mimeType),
202 base::UTF16ToASCII(capability.codecs))) {
203 continue;
205 // 3.12. If robustness is not the empty string, run the following steps:
206 if (!capability.robustness.isEmpty()) {
207 // 3.12.1. If robustness is an unrecognized value or not supported by
208 // implementation, continue to the next iteration. String
209 // comparison is case-sensitive.
210 if (!base::IsStringASCII(capability.robustness))
211 continue;
212 EmeConfigRule robustness_rule = key_systems.GetRobustnessConfigRule(
213 key_system, media_type, base::UTF16ToASCII(capability.robustness));
214 if (!config_state->IsRuleSupported(robustness_rule))
215 continue;
216 config_state->AddRule(robustness_rule);
217 // 3.12.2. Add robustness to configuration.
218 // (It's already added, we use capability as configuration.)
220 // 3.13. If the user agent and implementation do not support playback of
221 // encrypted media data as specified by configuration, including all
222 // media types, in combination with local accumulated capabilities,
223 // continue to the next iteration.
224 // (This is handled when adding rules to |config_state|.)
225 // 3.14. Add configuration to supported media capabilities.
226 supported_media_capabilities->push_back(capability);
227 // 3.15. Add configuration to local accumulated capabilities.
228 // (Skipped as we directly update |config_state|.)
230 // 4. If supported media capabilities is empty, return null.
231 if (supported_media_capabilities->empty()) {
232 DVLOG(2) << "Rejecting requested configuration because "
233 << "no capabilities were supported.";
234 return false;
236 // 5. Return media type capabilities.
237 return true;
240 static EmeFeatureRequirement ConvertRequirement(
241 blink::WebMediaKeySystemConfiguration::Requirement requirement) {
242 switch (requirement) {
243 case blink::WebMediaKeySystemConfiguration::Requirement::Required:
244 return EME_FEATURE_REQUIRED;
245 case blink::WebMediaKeySystemConfiguration::Requirement::Optional:
246 return EME_FEATURE_OPTIONAL;
247 case blink::WebMediaKeySystemConfiguration::Requirement::NotAllowed:
248 return EME_FEATURE_NOT_ALLOWED;
251 NOTREACHED();
252 return EME_FEATURE_NOT_ALLOWED;
255 static ConfigurationSupport GetSupportedConfiguration(
256 const KeySystems& key_systems,
257 const std::string& key_system,
258 const blink::WebMediaKeySystemConfiguration& candidate,
259 bool was_permission_requested,
260 bool is_permission_granted,
261 blink::WebMediaKeySystemConfiguration* accumulated_configuration) {
262 ConfigState config_state(was_permission_requested, is_permission_granted);
264 // From https://w3c.github.io/encrypted-media/#get-supported-configuration
265 // 1. Let accumulated configuration be empty. (Done by caller.)
266 // 2. If candidate configuration's initDataTypes attribute is not empty, run
267 // the following steps:
268 if (!candidate.initDataTypes.isEmpty()) {
269 // 2.1. Let supported types be empty.
270 std::vector<blink::WebEncryptedMediaInitDataType> supported_types;
272 // 2.2. For each value in candidate configuration's initDataTypes attribute:
273 for (size_t i = 0; i < candidate.initDataTypes.size(); i++) {
274 // 2.2.1. Let initDataType be the value.
275 blink::WebEncryptedMediaInitDataType init_data_type =
276 candidate.initDataTypes[i];
277 // 2.2.2. If initDataType is the empty string, return null.
278 if (init_data_type == blink::WebEncryptedMediaInitDataType::Unknown)
279 continue;
280 // 2.2.3. If the implementation supports generating requests based on
281 // initDataType, add initDataType to supported types. String
282 // comparison is case-sensitive.
283 // TODO(jrummell): |init_data_type| should be an enum all the way through
284 // Chromium. http://crbug.com/417440
285 std::string init_data_type_as_ascii = "unknown";
286 switch (init_data_type) {
287 case blink::WebEncryptedMediaInitDataType::Cenc:
288 init_data_type_as_ascii = "cenc";
289 break;
290 case blink::WebEncryptedMediaInitDataType::Keyids:
291 init_data_type_as_ascii = "keyids";
292 break;
293 case blink::WebEncryptedMediaInitDataType::Webm:
294 init_data_type_as_ascii = "webm";
295 break;
296 case blink::WebEncryptedMediaInitDataType::Unknown:
297 NOTREACHED();
298 break;
300 if (IsSupportedKeySystemWithInitDataType(key_system,
301 init_data_type_as_ascii)) {
302 supported_types.push_back(init_data_type);
306 // 2.3. If supported types is empty, return null.
307 if (supported_types.empty()) {
308 DVLOG(2) << "Rejecting requested configuration because "
309 << "no initDataType values were supported.";
310 return CONFIGURATION_NOT_SUPPORTED;
313 // 2.4. Add supported types to accumulated configuration.
314 accumulated_configuration->initDataTypes = supported_types;
317 // 3. Follow the steps for the value of candidate configuration's
318 // distinctiveIdentifier attribute from the following list:
319 // - "required": If the implementation does not support a persistent
320 // Distinctive Identifier in combination with accumulated configuration,
321 // return null.
322 // - "optional": Continue.
323 // - "not-allowed": If the implementation requires a Distinctive
324 // Identifier in combination with accumulated configuration, return
325 // null.
326 // We also reject OPTIONAL when distinctive identifiers are ALWAYS_ENABLED and
327 // permission has already been denied. This would happen anyway at step 11.
328 EmeConfigRule di_rule = key_systems.GetDistinctiveIdentifierConfigRule(
329 key_system, ConvertRequirement(candidate.distinctiveIdentifier));
330 if (!config_state.IsRuleSupported(di_rule)) {
331 DVLOG(2) << "Rejecting requested configuration because "
332 << "the distinctiveIdentifier requirement was not supported.";
333 return CONFIGURATION_NOT_SUPPORTED;
335 config_state.AddRule(di_rule);
337 // 4. Add the value of the candidate configuration's distinctiveIdentifier
338 // attribute to accumulated configuration.
339 accumulated_configuration->distinctiveIdentifier =
340 candidate.distinctiveIdentifier;
342 // 5. Follow the steps for the value of candidate configuration's
343 // persistentState attribute from the following list:
344 // - "required": If the implementation does not support persisting state
345 // in combination with accumulated configuration, return null.
346 // - "optional": Continue.
347 // - "not-allowed": If the implementation requires persisting state in
348 // combination with accumulated configuration, return null.
349 EmeConfigRule ps_rule = key_systems.GetPersistentStateConfigRule(
350 key_system, ConvertRequirement(candidate.persistentState));
351 if (!config_state.IsRuleSupported(ps_rule)) {
352 DVLOG(2) << "Rejecting requested configuration because "
353 << "the persistentState requirement was not supported.";
354 return CONFIGURATION_NOT_SUPPORTED;
356 config_state.AddRule(ps_rule);
358 // 6. Add the value of the candidate configuration's persistentState
359 // attribute to accumulated configuration.
360 accumulated_configuration->persistentState = candidate.persistentState;
362 // 7. If candidate configuration's videoCapabilities attribute is not empty,
363 // run the following steps:
364 if (!candidate.videoCapabilities.isEmpty()) {
365 // 7.1. Let video capabilities be the result of executing the Get Supported
366 // Capabilities for Media Type algorithm on Video, candidate
367 // configuration's videoCapabilities attribute, and accumulated
368 // configuration.
369 // 7.2. If video capabilities is null, return null.
370 std::vector<blink::WebMediaKeySystemMediaCapability> video_capabilities;
371 if (!GetSupportedCapabilities(key_systems, key_system, EmeMediaType::VIDEO,
372 candidate.videoCapabilities,
373 &config_state, &video_capabilities)) {
374 return CONFIGURATION_NOT_SUPPORTED;
377 // 7.3. Add video capabilities to accumulated configuration.
378 accumulated_configuration->videoCapabilities = video_capabilities;
381 // 8. If candidate configuration's audioCapabilities attribute is not empty,
382 // run the following steps:
383 if (!candidate.audioCapabilities.isEmpty()) {
384 // 8.1. Let audio capabilities be the result of executing the Get Supported
385 // Capabilities for Media Type algorithm on Audio, candidate
386 // configuration's audioCapabilities attribute, and accumulated
387 // configuration.
388 // 8.2. If audio capabilities is null, return null.
389 std::vector<blink::WebMediaKeySystemMediaCapability> audio_capabilities;
390 if (!GetSupportedCapabilities(key_systems, key_system, EmeMediaType::AUDIO,
391 candidate.audioCapabilities,
392 &config_state, &audio_capabilities)) {
393 return CONFIGURATION_NOT_SUPPORTED;
396 // 8.3. Add audio capabilities to accumulated configuration.
397 accumulated_configuration->audioCapabilities = audio_capabilities;
400 // 9. If accumulated configuration's distinctiveIdentifier value is
401 // "optional", follow the steps for the first matching condition from the
402 // following list:
403 // - If the implementation requires a Distinctive Identifier for any of
404 // the combinations in accumulated configuration, change accumulated
405 // configuration's distinctiveIdentifier value to "required".
406 // - Otherwise, change accumulated configuration's distinctiveIdentifier
407 // value to "not-allowed".
408 if (accumulated_configuration->distinctiveIdentifier ==
409 blink::WebMediaKeySystemConfiguration::Requirement::Optional) {
410 EmeConfigRule not_allowed_rule =
411 key_systems.GetDistinctiveIdentifierConfigRule(
412 key_system, EME_FEATURE_NOT_ALLOWED);
413 EmeConfigRule required_rule =
414 key_systems.GetDistinctiveIdentifierConfigRule(
415 key_system, EME_FEATURE_REQUIRED);
416 bool not_allowed_supported = config_state.IsRuleSupported(not_allowed_rule);
417 bool required_supported = config_state.IsRuleSupported(required_rule);
418 if (not_allowed_supported) {
419 bool prefer_required = config_state.IsIdentifierRequired() ||
420 (config_state.IsIdentifierRecommended() &&
421 config_state.IsPermissionPossible());
422 if (required_supported && prefer_required) {
423 accumulated_configuration->distinctiveIdentifier =
424 blink::WebMediaKeySystemConfiguration::Requirement::Required;
425 config_state.AddRule(required_rule);
426 DCHECK(config_state.IsIdentifierRequired());
427 } else {
428 accumulated_configuration->distinctiveIdentifier =
429 blink::WebMediaKeySystemConfiguration::Requirement::NotAllowed;
430 config_state.AddRule(not_allowed_rule);
432 } else if (required_supported) {
433 accumulated_configuration->distinctiveIdentifier =
434 blink::WebMediaKeySystemConfiguration::Requirement::Required;
435 config_state.AddRule(required_rule);
436 } else {
437 // We should not have passed step 3.
438 NOTREACHED();
439 return CONFIGURATION_NOT_SUPPORTED;
443 // If permission is required but we couldn't enable it, reject the
444 // configuration.
445 if (config_state.IsIdentifierRequired() &&
446 accumulated_configuration->distinctiveIdentifier !=
447 blink::WebMediaKeySystemConfiguration::Requirement::Required) {
448 DVLOG(2) << "Rejecting requested configuration because "
449 << "distinctiveIdentifier was implicitly required but "
450 << "not allowed.";
451 return CONFIGURATION_NOT_SUPPORTED;
454 // 10. If accumulated configuration's persistentState value is "optional",
455 // follow the steps for the first matching condition from the following
456 // list:
457 // - If the implementation requires persisting state for any of the
458 // combinations in accumulated configuration, change accumulated
459 // configuration's persistentState value to "required".
460 // - Otherwise, change accumulated configuration's persistentState value
461 // to "not-allowed".
462 if (accumulated_configuration->persistentState ==
463 blink::WebMediaKeySystemConfiguration::Requirement::Optional) {
464 EmeConfigRule not_allowed_rule =
465 key_systems.GetPersistentStateConfigRule(
466 key_system, EME_FEATURE_NOT_ALLOWED);
467 EmeConfigRule required_rule =
468 key_systems.GetPersistentStateConfigRule(
469 key_system, EME_FEATURE_REQUIRED);
470 // Now that distinctiveIdentifier has been resolved, it is too late to allow
471 // persistentState to affect the configuration.
472 bool not_allowed_supported =
473 config_state.IsRuleSupportedWithCurrentState(not_allowed_rule);
474 bool required_supported =
475 config_state.IsRuleSupportedWithCurrentState(required_rule);
476 if (not_allowed_supported) {
477 accumulated_configuration->persistentState =
478 blink::WebMediaKeySystemConfiguration::Requirement::NotAllowed;
479 } else if (required_supported) {
480 accumulated_configuration->persistentState =
481 blink::WebMediaKeySystemConfiguration::Requirement::Required;
482 } else {
483 // We should not have passed step 5.
484 NOTREACHED();
485 return CONFIGURATION_NOT_SUPPORTED;
489 // 11. If implementation in the configuration specified by the combination of
490 // the values in accumulated configuration is not supported or not allowed
491 // in the origin, return null.
492 // 12. If accumulated configuration's distinctiveIdentifier value is
493 // "required", [prompt the user for consent].
494 if (accumulated_configuration->distinctiveIdentifier ==
495 blink::WebMediaKeySystemConfiguration::Requirement::Required) {
496 // The caller is responsible for resolving what to do if permission is
497 // required but has been denied (it should treat it as NOT_SUPPORTED).
498 if (!config_state.IsPermissionGranted())
499 return CONFIGURATION_REQUIRES_PERMISSION;
502 // 13. Return accumulated configuration.
504 // We also record the available session types so that createSession() can be
505 // synchronous.
506 std::vector<blink::WebEncryptedMediaSessionType> session_types;
507 session_types.push_back(blink::WebEncryptedMediaSessionType::Temporary);
508 if (accumulated_configuration->persistentState ==
509 blink::WebMediaKeySystemConfiguration::Requirement::Required) {
510 if (config_state.IsRuleSupportedWithCurrentState(
511 key_systems.GetPersistentLicenseSessionConfigRule(key_system))) {
512 session_types.push_back(
513 blink::WebEncryptedMediaSessionType::PersistentLicense);
515 if (config_state.IsRuleSupportedWithCurrentState(
516 key_systems.GetPersistentReleaseMessageSessionConfigRule(
517 key_system))) {
518 session_types.push_back(
519 blink::WebEncryptedMediaSessionType::PersistentReleaseMessage);
522 accumulated_configuration->sessionTypes = session_types;
524 return CONFIGURATION_SUPPORTED;
527 // Report usage of key system to UMA. There are 2 different counts logged:
528 // 1. The key system is requested.
529 // 2. The requested key system and options are supported.
530 // Each stat is only reported once per renderer frame per key system.
531 // Note that WebEncryptedMediaClientImpl is only created once by each
532 // renderer frame.
533 class WebEncryptedMediaClientImpl::Reporter {
534 public:
535 enum KeySystemSupportStatus {
536 KEY_SYSTEM_REQUESTED = 0,
537 KEY_SYSTEM_SUPPORTED = 1,
538 KEY_SYSTEM_SUPPORT_STATUS_COUNT
541 explicit Reporter(const std::string& key_system_for_uma)
542 : uma_name_(kKeySystemSupportUMAPrefix + key_system_for_uma),
543 is_request_reported_(false),
544 is_support_reported_(false) {}
545 ~Reporter() {}
547 void ReportRequested() {
548 if (is_request_reported_)
549 return;
550 Report(KEY_SYSTEM_REQUESTED);
551 is_request_reported_ = true;
554 void ReportSupported() {
555 DCHECK(is_request_reported_);
556 if (is_support_reported_)
557 return;
558 Report(KEY_SYSTEM_SUPPORTED);
559 is_support_reported_ = true;
562 private:
563 void Report(KeySystemSupportStatus status) {
564 // Not using UMA_HISTOGRAM_ENUMERATION directly because UMA_* macros
565 // require the names to be constant throughout the process' lifetime.
566 base::LinearHistogram::FactoryGet(
567 uma_name_, 1, KEY_SYSTEM_SUPPORT_STATUS_COUNT,
568 KEY_SYSTEM_SUPPORT_STATUS_COUNT + 1,
569 base::Histogram::kUmaTargetedHistogramFlag)->Add(status);
572 const std::string uma_name_;
573 bool is_request_reported_;
574 bool is_support_reported_;
577 WebEncryptedMediaClientImpl::WebEncryptedMediaClientImpl(
578 scoped_ptr<CdmFactory> cdm_factory,
579 MediaPermission* media_permission)
580 : key_systems_(KeySystems::GetInstance()),
581 cdm_factory_(cdm_factory.Pass()),
582 media_permission_(media_permission),
583 weak_factory_(this) {
584 DCHECK(media_permission);
587 WebEncryptedMediaClientImpl::~WebEncryptedMediaClientImpl() {
590 void WebEncryptedMediaClientImpl::requestMediaKeySystemAccess(
591 blink::WebEncryptedMediaRequest request) {
592 // TODO(jrummell): This should be asynchronous, ideally not on the main
593 // thread.
595 // Continued from requestMediaKeySystemAccess(), step 7, from
596 // https://w3c.github.io/encrypted-media/#requestmediakeysystemaccess
598 // 7.1. If keySystem is not one of the Key Systems supported by the user
599 // agent, reject promise with with a new DOMException whose name is
600 // NotSupportedError. String comparison is case-sensitive.
601 if (!base::IsStringASCII(request.keySystem())) {
602 request.requestNotSupported("Only ASCII keySystems are supported");
603 return;
606 // Report this request to the UMA.
607 std::string key_system = base::UTF16ToASCII(request.keySystem());
608 GetReporter(key_system)->ReportRequested();
610 if (!key_systems_.IsSupportedKeySystem(key_system)) {
611 request.requestNotSupported("Unsupported keySystem");
612 return;
615 // 7.2-7.4. Implemented by SelectSupportedConfiguration().
616 SelectSupportedConfiguration(request, false, false);
619 void WebEncryptedMediaClientImpl::SelectSupportedConfiguration(
620 blink::WebEncryptedMediaRequest request,
621 bool was_permission_requested,
622 bool is_permission_granted) {
623 // Continued from requestMediaKeySystemAccess(), step 7.1, from
624 // https://w3c.github.io/encrypted-media/#requestmediakeysystemaccess
626 // 7.2. Let implementation be the implementation of keySystem.
627 std::string key_system = base::UTF16ToASCII(request.keySystem());
629 // 7.3. For each value in supportedConfigurations:
630 const blink::WebVector<blink::WebMediaKeySystemConfiguration>&
631 configurations = request.supportedConfigurations();
632 for (size_t i = 0; i < configurations.size(); i++) {
633 // 7.3.1. Let candidate configuration be the value.
634 const blink::WebMediaKeySystemConfiguration& candidate_configuration =
635 configurations[i];
636 // 7.3.2. Let supported configuration be the result of executing the Get
637 // Supported Configuration algorithm on implementation, candidate
638 // configuration, and origin.
639 // 7.3.3. If supported configuration is not null, [initialize and return a
640 // new MediaKeySystemAccess object.]
641 blink::WebMediaKeySystemConfiguration accumulated_configuration;
642 ConfigurationSupport supported = GetSupportedConfiguration(
643 key_systems_, key_system, candidate_configuration,
644 was_permission_requested, is_permission_granted,
645 &accumulated_configuration);
646 switch (supported) {
647 case CONFIGURATION_NOT_SUPPORTED:
648 continue;
649 case CONFIGURATION_REQUIRES_PERMISSION:
650 if (was_permission_requested) {
651 DVLOG(2) << "Rejecting requested configuration because "
652 << "permission was denied.";
653 continue;
655 media_permission_->RequestPermission(
656 MediaPermission::PROTECTED_MEDIA_IDENTIFIER,
657 GURL(request.securityOrigin().toString()),
658 // Try again with |was_permission_requested| true and
659 // |is_permission_granted| the value of the permission.
660 base::Bind(
661 &WebEncryptedMediaClientImpl::SelectSupportedConfiguration,
662 weak_factory_.GetWeakPtr(), request, true));
663 return;
664 case CONFIGURATION_SUPPORTED:
665 // Report that this request succeeded to the UMA.
666 GetReporter(key_system)->ReportSupported();
667 request.requestSucceeded(WebContentDecryptionModuleAccessImpl::Create(
668 request.keySystem(), accumulated_configuration,
669 request.securityOrigin(), weak_factory_.GetWeakPtr()));
670 return;
674 // 7.4. Reject promise with a new DOMException whose name is
675 // NotSupportedError.
676 request.requestNotSupported(
677 "None of the requested configurations were supported.");
680 void WebEncryptedMediaClientImpl::CreateCdm(
681 const blink::WebString& key_system,
682 bool allow_distinctive_identifier,
683 bool allow_persistent_state,
684 const blink::WebSecurityOrigin& security_origin,
685 blink::WebContentDecryptionModuleResult result) {
686 WebContentDecryptionModuleImpl::Create(cdm_factory_.get(), key_system,
687 allow_distinctive_identifier,
688 allow_persistent_state,
689 security_origin, result);
692 // Lazily create Reporters.
693 WebEncryptedMediaClientImpl::Reporter* WebEncryptedMediaClientImpl::GetReporter(
694 const std::string& key_system) {
695 std::string uma_name = GetKeySystemNameForUMA(key_system);
696 Reporter* reporter = reporters_.get(uma_name);
697 if (reporter != nullptr)
698 return reporter;
700 // Reporter not found, so create one.
701 auto result =
702 reporters_.add(uma_name, make_scoped_ptr(new Reporter(uma_name)));
703 DCHECK(result.second);
704 return result.first->second;
707 } // namespace media