proxy_decryptor.h revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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 CONTENT_RENDERER_MEDIA_CRYPTO_PROXY_DECRYPTOR_H_
6#define CONTENT_RENDERER_MEDIA_CRYPTO_PROXY_DECRYPTOR_H_
7
8#include <map>
9#include <set>
10#include <string>
11#include <vector>
12
13#include "base/basictypes.h"
14#include "base/memory/scoped_ptr.h"
15#include "base/memory/weak_ptr.h"
16#include "base/synchronization/lock.h"
17#include "media/base/decryptor.h"
18#include "media/base/media_keys.h"
19
20#if defined(ENABLE_PEPPER_CDMS)
21#include "content/renderer/media/crypto/pepper_cdm_wrapper.h"
22#endif
23
24class GURL;
25
26namespace content {
27
28#if defined(OS_ANDROID)
29class RendererMediaPlayerManager;
30#endif  // defined(OS_ANDROID)
31
32// ProxyDecryptor is for EME v0.1b only. It should not be used for the WD API.
33// A decryptor proxy that creates a real decryptor object on demand and
34// forwards decryptor calls to it.
35//
36// Now that the Pepper API calls use session ID to match responses with
37// requests, this class maintains a mapping between session ID and web session
38// ID. Callers of this class expect web session IDs in the responses.
39// Session IDs are internal unique references to the session. Web session IDs
40// are the CDM generated ID for the session, and are what are visible to users.
41//
42// TODO(xhwang): Currently we don't support run-time switching among decryptor
43// objects. Fix this when needed.
44// TODO(xhwang): The ProxyDecryptor is not a Decryptor. Find a better name!
45class ProxyDecryptor {
46 public:
47  // These are similar to the callbacks in media_keys.h, but pass back the
48  // web session ID rather than the internal session ID.
49  typedef base::Callback<void(const std::string& session_id)> KeyAddedCB;
50  typedef base::Callback<void(const std::string& session_id,
51                              media::MediaKeys::KeyError error_code,
52                              uint32 system_code)> KeyErrorCB;
53  typedef base::Callback<void(const std::string& session_id,
54                              const std::vector<uint8>& message,
55                              const std::string& default_url)> KeyMessageCB;
56
57  ProxyDecryptor(
58#if defined(ENABLE_PEPPER_CDMS)
59      const CreatePepperCdmCB& create_pepper_cdm_cb,
60#elif defined(OS_ANDROID)
61      RendererMediaPlayerManager* manager,
62      int cdm_id,
63#endif  // defined(ENABLE_PEPPER_CDMS)
64      const KeyAddedCB& key_added_cb,
65      const KeyErrorCB& key_error_cb,
66      const KeyMessageCB& key_message_cb);
67  virtual ~ProxyDecryptor();
68
69  // Returns the Decryptor associated with this object. May be NULL if no
70  // Decryptor is associated.
71  media::Decryptor* GetDecryptor();
72
73  // Only call this once.
74  bool InitializeCDM(const std::string& key_system, const GURL& frame_url);
75
76  // May only be called after InitializeCDM() succeeds.
77  bool GenerateKeyRequest(const std::string& type,
78                          const uint8* init_data,
79                          int init_data_length);
80  void AddKey(const uint8* key, int key_length,
81              const uint8* init_data, int init_data_length,
82              const std::string& session_id);
83  void CancelKeyRequest(const std::string& session_id);
84
85 private:
86  // Session_id <-> web_session_id map.
87  typedef std::map<uint32, std::string> SessionIdMap;
88
89  // Helper function to create MediaKeys to handle the given |key_system|.
90  scoped_ptr<media::MediaKeys> CreateMediaKeys(const std::string& key_system,
91                                               const GURL& frame_url);
92
93  // Callbacks for firing session events.
94  void OnSessionCreated(uint32 session_id, const std::string& web_session_id);
95  void OnSessionMessage(uint32 session_id,
96                        const std::vector<uint8>& message,
97                        const std::string& default_url);
98  void OnSessionReady(uint32 session_id);
99  void OnSessionClosed(uint32 session_id);
100  void OnSessionError(uint32 session_id,
101                      media::MediaKeys::KeyError error_code,
102                      uint32 system_code);
103
104  // Helper function to determine session_id for the provided |web_session_id|.
105  uint32 LookupSessionId(const std::string& web_session_id) const;
106
107  // Helper function to determine web_session_id for the provided |session_id|.
108  // The returned web_session_id is only valid on the main thread, and should be
109  // stored by copy.
110  const std::string& LookupWebSessionId(uint32 session_id) const;
111
112  base::WeakPtrFactory<ProxyDecryptor> weak_ptr_factory_;
113
114#if defined(ENABLE_PEPPER_CDMS)
115  // Callback to create the Pepper plugin.
116  CreatePepperCdmCB create_pepper_cdm_cb_;
117#elif defined(OS_ANDROID)
118  RendererMediaPlayerManager* manager_;
119  int cdm_id_;
120#endif  // defined(ENABLE_PEPPER_CDMS)
121
122  // The real MediaKeys that manages key operations for the ProxyDecryptor.
123  scoped_ptr<media::MediaKeys> media_keys_;
124
125  // Callbacks for firing key events.
126  KeyAddedCB key_added_cb_;
127  KeyErrorCB key_error_cb_;
128  KeyMessageCB key_message_cb_;
129
130  // Session IDs are used to uniquely track sessions so that CDM callbacks
131  // can get mapped to the correct session ID. Session ID should be unique
132  // per renderer process for debugging purposes.
133  static uint32 next_session_id_;
134
135  SessionIdMap sessions_;
136
137  std::set<uint32> persistent_sessions_;
138
139  bool is_clear_key_;
140
141  DISALLOW_COPY_AND_ASSIGN(ProxyDecryptor);
142};
143
144}  // namespace content
145
146#endif  // CONTENT_RENDERER_MEDIA_CRYPTO_PROXY_DECRYPTOR_H_
147