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_CRYPTO_PROXY_MEDIA_KEYS_H_ 6 #define CONTENT_RENDERER_MEDIA_CRYPTO_PROXY_MEDIA_KEYS_H_ 7 8 #include <map> 9 #include <string> 10 11 #include "base/basictypes.h" 12 #include "base/containers/hash_tables.h" 13 #include "base/containers/scoped_ptr_hash_map.h" 14 #include "media/base/cdm_promise.h" 15 #include "media/base/media_keys.h" 16 17 class GURL; 18 19 namespace content { 20 21 class RendererCdmManager; 22 23 // A MediaKeys proxy that wraps the EME part of RendererCdmManager. 24 class ProxyMediaKeys : public media::MediaKeys { 25 public: 26 static scoped_ptr<ProxyMediaKeys> Create( 27 const std::string& key_system, 28 const GURL& security_origin, 29 RendererCdmManager* manager, 30 const media::SessionMessageCB& session_message_cb, 31 const media::SessionReadyCB& session_ready_cb, 32 const media::SessionClosedCB& session_closed_cb, 33 const media::SessionErrorCB& session_error_cb); 34 35 virtual ~ProxyMediaKeys(); 36 37 // MediaKeys implementation. 38 virtual void CreateSession( 39 const std::string& init_data_type, 40 const uint8* init_data, 41 int init_data_length, 42 SessionType session_type, 43 scoped_ptr<media::NewSessionCdmPromise> promise) OVERRIDE; 44 virtual void LoadSession( 45 const std::string& web_session_id, 46 scoped_ptr<media::NewSessionCdmPromise> promise) OVERRIDE; 47 virtual void UpdateSession( 48 const std::string& web_session_id, 49 const uint8* response, 50 int response_length, 51 scoped_ptr<media::SimpleCdmPromise> promise) OVERRIDE; 52 virtual void ReleaseSession( 53 const std::string& web_session_id, 54 scoped_ptr<media::SimpleCdmPromise> promise) OVERRIDE; 55 56 // Callbacks. 57 void OnSessionCreated(uint32 session_id, const std::string& web_session_id); 58 void OnSessionMessage(uint32 session_id, 59 const std::vector<uint8>& message, 60 const GURL& destination_url); 61 void OnSessionReady(uint32 session_id); 62 void OnSessionClosed(uint32 session_id); 63 void OnSessionError(uint32 session_id, 64 media::MediaKeys::KeyError error_code, 65 uint32 system_code); 66 67 int GetCdmId() const; 68 69 private: 70 // The Android-specific code that handles sessions uses integer session ids 71 // (basically a reference id), but media::MediaKeys bases everything on 72 // web_session_id (a string representing the actual session id as generated 73 // by the CDM). SessionIdMap is used to map between the web_session_id and 74 // the session_id used by the Android-specific code. 75 typedef base::hash_map<std::string, uint32_t> SessionIdMap; 76 77 // The following types keep track of Promises. The index is the 78 // Android-specific session_id, so that returning results can be matched to 79 // the corresponding promise. 80 typedef base::ScopedPtrHashMap<uint32_t, media::CdmPromise> PromiseMap; 81 82 ProxyMediaKeys(RendererCdmManager* manager, 83 const media::SessionMessageCB& session_message_cb, 84 const media::SessionReadyCB& session_ready_cb, 85 const media::SessionClosedCB& session_closed_cb, 86 const media::SessionErrorCB& session_error_cb); 87 88 void InitializeCdm(const std::string& key_system, 89 const GURL& security_origin); 90 91 // These functions keep track of Android-specific session_ids <-> 92 // web_session_ids mappings. 93 // TODO(jrummell): Remove this once the Android-specific code changes to 94 // support string web session ids. 95 uint32_t CreateSessionId(); 96 void AssignWebSessionId(uint32_t session_id, 97 const std::string& web_session_id); 98 uint32_t LookupSessionId(const std::string& web_session_id) const; 99 std::string LookupWebSessionId(uint32_t session_id) const; 100 void DropWebSessionId(const std::string& web_session_id); 101 102 // Helper function to keep track of promises. Adding takes ownership of the 103 // promise, transferred back to caller on take. 104 void SavePromise(uint32_t session_id, scoped_ptr<media::CdmPromise> promise); 105 scoped_ptr<media::CdmPromise> TakePromise(uint32_t session_id); 106 107 RendererCdmManager* manager_; 108 int cdm_id_; 109 110 media::SessionMessageCB session_message_cb_; 111 media::SessionReadyCB session_ready_cb_; 112 media::SessionClosedCB session_closed_cb_; 113 media::SessionErrorCB session_error_cb_; 114 115 // Android-specific. See comment above CreateSessionId(). 116 uint32_t next_session_id_; 117 SessionIdMap web_session_to_session_id_map_; 118 119 // Keep track of outstanding promises. This map owns the promise object. 120 PromiseMap session_id_to_promise_map_; 121 122 DISALLOW_COPY_AND_ASSIGN(ProxyMediaKeys); 123 }; 124 125 } // namespace content 126 127 #endif // CONTENT_RENDERER_MEDIA_CRYPTO_PROXY_MEDIA_KEYS_H_ 128