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. 4 5 #ifndef MEDIA_BASE_MEDIA_KEYS_H_ 6 #define MEDIA_BASE_MEDIA_KEYS_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "base/basictypes.h" 12 #include "base/callback.h" 13 #include "base/memory/scoped_ptr.h" 14 #include "media/base/media_export.h" 15 16 namespace media { 17 18 class Decryptor; 19 20 // Performs media key operations. 21 // 22 // All key operations are called on the renderer thread. Therefore, these calls 23 // should be fast and nonblocking; key events should be fired asynchronously. 24 class MEDIA_EXPORT MediaKeys { 25 public: 26 // Reported to UMA, so never reuse a value! 27 // Must be kept in sync with WebKit::WebMediaPlayerClient::MediaKeyErrorCode 28 // (enforced in webmediaplayer_impl.cc). 29 enum KeyError { 30 kUnknownError = 1, 31 kClientError, 32 // The following v0.1b values have never been used. 33 // kServiceError, 34 // kOutputError, 35 // kHardwareChangeError, 36 // kDomainError, 37 kMaxKeyError // Must be last and greater than any legit value. 38 }; 39 40 MediaKeys(); 41 virtual ~MediaKeys(); 42 43 // Generates a key request with the |type| and |init_data| provided. 44 // Returns true if generating key request succeeded, false otherwise. 45 // Note: AddKey() and CancelKeyRequest() should only be called after 46 // GenerateKeyRequest() returns true. 47 virtual bool GenerateKeyRequest(const std::string& type, 48 const uint8* init_data, 49 int init_data_length) = 0; 50 51 // Adds a |key| to the session. The |key| is not limited to a decryption 52 // key. It can be any data that the key system accepts, such as a license. 53 // If multiple calls of this function set different keys for the same 54 // key ID, the older key will be replaced by the newer key. 55 virtual void AddKey(const uint8* key, int key_length, 56 const uint8* init_data, int init_data_length, 57 const std::string& session_id) = 0; 58 59 // Cancels the key request specified by |session_id|. 60 virtual void CancelKeyRequest(const std::string& session_id) = 0; 61 62 // Gets the Decryptor object associated with the MediaKeys. Returns NULL if 63 // no Decryptor object is associated. The returned object is only guaranteed 64 // to be valid during the MediaKeys' lifetime. 65 virtual Decryptor* GetDecryptor(); 66 67 private: 68 DISALLOW_COPY_AND_ASSIGN(MediaKeys); 69 }; 70 71 // Key event callbacks. See the spec for details: 72 // http://dvcs.w3.org/hg/html-media/raw-file/eme-v0.1b/encrypted-media/encrypted-media.html#event-summary 73 typedef base::Callback<void(const std::string& session_id)> KeyAddedCB; 74 75 typedef base::Callback<void(const std::string& session_id, 76 media::MediaKeys::KeyError error_code, 77 int system_code)> KeyErrorCB; 78 79 typedef base::Callback<void(const std::string& session_id, 80 const std::vector<uint8>& message, 81 const std::string& default_url)> KeyMessageCB; 82 83 typedef base::Callback<void(const std::string& session_id, 84 const std::string& type, 85 scoped_ptr<uint8[]> init_data, 86 int init_data_size)> NeedKeyCB; 87 88 } // namespace media 89 90 #endif // MEDIA_BASE_MEDIA_KEYS_H_ 91