Revert of Roll src/third_party/WebKit e0eac24:489c548 (svn 193311:193320) (patchset...
[chromium-blink-merge.git] / media / cdm / proxy_decryptor.h
blobff611ad3197a60da3268b51bfdbfbea698b1164c
1 // Copyright 2013 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_CDM_PROXY_DECRYPTOR_H_
6 #define MEDIA_CDM_PROXY_DECRYPTOR_H_
8 #include <string>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/containers/hash_tables.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "media/base/decryptor.h"
16 #include "media/base/eme_constants.h"
17 #include "media/base/media_export.h"
18 #include "media/base/media_keys.h"
19 #include "url/gurl.h"
21 namespace media {
23 class CdmFactory;
24 class MediaPermission;
26 // ProxyDecryptor is for EME v0.1b only. It should not be used for the WD API.
27 // A decryptor proxy that creates a real decryptor object on demand and
28 // forwards decryptor calls to it.
30 // TODO(xhwang): Currently we don't support run-time switching among decryptor
31 // objects. Fix this when needed.
32 // TODO(xhwang): The ProxyDecryptor is not a Decryptor. Find a better name!
33 class MEDIA_EXPORT ProxyDecryptor {
34 public:
35 // These are similar to the callbacks in media_keys.h, but pass back the
36 // session ID rather than the internal session ID.
37 typedef base::Callback<void(const std::string& session_id)> KeyAddedCB;
38 typedef base::Callback<void(const std::string& session_id,
39 MediaKeys::KeyError error_code,
40 uint32 system_code)> KeyErrorCB;
41 typedef base::Callback<void(const std::string& session_id,
42 const std::vector<uint8>& message,
43 const GURL& destination_url)> KeyMessageCB;
45 ProxyDecryptor(MediaPermission* media_permission,
46 const KeyAddedCB& key_added_cb,
47 const KeyErrorCB& key_error_cb,
48 const KeyMessageCB& key_message_cb);
49 virtual ~ProxyDecryptor();
51 // Returns the CdmContext associated with this object.
52 CdmContext* GetCdmContext();
54 // Only call this once.
55 bool InitializeCDM(CdmFactory* cdm_factory,
56 const std::string& key_system,
57 const GURL& security_origin);
59 // May only be called after InitializeCDM() succeeds.
60 bool GenerateKeyRequest(EmeInitDataType init_data_type,
61 const uint8* init_data,
62 int init_data_length);
63 void AddKey(const uint8* key, int key_length,
64 const uint8* init_data, int init_data_length,
65 const std::string& session_id);
66 void CancelKeyRequest(const std::string& session_id);
68 private:
69 // Helper function to create MediaKeys to handle the given |key_system|.
70 scoped_ptr<MediaKeys> CreateMediaKeys(
71 CdmFactory* cdm_factory,
72 const std::string& key_system,
73 const GURL& security_origin);
75 // Callbacks for firing session events.
76 void OnSessionMessage(const std::string& session_id,
77 MediaKeys::MessageType message_type,
78 const std::vector<uint8>& message,
79 const GURL& legacy_destination_url);
80 void OnSessionKeysChange(const std::string& session_id,
81 bool has_additional_usable_key,
82 CdmKeysInfo keys_info);
83 void OnSessionExpirationUpdate(const std::string& session_id,
84 const base::Time& new_expiry_time);
85 void GenerateKeyAdded(const std::string& session_id);
86 void OnSessionClosed(const std::string& session_id);
87 void OnLegacySessionError(const std::string& session_id,
88 MediaKeys::Exception exception_code,
89 uint32 system_code,
90 const std::string& error_message);
92 // Callback for permission request.
93 void OnPermissionStatus(MediaKeys::SessionType session_type,
94 EmeInitDataType init_data_type,
95 const std::vector<uint8>& init_data,
96 scoped_ptr<NewSessionCdmPromise> promise,
97 bool granted);
99 enum SessionCreationType {
100 TemporarySession,
101 PersistentSession,
102 LoadSession
105 // Called when a session is actually created or loaded.
106 void SetSessionId(SessionCreationType session_type,
107 const std::string& session_id);
109 // The real MediaKeys that manages key operations for the ProxyDecryptor.
110 scoped_ptr<MediaKeys> media_keys_;
112 MediaPermission* media_permission_;
114 // Callbacks for firing key events.
115 KeyAddedCB key_added_cb_;
116 KeyErrorCB key_error_cb_;
117 KeyMessageCB key_message_cb_;
119 std::string key_system_;
120 GURL security_origin_;
122 // Keep track of both persistent and non-persistent sessions.
123 base::hash_map<std::string, bool> active_sessions_;
125 bool is_clear_key_;
127 // NOTE: Weak pointers must be invalidated before all other member variables.
128 base::WeakPtrFactory<ProxyDecryptor> weak_ptr_factory_;
130 DISALLOW_COPY_AND_ASSIGN(ProxyDecryptor);
133 } // namespace media
135 #endif // MEDIA_CDM_PROXY_DECRYPTOR_H_