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"
23 #if defined(ENABLE_PEPPER_CDMS)
25 class WebMediaPlayerClient;
26 #endif // defined(ENABLE_PEPPER_CDMS)
31 #if defined(OS_ANDROID)
32 class RendererMediaPlayerManager;
33 #endif // defined(OS_ANDROID)
35 // ProxyDecryptor is for EME v0.1b only. It should not be used for the WD API.
36 // A decryptor proxy that creates a real decryptor object on demand and
37 // forwards decryptor calls to it.
39 // Now that the Pepper API calls use session ID to match responses with
40 // requests, this class maintains a mapping between session ID and web session
41 // ID. Callers of this class expect web session IDs in the responses.
42 // Session IDs are internal unique references to the session. Web session IDs
43 // are the CDM generated ID for the session, and are what are visible to users.
45 // TODO(xhwang): Currently we don't support run-time switching among decryptor
46 // objects. Fix this when needed.
47 // TODO(xhwang): The ProxyDecryptor is not a Decryptor. Find a better name!
48 class ProxyDecryptor {
50 // These are similar to the callbacks in media_keys.h, but pass back the
51 // web session ID rather than the internal session ID.
52 typedef base::Callback<void(const std::string& session_id)> KeyAddedCB;
53 typedef base::Callback<void(const std::string& session_id,
54 media::MediaKeys::KeyError error_code,
55 int system_code)> KeyErrorCB;
56 typedef base::Callback<void(const std::string& session_id,
57 const std::vector<uint8>& message,
58 const std::string& default_url)> KeyMessageCB;
61 #if defined(ENABLE_PEPPER_CDMS)
62 blink::WebMediaPlayerClient* web_media_player_client,
63 blink::WebFrame* web_frame,
64 #elif defined(OS_ANDROID)
65 RendererMediaPlayerManager* manager,
67 #endif // defined(ENABLE_PEPPER_CDMS)
68 const KeyAddedCB& key_added_cb,
69 const KeyErrorCB& key_error_cb,
70 const KeyMessageCB& key_message_cb);
71 virtual ~ProxyDecryptor();
73 // Returns the Decryptor associated with this object. May be NULL if no
74 // Decryptor is associated.
75 media::Decryptor* GetDecryptor();
77 // Only call this once.
78 bool InitializeCDM(const std::string& key_system, const GURL& frame_url);
80 // May only be called after InitializeCDM() succeeds.
81 bool GenerateKeyRequest(const std::string& type,
82 const uint8* init_data,
83 int init_data_length);
84 void AddKey(const uint8* key, int key_length,
85 const uint8* init_data, int init_data_length,
86 const std::string& session_id);
87 void CancelKeyRequest(const std::string& session_id);
90 // Session_id <-> web_session_id map.
91 typedef std::map<uint32, std::string> SessionIdMap;
93 // Helper function to create MediaKeys to handle the given |key_system|.
94 scoped_ptr<media::MediaKeys> CreateMediaKeys(const std::string& key_system,
95 const GURL& frame_url);
97 // Callbacks for firing session events.
98 void OnSessionCreated(uint32 session_id, const std::string& web_session_id);
99 void OnSessionMessage(uint32 session_id,
100 const std::vector<uint8>& message,
101 const std::string& default_url);
102 void OnSessionReady(uint32 session_id);
103 void OnSessionClosed(uint32 session_id);
104 void OnSessionError(uint32 session_id,
105 media::MediaKeys::KeyError error_code,
108 // Helper function to determine session_id for the provided |web_session_id|.
109 uint32 LookupSessionId(const std::string& web_session_id) const;
111 // Helper function to determine web_session_id for the provided |session_id|.
112 // The returned web_session_id is only valid on the main thread, and should be
114 const std::string& LookupWebSessionId(uint32 session_id) const;
116 base::WeakPtrFactory<ProxyDecryptor> weak_ptr_factory_;
118 #if defined(ENABLE_PEPPER_CDMS)
119 // Callback for cleaning up a Pepper-based CDM.
120 void DestroyHelperPlugin();
122 // Needed to create the PpapiDecryptor.
123 blink::WebMediaPlayerClient* web_media_player_client_;
124 blink::WebFrame* web_frame_;
125 #elif defined(OS_ANDROID)
126 RendererMediaPlayerManager* manager_;
128 #endif // defined(ENABLE_PEPPER_CDMS)
130 // The real MediaKeys that manages key operations for the ProxyDecryptor.
131 scoped_ptr<media::MediaKeys> media_keys_;
133 // Callbacks for firing key events.
134 KeyAddedCB key_added_cb_;
135 KeyErrorCB key_error_cb_;
136 KeyMessageCB key_message_cb_;
138 // Session IDs are used to uniquely track sessions so that CDM callbacks
139 // can get mapped to the correct session ID. Session ID should be unique
140 // per renderer process for debugging purposes.
141 static uint32 next_session_id_;
143 SessionIdMap sessions_;
145 std::set<uint32> persistent_sessions_;
149 DISALLOW_COPY_AND_ASSIGN(ProxyDecryptor);
152 } // namespace content
154 #endif // CONTENT_RENDERER_MEDIA_CRYPTO_PROXY_DECRYPTOR_H_