summaryrefslogtreecommitdiff
path: root/chromium/content/browser/media/cdm/browser_cdm_manager.cc
blob: a71bfafbd5f64f6a3726f9a81e08fed301183ae7 (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// 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.

#include "content/browser/media/cdm/browser_cdm_manager.h"

#include "base/command_line.h"
#include "content/common/media/cdm_messages.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_switches.h"
#include "media/base/browser_cdm.h"
#include "media/base/browser_cdm_factory.h"
#include "media/base/media_switches.h"

namespace content {

using media::BrowserCdm;
using media::MediaKeys;

// Maximum lengths for various EME API parameters. These are checks to
// prevent unnecessarily large parameters from being passed around, and the
// lengths are somewhat arbitrary as the EME spec doesn't specify any limits.
const size_t kMaxInitDataLength = 64 * 1024;  // 64 KB
const size_t kMaxSessionResponseLength = 64 * 1024;  // 64 KB
const size_t kMaxKeySystemLength = 256;

// static
BrowserCdmManager* BrowserCdmManager::Create(RenderFrameHost* rfh) {
  return new BrowserCdmManager(rfh);
}

BrowserCdmManager::BrowserCdmManager(RenderFrameHost* render_frame_host)
    : render_frame_host_(render_frame_host),
      web_contents_(WebContents::FromRenderFrameHost(render_frame_host)),
      weak_ptr_factory_(this) {
}

BrowserCdmManager::~BrowserCdmManager() {
  // During the tear down process, OnDestroyCdm() may or may not be called
  // (e.g. WebContents may be destroyed before the render process is killed). So
  // we cannot DCHECK(cdm_map_.empty()) here. Instead, all CDMs in |cdm_map_|
  // will be destroyed here because they are owned by BrowserCdmManager.
}

BrowserCdm* BrowserCdmManager::GetCdm(int cdm_id) {
  return cdm_map_.get(cdm_id);
}

void BrowserCdmManager::OnSessionCreated(
    int cdm_id,
    uint32 session_id,
    const std::string& web_session_id) {
  Send(new CdmMsg_SessionCreated(
      RoutingID(), cdm_id, session_id, web_session_id));
}

void BrowserCdmManager::OnSessionMessage(
    int cdm_id,
    uint32 session_id,
    const std::vector<uint8>& message,
    const GURL& destination_url) {
  GURL verified_gurl = destination_url;
  if (!verified_gurl.is_valid() && !verified_gurl.is_empty()) {
    DLOG(WARNING) << "SessionMessage destination_url is invalid : "
                  << destination_url.possibly_invalid_spec();
    verified_gurl = GURL::EmptyGURL();  // Replace invalid destination_url.
  }

  Send(new CdmMsg_SessionMessage(
      RoutingID(), cdm_id, session_id, message, verified_gurl));
}

void BrowserCdmManager::OnSessionReady(int cdm_id, uint32 session_id) {
  Send(new CdmMsg_SessionReady(RoutingID(), cdm_id, session_id));
}

void BrowserCdmManager::OnSessionClosed(int cdm_id, uint32 session_id) {
  Send(new CdmMsg_SessionClosed(RoutingID(), cdm_id, session_id));
}

void BrowserCdmManager::OnSessionError(int cdm_id,
                                       uint32 session_id,
                                       MediaKeys::KeyError error_code,
                                       uint32 system_code) {
  Send(new CdmMsg_SessionError(
      RoutingID(), cdm_id, session_id, error_code, system_code));
}

void BrowserCdmManager::OnInitializeCdm(int cdm_id,
                                                const std::string& key_system,
                                                const GURL& security_origin) {
  if (key_system.size() > kMaxKeySystemLength) {
    // This failure will be discovered and reported by OnCreateSession()
    // as GetCdm() will return null.
    NOTREACHED() << "Invalid key system: " << key_system;
    return;
  }

  AddCdm(cdm_id, key_system, security_origin);
}

void BrowserCdmManager::OnCreateSession(
    int cdm_id,
    uint32 session_id,
    CdmHostMsg_CreateSession_ContentType content_type,
    const std::vector<uint8>& init_data) {
  if (init_data.size() > kMaxInitDataLength) {
    LOG(WARNING) << "InitData for ID: " << cdm_id
                 << " too long: " << init_data.size();
    OnSessionError(cdm_id, session_id, MediaKeys::kUnknownError, 0);
    return;
  }

  // Convert the session content type into a MIME type. "audio" and "video"
  // don't matter, so using "video" for the MIME type.
  // Ref:
  // https://dvcs.w3.org/hg/html-media/raw-file/default/encrypted-media/encrypted-media.html#dom-createsession
  std::string mime_type;
  switch (content_type) {
    case CREATE_SESSION_TYPE_WEBM:
      mime_type = "video/webm";
      break;
    case CREATE_SESSION_TYPE_MP4:
      mime_type = "video/mp4";
      break;
    default:
      NOTREACHED();
      return;
  }

#if defined(OS_ANDROID)
  if (CommandLine::ForCurrentProcess()
      ->HasSwitch(switches::kDisableInfobarForProtectedMediaIdentifier)) {
    CreateSessionIfPermitted(cdm_id, session_id, mime_type, init_data, true);
    return;
  }
#endif

  BrowserCdm* cdm = GetCdm(cdm_id);
  if (!cdm) {
    DLOG(WARNING) << "No CDM for ID " << cdm_id << " found";
    OnSessionError(cdm_id, session_id, MediaKeys::kUnknownError, 0);
    return;
  }

  std::map<int, GURL>::const_iterator iter =
      cdm_security_origin_map_.find(cdm_id);
  if (iter == cdm_security_origin_map_.end()) {
    NOTREACHED();
    OnSessionError(cdm_id, session_id, MediaKeys::kUnknownError, 0);
    return;
  }

  base::Closure cancel_callback;
  GetContentClient()->browser()->RequestProtectedMediaIdentifierPermission(
      web_contents_,
      iter->second,
      base::Bind(&BrowserCdmManager::CreateSessionIfPermitted,
                 weak_ptr_factory_.GetWeakPtr(),
                 cdm_id,
                 session_id,
                 mime_type,
                 init_data),
      &cancel_callback);
  if (!cancel_callback.is_null())
    cdm_cancel_permision_map_[cdm_id] = cancel_callback;
}

void BrowserCdmManager::OnUpdateSession(
    int cdm_id,
    uint32 session_id,
    const std::vector<uint8>& response) {
  BrowserCdm* cdm = GetCdm(cdm_id);
  if (!cdm) {
    DLOG(WARNING) << "No CDM for ID " << cdm_id << " found";
    OnSessionError(cdm_id, session_id, MediaKeys::kUnknownError, 0);
    return;
  }

  if (response.size() > kMaxSessionResponseLength) {
    LOG(WARNING) << "Response for ID " << cdm_id
                 << " is too long: " << response.size();
    OnSessionError(cdm_id, session_id, MediaKeys::kUnknownError, 0);
    return;
  }

  cdm->UpdateSession(session_id, &response[0], response.size());
}

void BrowserCdmManager::OnReleaseSession(int cdm_id, uint32 session_id) {
  BrowserCdm* cdm = GetCdm(cdm_id);
  if (!cdm) {
    DLOG(WARNING) << "No CDM for ID " << cdm_id << " found";
    OnSessionError(cdm_id, session_id, MediaKeys::kUnknownError, 0);
    return;
  }

  cdm->ReleaseSession(session_id);
}

void BrowserCdmManager::OnDestroyCdm(int cdm_id) {
  BrowserCdm* cdm = GetCdm(cdm_id);
  if (!cdm)
    return;

  CancelAllPendingSessionCreations(cdm_id);
  RemoveCdm(cdm_id);
}

void BrowserCdmManager::CancelAllPendingSessionCreations(int cdm_id) {
  if (cdm_cancel_permision_map_.count(cdm_id)) {
    cdm_cancel_permision_map_[cdm_id].Run();
    cdm_cancel_permision_map_.erase(cdm_id);
  }
}

void BrowserCdmManager::AddCdm(int cdm_id,
                               const std::string& key_system,
                               const GURL& security_origin) {
  DCHECK(!GetCdm(cdm_id));
  base::WeakPtr<BrowserCdmManager> weak_this = weak_ptr_factory_.GetWeakPtr();
  scoped_ptr<BrowserCdm> cdm(media::CreateBrowserCdm(
      key_system,
      base::Bind(&BrowserCdmManager::OnSessionCreated, weak_this, cdm_id),
      base::Bind(&BrowserCdmManager::OnSessionMessage, weak_this, cdm_id),
      base::Bind(&BrowserCdmManager::OnSessionReady, weak_this, cdm_id),
      base::Bind(&BrowserCdmManager::OnSessionClosed, weak_this, cdm_id),
      base::Bind(&BrowserCdmManager::OnSessionError, weak_this, cdm_id)));

  if (!cdm) {
    // This failure will be discovered and reported by OnCreateSession()
    // as GetCdm() will return null.
    DVLOG(1) << "failed to create CDM.";
    return;
  }

  cdm_map_.add(cdm_id, cdm.Pass());
  cdm_security_origin_map_[cdm_id] = security_origin;
}

void BrowserCdmManager::RemoveCdm(int cdm_id) {
  // TODO(xhwang): Detach CDM from the player it's set to. In prefixed
  // EME implementation the current code is fine because we always destroy the
  // player before we destroy the DrmBridge. This will not always be the case
  // in unprefixed EME implementation.
  cdm_map_.erase(cdm_id);
  cdm_security_origin_map_.erase(cdm_id);
  cdm_cancel_permision_map_.erase(cdm_id);
}

int BrowserCdmManager::RoutingID() {
  return render_frame_host_->GetRoutingID();
}

bool BrowserCdmManager::Send(IPC::Message* msg) {
  return render_frame_host_->Send(msg);
}

void BrowserCdmManager::CreateSessionIfPermitted(
    int cdm_id,
    uint32 session_id,
    const std::string& content_type,
    const std::vector<uint8>& init_data,
    bool permitted) {
  cdm_cancel_permision_map_.erase(cdm_id);
  if (!permitted) {
    OnSessionError(cdm_id, session_id, MediaKeys::kUnknownError, 0);
    return;
  }

  BrowserCdm* cdm = GetCdm(cdm_id);
  if (!cdm) {
    DLOG(WARNING) << "No CDM for ID: " << cdm_id << " found";
    OnSessionError(cdm_id, session_id, MediaKeys::kUnknownError, 0);
    return;
  }

  // This could fail, in which case a SessionError will be fired.
  cdm->CreateSession(session_id, content_type, &init_data[0], init_data.size());
}

}  // namespace content