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 const KeyAddedCB
& key_added_cb
,
53 const KeyErrorCB
& key_error_cb
,
54 const KeyMessageCB
& key_message_cb
);
55 virtual ~ProxyDecryptor();
57 // Creates the CDM and fires |cdm_created_cb|. This method should only be
58 // called once. If CDM creation failed, all following GenerateKeyRequest,
59 // AddKey and CancelKeyRequest calls will result in a KeyError.
60 void CreateCdm(CdmFactory
* cdm_factory
,
61 const std::string
& key_system
,
62 const GURL
& security_origin
,
63 const CdmContextReadyCB
& cdm_context_ready_cb
);
65 // May only be called after CreateCDM().
66 void GenerateKeyRequest(EmeInitDataType init_data_type
,
67 const uint8
* init_data
,
68 int init_data_length
);
69 void AddKey(const uint8
* key
, int key_length
,
70 const uint8
* init_data
, int init_data_length
,
71 const std::string
& session_id
);
72 void CancelKeyRequest(const std::string
& session_id
);
75 // Callback for CreateCdm().
76 void OnCdmCreated(const std::string
& key_system
,
77 const GURL
& security_origin
,
78 const CdmContextReadyCB
& cdm_context_ready_cb
,
79 scoped_ptr
<MediaKeys
> cdm
);
81 void GenerateKeyRequestInternal(EmeInitDataType init_data_type
,
82 const std::vector
<uint8
>& init_data
);
84 // Callbacks for firing session events.
85 void OnSessionMessage(const std::string
& session_id
,
86 MediaKeys::MessageType message_type
,
87 const std::vector
<uint8
>& message
,
88 const GURL
& legacy_destination_url
);
89 void OnSessionKeysChange(const std::string
& session_id
,
90 bool has_additional_usable_key
,
91 CdmKeysInfo keys_info
);
92 void OnSessionExpirationUpdate(const std::string
& session_id
,
93 const base::Time
& new_expiry_time
);
94 void GenerateKeyAdded(const std::string
& session_id
);
95 void OnSessionClosed(const std::string
& session_id
);
96 void OnLegacySessionError(const std::string
& session_id
,
97 MediaKeys::Exception exception_code
,
99 const std::string
& error_message
);
101 // Callback for permission request.
102 void OnPermissionStatus(MediaKeys::SessionType session_type
,
103 EmeInitDataType init_data_type
,
104 const std::vector
<uint8
>& init_data
,
105 scoped_ptr
<NewSessionCdmPromise
> promise
,
108 enum SessionCreationType
{
114 // Called when a session is actually created or loaded.
115 void SetSessionId(SessionCreationType session_type
,
116 const std::string
& session_id
);
118 struct PendingGenerateKeyRequestData
{
119 PendingGenerateKeyRequestData(EmeInitDataType init_data_type
,
120 const std::vector
<uint8
>& init_data
);
121 ~PendingGenerateKeyRequestData();
123 const EmeInitDataType init_data_type
;
124 const std::vector
<uint8
> init_data
;
127 bool is_creating_cdm_
;
129 // The real MediaKeys that manages key operations for the ProxyDecryptor.
130 scoped_ptr
<MediaKeys
> media_keys_
;
132 MediaPermission
* media_permission_
;
134 // Callbacks for firing key events.
135 KeyAddedCB key_added_cb_
;
136 KeyErrorCB key_error_cb_
;
137 KeyMessageCB key_message_cb_
;
139 std::string key_system_
;
140 GURL security_origin_
;
142 // Keep track of both persistent and non-persistent sessions.
143 base::hash_map
<std::string
, bool> active_sessions_
;
147 ScopedVector
<PendingGenerateKeyRequestData
> pending_requests_
;
149 // NOTE: Weak pointers must be invalidated before all other member variables.
150 base::WeakPtrFactory
<ProxyDecryptor
> weak_ptr_factory_
;
152 DISALLOW_COPY_AND_ASSIGN(ProxyDecryptor
);
157 #endif // MEDIA_CDM_PROXY_DECRYPTOR_H_