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 CONTENT_RENDERER_MEDIA_CDM_SESSION_ADAPTER_H_
6 #define CONTENT_RENDERER_MEDIA_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"
18 #if defined(ENABLE_PEPPER_CDMS)
19 #include "content/renderer/media/crypto/pepper_cdm_wrapper.h"
26 #if defined(OS_ANDROID)
27 class RendererCdmManager
;
29 class WebContentDecryptionModuleSessionImpl
;
31 // Owns the CDM instance and makes calls from session objects to the CDM.
32 // Forwards the web session ID-based callbacks of the MediaKeys interface to the
33 // appropriate session object. Callers should hold references to this class
34 // as long as they need the CDM instance.
35 class CdmSessionAdapter
: public base::RefCounted
<CdmSessionAdapter
> {
39 // Returns true on success.
41 #if defined(ENABLE_PEPPER_CDMS)
42 const CreatePepperCdmCB
& create_pepper_cdm_cb
,
43 #elif defined(OS_ANDROID)
44 RendererCdmManager
* manager
,
46 const std::string
& key_system
,
47 const GURL
& security_origin
);
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(
53 blink::WebContentDecryptionModuleSession::Client
* client
);
55 // Adds a session to the internal map. Called once the session is successfully
58 const std::string
& web_session_id
,
59 base::WeakPtr
<WebContentDecryptionModuleSessionImpl
> session
);
61 // Removes a session from the internal map.
62 void RemoveSession(const std::string
& web_session_id
);
64 // Initializes a session with the |init_data_type|, |init_data| and
65 // |session_type| provided. Takes ownership of |promise|.
66 void InitializeNewSession(const std::string
& init_data_type
,
67 const uint8
* init_data
,
69 media::MediaKeys::SessionType session_type
,
70 scoped_ptr
<media::NewSessionCdmPromise
> promise
);
72 // Updates the session specified by |web_session_id| with |response|.
73 // Takes ownership of |promise|.
74 void UpdateSession(const std::string
& web_session_id
,
75 const uint8
* response
,
77 scoped_ptr
<media::SimpleCdmPromise
> promise
);
79 // Releases the session specified by |web_session_id|.
80 // Takes ownership of |promise|.
81 void ReleaseSession(const std::string
& web_session_id
,
82 scoped_ptr
<media::SimpleCdmPromise
> promise
);
84 // Returns the Decryptor associated with this CDM. May be NULL if no
85 // Decryptor is associated with the MediaKeys object.
86 // TODO(jrummell): Figure out lifetimes, as WMPI may still use the decryptor
87 // after WebContentDecryptionModule is freed. http://crbug.com/330324
88 media::Decryptor
* GetDecryptor();
90 #if defined(OS_ANDROID)
91 // Returns the CDM ID associated with the |media_keys_|. May be kInvalidCdmId
92 // if no CDM ID is associated.
97 friend class base::RefCounted
<CdmSessionAdapter
>;
98 typedef base::hash_map
<std::string
,
99 base::WeakPtr
<WebContentDecryptionModuleSessionImpl
> >
102 ~CdmSessionAdapter();
104 // Callbacks for firing session events.
105 void OnSessionMessage(const std::string
& web_session_id
,
106 const std::vector
<uint8
>& message
,
107 const GURL
& destination_url
);
108 void OnSessionReady(const std::string
& web_session_id
);
109 void OnSessionClosed(const std::string
& web_session_id
);
110 void OnSessionError(const std::string
& web_session_id
,
111 media::MediaKeys::Exception exception_code
,
113 const std::string
& error_message
);
115 // Helper function of the callbacks.
116 WebContentDecryptionModuleSessionImpl
* GetSession(
117 const std::string
& web_session_id
);
119 scoped_ptr
<media::MediaKeys
> media_keys_
;
121 SessionMap sessions_
;
123 #if defined(OS_ANDROID)
127 // NOTE: Weak pointers must be invalidated before all other member variables.
128 base::WeakPtrFactory
<CdmSessionAdapter
> weak_ptr_factory_
;
130 DISALLOW_COPY_AND_ASSIGN(CdmSessionAdapter
);
133 } // namespace content
135 #endif // CONTENT_RENDERER_MEDIA_CDM_SESSION_ADAPTER_H_