Prevent UAF of RenderFrames from GuestViewContainer
[chromium-blink-merge.git] / media / blink / cdm_session_adapter.h
blob768f1c419fe18a9a4ce267c9339301243e2ef486
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.
5 #ifndef MEDIA_BLINK_CDM_SESSION_ADAPTER_H_
6 #define MEDIA_BLINK_CDM_SESSION_ADAPTER_H_
8 #include <map>
9 #include <string>
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"
18 class GURL;
20 namespace media {
22 class CdmFactory;
23 class WebContentDecryptionModuleSessionImpl;
25 // Owns the CDM instance and makes calls from session objects to the CDM.
26 // Forwards the web session ID-based callbacks of the MediaKeys interface to the
27 // appropriate session object. Callers should hold references to this class
28 // as long as they need the CDM instance.
29 class CdmSessionAdapter : public base::RefCounted<CdmSessionAdapter> {
30 public:
31 CdmSessionAdapter();
33 // Returns true on success.
34 bool Initialize(CdmFactory* cdm_factory,
35 const std::string& key_system,
36 const GURL& security_origin);
38 // Provides a server certificate to be used to encrypt messages to the
39 // license server.
40 void SetServerCertificate(const uint8* server_certificate,
41 int server_certificate_length,
42 scoped_ptr<SimpleCdmPromise> promise);
44 // Creates a new session and adds it to the internal map. The caller owns the
45 // created session. RemoveSession() must be called when destroying it, if
46 // RegisterSession() was called.
47 WebContentDecryptionModuleSessionImpl* CreateSession();
49 // Adds a session to the internal map. Called once the session is successfully
50 // initialized. Returns true if the session was registered, false if there is
51 // already an existing session with the same |web_session_id|.
52 bool RegisterSession(
53 const std::string& web_session_id,
54 base::WeakPtr<WebContentDecryptionModuleSessionImpl> session);
56 // Removes a session from the internal map.
57 void UnregisterSession(const std::string& web_session_id);
59 // Initializes a session with the |init_data_type|, |init_data| and
60 // |session_type| provided.
61 void InitializeNewSession(const std::string& init_data_type,
62 const uint8* init_data,
63 int init_data_length,
64 MediaKeys::SessionType session_type,
65 scoped_ptr<NewSessionCdmPromise> promise);
67 // Loads the session specified by |web_session_id|.
68 void LoadSession(MediaKeys::SessionType session_type,
69 const std::string& web_session_id,
70 scoped_ptr<NewSessionCdmPromise> promise);
72 // Updates the session specified by |web_session_id| with |response|.
73 void UpdateSession(const std::string& web_session_id,
74 const uint8* response,
75 int response_length,
76 scoped_ptr<SimpleCdmPromise> promise);
78 // Closes the session specified by |web_session_id|.
79 void CloseSession(const std::string& web_session_id,
80 scoped_ptr<SimpleCdmPromise> promise);
82 // Removes stored session data associated with the session specified by
83 // |web_session_id|.
84 void RemoveSession(const std::string& web_session_id,
85 scoped_ptr<SimpleCdmPromise> promise);
87 // Returns the CdmContext associated with |media_keys_|.
88 // TODO(jrummell): Figure out lifetimes, as WMPI may still use the decryptor
89 // after WebContentDecryptionModule is freed. http://crbug.com/330324
90 CdmContext* GetCdmContext();
92 // Returns a prefix to use for UMAs.
93 const std::string& GetKeySystemUMAPrefix() const;
95 private:
96 friend class base::RefCounted<CdmSessionAdapter>;
97 typedef base::hash_map<std::string,
98 base::WeakPtr<WebContentDecryptionModuleSessionImpl> >
99 SessionMap;
101 ~CdmSessionAdapter();
103 // Callbacks for firing session events.
104 void OnSessionMessage(const std::string& web_session_id,
105 MediaKeys::MessageType message_type,
106 const std::vector<uint8>& message,
107 const GURL& legacy_destination_url);
108 void OnSessionKeysChange(const std::string& web_session_id,
109 bool has_additional_usable_key,
110 CdmKeysInfo keys_info);
111 void OnSessionExpirationUpdate(const std::string& web_session_id,
112 const base::Time& new_expiry_time);
113 void OnSessionClosed(const std::string& web_session_id);
114 void OnSessionError(const std::string& web_session_id,
115 MediaKeys::Exception exception_code,
116 uint32 system_code,
117 const std::string& error_message);
119 // Helper function of the callbacks.
120 WebContentDecryptionModuleSessionImpl* GetSession(
121 const std::string& web_session_id);
123 scoped_ptr<MediaKeys> media_keys_;
125 SessionMap sessions_;
127 std::string key_system_uma_prefix_;
129 // NOTE: Weak pointers must be invalidated before all other member variables.
130 base::WeakPtrFactory<CdmSessionAdapter> weak_ptr_factory_;
132 DISALLOW_COPY_AND_ASSIGN(CdmSessionAdapter);
135 } // namespace media
137 #endif // MEDIA_BLINK_CDM_SESSION_ADAPTER_H_