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_
12 #include "base/basictypes.h"
13 #include "base/containers/hash_tables.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/weak_ptr.h"
16 #include "media/base/media_keys.h"
17 #include "third_party/WebKit/public/platform/WebContentDecryptionModuleResult.h"
18 #include "third_party/WebKit/public/platform/WebContentDecryptionModuleSession.h"
26 class WebContentDecryptionModuleSessionImpl
;
28 // Owns the CDM instance and makes calls from session objects to the CDM.
29 // Forwards the session ID-based callbacks of the MediaKeys interface to the
30 // appropriate session object. Callers should hold references to this class
31 // as long as they need the CDM instance.
32 class CdmSessionAdapter
: public base::RefCounted
<CdmSessionAdapter
> {
36 // Creates the CDM for |key_system| using |cdm_factory| and returns the result
38 void CreateCdm(CdmFactory
* cdm_factory
,
39 const std::string
& key_system
,
40 const GURL
& security_origin
,
41 const CdmConfig
& cdm_config
,
42 blink::WebContentDecryptionModuleResult result
);
44 // Provides a server certificate to be used to encrypt messages to the
46 void SetServerCertificate(const std::vector
<uint8_t>& certificate
,
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 std::vector
<uint8_t>& init_data
,
68 MediaKeys::SessionType session_type
,
69 scoped_ptr
<NewSessionCdmPromise
> promise
);
71 // Loads the session specified by |session_id|.
72 void LoadSession(MediaKeys::SessionType session_type
,
73 const std::string
& session_id
,
74 scoped_ptr
<NewSessionCdmPromise
> promise
);
76 // Updates the session specified by |session_id| with |response|.
77 void UpdateSession(const std::string
& session_id
,
78 const std::vector
<uint8_t>& response
,
79 scoped_ptr
<SimpleCdmPromise
> promise
);
81 // Closes the session specified by |session_id|.
82 void CloseSession(const std::string
& session_id
,
83 scoped_ptr
<SimpleCdmPromise
> promise
);
85 // Removes stored session data associated with the session specified by
87 void RemoveSession(const std::string
& session_id
,
88 scoped_ptr
<SimpleCdmPromise
> promise
);
90 // Returns the CdmContext associated with |media_keys_|.
91 // TODO(jrummell): Figure out lifetimes, as WMPI may still use the decryptor
92 // after WebContentDecryptionModule is freed. http://crbug.com/330324
93 CdmContext
* GetCdmContext();
95 // Returns the key system name.
96 const std::string
& GetKeySystem() const;
98 // Returns a prefix to use for UMAs.
99 const std::string
& GetKeySystemUMAPrefix() const;
102 friend class base::RefCounted
<CdmSessionAdapter
>;
104 // Session ID to WebContentDecryptionModuleSessionImpl mapping.
105 typedef base::hash_map
<std::string
,
106 base::WeakPtr
<WebContentDecryptionModuleSessionImpl
> >
109 ~CdmSessionAdapter();
111 // Callback for CreateCdm().
112 void OnCdmCreated(const std::string
& key_system
,
113 blink::WebContentDecryptionModuleResult result
,
114 scoped_ptr
<MediaKeys
> cdm
,
115 const std::string
& error_message
);
117 // Callbacks for firing session events.
118 void OnSessionMessage(const std::string
& session_id
,
119 MediaKeys::MessageType message_type
,
120 const std::vector
<uint8_t>& message
,
121 const GURL
& legacy_destination_url
);
122 void OnSessionKeysChange(const std::string
& session_id
,
123 bool has_additional_usable_key
,
124 CdmKeysInfo keys_info
);
125 void OnSessionExpirationUpdate(const std::string
& session_id
,
126 const base::Time
& new_expiry_time
);
127 void OnSessionClosed(const std::string
& session_id
);
128 void OnLegacySessionError(const std::string
& session_id
,
129 MediaKeys::Exception exception_code
,
130 uint32_t system_code
,
131 const std::string
& error_message
);
133 // Helper function of the callbacks.
134 WebContentDecryptionModuleSessionImpl
* GetSession(
135 const std::string
& session_id
);
137 scoped_ptr
<MediaKeys
> cdm_
;
139 SessionMap sessions_
;
141 std::string key_system_
;
142 std::string key_system_uma_prefix_
;
144 // NOTE: Weak pointers must be invalidated before all other member variables.
145 base::WeakPtrFactory
<CdmSessionAdapter
> weak_ptr_factory_
;
147 DISALLOW_COPY_AND_ASSIGN(CdmSessionAdapter
);
152 #endif // MEDIA_BLINK_CDM_SESSION_ADAPTER_H_