summaryrefslogtreecommitdiffstats
path: root/content/renderer/media/webcontentdecryptionmodule_impl.cc
blob: d3201247bcdcb33c4f525d857da22d2f8af24695 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright 2013 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.

#include "content/renderer/media/webcontentdecryptionmodule_impl.h"

#include <map>
#include <vector>

#include "base/basictypes.h"
#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/logging.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/string_util.h"
#include "content/renderer/media/crypto/content_decryption_module_factory.h"
#include "content/renderer/media/webcontentdecryptionmodulesession_impl.h"
#include "media/base/media_keys.h"
#include "url/gurl.h"

namespace content {

// Forwards the reference ID-based callbacks of the MediaKeys interface to the
// appropriate session object.
class ReferenceIdAdapter {
 public:
  ReferenceIdAdapter();
  ~ReferenceIdAdapter();

  // On success, creates a MediaKeys, returns it in |media_keys|, returns true.
  bool Initialize(const std::string& key_system,
                  scoped_ptr<media::MediaKeys>* media_keys);

  // Adds a session to the internal map. Does not take ownership of the session.
  void AddSession(uint32 reference_id,
                  WebContentDecryptionModuleSessionImpl* session);

  // Removes a session from the internal map.
  void RemoveSession(uint32 reference_id);

 private:
  typedef std::map<uint32, WebContentDecryptionModuleSessionImpl*> SessionMap;

  // Callbacks for firing session events.
  void OnSessionCreated(uint32 reference_id, const std::string& session_id);
  void OnSessionMessage(uint32 reference_id,
                        const std::vector<uint8>& message,
                        const std::string& destination_url);
  void OnSessionReady(uint32 reference_id);
  void OnSessionClosed(uint32 reference_id);
  void OnSessionError(uint32 reference_id,
                      media::MediaKeys::KeyError error_code,
                      int system_code);

  // Helper function of the callbacks.
  WebContentDecryptionModuleSessionImpl* GetSession(uint32 reference_id);

  base::WeakPtrFactory<ReferenceIdAdapter> weak_ptr_factory_;

  SessionMap sessions_;

  DISALLOW_COPY_AND_ASSIGN(ReferenceIdAdapter);
};

ReferenceIdAdapter::ReferenceIdAdapter()
    : weak_ptr_factory_(this) {
}

ReferenceIdAdapter::~ReferenceIdAdapter() {
}

bool ReferenceIdAdapter::Initialize(const std::string& key_system,
                                    scoped_ptr<media::MediaKeys>* media_keys) {
  DCHECK(media_keys);
  DCHECK(!*media_keys);

  base::WeakPtr<ReferenceIdAdapter> weak_this = weak_ptr_factory_.GetWeakPtr();
  scoped_ptr<media::MediaKeys> created_media_keys =
      ContentDecryptionModuleFactory::Create(
          // TODO(ddorwin): Address lower in the stack: http://crbug.com/252065
          "webkit-" + key_system,
#if defined(ENABLE_PEPPER_CDMS)
          // TODO(ddorwin): Support Pepper-based CDMs: http://crbug.com/250049
          NULL,
          NULL,
          base::Closure(),
#elif defined(OS_ANDROID)
          // TODO(xhwang): Support Android.
          NULL,
          0,
          // TODO(ddorwin): Get the URL for the frame containing the MediaKeys.
          GURL(),
#endif  // defined(ENABLE_PEPPER_CDMS)
          base::Bind(&ReferenceIdAdapter::OnSessionCreated, weak_this),
          base::Bind(&ReferenceIdAdapter::OnSessionMessage, weak_this),
          base::Bind(&ReferenceIdAdapter::OnSessionReady, weak_this),
          base::Bind(&ReferenceIdAdapter::OnSessionClosed, weak_this),
          base::Bind(&ReferenceIdAdapter::OnSessionError, weak_this));
  if (!created_media_keys)
    return false;

  *media_keys = created_media_keys.Pass();
  return true;
}

void ReferenceIdAdapter::AddSession(
    uint32 reference_id,
    WebContentDecryptionModuleSessionImpl* session) {
  DCHECK(sessions_.find(reference_id) == sessions_.end());
  sessions_[reference_id] = session;
}

void ReferenceIdAdapter::RemoveSession(uint32 reference_id) {
  DCHECK(sessions_.find(reference_id) != sessions_.end());
  sessions_.erase(reference_id);
}

void ReferenceIdAdapter::OnSessionCreated(uint32 reference_id,
                                          const std::string& session_id) {
  GetSession(reference_id)->OnSessionCreated(session_id);
}

void ReferenceIdAdapter::OnSessionMessage(uint32 reference_id,
                                          const std::vector<uint8>& message,
                                          const std::string& destination_url) {
  GetSession(reference_id)->OnSessionMessage(message, destination_url);
}

void ReferenceIdAdapter::OnSessionReady(uint32 reference_id) {
  GetSession(reference_id)->OnSessionReady();
}

void ReferenceIdAdapter::OnSessionClosed(uint32 reference_id) {
  GetSession(reference_id)->OnSessionClosed();
}

void ReferenceIdAdapter::OnSessionError(uint32 reference_id,
                                        media::MediaKeys::KeyError error_code,
                                        int system_code) {
  GetSession(reference_id)->OnSessionError(error_code, system_code);
}

WebContentDecryptionModuleSessionImpl* ReferenceIdAdapter::GetSession(
    uint32 reference_id) {
  DCHECK(sessions_.find(reference_id) != sessions_.end());
  return sessions_[reference_id];
}

//------------------------------------------------------------------------------

WebContentDecryptionModuleImpl*
WebContentDecryptionModuleImpl::Create(const base::string16& key_system) {
  // TODO(ddorwin): Guard against this in supported types check and remove this.
  // Chromium only supports ASCII key systems.
  if (!IsStringASCII(key_system)) {
    NOTREACHED();
    return NULL;
  }

  // ReferenceIdAdapter creates the MediaKeys so it can provide its callbacks to
  // during creation of the MediaKeys.
  scoped_ptr<media::MediaKeys> media_keys;
  scoped_ptr<ReferenceIdAdapter> adapter(new ReferenceIdAdapter());
  if (!adapter->Initialize(UTF16ToASCII(key_system), &media_keys))
    return NULL;

  return new WebContentDecryptionModuleImpl(media_keys.Pass(), adapter.Pass());
}

WebContentDecryptionModuleImpl::WebContentDecryptionModuleImpl(
    scoped_ptr<media::MediaKeys> media_keys,
    scoped_ptr<ReferenceIdAdapter> adapter)
    : media_keys_(media_keys.Pass()),
      adapter_(adapter.Pass()) {
}

WebContentDecryptionModuleImpl::~WebContentDecryptionModuleImpl() {
}

// The caller owns the created session.
blink::WebContentDecryptionModuleSession*
WebContentDecryptionModuleImpl::createSession(
    blink::WebContentDecryptionModuleSession::Client* client) {
  DCHECK(media_keys_);
  WebContentDecryptionModuleSessionImpl* session =
      new WebContentDecryptionModuleSessionImpl(
          media_keys_.get(),
          client,
          base::Bind(&WebContentDecryptionModuleImpl::OnSessionClosed,
                     base::Unretained(this)));

  adapter_->AddSession(session->reference_id(), session);
  return session;
}

void WebContentDecryptionModuleImpl::OnSessionClosed(uint32 reference_id) {
  adapter_->RemoveSession(reference_id);
}

}  // namespace content