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_
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/WebContentDecryptionModuleResult.h"
17 #include "third_party/WebKit/public/platform/WebContentDecryptionModuleSession.h"
24 class WebContentDecryptionModuleSessionImpl
;
26 // 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
28 // appropriate session object. Callers should hold references to this class
29 // as long as they need the CDM instance.
30 class CdmSessionAdapter
: public base::RefCounted
<CdmSessionAdapter
> {
34 // Creates the CDM for |key_system| using |cdm_factory| and returns the result
36 void CreateCdm(CdmFactory
* cdm_factory
,
37 const std::string
& key_system
,
38 bool allow_distinctive_identifier
,
39 bool allow_persistent_state
,
40 const GURL
& security_origin
,
41 blink::WebContentDecryptionModuleResult result
);
43 // Provides a server certificate to be used to encrypt messages to the
45 void SetServerCertificate(const uint8
* server_certificate
,
46 int server_certificate_length
,
47 scoped_ptr
<SimpleCdmPromise
> promise
);
49 // Creates a new session and adds it to the internal map. The caller owns the
50 // created session. RemoveSession() must be called when destroying it, if
51 // RegisterSession() was called.
52 WebContentDecryptionModuleSessionImpl
* CreateSession();
54 // Adds a session to the internal map. Called once the session is successfully
55 // initialized. Returns true if the session was registered, false if there is
56 // already an existing session with the same |session_id|.
58 const std::string
& session_id
,
59 base::WeakPtr
<WebContentDecryptionModuleSessionImpl
> session
);
61 // Removes a session from the internal map.
62 void UnregisterSession(const std::string
& session_id
);
64 // Initializes a session with the |init_data_type|, |init_data| and
65 // |session_type| provided.
66 void InitializeNewSession(EmeInitDataType init_data_type
,
67 const uint8
* init_data
,
69 MediaKeys::SessionType session_type
,
70 scoped_ptr
<NewSessionCdmPromise
> promise
);
72 // Loads the session specified by |session_id|.
73 void LoadSession(MediaKeys::SessionType session_type
,
74 const std::string
& session_id
,
75 scoped_ptr
<NewSessionCdmPromise
> promise
);
77 // Updates the session specified by |session_id| with |response|.
78 void UpdateSession(const std::string
& session_id
,
79 const uint8
* response
,
81 scoped_ptr
<SimpleCdmPromise
> promise
);
83 // Closes the session specified by |session_id|.
84 void CloseSession(const std::string
& session_id
,
85 scoped_ptr
<SimpleCdmPromise
> promise
);
87 // Removes stored session data associated with the session specified by
89 void RemoveSession(const std::string
& session_id
,
90 scoped_ptr
<SimpleCdmPromise
> promise
);
92 // Returns the CdmContext associated with |media_keys_|.
93 // TODO(jrummell): Figure out lifetimes, as WMPI may still use the decryptor
94 // after WebContentDecryptionModule is freed. http://crbug.com/330324
95 CdmContext
* GetCdmContext();
97 // Returns the key system name.
98 const std::string
& GetKeySystem() const;
100 // Returns a prefix to use for UMAs.
101 const std::string
& GetKeySystemUMAPrefix() const;
104 friend class base::RefCounted
<CdmSessionAdapter
>;
106 // Session ID to WebContentDecryptionModuleSessionImpl mapping.
107 typedef base::hash_map
<std::string
,
108 base::WeakPtr
<WebContentDecryptionModuleSessionImpl
> >
111 ~CdmSessionAdapter();
113 // Callback for CreateCdm().
114 void OnCdmCreated(const std::string
& key_system
,
115 blink::WebContentDecryptionModuleResult result
,
116 scoped_ptr
<MediaKeys
> cdm
);
118 // Callbacks for firing session events.
119 void OnSessionMessage(const std::string
& session_id
,
120 MediaKeys::MessageType message_type
,
121 const std::vector
<uint8
>& message
,
122 const GURL
& legacy_destination_url
);
123 void OnSessionKeysChange(const std::string
& session_id
,
124 bool has_additional_usable_key
,
125 CdmKeysInfo keys_info
);
126 void OnSessionExpirationUpdate(const std::string
& session_id
,
127 const base::Time
& new_expiry_time
);
128 void OnSessionClosed(const std::string
& session_id
);
129 void OnLegacySessionError(const std::string
& session_id
,
130 MediaKeys::Exception exception_code
,
132 const std::string
& error_message
);
134 // Helper function of the callbacks.
135 WebContentDecryptionModuleSessionImpl
* GetSession(
136 const std::string
& session_id
);
138 scoped_ptr
<MediaKeys
> cdm_
;
140 SessionMap sessions_
;
142 std::string key_system_
;
143 std::string key_system_uma_prefix_
;
145 // NOTE: Weak pointers must be invalidated before all other member variables.
146 base::WeakPtrFactory
<CdmSessionAdapter
> weak_ptr_factory_
;
148 DISALLOW_COPY_AND_ASSIGN(CdmSessionAdapter
);
153 #endif // MEDIA_BLINK_CDM_SESSION_ADAPTER_H_