File: media_drm_credential_manager.cc

package info (click to toggle)
chromium-browser 37.0.2062.120-1~deb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,707,260 kB
  • sloc: cpp: 8,976,677; ansic: 3,473,199; python: 586,578; asm: 449,013; xml: 184,195; java: 142,924; sh: 118,496; perl: 81,467; makefile: 27,557; yacc: 10,506; objc: 8,886; tcl: 3,186; cs: 2,252; lex: 2,213; sql: 1,198; pascal: 1,170; lisp: 790; awk: 407; ruby: 155; php: 83; sed: 52; exp: 11
file content (117 lines) | stat: -rw-r--r-- 3,815 bytes parent folder | download | duplicates (2)
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
// 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/browser/media/android/media_drm_credential_manager.h"

#include "base/android/jni_android.h"
#include "base/android/scoped_java_ref.h"
#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/location.h"
#include "base/message_loop/message_loop_proxy.h"
#include "jni/MediaDrmCredentialManager_jni.h"
#include "media/base/android/media_drm_bridge.h"
#include "url/gurl.h"

#include "widevine_cdm_version.h"  // In SHARED_INTERMEDIATE_DIR.

using base::android::ScopedJavaGlobalRef;

namespace {

void MediaDrmCredentialManagerCallback(
    const ScopedJavaGlobalRef<jobject>& j_media_drm_credential_manager_callback,
    bool succeeded) {
  JNIEnv* env = base::android::AttachCurrentThread();
  content::Java_MediaDrmCredentialManagerCallback_onCredentialResetFinished(
      env, j_media_drm_credential_manager_callback.obj(), succeeded);
}

}  // namespace

namespace content {

MediaDrmCredentialManager::MediaDrmCredentialManager() {};

MediaDrmCredentialManager::~MediaDrmCredentialManager() {};

// static
MediaDrmCredentialManager* MediaDrmCredentialManager::GetInstance() {
  return Singleton<MediaDrmCredentialManager>::get();
}

void MediaDrmCredentialManager::ResetCredentials(
    const ResetCredentialsCB& reset_credentials_cb) {
  // Ignore reset request if one is already in progress.
  if (!reset_credentials_cb_.is_null())
    return;

  reset_credentials_cb_ = reset_credentials_cb;

  // First reset the L3 credential.
  if (!ResetCredentialsInternal(media::MediaDrmBridge::SECURITY_LEVEL_3)) {
    // TODO(qinmin): We should post a task instead.
    base::ResetAndReturn(&reset_credentials_cb_).Run(false);
  }
}

// static
void ResetCredentials(
    JNIEnv* env,
    jclass clazz,
    jobject j_media_drm_credential_manager_callback) {
  MediaDrmCredentialManager* media_drm_credential_manager =
      MediaDrmCredentialManager::GetInstance();

  ScopedJavaGlobalRef<jobject> j_scoped_media_drm_credential_manager_callback;
  j_scoped_media_drm_credential_manager_callback.Reset(
      env, j_media_drm_credential_manager_callback);

  MediaDrmCredentialManager::ResetCredentialsCB callback_runner =
      base::Bind(&MediaDrmCredentialManagerCallback,
                 j_scoped_media_drm_credential_manager_callback);

  media_drm_credential_manager->ResetCredentials(callback_runner);
}

void MediaDrmCredentialManager::OnResetCredentialsCompleted(
    SecurityLevel security_level, bool success) {
  if (security_level == media::MediaDrmBridge::SECURITY_LEVEL_3 && success) {
    if (ResetCredentialsInternal(media::MediaDrmBridge::SECURITY_LEVEL_1))
      return;
    success = false;
  }

  base::ResetAndReturn(&reset_credentials_cb_).Run(success);
  media_drm_bridge_.reset();
}

bool MediaDrmCredentialManager::ResetCredentialsInternal(
    SecurityLevel security_level) {
  media_drm_bridge_ =
      media::MediaDrmBridge::CreateSessionless(kWidevineKeySystem);
  if (!media_drm_bridge_)
    return false;

  ResetCredentialsCB reset_credentials_cb =
      base::Bind(&MediaDrmCredentialManager::OnResetCredentialsCompleted,
                 base::Unretained(this), security_level);

  if (!media_drm_bridge_->SetSecurityLevel(security_level)) {
    // No need to reset credentials for unsupported |security_level|.
    base::MessageLoopProxy::current()->PostTask(
        FROM_HERE, base::Bind(reset_credentials_cb, true));
    return true;
  }

  media_drm_bridge_->ResetDeviceCredentials(reset_credentials_cb);
  return true;
}

// static
bool MediaDrmCredentialManager::RegisterMediaDrmCredentialManager(JNIEnv* env) {
  return RegisterNativesImpl(env);
}

}  // namespace content