base::ManualConstructor improvements
[chromium-blink-merge.git] / media / cdm / aes_decryptor.h
blob5c7a06c2cc44a39af31bf2a4e649e5086dd1a01e
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_CRYPTO_AES_DECRYPTOR_H_
6 #define MEDIA_CRYPTO_AES_DECRYPTOR_H_
8 #include <set>
9 #include <string>
11 #include "base/basictypes.h"
12 #include "base/containers/scoped_ptr_hash_map.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/synchronization/lock.h"
16 #include "media/base/cdm_context.h"
17 #include "media/base/decryptor.h"
18 #include "media/base/media_export.h"
19 #include "media/base/media_keys.h"
21 class GURL;
23 namespace crypto {
24 class SymmetricKey;
27 namespace media {
29 // Decrypts an AES encrypted buffer into an unencrypted buffer. The AES
30 // encryption must be CTR with a key size of 128bits.
31 class MEDIA_EXPORT AesDecryptor : public MediaKeys,
32 public CdmContext,
33 public Decryptor {
34 public:
35 AesDecryptor(const GURL& security_origin,
36 const SessionMessageCB& session_message_cb,
37 const SessionClosedCB& session_closed_cb,
38 const SessionKeysChangeCB& session_keys_change_cb);
39 ~AesDecryptor() override;
41 // MediaKeys implementation.
42 void SetServerCertificate(const uint8* certificate_data,
43 int certificate_data_length,
44 scoped_ptr<SimpleCdmPromise> promise) override;
45 void CreateSessionAndGenerateRequest(
46 SessionType session_type,
47 EmeInitDataType init_data_type,
48 const uint8* init_data,
49 int init_data_length,
50 scoped_ptr<NewSessionCdmPromise> promise) override;
51 void LoadSession(SessionType session_type,
52 const std::string& session_id,
53 scoped_ptr<NewSessionCdmPromise> promise) override;
54 void UpdateSession(const std::string& session_id,
55 const uint8* response,
56 int response_length,
57 scoped_ptr<SimpleCdmPromise> promise) override;
58 void CloseSession(const std::string& session_id,
59 scoped_ptr<SimpleCdmPromise> promise) override;
60 void RemoveSession(const std::string& session_id,
61 scoped_ptr<SimpleCdmPromise> promise) override;
62 CdmContext* GetCdmContext() override;
64 // CdmContext implementation.
65 Decryptor* GetDecryptor() override;
66 int GetCdmId() const override;
68 // Decryptor implementation.
69 void RegisterNewKeyCB(StreamType stream_type,
70 const NewKeyCB& key_added_cb) override;
71 void Decrypt(StreamType stream_type,
72 const scoped_refptr<DecoderBuffer>& encrypted,
73 const DecryptCB& decrypt_cb) override;
74 void CancelDecrypt(StreamType stream_type) override;
75 void InitializeAudioDecoder(const AudioDecoderConfig& config,
76 const DecoderInitCB& init_cb) override;
77 void InitializeVideoDecoder(const VideoDecoderConfig& config,
78 const DecoderInitCB& init_cb) override;
79 void DecryptAndDecodeAudio(const scoped_refptr<DecoderBuffer>& encrypted,
80 const AudioDecodeCB& audio_decode_cb) override;
81 void DecryptAndDecodeVideo(const scoped_refptr<DecoderBuffer>& encrypted,
82 const VideoDecodeCB& video_decode_cb) override;
83 void ResetDecoder(StreamType stream_type) override;
84 void DeinitializeDecoder(StreamType stream_type) override;
86 private:
87 // TODO(fgalligan): Remove this and change KeyMap to use crypto::SymmetricKey
88 // as there are no decryptors that are performing an integrity check.
89 // Helper class that manages the decryption key.
90 class DecryptionKey {
91 public:
92 explicit DecryptionKey(const std::string& secret);
93 ~DecryptionKey();
95 // Creates the encryption key.
96 bool Init();
98 crypto::SymmetricKey* decryption_key() { return decryption_key_.get(); }
100 private:
101 // The base secret that is used to create the decryption key.
102 const std::string secret_;
104 // The key used to decrypt the data.
105 scoped_ptr<crypto::SymmetricKey> decryption_key_;
107 DISALLOW_COPY_AND_ASSIGN(DecryptionKey);
110 // Keep track of the keys for a key ID. If multiple sessions specify keys
111 // for the same key ID, then the last key inserted is used. The structure is
112 // optimized so that Decrypt() has fast access, at the cost of slow deletion
113 // of keys when a session is released.
114 class SessionIdDecryptionKeyMap;
116 // Key ID <-> SessionIdDecryptionKeyMap map.
117 typedef base::ScopedPtrHashMap<std::string, SessionIdDecryptionKeyMap>
118 KeyIdToSessionKeysMap;
120 // Creates a DecryptionKey using |key_string| and associates it with |key_id|.
121 // Returns true if successful.
122 bool AddDecryptionKey(const std::string& session_id,
123 const std::string& key_id,
124 const std::string& key_string);
126 // Gets a DecryptionKey associated with |key_id|. The AesDecryptor still owns
127 // the key. Returns NULL if no key is associated with |key_id|.
128 DecryptionKey* GetKey(const std::string& key_id) const;
130 // Determines if |key_id| is already specified for |session_id|.
131 bool HasKey(const std::string& session_id, const std::string& key_id);
133 // Deletes all keys associated with |session_id|.
134 void DeleteKeysForSession(const std::string& session_id);
136 // Callbacks for firing session events.
137 SessionMessageCB session_message_cb_;
138 SessionClosedCB session_closed_cb_;
139 SessionKeysChangeCB session_keys_change_cb_;
141 // Since only Decrypt() is called off the renderer thread, we only need to
142 // protect |key_map_|, the only member variable that is shared between
143 // Decrypt() and other methods.
144 KeyIdToSessionKeysMap key_map_; // Protected by |key_map_lock_|.
145 mutable base::Lock key_map_lock_; // Protects the |key_map_|.
147 // Keeps track of current valid sessions.
148 std::set<std::string> valid_sessions_;
150 // Make session ID unique per renderer by making it static. Session
151 // IDs seen by the app will be "1", "2", etc.
152 static uint32 next_session_id_;
154 NewKeyCB new_audio_key_cb_;
155 NewKeyCB new_video_key_cb_;
157 // Protect |new_audio_key_cb_| and |new_video_key_cb_| as they are set on the
158 // main thread but called on the media thread.
159 mutable base::Lock new_key_cb_lock_;
161 DISALLOW_COPY_AND_ASSIGN(AesDecryptor);
164 } // namespace media
166 #endif // MEDIA_CRYPTO_AES_DECRYPTOR_H_