summaryrefslogtreecommitdiff
path: root/chromium/media/blink/key_system_config_selector.h
blob: 92fc3353e5b90bf6d2033bd12c60d15d211375b6 (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
// Copyright 2015 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.

#ifndef MEDIA_BLINK_KEY_SYSTEM_CONFIG_SELECTOR_H_
#define MEDIA_BLINK_KEY_SYSTEM_CONFIG_SELECTOR_H_

#include <memory>
#include <string>
#include <vector>

#include "base/bind.h"
#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "media/base/eme_constants.h"
#include "media/blink/media_blink_export.h"
#include "third_party/blink/public/platform/web_media_key_system_media_capability.h"
#include "third_party/blink/public/platform/web_vector.h"

namespace blink {

struct WebMediaKeySystemConfiguration;
class WebString;

}  // namespace blink

namespace media {

struct CdmConfig;
class KeySystems;
class MediaPermission;

class MEDIA_BLINK_EXPORT KeySystemConfigSelector {
 public:
  KeySystemConfigSelector(KeySystems* key_systems,
                          MediaPermission* media_permission);

  ~KeySystemConfigSelector();

  // The unsupported statuses will be mapped to different rejection messages.
  // The statuses should not leak sensitive information, e.g. incognito mode or
  // user settings. See https://crbug.com/760720
  enum class Status {
    kSupported,
    kUnsupportedPlatform,
    kUnsupportedKeySystem,
    kUnsupportedConfigs,
  };

  // Callback for the result of `SelectConfig()`. The returned configs must be
  // non-null iff `status` is `kSupported`.
  using SelectConfigCB = base::OnceCallback<
      void(Status status, blink::WebMediaKeySystemConfiguration*, CdmConfig*)>;

  void SelectConfig(
      const blink::WebString& key_system,
      const blink::WebVector<blink::WebMediaKeySystemConfiguration>&
          candidate_configurations,
      SelectConfigCB cb);

  using IsSupportedMediaTypeCB =
      base::RepeatingCallback<bool(const std::string& container_mime_type,
                                   const std::string& codecs,
                                   bool use_aes_decryptor)>;

  void SetIsSupportedMediaTypeCBForTesting(IsSupportedMediaTypeCB cb) {
    is_supported_media_type_cb_ = std::move(cb);
  }

 private:
  struct SelectionRequest;
  class ConfigState;

  enum ConfigurationSupport {
    CONFIGURATION_NOT_SUPPORTED,
    CONFIGURATION_REQUIRES_PERMISSION,
    CONFIGURATION_SUPPORTED,
  };

  void SelectConfigInternal(std::unique_ptr<SelectionRequest> request);

  void OnPermissionResult(std::unique_ptr<SelectionRequest> request,
                          bool is_permission_granted);

  ConfigurationSupport GetSupportedConfiguration(
      const std::string& key_system,
      const blink::WebMediaKeySystemConfiguration& candidate,
      ConfigState* config_state,
      blink::WebMediaKeySystemConfiguration* accumulated_configuration);

  bool GetSupportedCapabilities(
      const std::string& key_system,
      EmeMediaType media_type,
      const blink::WebVector<blink::WebMediaKeySystemMediaCapability>&
          requested_media_capabilities,
      ConfigState* config_state,
      std::vector<blink::WebMediaKeySystemMediaCapability>*
          supported_media_capabilities);

  bool IsSupportedContentType(const std::string& key_system,
                              EmeMediaType media_type,
                              const std::string& container_mime_type,
                              const std::string& codecs,
                              ConfigState* config_state);

  EmeConfigRule GetEncryptionSchemeConfigRule(
      const std::string& key_system,
      const blink::WebMediaKeySystemMediaCapability::EncryptionScheme
          encryption_scheme);

  KeySystems* const key_systems_;
  MediaPermission* media_permission_;

  // A callback used to check whether a media type is supported. Only set in
  // tests. If null the implementation will check the support using MimeUtil.
  IsSupportedMediaTypeCB is_supported_media_type_cb_;

  base::WeakPtrFactory<KeySystemConfigSelector> weak_factory_{this};

  DISALLOW_COPY_AND_ASSIGN(KeySystemConfigSelector);
};

}  // namespace media

#endif  // MEDIA_BLINK_KEY_SYSTEM_CONFIG_SELECTOR_H_