summaryrefslogtreecommitdiffstats
path: root/content/renderer/media/cdm_session_adapter.h
blob: 7e32669962268deb2e75cc79f5f76de5bddd8666 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_RENDERER_MEDIA_CDM_SESSION_ADAPTER_H_
#define CONTENT_RENDERER_MEDIA_CDM_SESSION_ADAPTER_H_

#include <map>
#include <string>

#include "base/basictypes.h"
#include "base/containers/hash_tables.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "media/base/media_keys.h"
#include "third_party/WebKit/public/platform/WebContentDecryptionModuleSession.h"

#if defined(ENABLE_PEPPER_CDMS)
#include "content/renderer/media/crypto/pepper_cdm_wrapper.h"
#endif

class GURL;

namespace content {

#if defined(ENABLE_BROWSER_CDMS)
class RendererCdmManager;
#endif

class WebContentDecryptionModuleSessionImpl;

// Owns the CDM instance and makes calls from session objects to the CDM.
// Forwards the web session ID-based callbacks of the MediaKeys interface to the
// appropriate session object. Callers should hold references to this class
// as long as they need the CDM instance.
class CdmSessionAdapter : public base::RefCounted<CdmSessionAdapter> {
 public:
  CdmSessionAdapter();

  // Returns true on success.
  bool Initialize(
#if defined(ENABLE_PEPPER_CDMS)
      const CreatePepperCdmCB& create_pepper_cdm_cb,
#elif defined(ENABLE_BROWSER_CDMS)
      RendererCdmManager* manager,
#endif
      const std::string& key_system,
      const GURL& security_origin);

  // Creates a new session and adds it to the internal map. The caller owns the
  // created session. RemoveSession() must be called when destroying it, if
  // RegisterSession() was called.
  WebContentDecryptionModuleSessionImpl* CreateSession();

  // Adds a session to the internal map. Called once the session is successfully
  // initialized. Returns true if the session was registered, false if there is
  // already an existing session with the same |web_session_id|.
  bool RegisterSession(
      const std::string& web_session_id,
      base::WeakPtr<WebContentDecryptionModuleSessionImpl> session);

  // Removes a session from the internal map.
  void RemoveSession(const std::string& web_session_id);

  // Initializes a session with the |init_data_type|, |init_data| and
  // |session_type| provided. Takes ownership of |promise|.
  void InitializeNewSession(const std::string& init_data_type,
                            const uint8* init_data,
                            int init_data_length,
                            media::MediaKeys::SessionType session_type,
                            scoped_ptr<media::NewSessionCdmPromise> promise);

  // Updates the session specified by |web_session_id| with |response|.
  // Takes ownership of |promise|.
  void UpdateSession(const std::string& web_session_id,
                     const uint8* response,
                     int response_length,
                     scoped_ptr<media::SimpleCdmPromise> promise);

  // Releases the session specified by |web_session_id|.
  // Takes ownership of |promise|.
  void ReleaseSession(const std::string& web_session_id,
                      scoped_ptr<media::SimpleCdmPromise> promise);

  // Returns the Decryptor associated with this CDM. May be NULL if no
  // Decryptor is associated with the MediaKeys object.
  // TODO(jrummell): Figure out lifetimes, as WMPI may still use the decryptor
  // after WebContentDecryptionModule is freed. http://crbug.com/330324
  media::Decryptor* GetDecryptor();

  // Returns a prefix to use for UMAs.
  const std::string& GetKeySystemUMAPrefix() const;

#if defined(ENABLE_BROWSER_CDMS)
  // Returns the CDM ID associated with the |media_keys_|. May be kInvalidCdmId
  // if no CDM ID is associated.
  int GetCdmId() const;
#endif

 private:
  friend class base::RefCounted<CdmSessionAdapter>;
  typedef base::hash_map<std::string,
                         base::WeakPtr<WebContentDecryptionModuleSessionImpl> >
      SessionMap;

  ~CdmSessionAdapter();

  // Callbacks for firing session events.
  void OnSessionMessage(const std::string& web_session_id,
                        const std::vector<uint8>& message,
                        const GURL& destination_url);
  void OnSessionReady(const std::string& web_session_id);
  void OnSessionClosed(const std::string& web_session_id);
  void OnSessionError(const std::string& web_session_id,
                      media::MediaKeys::Exception exception_code,
                      uint32 system_code,
                      const std::string& error_message);

  // Helper function of the callbacks.
  WebContentDecryptionModuleSessionImpl* GetSession(
      const std::string& web_session_id);

  scoped_ptr<media::MediaKeys> media_keys_;

  SessionMap sessions_;

#if defined(ENABLE_BROWSER_CDMS)
  int cdm_id_;
#endif

  std::string key_system_uma_prefix_;

  // NOTE: Weak pointers must be invalidated before all other member variables.
  base::WeakPtrFactory<CdmSessionAdapter> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(CdmSessionAdapter);
};

}  // namespace content

#endif  // CONTENT_RENDERER_MEDIA_CDM_SESSION_ADAPTER_H_