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"
8 #include "base/metrics/histogram.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/webcontentdecryptionmodule_impl.h"
14 #include "media/blink/webcontentdecryptionmoduleaccess_impl.h"
15 #include "third_party/WebKit/public/platform/WebContentDecryptionModuleResult.h"
16 #include "third_party/WebKit/public/platform/WebEncryptedMediaRequest.h"
17 #include "third_party/WebKit/public/platform/WebMediaKeySystemConfiguration.h"
18 #include "third_party/WebKit/public/platform/WebString.h"
24 // Used to name UMAs in Reporter.
25 const char kKeySystemSupportUMAPrefix
[] =
26 "Media.EME.RequestMediaKeySystemAccess.";
30 // Report usage of key system to UMA. There are 2 different counts logged:
31 // 1. The key system is requested.
32 // 2. The requested key system and options are supported.
33 // Each stat is only reported once per renderer frame per key system.
34 // Note that WebEncryptedMediaClientImpl is only created once by each
36 class WebEncryptedMediaClientImpl::Reporter
{
38 enum KeySystemSupportStatus
{
39 KEY_SYSTEM_REQUESTED
= 0,
40 KEY_SYSTEM_SUPPORTED
= 1,
41 KEY_SYSTEM_SUPPORT_STATUS_COUNT
44 explicit Reporter(const std::string
& key_system_for_uma
)
45 : uma_name_(kKeySystemSupportUMAPrefix
+ key_system_for_uma
),
46 is_request_reported_(false),
47 is_support_reported_(false) {}
50 void ReportRequested() {
51 if (is_request_reported_
)
53 Report(KEY_SYSTEM_REQUESTED
);
54 is_request_reported_
= true;
57 void ReportSupported() {
58 DCHECK(is_request_reported_
);
59 if (is_support_reported_
)
61 Report(KEY_SYSTEM_SUPPORTED
);
62 is_support_reported_
= true;
66 void Report(KeySystemSupportStatus status
) {
67 // Not using UMA_HISTOGRAM_ENUMERATION directly because UMA_* macros
68 // require the names to be constant throughout the process' lifetime.
69 base::LinearHistogram::FactoryGet(
70 uma_name_
, 1, KEY_SYSTEM_SUPPORT_STATUS_COUNT
,
71 KEY_SYSTEM_SUPPORT_STATUS_COUNT
+ 1,
72 base::Histogram::kUmaTargetedHistogramFlag
)->Add(status
);
75 const std::string uma_name_
;
76 bool is_request_reported_
;
77 bool is_support_reported_
;
80 WebEncryptedMediaClientImpl::WebEncryptedMediaClientImpl(
81 CdmFactory
* cdm_factory
,
82 MediaPermission
* media_permission
)
83 : cdm_factory_(cdm_factory
),
84 key_system_config_selector_(KeySystems::GetInstance(), media_permission
),
89 WebEncryptedMediaClientImpl::~WebEncryptedMediaClientImpl() {
92 void WebEncryptedMediaClientImpl::requestMediaKeySystemAccess(
93 blink::WebEncryptedMediaRequest request
) {
94 GetReporter(request
.keySystem())->ReportRequested();
95 key_system_config_selector_
.SelectConfig(
96 request
.keySystem(), request
.supportedConfigurations(),
97 request
.securityOrigin(),
98 base::Bind(&WebEncryptedMediaClientImpl::OnRequestSucceeded
,
99 weak_factory_
.GetWeakPtr(), request
),
100 base::Bind(&WebEncryptedMediaClientImpl::OnRequestNotSupported
,
101 weak_factory_
.GetWeakPtr(), request
));
104 void WebEncryptedMediaClientImpl::CreateCdm(
105 const blink::WebString
& key_system
,
106 bool allow_distinctive_identifier
,
107 bool allow_persistent_state
,
108 const blink::WebSecurityOrigin
& security_origin
,
109 blink::WebContentDecryptionModuleResult result
) {
110 WebContentDecryptionModuleImpl::Create(
111 cdm_factory_
, key_system
, allow_distinctive_identifier
,
112 allow_persistent_state
, security_origin
, result
);
115 void WebEncryptedMediaClientImpl::OnRequestSucceeded(
116 blink::WebEncryptedMediaRequest request
,
117 const blink::WebMediaKeySystemConfiguration
& accumulated_configuration
) {
118 GetReporter(request
.keySystem())->ReportSupported();
119 request
.requestSucceeded(WebContentDecryptionModuleAccessImpl::Create(
120 request
.keySystem(), accumulated_configuration
, request
.securityOrigin(),
121 weak_factory_
.GetWeakPtr()));
124 void WebEncryptedMediaClientImpl::OnRequestNotSupported(
125 blink::WebEncryptedMediaRequest request
,
126 const blink::WebString
& error_message
) {
127 request
.requestNotSupported(error_message
);
130 WebEncryptedMediaClientImpl::Reporter
* WebEncryptedMediaClientImpl::GetReporter(
131 const blink::WebString
& key_system
) {
132 // Assumes that empty will not be found by GetKeySystemNameForUMA().
133 // TODO(sandersd): Avoid doing ASCII conversion more than once.
134 std::string key_system_ascii
;
135 if (base::IsStringASCII(key_system
))
136 key_system_ascii
= base::UTF16ToASCII(key_system
);
138 // Return a per-frame singleton so that UMA reports will be once-per-frame.
139 std::string uma_name
= GetKeySystemNameForUMA(key_system_ascii
);
140 Reporter
* reporter
= reporters_
.get(uma_name
);
142 reporter
= new Reporter(uma_name
);
143 reporters_
.add(uma_name
, make_scoped_ptr(reporter
));