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.
5 #ifndef CONTENT_RENDERER_MEDIA_CRYPTO_PROXY_DECRYPTOR_H_
6 #define CONTENT_RENDERER_MEDIA_CRYPTO_PROXY_DECRYPTOR_H_
13 #include "base/basictypes.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/synchronization/lock.h"
17 #include "media/base/decryptor.h"
18 #include "media/base/media_keys.h"
20 #if defined(ENABLE_PEPPER_CDMS)
21 #include "content/renderer/media/crypto/pepper_cdm_wrapper.h"
28 #if defined(OS_ANDROID)
29 class RendererMediaPlayerManager;
30 #endif // defined(OS_ANDROID)
32 // ProxyDecryptor is for EME v0.1b only. It should not be used for the WD API.
33 // A decryptor proxy that creates a real decryptor object on demand and
34 // forwards decryptor calls to it.
36 // Now that the Pepper API calls use session ID to match responses with
37 // requests, this class maintains a mapping between session ID and web session
38 // ID. Callers of this class expect web session IDs in the responses.
39 // Session IDs are internal unique references to the session. Web session IDs
40 // are the CDM generated ID for the session, and are what are visible to users.
42 // TODO(xhwang): Currently we don't support run-time switching among decryptor
43 // objects. Fix this when needed.
44 // TODO(xhwang): The ProxyDecryptor is not a Decryptor. Find a better name!
45 class ProxyDecryptor {
47 // These are similar to the callbacks in media_keys.h, but pass back the
48 // web session ID rather than the internal session ID.
49 typedef base::Callback<void(const std::string& session_id)> KeyAddedCB;
50 typedef base::Callback<void(const std::string& session_id,
51 media::MediaKeys::KeyError error_code,
52 uint32 system_code)> KeyErrorCB;
53 typedef base::Callback<void(const std::string& session_id,
54 const std::vector<uint8>& message,
55 const std::string& default_url)> KeyMessageCB;
58 #if defined(ENABLE_PEPPER_CDMS)
59 const CreatePepperCdmCB& create_pepper_cdm_cb,
60 #elif defined(OS_ANDROID)
61 RendererMediaPlayerManager* manager,
62 #endif // defined(ENABLE_PEPPER_CDMS)
63 const KeyAddedCB& key_added_cb,
64 const KeyErrorCB& key_error_cb,
65 const KeyMessageCB& key_message_cb);
66 virtual ~ProxyDecryptor();
68 // Returns the Decryptor associated with this object. May be NULL if no
69 // Decryptor is associated.
70 media::Decryptor* GetDecryptor();
72 #if defined(OS_ANDROID)
73 // Returns the CDM ID associated with this object. May be kInvalidCdmId if no
74 // CDM ID is associated, such as when Clear Key is used.
78 // Only call this once.
79 bool InitializeCDM(const std::string& key_system,
80 const GURL& security_origin);
82 // May only be called after InitializeCDM() succeeds.
83 bool GenerateKeyRequest(const std::string& type,
84 const uint8* init_data,
85 int init_data_length);
86 void AddKey(const uint8* key, int key_length,
87 const uint8* init_data, int init_data_length,
88 const std::string& session_id);
89 void CancelKeyRequest(const std::string& session_id);
92 // Session_id <-> web_session_id map.
93 typedef std::map<uint32, std::string> SessionIdMap;
95 // Helper function to create MediaKeys to handle the given |key_system|.
96 scoped_ptr<media::MediaKeys> CreateMediaKeys(const std::string& key_system,
97 const GURL& security_origin);
99 // Callbacks for firing session events.
100 void OnSessionCreated(uint32 session_id, const std::string& web_session_id);
101 void OnSessionMessage(uint32 session_id,
102 const std::vector<uint8>& message,
103 const std::string& default_url);
104 void OnSessionReady(uint32 session_id);
105 void OnSessionClosed(uint32 session_id);
106 void OnSessionError(uint32 session_id,
107 media::MediaKeys::KeyError error_code,
110 // Helper function to determine session_id for the provided |web_session_id|.
111 uint32 LookupSessionId(const std::string& web_session_id) const;
113 // Helper function to determine web_session_id for the provided |session_id|.
114 // The returned web_session_id is only valid on the main thread, and should be
116 const std::string& LookupWebSessionId(uint32 session_id) const;
118 #if defined(ENABLE_PEPPER_CDMS)
119 // Callback to create the Pepper plugin.
120 CreatePepperCdmCB create_pepper_cdm_cb_;
121 #elif defined(OS_ANDROID)
122 RendererMediaPlayerManager* manager_;
124 #endif // defined(ENABLE_PEPPER_CDMS)
126 // The real MediaKeys that manages key operations for the ProxyDecryptor.
127 scoped_ptr<media::MediaKeys> media_keys_;
129 // Callbacks for firing key events.
130 KeyAddedCB key_added_cb_;
131 KeyErrorCB key_error_cb_;
132 KeyMessageCB key_message_cb_;
134 // Session IDs are used to uniquely track sessions so that CDM callbacks
135 // can get mapped to the correct session ID. Session ID should be unique
136 // per renderer process for debugging purposes.
137 static uint32 next_session_id_;
139 SessionIdMap sessions_;
141 std::set<uint32> persistent_sessions_;
145 // NOTE: Weak pointers must be invalidated before all other member variables.
146 base::WeakPtrFactory<ProxyDecryptor> weak_ptr_factory_;
148 DISALLOW_COPY_AND_ASSIGN(ProxyDecryptor);
151 } // namespace content
153 #endif // CONTENT_RENDERER_MEDIA_CRYPTO_PROXY_DECRYPTOR_H_