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