summaryrefslogtreecommitdiffstats
path: root/media/blink/webencryptedmediaclient_impl.cc
blob: 462a3e830105b17981a6021aabf76271e0c447ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "webencryptedmediaclient_impl.h"

#include "base/logging.h"
#include "base/metrics/histogram.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "media/base/key_systems.h"
#include "media/base/media_permission.h"
#include "net/base/mime_util.h"
#include "third_party/WebKit/public/platform/WebEncryptedMediaRequest.h"
#include "third_party/WebKit/public/platform/WebMediaKeySystemConfiguration.h"
#include "third_party/WebKit/public/platform/WebString.h"
#include "third_party/WebKit/public/platform/WebVector.h"
#include "webcontentdecryptionmodule_impl.h"
#include "webcontentdecryptionmoduleaccess_impl.h"

namespace media {

// These names are used by UMA.
const char kKeySystemSupportUMAPrefix[] =
    "Media.EME.RequestMediaKeySystemAccess.";

static bool IsSupportedContentType(
    const std::string& key_system,
    const std::string& mime_type,
    const std::string& codecs) {
  // Per RFC 6838, "Both top-level type and subtype names are case-insensitive."
  // TODO(sandersd): Check that |container| matches the capability:
  //   - audioCapabilitys: audio/mp4 or audio/webm.
  //   - videoCapabilitys: video/mp4 or video/webm.
  // http://crbug.com/457384.
  std::string container = base::StringToLowerASCII(mime_type);

  // Check that |codecs| are supported as specified (e.g. "mp4a.40.2").
  std::vector<std::string> codec_vector;
  net::ParseCodecString(codecs, &codec_vector, false);
  if (!net::AreSupportedMediaCodecs(codec_vector))
    return false;

  // IsSupportedKeySystemWithMediaMimeType() only works with base codecs
  // (e.g. "mp4a"), so reparse |codecs| to get the base only.
  codec_vector.clear();
  net::ParseCodecString(codecs, &codec_vector, true);
  return IsSupportedKeySystemWithMediaMimeType(container, codec_vector,
                                               key_system);
}

static bool GetSupportedConfiguration(
    const std::string& key_system,
    const blink::WebMediaKeySystemConfiguration& candidate,
    const blink::WebSecurityOrigin& security_origin,
    blink::WebMediaKeySystemConfiguration* accumulated_configuration) {
  if (!candidate.initDataTypes.isEmpty()) {
    std::vector<blink::WebString> init_data_types;

    for (size_t i = 0; i < candidate.initDataTypes.size(); i++) {
      const blink::WebString& init_data_type = candidate.initDataTypes[i];
      if (init_data_type.isEmpty())
        return false;
      if (base::IsStringASCII(init_data_type) &&
          IsSupportedKeySystemWithInitDataType(
              key_system, base::UTF16ToASCII(init_data_type))) {
        init_data_types.push_back(init_data_type);
      }
    }

    if (init_data_types.empty())
      return false;

    accumulated_configuration->initDataTypes = init_data_types;
  }

  // TODO(sandersd): Implement distinctiveIdentifier and persistentState checks.
  if (candidate.distinctiveIdentifier !=
      blink::WebMediaKeySystemConfiguration::Requirement::Optional ||
      candidate.persistentState !=
      blink::WebMediaKeySystemConfiguration::Requirement::Optional) {
    return false;
  }

  if (!candidate.audioCapabilities.isEmpty()) {
    std::vector<blink::WebMediaKeySystemMediaCapability> audio_capabilities;

    for (size_t i = 0; i < candidate.audioCapabilities.size(); i++) {
      const blink::WebMediaKeySystemMediaCapability& capabilities =
          candidate.audioCapabilities[i];
      if (capabilities.mimeType.isEmpty())
        return false;
      if (!base::IsStringASCII(capabilities.mimeType) ||
          !base::IsStringASCII(capabilities.codecs) ||
          !IsSupportedContentType(
              key_system, base::UTF16ToASCII(capabilities.mimeType),
              base::UTF16ToASCII(capabilities.codecs))) {
        continue;
      }
      // TODO(sandersd): Support robustness.
      if (!capabilities.robustness.isEmpty())
        continue;
      audio_capabilities.push_back(capabilities);
    }

    if (audio_capabilities.empty())
      return false;

    accumulated_configuration->audioCapabilities = audio_capabilities;
  }

  if (!candidate.videoCapabilities.isEmpty()) {
    std::vector<blink::WebMediaKeySystemMediaCapability> video_capabilities;

    for (size_t i = 0; i < candidate.videoCapabilities.size(); i++) {
      const blink::WebMediaKeySystemMediaCapability& capabilities =
          candidate.videoCapabilities[i];
      if (capabilities.mimeType.isEmpty())
        return false;
      if (!base::IsStringASCII(capabilities.mimeType) ||
          !base::IsStringASCII(capabilities.codecs) ||
          !IsSupportedContentType(
              key_system, base::UTF16ToASCII(capabilities.mimeType),
              base::UTF16ToASCII(capabilities.codecs))) {
        continue;
      }
      // TODO(sandersd): Support robustness.
      if (!capabilities.robustness.isEmpty())
        continue;
      video_capabilities.push_back(capabilities);
    }

    if (video_capabilities.empty())
      return false;

    accumulated_configuration->videoCapabilities = video_capabilities;
  }

  // TODO(sandersd): Prompt for distinctive identifiers and/or persistent state
  // if required. Make sure that future checks are silent.
  // http://crbug.com/446263.

  return true;
}

// Report usage of key system to UMA. There are 2 different counts logged:
// 1. The key system is requested.
// 2. The requested key system and options are supported.
// Each stat is only reported once per renderer frame per key system.
// Note that WebEncryptedMediaClientImpl is only created once by each
// renderer frame.
class WebEncryptedMediaClientImpl::Reporter {
 public:
  enum KeySystemSupportStatus {
    KEY_SYSTEM_REQUESTED = 0,
    KEY_SYSTEM_SUPPORTED = 1,
    KEY_SYSTEM_SUPPORT_STATUS_COUNT
  };

  explicit Reporter(const std::string& key_system_for_uma)
      : uma_name_(kKeySystemSupportUMAPrefix + key_system_for_uma),
        is_request_reported_(false),
        is_support_reported_(false) {}
  ~Reporter() {}

  void ReportRequested() {
    if (is_request_reported_)
      return;
    Report(KEY_SYSTEM_REQUESTED);
    is_request_reported_ = true;
  }

  void ReportSupported() {
    DCHECK(is_request_reported_);
    if (is_support_reported_)
      return;
    Report(KEY_SYSTEM_SUPPORTED);
    is_support_reported_ = true;
  }

 private:
  void Report(KeySystemSupportStatus status) {
    // Not using UMA_HISTOGRAM_ENUMERATION directly because UMA_* macros
    // require the names to be constant throughout the process' lifetime.
    base::LinearHistogram::FactoryGet(
        uma_name_, 1, KEY_SYSTEM_SUPPORT_STATUS_COUNT,
        KEY_SYSTEM_SUPPORT_STATUS_COUNT + 1,
        base::Histogram::kUmaTargetedHistogramFlag)->Add(status);
  }

  const std::string uma_name_;
  bool is_request_reported_;
  bool is_support_reported_;
};

WebEncryptedMediaClientImpl::WebEncryptedMediaClientImpl(
    scoped_ptr<CdmFactory> cdm_factory,
    MediaPermission* media_permission)
    : cdm_factory_(cdm_factory.Pass()), weak_factory_(this) {
  // TODO(sandersd): Use |media_permission| to check for media permissions in
  // this class.
  DCHECK(media_permission);
}

WebEncryptedMediaClientImpl::~WebEncryptedMediaClientImpl() {
}

void WebEncryptedMediaClientImpl::requestMediaKeySystemAccess(
    blink::WebEncryptedMediaRequest request) {
  // TODO(jrummell): This should be asynchronous.

  // Continued from requestMediaKeySystemAccess(), step 7, from
  // https://w3c.github.io/encrypted-media/#requestmediakeysystemaccess
  //
  // 7.1 If keySystem is not one of the Key Systems supported by the user
  //     agent, reject promise with with a new DOMException whose name is
  //     NotSupportedError. String comparison is case-sensitive.
  if (!base::IsStringASCII(request.keySystem())) {
    request.requestNotSupported("Only ASCII keySystems are supported");
    return;
  }

  std::string key_system = base::UTF16ToASCII(request.keySystem());

  // Report this request to the appropriate Reporter.
  Reporter* reporter = GetReporter(key_system);
  reporter->ReportRequested();

  if (!IsConcreteSupportedKeySystem(key_system)) {
    request.requestNotSupported("Unsupported keySystem");
    return;
  }

  // 7.2 Let implementation be the implementation of keySystem.
  // 7.3 For each value in supportedConfigurations, run the GetSupported
  //     Configuration algorithm and if successful, resolve promise with access
  //     and abort these steps.
  const blink::WebVector<blink::WebMediaKeySystemConfiguration>&
      configurations = request.supportedConfigurations();

  // TODO(sandersd): Remove once Blink requires the configurations parameter for
  // requestMediaKeySystemAccess().
  if (configurations.isEmpty()) {
    reporter->ReportSupported();
    request.requestSucceeded(WebContentDecryptionModuleAccessImpl::Create(
        request.keySystem(), blink::WebMediaKeySystemConfiguration(),
        request.securityOrigin(), weak_factory_.GetWeakPtr()));
    return;
  }

  for (size_t i = 0; i < configurations.size(); i++) {
    const blink::WebMediaKeySystemConfiguration& candidate = configurations[i];
    blink::WebMediaKeySystemConfiguration accumulated_configuration;
    if (GetSupportedConfiguration(key_system, candidate,
                                  request.securityOrigin(),
                                  &accumulated_configuration)) {
      reporter->ReportSupported();
      request.requestSucceeded(WebContentDecryptionModuleAccessImpl::Create(
          request.keySystem(), accumulated_configuration,
          request.securityOrigin(), weak_factory_.GetWeakPtr()));
      return;
    }
  }

  // 7.4 Reject promise with a new DOMException whose name is NotSupportedError.
  request.requestNotSupported(
      "None of the requested configurations were supported.");
}

void WebEncryptedMediaClientImpl::CreateCdm(
    const blink::WebString& key_system,
    const blink::WebSecurityOrigin& security_origin,
    blink::WebContentDecryptionModuleResult result) {
  WebContentDecryptionModuleImpl::Create(cdm_factory_.get(), security_origin,
                                         key_system, result);
}

// Lazily create Reporters.
WebEncryptedMediaClientImpl::Reporter* WebEncryptedMediaClientImpl::GetReporter(
    const std::string& key_system) {
  std::string uma_name = GetKeySystemNameForUMA(key_system);
  Reporter* reporter = reporters_.get(uma_name);
  if (reporter != nullptr)
    return reporter;

  // Reporter not found, so create one.
  auto result =
      reporters_.add(uma_name, make_scoped_ptr(new Reporter(uma_name)));
  DCHECK(result.second);
  return result.first->second;
}

}  // namespace media