downloads.ItemView -> downloads.Item
[chromium-blink-merge.git] / media / blink / cdm_session_adapter.h
blobac4219e682867548112d81b7b318188994415a09
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>
10 #include <vector>
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"
20 class GURL;
22 namespace media {
24 struct CdmConfig;
25 class CdmFactory;
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> {
33 public:
34 CdmSessionAdapter();
36 // Creates the CDM for |key_system| using |cdm_factory| and returns the result
37 // via |result|.
38 void CreateCdm(CdmFactory* cdm_factory,
39 const std::string& key_system,
40 const GURL& security_origin,
41 const CdmConfig& cdm_config,
42 scoped_ptr<blink::WebContentDecryptionModuleResult> result);
44 // Provides a server certificate to be used to encrypt messages to the
45 // license server.
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|.
57 bool RegisterSession(
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
86 // |session_id|.
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;
101 private:
102 friend class base::RefCounted<CdmSessionAdapter>;
104 // Session ID to WebContentDecryptionModuleSessionImpl mapping.
105 typedef base::hash_map<std::string,
106 base::WeakPtr<WebContentDecryptionModuleSessionImpl> >
107 SessionMap;
109 ~CdmSessionAdapter();
111 // Callback for CreateCdm().
112 void OnCdmCreated(const std::string& key_system,
113 base::TimeTicks start_time,
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 void ReportTimeToCreateCdmUMA(base::TimeDelta cdm_creation_time) const;
139 scoped_ptr<MediaKeys> cdm_;
141 SessionMap sessions_;
143 std::string key_system_;
144 std::string key_system_uma_prefix_;
146 // A unique ID to trace CdmSessionAdapter::CreateCdm() call and the matching
147 // OnCdmCreated() call.
148 uint32 trace_id_;
150 scoped_ptr<blink::WebContentDecryptionModuleResult> cdm_created_result_;
152 // NOTE: Weak pointers must be invalidated before all other member variables.
153 base::WeakPtrFactory<CdmSessionAdapter> weak_ptr_factory_;
155 DISALLOW_COPY_AND_ASSIGN(CdmSessionAdapter);
158 } // namespace media
160 #endif // MEDIA_BLINK_CDM_SESSION_ADAPTER_H_