1 // Copyright 2014 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 CONTENT_RENDERER_MEDIA_CDM_SESSION_ADAPTER_H_ 6 #define CONTENT_RENDERER_MEDIA_CDM_SESSION_ADAPTER_H_ 7 8 #include <map> 9 #include <string> 10 11 #include "base/basictypes.h" 12 #include "base/containers/hash_tables.h" 13 #include "base/memory/ref_counted.h" 14 #include "base/memory/weak_ptr.h" 15 #include "media/base/media_keys.h" 16 #include "third_party/WebKit/public/platform/WebContentDecryptionModuleSession.h" 17 18 #if defined(ENABLE_PEPPER_CDMS) 19 #include "content/renderer/media/crypto/pepper_cdm_wrapper.h" 20 #endif 21 22 class GURL; 23 24 namespace content { 25 26 #if defined(ENABLE_BROWSER_CDMS) 27 class RendererCdmManager; 28 #endif 29 30 class WebContentDecryptionModuleSessionImpl; 31 32 // Owns the CDM instance and makes calls from session objects to the CDM. 33 // Forwards the web session ID-based callbacks of the MediaKeys interface to the 34 // appropriate session object. Callers should hold references to this class 35 // as long as they need the CDM instance. 36 class CdmSessionAdapter : public base::RefCounted<CdmSessionAdapter> { 37 public: 38 CdmSessionAdapter(); 39 40 // Returns true on success. 41 bool Initialize( 42 #if defined(ENABLE_PEPPER_CDMS) 43 const CreatePepperCdmCB& create_pepper_cdm_cb, 44 #elif defined(ENABLE_BROWSER_CDMS) 45 RendererCdmManager* manager, 46 #endif 47 const std::string& key_system, 48 const GURL& security_origin); 49 50 // Creates a new session and adds it to the internal map. The caller owns the 51 // created session. RemoveSession() must be called when destroying it, if 52 // RegisterSession() was called. 53 WebContentDecryptionModuleSessionImpl* CreateSession( 54 blink::WebContentDecryptionModuleSession::Client* client); 55 56 // Adds a session to the internal map. Called once the session is successfully 57 // initialized. 58 void RegisterSession( 59 const std::string& web_session_id, 60 base::WeakPtr<WebContentDecryptionModuleSessionImpl> session); 61 62 // Removes a session from the internal map. 63 void RemoveSession(const std::string& web_session_id); 64 65 // Initializes a session with the |init_data_type|, |init_data| and 66 // |session_type| provided. Takes ownership of |promise|. 67 void InitializeNewSession(const std::string& init_data_type, 68 const uint8* init_data, 69 int init_data_length, 70 media::MediaKeys::SessionType session_type, 71 scoped_ptr<media::NewSessionCdmPromise> promise); 72 73 // Updates the session specified by |web_session_id| with |response|. 74 // Takes ownership of |promise|. 75 void UpdateSession(const std::string& web_session_id, 76 const uint8* response, 77 int response_length, 78 scoped_ptr<media::SimpleCdmPromise> promise); 79 80 // Releases the session specified by |web_session_id|. 81 // Takes ownership of |promise|. 82 void ReleaseSession(const std::string& web_session_id, 83 scoped_ptr<media::SimpleCdmPromise> promise); 84 85 // Returns the Decryptor associated with this CDM. May be NULL if no 86 // Decryptor is associated with the MediaKeys object. 87 // TODO(jrummell): Figure out lifetimes, as WMPI may still use the decryptor 88 // after WebContentDecryptionModule is freed. http://crbug.com/330324 89 media::Decryptor* GetDecryptor(); 90 91 #if defined(ENABLE_BROWSER_CDMS) 92 // Returns the CDM ID associated with the |media_keys_|. May be kInvalidCdmId 93 // if no CDM ID is associated. 94 int GetCdmId() const; 95 #endif 96 97 private: 98 friend class base::RefCounted<CdmSessionAdapter>; 99 typedef base::hash_map<std::string, 100 base::WeakPtr<WebContentDecryptionModuleSessionImpl> > 101 SessionMap; 102 103 ~CdmSessionAdapter(); 104 105 // Callbacks for firing session events. 106 void OnSessionMessage(const std::string& web_session_id, 107 const std::vector<uint8>& message, 108 const GURL& destination_url); 109 void OnSessionReady(const std::string& web_session_id); 110 void OnSessionClosed(const std::string& web_session_id); 111 void OnSessionError(const std::string& web_session_id, 112 media::MediaKeys::Exception exception_code, 113 uint32 system_code, 114 const std::string& error_message); 115 116 // Helper function of the callbacks. 117 WebContentDecryptionModuleSessionImpl* GetSession( 118 const std::string& web_session_id); 119 120 scoped_ptr<media::MediaKeys> media_keys_; 121 122 SessionMap sessions_; 123 124 #if defined(ENABLE_BROWSER_CDMS) 125 int cdm_id_; 126 #endif 127 128 // NOTE: Weak pointers must be invalidated before all other member variables. 129 base::WeakPtrFactory<CdmSessionAdapter> weak_ptr_factory_; 130 131 DISALLOW_COPY_AND_ASSIGN(CdmSessionAdapter); 132 }; 133 134 } // namespace content 135 136 #endif // CONTENT_RENDERER_MEDIA_CDM_SESSION_ADAPTER_H_ 137