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/WebContentDecryptionModuleSession.h"
23 class WebContentDecryptionModuleSessionImpl
;
25 // Owns the CDM instance and makes calls from session objects to the CDM.
26 // Forwards the 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
> {
33 // Returns true on success.
34 bool Initialize(CdmFactory
* cdm_factory
,
35 const std::string
& key_system
,
36 bool allow_distinctive_identifier
,
37 bool allow_persistent_state
,
38 const GURL
& security_origin
);
40 // Provides a server certificate to be used to encrypt messages to the
42 void SetServerCertificate(const uint8
* server_certificate
,
43 int server_certificate_length
,
44 scoped_ptr
<SimpleCdmPromise
> promise
);
46 // Creates a new session and adds it to the internal map. The caller owns the
47 // created session. RemoveSession() must be called when destroying it, if
48 // RegisterSession() was called.
49 WebContentDecryptionModuleSessionImpl
* CreateSession();
51 // Adds a session to the internal map. Called once the session is successfully
52 // initialized. Returns true if the session was registered, false if there is
53 // already an existing session with the same |session_id|.
55 const std::string
& session_id
,
56 base::WeakPtr
<WebContentDecryptionModuleSessionImpl
> session
);
58 // Removes a session from the internal map.
59 void UnregisterSession(const std::string
& session_id
);
61 // Initializes a session with the |init_data_type|, |init_data| and
62 // |session_type| provided.
63 void InitializeNewSession(EmeInitDataType init_data_type
,
64 const uint8
* init_data
,
66 MediaKeys::SessionType session_type
,
67 scoped_ptr
<NewSessionCdmPromise
> promise
);
69 // Loads the session specified by |session_id|.
70 void LoadSession(MediaKeys::SessionType session_type
,
71 const std::string
& session_id
,
72 scoped_ptr
<NewSessionCdmPromise
> promise
);
74 // Updates the session specified by |session_id| with |response|.
75 void UpdateSession(const std::string
& session_id
,
76 const uint8
* response
,
78 scoped_ptr
<SimpleCdmPromise
> promise
);
80 // Closes the session specified by |session_id|.
81 void CloseSession(const std::string
& session_id
,
82 scoped_ptr
<SimpleCdmPromise
> promise
);
84 // Removes stored session data associated with the session specified by
86 void RemoveSession(const std::string
& session_id
,
87 scoped_ptr
<SimpleCdmPromise
> promise
);
89 // Returns the CdmContext associated with |media_keys_|.
90 // TODO(jrummell): Figure out lifetimes, as WMPI may still use the decryptor
91 // after WebContentDecryptionModule is freed. http://crbug.com/330324
92 CdmContext
* GetCdmContext();
94 // Returns the key system name.
95 const std::string
& GetKeySystem() const;
97 // Returns a prefix to use for UMAs.
98 const std::string
& GetKeySystemUMAPrefix() const;
101 friend class base::RefCounted
<CdmSessionAdapter
>;
102 typedef base::hash_map
<std::string
,
103 base::WeakPtr
<WebContentDecryptionModuleSessionImpl
> >
106 ~CdmSessionAdapter();
108 // Callbacks for firing session events.
109 void OnSessionMessage(const std::string
& session_id
,
110 MediaKeys::MessageType message_type
,
111 const std::vector
<uint8
>& message
,
112 const GURL
& legacy_destination_url
);
113 void OnSessionKeysChange(const std::string
& session_id
,
114 bool has_additional_usable_key
,
115 CdmKeysInfo keys_info
);
116 void OnSessionExpirationUpdate(const std::string
& session_id
,
117 const base::Time
& new_expiry_time
);
118 void OnSessionClosed(const std::string
& session_id
);
119 void OnLegacySessionError(const std::string
& session_id
,
120 MediaKeys::Exception exception_code
,
122 const std::string
& error_message
);
124 // Helper function of the callbacks.
125 WebContentDecryptionModuleSessionImpl
* GetSession(
126 const std::string
& session_id
);
128 scoped_ptr
<MediaKeys
> media_keys_
;
130 SessionMap sessions_
;
132 std::string key_system_
;
133 std::string key_system_uma_prefix_
;
135 // NOTE: Weak pointers must be invalidated before all other member variables.
136 base::WeakPtrFactory
<CdmSessionAdapter
> weak_ptr_factory_
;
138 DISALLOW_COPY_AND_ASSIGN(CdmSessionAdapter
);
143 #endif // MEDIA_BLINK_CDM_SESSION_ADAPTER_H_