Report errors from ChromiumEnv::GetChildren in Posix.
[chromium-blink-merge.git] / media / cdm / aes_decryptor.h
blob3ab4bc0f9f47aeddff6b59bb961069b8ad8f3ecb
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 <string>
10 #include "base/basictypes.h"
11 #include "base/containers/hash_tables.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/strings/string_piece.h"
15 #include "base/synchronization/lock.h"
16 #include "media/base/decryptor.h"
17 #include "media/base/media_export.h"
18 #include "media/base/media_keys.h"
20 namespace crypto {
21 class SymmetricKey;
24 namespace media {
26 // Decrypts an AES encrypted buffer into an unencrypted buffer. The AES
27 // encryption must be CTR with a key size of 128bits.
28 class MEDIA_EXPORT AesDecryptor : public MediaKeys, public Decryptor {
29 public:
30 AesDecryptor(const KeyAddedCB& key_added_cb,
31 const KeyErrorCB& key_error_cb,
32 const KeyMessageCB& key_message_cb);
33 virtual ~AesDecryptor();
35 // MediaKeys implementation.
36 virtual bool GenerateKeyRequest(const std::string& type,
37 const uint8* init_data,
38 int init_data_length) OVERRIDE;
39 virtual void AddKey(const uint8* key, int key_length,
40 const uint8* init_data, int init_data_length,
41 const std::string& session_id) OVERRIDE;
42 virtual void CancelKeyRequest(const std::string& session_id) OVERRIDE;
43 virtual Decryptor* GetDecryptor() OVERRIDE;
45 // Decryptor implementation.
46 virtual void RegisterNewKeyCB(StreamType stream_type,
47 const NewKeyCB& key_added_cb) OVERRIDE;
48 virtual void Decrypt(StreamType stream_type,
49 const scoped_refptr<DecoderBuffer>& encrypted,
50 const DecryptCB& decrypt_cb) OVERRIDE;
51 virtual void CancelDecrypt(StreamType stream_type) OVERRIDE;
52 virtual void InitializeAudioDecoder(const AudioDecoderConfig& config,
53 const DecoderInitCB& init_cb) OVERRIDE;
54 virtual void InitializeVideoDecoder(const VideoDecoderConfig& config,
55 const DecoderInitCB& init_cb) OVERRIDE;
56 virtual void DecryptAndDecodeAudio(
57 const scoped_refptr<DecoderBuffer>& encrypted,
58 const AudioDecodeCB& audio_decode_cb) OVERRIDE;
59 virtual void DecryptAndDecodeVideo(
60 const scoped_refptr<DecoderBuffer>& encrypted,
61 const VideoDecodeCB& video_decode_cb) OVERRIDE;
62 virtual void ResetDecoder(StreamType stream_type) OVERRIDE;
63 virtual void DeinitializeDecoder(StreamType stream_type) OVERRIDE;
65 private:
66 // TODO(fgalligan): Remove this and change KeyMap to use crypto::SymmetricKey
67 // as there are no decryptors that are performing an integrity check.
68 // Helper class that manages the decryption key.
69 class DecryptionKey {
70 public:
71 explicit DecryptionKey(const std::string& secret);
72 ~DecryptionKey();
74 // Creates the encryption key.
75 bool Init();
77 crypto::SymmetricKey* decryption_key() { return decryption_key_.get(); }
79 private:
80 // The base secret that is used to create the decryption key.
81 const std::string secret_;
83 // The key used to decrypt the data.
84 scoped_ptr<crypto::SymmetricKey> decryption_key_;
86 DISALLOW_COPY_AND_ASSIGN(DecryptionKey);
89 // Creates a DecryptionKey using |key_string| and associates it with |key_id|.
90 // Returns true if successful.
91 bool AddDecryptionKey(const std::string& key_id,
92 const std::string& key_string);
94 // Gets a DecryptionKey associated with |key_id|. The AesDecryptor still owns
95 // the key. Returns NULL if no key is associated with |key_id|.
96 DecryptionKey* GetKey(const std::string& key_id) const;
98 // Callbacks for firing key events.
99 KeyAddedCB key_added_cb_;
100 KeyErrorCB key_error_cb_;
101 KeyMessageCB key_message_cb_;
103 // KeyMap owns the DecryptionKey* and must delete them when they are
104 // not needed any more.
105 typedef base::hash_map<std::string, DecryptionKey*> KeyMap;
107 // Since only Decrypt() is called off the renderer thread, we only need to
108 // protect |key_map_|, the only member variable that is shared between
109 // Decrypt() and other methods.
110 KeyMap key_map_; // Protected by the |key_map_lock_|.
111 mutable base::Lock key_map_lock_; // Protects the |key_map_|.
113 // Make session ID unique per renderer by making it static.
114 // TODO(xhwang): Make session ID more strictly defined if needed:
115 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=16739#c0
116 static uint32 next_session_id_;
118 NewKeyCB new_audio_key_cb_;
119 NewKeyCB new_video_key_cb_;
121 DISALLOW_COPY_AND_ASSIGN(AesDecryptor);
124 } // namespace media
126 #endif // MEDIA_CRYPTO_AES_DECRYPTOR_H_