cdm_session_adapter.h revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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.
4
5#ifndef CONTENT_RENDERER_MEDIA_CDM_SESSION_ADAPTER_H_
6#define CONTENT_RENDERER_MEDIA_CDM_SESSION_ADAPTER_H_
7
8#include <map>
9
10#include "base/basictypes.h"
11#include "base/memory/ref_counted.h"
12#include "base/memory/weak_ptr.h"
13#include "media/base/media_keys.h"
14#include "third_party/WebKit/public/platform/WebContentDecryptionModuleSession.h"
15
16#if defined(ENABLE_PEPPER_CDMS)
17#include "content/renderer/media/crypto/pepper_cdm_wrapper.h"
18#endif
19
20class GURL;
21
22namespace content {
23
24#if defined(OS_ANDROID)
25class RendererCdmManager;
26#endif
27class WebContentDecryptionModuleSessionImpl;
28
29// Owns the CDM instance and makes calls from session objects to the CDM.
30// Forwards the session ID-based callbacks of the MediaKeys interface to the
31// appropriate session object. Callers should hold references to this class
32// as long as they need the CDM instance.
33class CdmSessionAdapter : public base::RefCounted<CdmSessionAdapter> {
34 public:
35  CdmSessionAdapter();
36
37  // Returns true on success.
38  bool Initialize(
39#if defined(ENABLE_PEPPER_CDMS)
40      const CreatePepperCdmCB& create_pepper_cdm_cb,
41#elif defined(OS_ANDROID)
42      RendererCdmManager* manager,
43#endif
44      const std::string& key_system,
45      const GURL& security_origin);
46
47  // Creates a new session and adds it to the internal map. The caller owns the
48  // created session. RemoveSession() must be called when destroying it.
49  WebContentDecryptionModuleSessionImpl* CreateSession(
50      blink::WebContentDecryptionModuleSession::Client* client);
51
52  // Removes a session from the internal map.
53  void RemoveSession(uint32 session_id);
54
55  // Initializes the session specified by |session_id| with the |content_type|
56  // and |init_data| provided.
57  void InitializeNewSession(uint32 session_id,
58                            const std::string& content_type,
59                            const uint8* init_data,
60                            int init_data_length);
61
62  // Updates the session specified by |session_id| with |response|.
63  void UpdateSession(uint32 session_id,
64                     const uint8* response,
65                     int response_length);
66
67  // Releases the session specified by |session_id|.
68  void ReleaseSession(uint32 session_id);
69
70  // Returns the Decryptor associated with this CDM. May be NULL if no
71  // Decryptor is associated with the MediaKeys object.
72  // TODO(jrummell): Figure out lifetimes, as WMPI may still use the decryptor
73  // after WebContentDecryptionModule is freed. http://crbug.com/330324
74  media::Decryptor* GetDecryptor();
75
76#if defined(OS_ANDROID)
77  // Returns the CDM ID associated with the |media_keys_|. May be kInvalidCdmId
78  // if no CDM ID is associated.
79  int GetCdmId() const;
80#endif
81
82 private:
83  friend class base::RefCounted<CdmSessionAdapter>;
84  typedef std::map<uint32, WebContentDecryptionModuleSessionImpl*> SessionMap;
85
86  ~CdmSessionAdapter();
87
88  // Callbacks for firing session events.
89  void OnSessionCreated(uint32 session_id, const std::string& web_session_id);
90  void OnSessionMessage(uint32 session_id,
91                        const std::vector<uint8>& message,
92                        const GURL& destination_url);
93  void OnSessionReady(uint32 session_id);
94  void OnSessionClosed(uint32 session_id);
95  void OnSessionError(uint32 session_id,
96                      media::MediaKeys::KeyError error_code,
97                      uint32 system_code);
98
99  // Helper function of the callbacks.
100  WebContentDecryptionModuleSessionImpl* GetSession(uint32 session_id);
101
102  // Session ID should be unique per renderer process for debugging purposes.
103  static uint32 next_session_id_;
104
105  scoped_ptr<media::MediaKeys> media_keys_;
106
107  SessionMap sessions_;
108
109#if defined(OS_ANDROID)
110  int cdm_id_;
111#endif
112
113  // NOTE: Weak pointers must be invalidated before all other member variables.
114  base::WeakPtrFactory<CdmSessionAdapter> weak_ptr_factory_;
115
116  DISALLOW_COPY_AND_ASSIGN(CdmSessionAdapter);
117};
118
119}  // namespace content
120
121#endif  // CONTENT_RENDERER_MEDIA_CDM_SESSION_ADAPTER_H_
122