Sync defines with the GYP build.
[chromium-blink-merge.git] / media / blink / webencryptedmediaclient_impl.cc
blob3ce96ed8a2412559afcbf86e1833c6c78134b660
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/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"
20 namespace media {
22 namespace {
24 // Used to name UMAs in Reporter.
25 const char kKeySystemSupportUMAPrefix[] =
26 "Media.EME.RequestMediaKeySystemAccess.";
28 } // namespace
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
35 // renderer frame.
36 class WebEncryptedMediaClientImpl::Reporter {
37 public:
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) {}
48 ~Reporter() {}
50 void ReportRequested() {
51 if (is_request_reported_)
52 return;
53 Report(KEY_SYSTEM_REQUESTED);
54 is_request_reported_ = true;
57 void ReportSupported() {
58 DCHECK(is_request_reported_);
59 if (is_support_reported_)
60 return;
61 Report(KEY_SYSTEM_SUPPORTED);
62 is_support_reported_ = true;
65 private:
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 base::Callback<bool(void)> are_secure_codecs_supported_cb,
82 CdmFactory* cdm_factory,
83 MediaPermission* media_permission)
84 : are_secure_codecs_supported_cb_(are_secure_codecs_supported_cb),
85 cdm_factory_(cdm_factory),
86 key_system_config_selector_(KeySystems::GetInstance(), media_permission),
87 weak_factory_(this) {
88 DCHECK(cdm_factory_);
91 WebEncryptedMediaClientImpl::~WebEncryptedMediaClientImpl() {
94 void WebEncryptedMediaClientImpl::requestMediaKeySystemAccess(
95 blink::WebEncryptedMediaRequest request) {
96 GetReporter(request.keySystem())->ReportRequested();
97 key_system_config_selector_.SelectConfig(
98 request.keySystem(), request.supportedConfigurations(),
99 request.securityOrigin(), are_secure_codecs_supported_cb_.Run(),
100 base::Bind(&WebEncryptedMediaClientImpl::OnRequestSucceeded,
101 weak_factory_.GetWeakPtr(), request),
102 base::Bind(&WebEncryptedMediaClientImpl::OnRequestNotSupported,
103 weak_factory_.GetWeakPtr(), request));
106 void WebEncryptedMediaClientImpl::CreateCdm(
107 const blink::WebString& key_system,
108 bool allow_distinctive_identifier,
109 bool allow_persistent_state,
110 const blink::WebSecurityOrigin& security_origin,
111 blink::WebContentDecryptionModuleResult result) {
112 WebContentDecryptionModuleImpl::Create(
113 cdm_factory_, key_system, allow_distinctive_identifier,
114 allow_persistent_state, security_origin, result);
117 void WebEncryptedMediaClientImpl::OnRequestSucceeded(
118 blink::WebEncryptedMediaRequest request,
119 const blink::WebMediaKeySystemConfiguration& accumulated_configuration,
120 bool are_secure_codecs_required) {
121 GetReporter(request.keySystem())->ReportSupported();
122 // TODO(sandersd): Pass |are_secure_codecs_required| along and use it to
123 // configure the CDM security level and use of secure surfaces on Android.
124 request.requestSucceeded(WebContentDecryptionModuleAccessImpl::Create(
125 request.keySystem(), accumulated_configuration, request.securityOrigin(),
126 weak_factory_.GetWeakPtr()));
129 void WebEncryptedMediaClientImpl::OnRequestNotSupported(
130 blink::WebEncryptedMediaRequest request,
131 const blink::WebString& error_message) {
132 request.requestNotSupported(error_message);
135 WebEncryptedMediaClientImpl::Reporter* WebEncryptedMediaClientImpl::GetReporter(
136 const blink::WebString& key_system) {
137 // Assumes that empty will not be found by GetKeySystemNameForUMA().
138 // TODO(sandersd): Avoid doing ASCII conversion more than once.
139 std::string key_system_ascii;
140 if (base::IsStringASCII(key_system))
141 key_system_ascii = base::UTF16ToASCII(key_system);
143 // Return a per-frame singleton so that UMA reports will be once-per-frame.
144 std::string uma_name = GetKeySystemNameForUMA(key_system_ascii);
145 Reporter* reporter = reporters_.get(uma_name);
146 if (!reporter) {
147 reporter = new Reporter(uma_name);
148 reporters_.add(uma_name, make_scoped_ptr(reporter));
150 return reporter;
153 } // namespace media