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.
4
5#ifndef MEDIA_BASE_MEDIA_KEYS_H_
6#define MEDIA_BASE_MEDIA_KEYS_H_
7
8#include <string>
9#include <vector>
10
11#include "base/basictypes.h"
12#include "base/callback.h"
13#include "base/memory/scoped_ptr.h"
14#include "media/base/media_export.h"
15#include "url/gurl.h"
16
17namespace base {
18class Time;
19}
20
21namespace media {
22
23class Decryptor;
24
25template <typename T>
26class CdmPromiseTemplate;
27
28typedef CdmPromiseTemplate<std::string> NewSessionCdmPromise;
29typedef CdmPromiseTemplate<void> SimpleCdmPromise;
30typedef std::vector<std::vector<uint8> > KeyIdsVector;
31typedef CdmPromiseTemplate<KeyIdsVector> KeyIdsPromise;
32
33// Performs media key operations.
34//
35// All key operations are called on the renderer thread. Therefore, these calls
36// should be fast and nonblocking; key events should be fired asynchronously.
37class MEDIA_EXPORT MediaKeys {
38 public:
39  // Reported to UMA, so never reuse a value!
40  // Must be kept in sync with blink::WebMediaPlayerClient::MediaKeyErrorCode
41  // (enforced in webmediaplayer_impl.cc).
42  // TODO(jrummell): Can this be moved to proxy_decryptor as it should only be
43  // used by the prefixed EME code?
44  enum KeyError {
45    kUnknownError = 1,
46    kClientError,
47    // The commented v0.1b values below have never been used.
48    // kServiceError,
49    kOutputError = 4,
50    // kHardwareChangeError,
51    // kDomainError,
52    kMaxKeyError  // Must be last and greater than any legit value.
53  };
54
55  // Must be a superset of cdm::MediaKeyException.
56  enum Exception {
57    NOT_SUPPORTED_ERROR,
58    INVALID_STATE_ERROR,
59    INVALID_ACCESS_ERROR,
60    QUOTA_EXCEEDED_ERROR,
61    UNKNOWN_ERROR,
62    CLIENT_ERROR,
63    OUTPUT_ERROR
64  };
65
66  // Type of license required when creating/loading a session.
67  // Must be consistent with the values specified in the spec:
68  // https://dvcs.w3.org/hg/html-media/raw-file/default/encrypted-media/encrypted-media.html#extensions
69  enum SessionType {
70    TEMPORARY_SESSION,
71    PERSISTENT_SESSION
72  };
73
74  const static uint32 kInvalidSessionId = 0;
75
76  MediaKeys();
77  virtual ~MediaKeys();
78
79  // Provides a server certificate to be used to encrypt messages to the
80  // license server.
81  virtual void SetServerCertificate(const uint8* certificate_data,
82                                    int certificate_data_length,
83                                    scoped_ptr<SimpleCdmPromise> promise) = 0;
84
85  // Creates a session with the |init_data_type|, |init_data| and |session_type|
86  // provided.
87  // Note: UpdateSession() and ReleaseSession() should only be called after
88  // |promise| is resolved.
89  virtual void CreateSession(const std::string& init_data_type,
90                             const uint8* init_data,
91                             int init_data_length,
92                             SessionType session_type,
93                             scoped_ptr<NewSessionCdmPromise> promise) = 0;
94
95  // Loads a session with the |web_session_id| provided.
96  // Note: UpdateSession() and ReleaseSession() should only be called after
97  // |promise| is resolved.
98  virtual void LoadSession(const std::string& web_session_id,
99                           scoped_ptr<NewSessionCdmPromise> promise) = 0;
100
101  // Updates a session specified by |web_session_id| with |response|.
102  virtual void UpdateSession(const std::string& web_session_id,
103                             const uint8* response,
104                             int response_length,
105                             scoped_ptr<SimpleCdmPromise> promise) = 0;
106
107  // Closes the session specified by |web_session_id|.
108  virtual void CloseSession(const std::string& web_session_id,
109                            scoped_ptr<SimpleCdmPromise> promise) = 0;
110
111  // Removes stored session data associated with the session specified by
112  // |web_session_id|.
113  virtual void RemoveSession(const std::string& web_session_id,
114                             scoped_ptr<SimpleCdmPromise> promise) = 0;
115
116  // Retrieves the key IDs for keys in the session that the CDM knows are
117  // currently usable to decrypt media data.
118  virtual void GetUsableKeyIds(const std::string& web_session_id,
119                               scoped_ptr<KeyIdsPromise> promise) = 0;
120
121  // Gets the Decryptor object associated with the MediaKeys. Returns NULL if
122  // no Decryptor object is associated. The returned object is only guaranteed
123  // to be valid during the MediaKeys' lifetime.
124  virtual Decryptor* GetDecryptor();
125
126 private:
127  DISALLOW_COPY_AND_ASSIGN(MediaKeys);
128};
129
130// Key event callbacks. See the spec for details:
131// https://dvcs.w3.org/hg/html-media/raw-file/default/encrypted-media/encrypted-media.html#event-summary
132typedef base::Callback<void(const std::string& web_session_id,
133                            const std::vector<uint8>& message,
134                            const GURL& destination_url)> SessionMessageCB;
135
136typedef base::Callback<void(const std::string& web_session_id)> SessionReadyCB;
137
138typedef base::Callback<void(const std::string& web_session_id)> SessionClosedCB;
139
140typedef base::Callback<void(const std::string& web_session_id,
141                            MediaKeys::Exception exception_code,
142                            uint32 system_code,
143                            const std::string& error_message)> SessionErrorCB;
144
145typedef base::Callback<void(const std::string& web_session_id,
146                            bool has_additional_usable_key)>
147    SessionKeysChangeCB;
148
149typedef base::Callback<void(const std::string& web_session_id,
150                            const base::Time& new_expiry_time)>
151    SessionExpirationUpdateCB;
152
153}  // namespace media
154
155#endif  // MEDIA_BASE_MEDIA_KEYS_H_
156