1 // Copyright 2013 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 #ifndef MEDIA_CDM_PROXY_DECRYPTOR_H_
6 #define MEDIA_CDM_PROXY_DECRYPTOR_H_
11 #include "base/basictypes.h"
12 #include "base/containers/hash_tables.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/scoped_vector.h"
15 #include "base/memory/weak_ptr.h"
16 #include "media/base/cdm_context.h"
17 #include "media/base/decryptor.h"
18 #include "media/base/eme_constants.h"
19 #include "media/base/media_export.h"
20 #include "media/base/media_keys.h"
26 class MediaPermission
;
28 // ProxyDecryptor is for EME v0.1b only. It should not be used for the WD API.
29 // A decryptor proxy that creates a real decryptor object on demand and
30 // forwards decryptor calls to it.
32 // TODO(xhwang): Currently we don't support run-time switching among decryptor
33 // objects. Fix this when needed.
34 // TODO(xhwang): The ProxyDecryptor is not a Decryptor. Find a better name!
35 class MEDIA_EXPORT ProxyDecryptor
{
37 // Callback to provide a CdmContext when the CDM creation is finished.
38 // If CDM creation failed, |cdm_context| will be null.
39 typedef base::Callback
<void(CdmContext
* cdm_context
)> CdmContextReadyCB
;
41 // These are similar to the callbacks in media_keys.h, but pass back the
42 // session ID rather than the internal session ID.
43 typedef base::Callback
<void(const std::string
& session_id
)> KeyAddedCB
;
44 typedef base::Callback
<void(const std::string
& session_id
,
45 MediaKeys::KeyError error_code
,
46 uint32 system_code
)> KeyErrorCB
;
47 typedef base::Callback
<void(const std::string
& session_id
,
48 const std::vector
<uint8
>& message
,
49 const GURL
& destination_url
)> KeyMessageCB
;
51 ProxyDecryptor(MediaPermission
* media_permission
,
52 bool use_hw_secure_codecs
,
53 const KeyAddedCB
& key_added_cb
,
54 const KeyErrorCB
& key_error_cb
,
55 const KeyMessageCB
& key_message_cb
);
56 virtual ~ProxyDecryptor();
58 // Creates the CDM and fires |cdm_created_cb|. This method should only be
59 // called once. If CDM creation failed, all following GenerateKeyRequest,
60 // AddKey and CancelKeyRequest calls will result in a KeyError.
61 void CreateCdm(CdmFactory
* cdm_factory
,
62 const std::string
& key_system
,
63 const GURL
& security_origin
,
64 const CdmContextReadyCB
& cdm_context_ready_cb
);
66 // May only be called after CreateCDM().
67 void GenerateKeyRequest(EmeInitDataType init_data_type
,
68 const uint8
* init_data
,
69 int init_data_length
);
70 void AddKey(const uint8
* key
, int key_length
,
71 const uint8
* init_data
, int init_data_length
,
72 const std::string
& session_id
);
73 void CancelKeyRequest(const std::string
& session_id
);
76 // Callback for CreateCdm().
77 void OnCdmCreated(const std::string
& key_system
,
78 const GURL
& security_origin
,
79 const CdmContextReadyCB
& cdm_context_ready_cb
,
80 scoped_ptr
<MediaKeys
> cdm
,
81 const std::string
& error_message
);
83 void GenerateKeyRequestInternal(EmeInitDataType init_data_type
,
84 const std::vector
<uint8
>& init_data
);
86 // Callbacks for firing session events.
87 void OnSessionMessage(const std::string
& session_id
,
88 MediaKeys::MessageType message_type
,
89 const std::vector
<uint8
>& message
,
90 const GURL
& legacy_destination_url
);
91 void OnSessionKeysChange(const std::string
& session_id
,
92 bool has_additional_usable_key
,
93 CdmKeysInfo keys_info
);
94 void OnSessionExpirationUpdate(const std::string
& session_id
,
95 const base::Time
& new_expiry_time
);
96 void GenerateKeyAdded(const std::string
& session_id
);
97 void OnSessionClosed(const std::string
& session_id
);
98 void OnLegacySessionError(const std::string
& session_id
,
99 MediaKeys::Exception exception_code
,
101 const std::string
& error_message
);
103 // Callback for permission request.
104 void OnPermissionStatus(MediaKeys::SessionType session_type
,
105 EmeInitDataType init_data_type
,
106 const std::vector
<uint8
>& init_data
,
107 scoped_ptr
<NewSessionCdmPromise
> promise
,
110 enum SessionCreationType
{
116 // Called when a session is actually created or loaded.
117 void SetSessionId(SessionCreationType session_type
,
118 const std::string
& session_id
);
120 struct PendingGenerateKeyRequestData
{
121 PendingGenerateKeyRequestData(EmeInitDataType init_data_type
,
122 const std::vector
<uint8
>& init_data
);
123 ~PendingGenerateKeyRequestData();
125 const EmeInitDataType init_data_type
;
126 const std::vector
<uint8
> init_data
;
129 bool is_creating_cdm_
;
131 // The real MediaKeys that manages key operations for the ProxyDecryptor.
132 scoped_ptr
<MediaKeys
> media_keys_
;
134 #if defined(OS_CHROMEOS) || defined(OS_ANDROID)
135 MediaPermission
* media_permission_
;
138 bool use_hw_secure_codecs_
;
140 // Callbacks for firing key events.
141 KeyAddedCB key_added_cb_
;
142 KeyErrorCB key_error_cb_
;
143 KeyMessageCB key_message_cb_
;
145 std::string key_system_
;
146 GURL security_origin_
;
148 // Keep track of both persistent and non-persistent sessions.
149 base::hash_map
<std::string
, bool> active_sessions_
;
153 ScopedVector
<PendingGenerateKeyRequestData
> pending_requests_
;
155 // NOTE: Weak pointers must be invalidated before all other member variables.
156 base::WeakPtrFactory
<ProxyDecryptor
> weak_ptr_factory_
;
158 DISALLOW_COPY_AND_ASSIGN(ProxyDecryptor
);
163 #endif // MEDIA_CDM_PROXY_DECRYPTOR_H_