| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MEDIA_CRYPTO_AES_DECRYPTOR_H_ | 5 #ifndef MEDIA_CRYPTO_AES_DECRYPTOR_H_ |
| 6 #define MEDIA_CRYPTO_AES_DECRYPTOR_H_ | 6 #define MEDIA_CRYPTO_AES_DECRYPTOR_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/hash_tables.h" | 11 #include "base/hash_tables.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/synchronization/lock.h" | 13 #include "base/synchronization/lock.h" |
| 14 #include "media/base/media_export.h" | 14 #include "media/base/media_export.h" |
| 15 #include "media/crypto/decryptor.h" |
| 15 | 16 |
| 16 namespace crypto { | 17 namespace crypto { |
| 17 class SymmetricKey; | 18 class SymmetricKey; |
| 18 } | 19 } |
| 19 | 20 |
| 20 namespace media { | 21 namespace media { |
| 21 | 22 |
| 22 class DecoderBuffer; | 23 // Decrypts AES encrypted buffer into unencrypted buffer. |
| 23 class DecryptorClient; | 24 class MEDIA_EXPORT AesDecryptor : public Decryptor { |
| 25 public: |
| 26 explicit AesDecryptor(DecryptorClient* client); |
| 27 virtual ~AesDecryptor(); |
| 24 | 28 |
| 25 // Decrypts AES encrypted buffer into unencrypted buffer. | 29 // Decryptor implementation. |
| 26 // All public methods other than Decrypt() will be called on the renderer | 30 virtual void GenerateKeyRequest(const std::string& key_system, |
| 27 // thread. Therefore, these calls should be fast and nonblocking, with key | 31 const uint8* init_data, |
| 28 // events fired asynchronously. Decrypt() will be called on the (video/audio) | 32 int init_data_length) OVERRIDE; |
| 29 // decoder thread synchronously. | 33 virtual void AddKey(const std::string& key_system, |
| 30 class MEDIA_EXPORT AesDecryptor { | 34 const uint8* key, |
| 31 public: | 35 int key_length, |
| 32 enum KeyError { | 36 const uint8* init_data, |
| 33 kUnknownError = 1, | 37 int init_data_length, |
| 34 kClientError, | 38 const std::string& session_id) OVERRIDE; |
| 35 kServiceError, | 39 virtual void CancelKeyRequest(const std::string& key_system, |
| 36 kOutputError, | 40 const std::string& session_id) OVERRIDE; |
| 37 kHardwareChangeError, | 41 virtual scoped_refptr<DecoderBuffer> Decrypt( |
| 38 kDomainError | 42 const scoped_refptr<DecoderBuffer>& input) OVERRIDE; |
| 39 }; | |
| 40 | |
| 41 // The AesDecryptor does not take ownership of the |client|. The |client| | |
| 42 // must be valid throughout the lifetime of the AesDecryptor. | |
| 43 explicit AesDecryptor(DecryptorClient* client); | |
| 44 ~AesDecryptor(); | |
| 45 | |
| 46 // Generates a key request. The result of this call will be reported via the | |
| 47 // client's KeyMessage() or KeyError() methods. | |
| 48 void GenerateKeyRequest(const std::string& key_system, | |
| 49 const uint8* init_data, | |
| 50 int init_data_length); | |
| 51 | |
| 52 // Adds a |key| to the key system. The key is not limited to a decryption key. | |
| 53 // It can be any data that the key system accepts, such as a license. | |
| 54 // If multiple calls of this function set different keys for the same | |
| 55 // |key_id|, the older key will be replaced by the newer key. | |
| 56 // The result of this call will be reported via the client's KeyAdded(), | |
| 57 // KeyMessage() or KeyError() methods. | |
| 58 void AddKey(const std::string& key_system, | |
| 59 const uint8* key, | |
| 60 int key_length, | |
| 61 const uint8* init_data, | |
| 62 int init_data_length, | |
| 63 const std::string& session_id); | |
| 64 | |
| 65 // Cancels the key request specified by |session_id|. | |
| 66 void CancelKeyRequest(const std::string& key_system, | |
| 67 const std::string& session_id); | |
| 68 | |
| 69 // Decrypts the |input| buffer, which should not be NULL. | |
| 70 // Returns a DecoderBuffer with the decrypted data if decryption succeeded. | |
| 71 // Returns NULL if decryption failed. | |
| 72 scoped_refptr<DecoderBuffer> Decrypt( | |
| 73 const scoped_refptr<DecoderBuffer>& input); | |
| 74 | 43 |
| 75 private: | 44 private: |
| 76 // KeyMap owns the crypto::SymmetricKey* and must delete them when they are | 45 // KeyMap owns the crypto::SymmetricKey* and must delete them when they are |
| 77 // not needed any more. | 46 // not needed any more. |
| 78 typedef base::hash_map<std::string, crypto::SymmetricKey*> KeyMap; | 47 typedef base::hash_map<std::string, crypto::SymmetricKey*> KeyMap; |
| 79 // Since only Decrypt() is called off the renderer thread, we only need to | 48 // Since only Decrypt() is called off the renderer thread, we only need to |
| 80 // protect |key_map_|, the only member variable that is shared between | 49 // protect |key_map_|, the only member variable that is shared between |
| 81 // Decrypt() and other methods. | 50 // Decrypt() and other methods. |
| 82 KeyMap key_map_; // Protected by the |key_map_lock_|. | 51 KeyMap key_map_; // Protected by the |key_map_lock_|. |
| 83 base::Lock key_map_lock_; // Protects the |key_map_|. | 52 base::Lock key_map_lock_; // Protects the |key_map_|. |
| 84 | 53 |
| 85 DecryptorClient* client_; | |
| 86 | |
| 87 // Make session ID unique per renderer by making it static. | 54 // Make session ID unique per renderer by making it static. |
| 88 // TODO(xhwang): Make session ID more strictly defined if needed: | 55 // TODO(xhwang): Make session ID more strictly defined if needed: |
| 89 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=16739#c0 | 56 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=16739#c0 |
| 90 static uint32_t next_session_id_; | 57 static uint32_t next_session_id_; |
| 91 | 58 |
| 92 DISALLOW_COPY_AND_ASSIGN(AesDecryptor); | 59 DISALLOW_COPY_AND_ASSIGN(AesDecryptor); |
| 93 }; | 60 }; |
| 94 | 61 |
| 95 } // namespace media | 62 } // namespace media |
| 96 | 63 |
| 97 #endif // MEDIA_CRYPTO_AES_DECRYPTOR_H_ | 64 #endif // MEDIA_CRYPTO_AES_DECRYPTOR_H_ |
| OLD | NEW |