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_client.h"
13 #include "media/base/media_permission.h"
14 #include "media/blink/webcontentdecryptionmodule_impl.h"
15 #include "media/blink/webcontentdecryptionmoduleaccess_impl.h"
16 #include "third_party/WebKit/public/platform/WebContentDecryptionModuleResult.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/web/WebSecurityOrigin.h"
26 // Used to name UMAs in Reporter.
27 const char kKeySystemSupportUMAPrefix
[] =
28 "Media.EME.RequestMediaKeySystemAccess.";
32 // Report usage of key system to UMA. There are 2 different counts logged:
33 // 1. The key system is requested.
34 // 2. The requested key system and options are supported.
35 // Each stat is only reported once per renderer frame per key system.
36 // Note that WebEncryptedMediaClientImpl is only created once by each
38 class WebEncryptedMediaClientImpl::Reporter
{
40 enum KeySystemSupportStatus
{
41 KEY_SYSTEM_REQUESTED
= 0,
42 KEY_SYSTEM_SUPPORTED
= 1,
43 KEY_SYSTEM_SUPPORT_STATUS_COUNT
46 explicit Reporter(const std::string
& key_system_for_uma
)
47 : uma_name_(kKeySystemSupportUMAPrefix
+ key_system_for_uma
),
48 is_request_reported_(false),
49 is_support_reported_(false) {}
52 void ReportRequested() {
53 if (is_request_reported_
)
55 Report(KEY_SYSTEM_REQUESTED
);
56 is_request_reported_
= true;
59 void ReportSupported() {
60 DCHECK(is_request_reported_
);
61 if (is_support_reported_
)
63 Report(KEY_SYSTEM_SUPPORTED
);
64 is_support_reported_
= true;
68 void Report(KeySystemSupportStatus status
) {
69 // Not using UMA_HISTOGRAM_ENUMERATION directly because UMA_* macros
70 // require the names to be constant throughout the process' lifetime.
71 base::LinearHistogram::FactoryGet(
72 uma_name_
, 1, KEY_SYSTEM_SUPPORT_STATUS_COUNT
,
73 KEY_SYSTEM_SUPPORT_STATUS_COUNT
+ 1,
74 base::Histogram::kUmaTargetedHistogramFlag
)->Add(status
);
77 const std::string uma_name_
;
78 bool is_request_reported_
;
79 bool is_support_reported_
;
82 WebEncryptedMediaClientImpl::WebEncryptedMediaClientImpl(
83 base::Callback
<bool(void)> are_secure_codecs_supported_cb
,
84 CdmFactory
* cdm_factory
,
85 MediaPermission
* media_permission
)
86 : are_secure_codecs_supported_cb_(are_secure_codecs_supported_cb
),
87 cdm_factory_(cdm_factory
),
88 key_system_config_selector_(KeySystems::GetInstance(), media_permission
),
93 WebEncryptedMediaClientImpl::~WebEncryptedMediaClientImpl() {
96 void WebEncryptedMediaClientImpl::requestMediaKeySystemAccess(
97 blink::WebEncryptedMediaRequest request
) {
98 GetReporter(request
.keySystem())->ReportRequested();
100 if (GetMediaClient()) {
101 GURL
security_origin(request
.securityOrigin().toString());
103 GetMediaClient()->RecordRapporURL("Media.OriginUrl.EME", security_origin
);
105 blink::WebString error_message
;
106 if (!request
.securityOrigin().isPotentiallyTrustworthy(error_message
)) {
107 GetMediaClient()->RecordRapporURL("Media.OriginUrl.EME.Insecure",
112 key_system_config_selector_
.SelectConfig(
113 request
.keySystem(), request
.supportedConfigurations(),
114 request
.securityOrigin(), are_secure_codecs_supported_cb_
.Run(),
115 base::Bind(&WebEncryptedMediaClientImpl::OnRequestSucceeded
,
116 weak_factory_
.GetWeakPtr(), request
),
117 base::Bind(&WebEncryptedMediaClientImpl::OnRequestNotSupported
,
118 weak_factory_
.GetWeakPtr(), request
));
121 void WebEncryptedMediaClientImpl::CreateCdm(
122 const blink::WebString
& key_system
,
123 const blink::WebSecurityOrigin
& security_origin
,
124 const CdmConfig
& cdm_config
,
125 blink::WebContentDecryptionModuleResult result
) {
126 WebContentDecryptionModuleImpl::Create(
127 cdm_factory_
, key_system
, security_origin
, cdm_config
, result
);
130 void WebEncryptedMediaClientImpl::OnRequestSucceeded(
131 blink::WebEncryptedMediaRequest request
,
132 const blink::WebMediaKeySystemConfiguration
& accumulated_configuration
,
133 const CdmConfig
& cdm_config
) {
134 GetReporter(request
.keySystem())->ReportSupported();
135 // TODO(sandersd): Pass |are_secure_codecs_required| along and use it to
136 // configure the CDM security level and use of secure surfaces on Android.
137 request
.requestSucceeded(WebContentDecryptionModuleAccessImpl::Create(
138 request
.keySystem(), request
.securityOrigin(), accumulated_configuration
,
139 cdm_config
, weak_factory_
.GetWeakPtr()));
142 void WebEncryptedMediaClientImpl::OnRequestNotSupported(
143 blink::WebEncryptedMediaRequest request
,
144 const blink::WebString
& error_message
) {
145 request
.requestNotSupported(error_message
);
148 WebEncryptedMediaClientImpl::Reporter
* WebEncryptedMediaClientImpl::GetReporter(
149 const blink::WebString
& key_system
) {
150 // Assumes that empty will not be found by GetKeySystemNameForUMA().
151 // TODO(sandersd): Avoid doing ASCII conversion more than once.
152 std::string key_system_ascii
;
153 if (base::IsStringASCII(key_system
))
154 key_system_ascii
= base::UTF16ToASCII(key_system
);
156 // Return a per-frame singleton so that UMA reports will be once-per-frame.
157 std::string uma_name
= GetKeySystemNameForUMA(key_system_ascii
);
158 Reporter
* reporter
= reporters_
.get(uma_name
);
160 reporter
= new Reporter(uma_name
);
161 reporters_
.add(uma_name
, make_scoped_ptr(reporter
));