Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / media / blink / cdm_session_adapter.h
blobcc60143837f0ebd5bf49e1404b605971f38865ba
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 class CdmFactory;
25 class WebContentDecryptionModuleSessionImpl;
27 // Owns the CDM instance and makes calls from session objects to the CDM.
28 // Forwards the session ID-based callbacks of the MediaKeys interface to the
29 // appropriate session object. Callers should hold references to this class
30 // as long as they need the CDM instance.
31 class CdmSessionAdapter : public base::RefCounted<CdmSessionAdapter> {
32 public:
33 CdmSessionAdapter();
35 // Creates the CDM for |key_system| using |cdm_factory| and returns the result
36 // via |result|.
37 void CreateCdm(CdmFactory* cdm_factory,
38 const std::string& key_system,
39 bool allow_distinctive_identifier,
40 bool allow_persistent_state,
41 const GURL& security_origin,
42 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 blink::WebContentDecryptionModuleResult result,
114 scoped_ptr<MediaKeys> cdm);
116 // Callbacks for firing session events.
117 void OnSessionMessage(const std::string& session_id,
118 MediaKeys::MessageType message_type,
119 const std::vector<uint8_t>& message,
120 const GURL& legacy_destination_url);
121 void OnSessionKeysChange(const std::string& session_id,
122 bool has_additional_usable_key,
123 CdmKeysInfo keys_info);
124 void OnSessionExpirationUpdate(const std::string& session_id,
125 const base::Time& new_expiry_time);
126 void OnSessionClosed(const std::string& session_id);
127 void OnLegacySessionError(const std::string& session_id,
128 MediaKeys::Exception exception_code,
129 uint32_t system_code,
130 const std::string& error_message);
132 // Helper function of the callbacks.
133 WebContentDecryptionModuleSessionImpl* GetSession(
134 const std::string& session_id);
136 scoped_ptr<MediaKeys> cdm_;
138 SessionMap sessions_;
140 std::string key_system_;
141 std::string key_system_uma_prefix_;
143 // NOTE: Weak pointers must be invalidated before all other member variables.
144 base::WeakPtrFactory<CdmSessionAdapter> weak_ptr_factory_;
146 DISALLOW_COPY_AND_ASSIGN(CdmSessionAdapter);
149 } // namespace media
151 #endif // MEDIA_BLINK_CDM_SESSION_ADAPTER_H_